From a56a80db881d53ec16973f45b7d04fc9861aa975 Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Tue, 24 Apr 2018 15:25:34 +0300 Subject: [PATCH] ShowWidget: Keep track of the podcast it was created from. Since ShowStack now keeps a refference to ShowWidget we no longer need to encode it in the widget name. --- hammond-gtk/src/stacks/show.rs | 37 +++++++++++++-------------------- hammond-gtk/src/widgets/show.rs | 16 +++++++++----- 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/hammond-gtk/src/stacks/show.rs b/hammond-gtk/src/stacks/show.rs index 9f72ae1..c9759cf 100644 --- a/hammond-gtk/src/stacks/show.rs +++ b/hammond-gtk/src/stacks/show.rs @@ -76,38 +76,33 @@ impl ShowStack { pub fn replace_widget(&mut self, pd: Arc) -> Result<(), Error> { let old = self.show.container.clone(); - let oldname = WidgetExt::get_name(&old); - debug!("Name: {:?}", oldname); - oldname - .clone() - .and_then(|id| id.parse().ok()) + // save the ShowWidget vertical scrollabar alignment + self.show + .podcast_id() .map(|id| self.show.save_vadjustment(id)); let new = ShowWidget::new(pd, self.sender.clone()); - // Each composite ShowWidget is a gtkBox with the Podcast.id encoded in the - // gtk::Widget name. It's a hack since we can't yet subclass GObject - // easily. - debug!( - "Old widget Name: {:?}\nNew widget Name: {:?}", - oldname, - WidgetExt::get_name(&new.container) - ); - self.show = new; self.stack.remove(&old); self.stack.add_named(&self.show.container, "widget"); + + // The current visible child might change depending on + // removal and insertion in the gtk::Stack, so we have + // to make sure it will stay the same. + let s = self.state.clone(); + self.switch_visible(s); + Ok(()) } pub fn update_widget(&mut self) -> Result<(), Error> { let old = self.show.container.clone(); - let id = WidgetExt::get_name(&old); - if id == Some("GtkBox".to_string()) || id.is_none() { + let id = self.show.podcast_id(); + if id.is_none() { return Ok(()); } - let id = id.ok_or_else(|| format_err!("Failed to get widget's name."))?; - let pd = dbqueries::get_podcast_from_id(id.parse::()?)?; + let pd = dbqueries::get_podcast_from_id(id.unwrap_or_default())?; self.replace_widget(Arc::new(pd))?; // The current visible child might change depending on @@ -122,13 +117,11 @@ impl ShowStack { // Only update widget if it's podcast_id is equal to pid. pub fn update_widget_if_same(&mut self, pid: i32) -> Result<(), Error> { - let old = &self.show.container.clone(); - - let id = WidgetExt::get_name(old); - if id != Some(pid.to_string()) || id.is_none() { + if self.show.podcast_id() == Some(pid) { debug!("Different widget. Early return"); return Ok(()); } + self.update_widget() } diff --git a/hammond-gtk/src/widgets/show.rs b/hammond-gtk/src/widgets/show.rs index 4a22c9a..5ce4206 100644 --- a/hammond-gtk/src/widgets/show.rs +++ b/hammond-gtk/src/widgets/show.rs @@ -36,6 +36,7 @@ pub struct ShowWidget { settings: gtk::MenuButton, unsub: gtk::Button, episodes: gtk::ListBox, + podcast_id: Option, } impl Default for ShowWidget { @@ -61,6 +62,7 @@ impl Default for ShowWidget { link, settings, episodes, + podcast_id: None, } } } @@ -68,27 +70,27 @@ impl Default for ShowWidget { impl ShowWidget { #[inline] pub fn new(pd: Arc, sender: Sender) -> Rc { - let pdw = Rc::new(ShowWidget::default()); + let mut pdw = ShowWidget::default(); pdw.init(pd.clone(), sender.clone()); + let pdw = Rc::new(pdw); populate_listbox(&pdw, pd, sender) .map_err(|err| error!("Failed to populate the listbox: {}", err)) .ok(); + pdw } #[inline] - pub fn init(&self, pd: Arc, sender: Sender) { + pub fn init(&mut self, pd: Arc, sender: Sender) { let builder = gtk::Builder::new_from_resource("/org/gnome/hammond/gtk/show_widget.ui"); - // Hacky workaround so the pd.id() can be retrieved from the `ShowStack`. - WidgetExt::set_name(&self.container, &pd.id().to_string()); - self.unsub .connect_clicked(clone!(pd, sender => move |bttn| { on_unsub_button_clicked(pd.clone(), bttn, sender.clone()); })); self.set_description(pd.description()); + self.podcast_id = Some(pd.id()); self.set_cover(pd.clone()) .map_err(|err| error!("Failed to set a cover: {}", err)) @@ -168,6 +170,10 @@ impl ShowWidget { Ok(()) } + + pub fn podcast_id(&self) -> Option { + self.podcast_id + } } #[inline]