From 533698115432fa24172693a4c5690faa23a90d8d Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Thu, 19 Apr 2018 07:15:12 +0300 Subject: [PATCH] h-gtk: Change the signature of utils::set_image_from_path to not require a Podcast. It was only used to call the podcast.id() method. This allows EpisodeViewWidget to be created whithout the need for a call to the database to get a Podcst Object for each widget. --- hammond-gtk/src/utils.rs | 33 ++++++++++++++----------------- hammond-gtk/src/views/episodes.rs | 4 +--- hammond-gtk/src/views/shows.rs | 14 ++++++------- hammond-gtk/src/widgets/show.rs | 2 +- 4 files changed, 24 insertions(+), 29 deletions(-) diff --git a/hammond-gtk/src/utils.rs b/hammond-gtk/src/utils.rs index 568d1df..6399582 100644 --- a/hammond-gtk/src/utils.rs +++ b/hammond-gtk/src/utils.rs @@ -18,7 +18,7 @@ use serde_json::Value; use hammond_data::dbqueries; use hammond_data::pipeline; use hammond_data::utils::checkup; -use hammond_data::{PodcastCoverQuery, Source}; +use hammond_data::Source; use hammond_downloader::downloader; use std::collections::{HashMap, HashSet}; @@ -212,19 +212,15 @@ lazy_static! { // Also lazy_static requires Sync trait, so that's what the mutexes are. // TODO: maybe use something that would just scale to requested size? #[inline] -pub fn set_image_from_path( - image: >k::Image, - pd: Arc, - size: u32, -) -> Result<(), Error> { +pub fn set_image_from_path(image: >k::Image, podcast_id: i32, size: u32) -> Result<(), Error> { // Check if there's an active download about this show cover. // If there is, a callback will be set so this function will be called again. // If the download succedes, there should be a quick return from the pixbuf cache_image // If it fails another download will be scheduled. if let Ok(guard) = COVER_DL_REGISTRY.read() { - if guard.contains(&pd.id()) { - let callback = clone!(image, pd => move || { - let _ = set_image_from_path(&image, pd.clone(), size); + if guard.contains(&podcast_id) { + let callback = clone!(image => move || { + let _ = set_image_from_path(&image, podcast_id, size); glib::Continue(false) }); gtk::timeout_add(250, callback); @@ -235,7 +231,7 @@ pub fn set_image_from_path( if let Ok(hashmap) = CACHED_PIXBUFS.read() { // Check if the requested (cover + size) is already in the chache // and if so do an early return after that. - if let Some(guard) = hashmap.get(&(pd.id(), size)) { + if let Some(guard) = hashmap.get(&(podcast_id, size)) { guard .lock() .map_err(|err| format_err!("SendCell Mutex: {}", err)) @@ -251,19 +247,20 @@ pub fn set_image_from_path( } let (sender, receiver) = channel(); - let pd_ = pd.clone(); THREADPOOL.spawn(move || { if let Ok(mut guard) = COVER_DL_REGISTRY.write() { - guard.insert(pd_.id()); + guard.insert(podcast_id); } - sender - .send(downloader::cache_image(&pd_)) - .map_err(|err| error!("Action Sender: {}", err)) - .ok(); + if let Ok(pd) = dbqueries::get_podcast_cover_from_id(podcast_id) { + sender + .send(downloader::cache_image(&pd)) + .map_err(|err| error!("Action Sender: {}", err)) + .ok(); + } if let Ok(mut guard) = COVER_DL_REGISTRY.write() { - guard.remove(&pd_.id()); + guard.remove(&podcast_id); } }); @@ -274,7 +271,7 @@ pub fn set_image_from_path( if let Ok(path) = path { if let Ok(px) = Pixbuf::new_from_file_at_scale(&path, s, s, true) { if let Ok(mut hashmap) = CACHED_PIXBUFS.write() { - hashmap.insert((pd.id(), size), Mutex::new(SendCell::new(px.clone()))); + hashmap.insert((podcast_id, size), Mutex::new(SendCell::new(px.clone()))); image.set_from_pixbuf(&px); } } diff --git a/hammond-gtk/src/views/episodes.rs b/hammond-gtk/src/views/episodes.rs index 649b105..185d2b8 100644 --- a/hammond-gtk/src/views/episodes.rs +++ b/hammond-gtk/src/views/episodes.rs @@ -13,7 +13,6 @@ use widgets::EpisodeWidget; use std::rc::Rc; use std::sync::mpsc::Sender; -use std::sync::Arc; #[derive(Debug, Clone)] enum ListSplit { @@ -213,7 +212,6 @@ impl EpisodesViewWidget { #[inline] fn set_cover(&self, podcast_id: i32) -> Result<(), Error> { - let pd = Arc::new(dbqueries::get_podcast_cover_from_id(podcast_id)?); - set_image_from_path(&self.image, pd, 64) + set_image_from_path(&self.image, podcast_id, 64) } } diff --git a/hammond-gtk/src/views/shows.rs b/hammond-gtk/src/views/shows.rs index 68dda23..ec33fb3 100644 --- a/hammond-gtk/src/views/shows.rs +++ b/hammond-gtk/src/views/shows.rs @@ -3,7 +3,7 @@ use gtk; use gtk::prelude::*; use hammond_data::dbqueries; -use hammond_data::{Podcast, PodcastCoverQuery}; +use hammond_data::Podcast; use app::Action; use utils::{get_ignored_shows, set_image_from_path}; @@ -58,7 +58,7 @@ impl ShowsPopulated { let ignore = get_ignored_shows()?; let podcasts = dbqueries::get_podcasts_filter(&ignore)?; - podcasts.into_iter().for_each(|parent| { + podcasts.iter().for_each(|parent| { let flowbox_child = ShowsChild::new(parent); self.flowbox.add(&flowbox_child.child); }); @@ -122,24 +122,24 @@ impl Default for ShowsChild { impl ShowsChild { #[inline] - pub fn new(pd: Podcast) -> ShowsChild { + pub fn new(pd: &Podcast) -> ShowsChild { let child = ShowsChild::default(); child.init(pd); child } #[inline] - fn init(&self, pd: Podcast) { + fn init(&self, pd: &Podcast) { self.container.set_tooltip_text(pd.title()); WidgetExt::set_name(&self.child, &pd.id().to_string()); - self.set_cover(Arc::new(pd.into())) + self.set_cover(pd.id()) .map_err(|err| error!("Failed to set a cover: {}", err)) .ok(); } #[inline] - fn set_cover(&self, pd: Arc) -> Result<(), Error> { - set_image_from_path(&self.cover, pd, 256) + fn set_cover(&self, podcast_id: i32) -> Result<(), Error> { + set_image_from_path(&self.cover, podcast_id, 256) } } diff --git a/hammond-gtk/src/widgets/show.rs b/hammond-gtk/src/widgets/show.rs index dc7372f..f7d38ba 100644 --- a/hammond-gtk/src/widgets/show.rs +++ b/hammond-gtk/src/widgets/show.rs @@ -115,7 +115,7 @@ impl ShowWidget { #[inline] /// Set the show cover. fn set_cover(&self, pd: Arc) -> Result<(), Error> { - utils::set_image_from_path(&self.cover, Arc::new(pd.into()), 128) + utils::set_image_from_path(&self.cover, pd.id(), 128) } #[inline]