diff --git a/hammond-gtk/src/content.rs b/hammond-gtk/src/content.rs index 1001dce..4b7d32a 100644 --- a/hammond-gtk/src/content.rs +++ b/hammond-gtk/src/content.rs @@ -1,13 +1,14 @@ use gtk; use gtk::prelude::*; -use hammond_data::Podcast; +// use hammond_data::Podcast; use hammond_data::dbqueries; use widgets::podcast::PodcastWidget; use views::podcasts::PopulatedView; use views::empty::EmptyView; +#[derive(Debug, Clone)] pub struct Content { pub stack: gtk::Stack, shows: ShowStateWrapper, @@ -34,50 +35,12 @@ impl Content { } pub fn update(&mut self) { - self.shows.update(); + self.shows = self.shows.clone().update(); + // FIXME: like above self.episodes.update(); } } -fn replace_widget(stack: >k::Stack, pdw: &PodcastWidget) { - let old = stack.get_child_by_name("widget").unwrap(); - stack.remove(&old); - stack.add_titled(&pdw.container, "widget", "Episode"); - old.destroy(); -} - -fn replace_podcasts(stack: >k::Stack, pop: &PopulatedView) { - let old = stack.get_child_by_name("podcasts").unwrap(); - stack.remove(&old); - stack.add_titled(&pop.container, "podcasts", "Shows"); - old.destroy(); -} - -pub fn update_podcasts(stack: >k::Stack) { - let pods = PopulatedView::new_initialized(); - - replace_podcasts(stack, &pods); -} - -pub fn update_widget(stack: >k::Stack, pd: &Podcast) { - let pdw = PodcastWidget::new_initialized(stack, pd); - replace_widget(stack, &pdw); -} - -pub fn update_podcasts_preserve_vis(stack: >k::Stack) { - let vis = stack.get_visible_child_name().unwrap(); - update_podcasts(stack); - if vis != "empty" { - stack.set_visible_child_name(&vis) - } -} - -pub fn update_widget_preserve_vis(stack: >k::Stack, pd: &Podcast) { - let vis = stack.get_visible_child_name().unwrap(); - update_widget(stack, pd); - stack.set_visible_child_name(&vis) -} - // pub fn on_podcasts_child_activate(stack: >k::Stack, pd: &Podcast) { // update_widget(stack, pd); // stack.set_visible_child_full("widget", gtk::StackTransitionType::SlideLeft); @@ -89,7 +52,9 @@ type ShowsEmpty = EmptyView; type EpisodesPopulated = PodcastWidget; type EpisodesEmpty = EmptyView; +#[derive(Debug, Clone)] struct Populated; +#[derive(Debug, Clone)] struct Empty; // struct Shows; // struct Episodes; @@ -111,6 +76,21 @@ struct ShowsMachine { } impl ShowsMachine { + fn new(state: S) -> ShowsMachine { + let stack = gtk::Stack::new(); + let pop = ShowsPopulated::new_initialized(); + let empty = EmptyView::new(); + stack.add_named(&pop.container, "populated"); + stack.add_named(&empty.container, "empty"); + + ShowsMachine { + empty, + populated: pop, + stack, + state, + } + } + fn update(&mut self) { let vis = self.stack.get_visible_child_name().unwrap(); let old = self.stack.get_child_by_name("populated").unwrap(); @@ -123,7 +103,7 @@ impl ShowsMachine { } } -#[derive(Debug)] +#[derive(Debug, Clone)] struct EpisodesMachine { populated: EpisodesPopulated, empty: EpisodesEmpty, @@ -235,11 +215,13 @@ impl Into> for EpisodesMachine { // Episodes(StackStateMachine), // } +#[derive(Debug, Clone)] enum ShowStateWrapper { Populated(ShowsMachine), Empty(ShowsMachine), } +#[derive(Debug, Clone)] enum EpisodeStateWrapper { Populated(EpisodesMachine), Empty(EpisodesMachine), @@ -256,44 +238,34 @@ enum EpisodeStateWrapper { impl ShowStateWrapper { fn new() -> Self { - let stack = gtk::Stack::new(); - let pop = ShowsPopulated::new_initialized(); - let empty = EmptyView::new(); - stack.add_named(&pop.container, "populated"); - stack.add_named(&empty.container, "empty"); + let machine = ShowsMachine::new(Populated {}); - if pop.flowbox.get_children().is_empty() { - stack.set_visible_child_name("empty"); - ShowStateWrapper::Empty(ShowsMachine { - empty, - populated: pop, - stack, - state: Empty {}, - }) + if machine.populated.flowbox.get_children().is_empty() { + machine.stack.set_visible_child_name("empty"); + ShowStateWrapper::Empty(machine.into()) } else { - stack.set_visible_child_name("populated"); - - ShowStateWrapper::Populated(ShowsMachine { - empty, - populated: pop, - stack, - state: Populated {}, - }) + machine.stack.set_visible_child_name("populated"); + ShowStateWrapper::Populated(machine) } } - fn switch(self) -> Self { + fn update(mut self) -> Self { match self { - ShowStateWrapper::Populated(val) => ShowStateWrapper::Empty(val.into()), - ShowStateWrapper::Empty(val) => ShowStateWrapper::Populated(val.into()), - } - } - - fn update(&mut self) { - match *self { ShowStateWrapper::Populated(ref mut val) => val.update(), ShowStateWrapper::Empty(ref mut val) => val.update(), } + + if self.is_empty() { + match self { + ShowStateWrapper::Populated(val) => ShowStateWrapper::Empty(val.into()), + _ => self, + } + } else { + match self { + ShowStateWrapper::Empty(val) => ShowStateWrapper::Populated(val.into()), + _ => self, + } + } } fn get_stack(&self) -> gtk::Stack { @@ -302,6 +274,13 @@ impl ShowStateWrapper { ShowStateWrapper::Empty(ref val) => val.stack.clone(), } } + + fn is_empty(&self) -> bool { + match *self { + ShowStateWrapper::Populated(ref val) => val.populated.flowbox.get_children().is_empty(), + ShowStateWrapper::Empty(ref val) => val.populated.flowbox.get_children().is_empty(), + } + } } impl EpisodeStateWrapper { @@ -330,13 +309,6 @@ impl EpisodeStateWrapper { } } - fn switch(self) -> Self { - match self { - EpisodeStateWrapper::Populated(val) => EpisodeStateWrapper::Empty(val.into()), - EpisodeStateWrapper::Empty(val) => EpisodeStateWrapper::Populated(val.into()), - } - } - fn get_stack(&self) -> gtk::Stack { match *self { EpisodeStateWrapper::Populated(ref val) => val.stack.clone(), diff --git a/hammond-gtk/src/headerbar.rs b/hammond-gtk/src/headerbar.rs index 34c8087..956e040 100644 --- a/hammond-gtk/src/headerbar.rs +++ b/hammond-gtk/src/headerbar.rs @@ -58,9 +58,8 @@ impl Header { println!("{:?}", url.get_text()); }); - let cont = content.clone(); - add_button.connect_clicked(clone!(cont, add_popover, new_url => move |_| { - on_add_bttn_clicked(cont.clone(), &new_url); + add_button.connect_clicked(clone!(content, add_popover, new_url => move |_| { + on_add_bttn_clicked(content.clone(), &new_url); // TODO: lock the button instead of hiding and add notification of feed added. // TODO: map the spinner @@ -70,9 +69,9 @@ impl Header { // FIXME: There appears to be a memmory leak here. let cont = content.clone(); - self.refresh.connect_clicked(clone!(cont => move |_| { + self.refresh.connect_clicked(move |_| { utils::refresh_feed(cont.clone(), None, None); - })); + }); } } diff --git a/hammond-gtk/src/widgets/podcast.rs b/hammond-gtk/src/widgets/podcast.rs index b447384..c0d4c34 100644 --- a/hammond-gtk/src/widgets/podcast.rs +++ b/hammond-gtk/src/widgets/podcast.rs @@ -10,9 +10,9 @@ use hammond_downloader::downloader; use widgets::episode::episodes_listbox; use utils::get_pixbuf_from_path; -use content; +// use content; -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct PodcastWidget { pub container: gtk::Box, cover: gtk::Image, @@ -111,11 +111,11 @@ fn on_unsub_button_clicked(stack: >k::Stack, pd: &Podcast, unsub_button: >k: } }; } - content::update_podcasts(stack); + // content::update_podcasts(stack); } fn on_played_button_clicked(stack: >k::Stack, pd: &Podcast) { let _ = dbqueries::update_none_to_played_now(pd); - content::update_widget_preserve_vis(stack, pd); + // content::update_widget_preserve_vis(stack, pd); }