diff --git a/hammond-gtk/src/app.rs b/hammond-gtk/src/app.rs index 55f2ca7..9f10fa4 100644 --- a/hammond-gtk/src/app.rs +++ b/hammond-gtk/src/app.rs @@ -10,7 +10,7 @@ use hammond_data::Podcast; use headerbar::Header; use settings::{self, WindowGeometry}; -use stacks::Content; +use stacks::{Content, ShowState}; use utils; use widgets::{mark_all_notif, remove_show_notif}; @@ -181,11 +181,11 @@ impl App { } Ok(Action::ShowWidgetAnimated) => { let mut shows = content.get_shows(); - shows.borrow().switch_widget_animated(); + shows.borrow_mut().switch_visible(ShowState::ShowWidget); } Ok(Action::ShowShowsAnimated) => { let mut shows = content.get_shows(); - shows.borrow().switch_podcasts_animated(); + shows.borrow_mut().switch_visible(ShowState::ShowsView); } Ok(Action::HeaderBarShowTile(title)) => headerbar.switch_to_back(&title), Ok(Action::HeaderBarNormal) => headerbar.switch_to_normal(), diff --git a/hammond-gtk/src/stacks/content.rs b/hammond-gtk/src/stacks/content.rs index 4d4861d..c0274c7 100644 --- a/hammond-gtk/src/stacks/content.rs +++ b/hammond-gtk/src/stacks/content.rs @@ -58,7 +58,7 @@ impl Content { pub fn update_shows_view(&self) { self.shows .borrow_mut() - .update_podcasts() + .update_shows() .map_err(|err| error!("Failed to update ShowsView: {}", err)) .ok(); } diff --git a/hammond-gtk/src/stacks/mod.rs b/hammond-gtk/src/stacks/mod.rs index ce00078..cfa465b 100644 --- a/hammond-gtk/src/stacks/mod.rs +++ b/hammond-gtk/src/stacks/mod.rs @@ -4,4 +4,4 @@ mod show; pub use self::content::Content; pub use self::home::HomeStack; -pub use self::show::ShowStack; +pub use self::show::{ShowStack, ShowState}; diff --git a/hammond-gtk/src/stacks/show.rs b/hammond-gtk/src/stacks/show.rs index 0d46344..9f72ae1 100644 --- a/hammond-gtk/src/stacks/show.rs +++ b/hammond-gtk/src/stacks/show.rs @@ -4,43 +4,45 @@ use gtk::prelude::*; use failure::Error; use hammond_data::dbqueries; -use hammond_data::errors::DataError; use hammond_data::Podcast; use app::Action; -use widgets::{EmptyView, ShowWidget, ShowsPopulated}; +use widgets::{ShowWidget, ShowsPopulated}; use std::rc::Rc; use std::sync::mpsc::Sender; use std::sync::Arc; +#[derive(Debug, Clone)] +pub enum ShowState { + ShowsView, + ShowWidget, +} + #[derive(Debug, Clone)] pub struct ShowStack { - stack: gtk::Stack, - podcasts: Rc, + populated: Rc, show: Rc, - empty: EmptyView, + stack: gtk::Stack, + state: ShowState, sender: Sender, } impl ShowStack { pub fn new(sender: Sender) -> Result { let stack = gtk::Stack::new(); - - let podcasts = ShowsPopulated::new(sender.clone())?; + let state = ShowState::ShowsView; + let populated = ShowsPopulated::new(sender.clone())?; let show = Rc::new(ShowWidget::default()); - let empty = EmptyView::new(); - stack.add_named(&podcasts.container, "podcasts"); + stack.add_named(&populated.container, "shows"); stack.add_named(&show.container, "widget"); - stack.add_named(&empty.container, "empty"); - set_stack_visible(&stack)?; let show = ShowStack { stack, - podcasts, + populated, show, - empty, + state, sender, }; @@ -52,26 +54,20 @@ impl ShowStack { // self.update_podcasts(); // } - pub fn update_podcasts(&mut self) -> Result<(), Error> { - let vis = self.stack - .get_visible_child_name() - .ok_or_else(|| format_err!("Failed to get visible child name."))?; - - let old = &self.podcasts.container.clone(); + pub fn update_shows(&mut self) -> Result<(), Error> { + let old = &self.populated.container.clone(); debug!("Name: {:?}", WidgetExt::get_name(old)); let pop = ShowsPopulated::new(self.sender.clone())?; - self.podcasts = pop; + self.populated = pop; self.stack.remove(old); - self.stack.add_named(&self.podcasts.container, "podcasts"); + self.stack.add_named(&self.populated.container, "shows"); - if !dbqueries::is_podcasts_populated()? { - self.stack.set_visible_child_name("empty"); - } else if vis != "empty" { - self.stack.set_visible_child_name(&vis); - } else { - self.stack.set_visible_child_name("podcasts"); - } + // 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); old.destroy(); Ok(()) @@ -104,10 +100,6 @@ impl ShowStack { } pub fn update_widget(&mut self) -> Result<(), Error> { - let vis = self.stack - .get_visible_child_name() - .ok_or_else(|| format_err!("Failed to get visible child name."))?; - let old = self.show.container.clone(); let id = WidgetExt::get_name(&old); if id == Some("GtkBox".to_string()) || id.is_none() { @@ -117,7 +109,13 @@ impl ShowStack { let id = id.ok_or_else(|| format_err!("Failed to get widget's name."))?; let pd = dbqueries::get_podcast_from_id(id.parse::()?)?; self.replace_widget(Arc::new(pd))?; - self.stack.set_visible_child_name(&vis); + + // 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); + old.destroy(); Ok(()) } @@ -134,28 +132,25 @@ impl ShowStack { self.update_widget() } - pub fn switch_podcasts_animated(&self) { - self.stack - .set_visible_child_full("podcasts", gtk::StackTransitionType::SlideRight); - } - - pub fn switch_widget_animated(&self) { - self.stack - .set_visible_child_full("widget", gtk::StackTransitionType::SlideLeft) - } - pub fn get_stack(&self) -> gtk::Stack { self.stack.clone() } -} -#[inline] -fn set_stack_visible(stack: >k::Stack) -> Result<(), DataError> { - if dbqueries::is_podcasts_populated()? { - stack.set_visible_child_name("podcasts") - } else { - stack.set_visible_child_name("empty") + #[inline] + pub fn switch_visible(&mut self, state: ShowState) { + use self::ShowState::*; + + match state { + ShowsView => { + self.stack + .set_visible_child_full("shows", gtk::StackTransitionType::SlideRight); + self.state = ShowsView; + } + ShowWidget => { + self.stack + .set_visible_child_full("widget", gtk::StackTransitionType::SlideLeft); + self.state = ShowWidget; + } + } } - - Ok(()) }