From ad7f5013f3459b81da88e40d104c5ce8ece26919 Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Mon, 11 Dec 2017 11:15:54 +0200 Subject: [PATCH] Extend ShowsMachine functionality. --- hammond-data/src/parser.rs | 1 - hammond-gtk/src/content.rs | 114 ++++++++++++++++--------------------- 2 files changed, 49 insertions(+), 66 deletions(-) diff --git a/hammond-data/src/parser.rs b/hammond-data/src/parser.rs index 1be8064..393ac54 100644 --- a/hammond-data/src/parser.rs +++ b/hammond-data/src/parser.rs @@ -81,7 +81,6 @@ pub(crate) fn new_episode(item: &Item, parent_id: i32) -> Result { .unwrap()) } - #[cfg(test)] mod tests { use std::fs::File; diff --git a/hammond-gtk/src/content.rs b/hammond-gtk/src/content.rs index be7a491..64063cc 100644 --- a/hammond-gtk/src/content.rs +++ b/hammond-gtk/src/content.rs @@ -97,69 +97,6 @@ pub struct PodcastsView {} #[derive(Debug)] pub struct WidgetsView {} -impl Into> for ContentState { - fn into(self) -> ContentState { - self.content.stack.set_visible_child_name("podcasts"); - - ContentState { - content: self.content, - state: PodcastsView {}, - } - } -} - -impl UpdateView for ContentState { - fn update(&mut self) {} -} - -impl Into> for ContentState { - fn into(self) -> ContentState { - self.content.stack.set_visible_child_name("empty"); - ContentState { - content: self.content, - state: Empty {}, - } - } -} - -impl Into> for ContentState { - fn into(self) -> ContentState { - self.content.stack.set_visible_child_name("widget"); - - ContentState { - content: self.content, - state: WidgetsView {}, - } - } -} - -impl UpdateView for ContentState { - fn update(&mut self) { - let pop = PopulatedView::new_initialized(&self.content.stack); - self.content.replace_podcasts(pop) - } -} - -impl Into> for ContentState { - fn into(self) -> ContentState { - self.content.stack.set_visible_child_name("podcasts"); - ContentState { - content: self.content, - state: PodcastsView {}, - } - } -} - -impl Into> for ContentState { - fn into(self) -> ContentState { - self.content.stack.set_visible_child_name("empty"); - ContentState { - content: self.content, - state: Empty {}, - } - } -} - impl UpdateView for ContentState { fn update(&mut self) { let old = self.content.stack.get_child_by_name("widget").unwrap(); @@ -286,6 +223,20 @@ struct ShowsMachine { state: S, } +impl ShowsMachine { + fn update(&mut self) { + let vis = self.stack.get_visible_child_name().unwrap(); + let old = self.stack.get_child_by_name("shows").unwrap(); + self.stack.remove(&old); + + let pop = ShowsPopulated::new_initialized(&self.stack); + self.populated = pop; + self.stack + .add_titled(&self.populated.container, "shows", "Shows"); + self.stack.set_visible_child_name(&vis); + } +} + #[derive(Debug)] struct EpisodesMachine { populated: EpisodesPopulated, @@ -320,6 +271,7 @@ struct EpisodesMachine { // } // } +// TODO: Impl instead of impl Into> for ShowsMachine { fn into(self) -> ShowsMachine { self.stack.set_visible_child_name("populated"); @@ -378,8 +330,8 @@ impl Into> for EpisodesMachine { // } enum ShowStateWrapper { - Populated(EpisodesMachine), - Empty(EpisodesMachine), + Populated(ShowsMachine), + Empty(ShowsMachine), } enum EpisodeStateWrapper { @@ -397,12 +349,44 @@ enum EpisodeStateWrapper { // } impl ShowStateWrapper { + fn new() -> Self { + let stack = gtk::Stack::new(); + let pop = ShowsPopulated::new_initialized(&stack); + let empty = EmptyView::new(); + + if pop.flowbox.get_children().is_empty() { + stack.set_visible_child_name("empty"); + ShowStateWrapper::Empty(ShowsMachine { + empty, + populated: pop, + stack, + state: Empty {}, + }) + } else { + stack.set_visible_child_name("shows"); + + ShowStateWrapper::Populated(ShowsMachine { + empty, + populated: pop, + stack, + state: Populated {}, + }) + } + } + fn switch(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(), + } + } } impl EpisodeStateWrapper {