From 8bd48a09a6f2b43abcf9e01c66bb4cb9744ba6ec Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Sat, 2 Dec 2017 08:58:28 +0200 Subject: [PATCH] Groupd stack manipulation functions into content module. --- hammond-gtk/src/content.rs | 79 ++++++++++++++++++++++++++++++ hammond-gtk/src/headerbar.rs | 8 +-- hammond-gtk/src/utils.rs | 4 +- hammond-gtk/src/views/podcasts.rs | 39 ++------------- hammond-gtk/src/widgets/podcast.rs | 28 ++++------- org.gnome.Hammond.json | 2 +- 6 files changed, 97 insertions(+), 63 deletions(-) diff --git a/hammond-gtk/src/content.rs b/hammond-gtk/src/content.rs index 99194dc..2d91541 100644 --- a/hammond-gtk/src/content.rs +++ b/hammond-gtk/src/content.rs @@ -3,6 +3,7 @@ use gtk::prelude::*; // use gdk_pixbuf::Pixbuf; // use diesel::associations::Identifiable; +use hammond_data::Podcast; use widgets::podcast::PodcastWidget; use views::podcasts::PopulatedView; @@ -65,3 +66,81 @@ impl Content { } } } + +fn replace_widget(stack: >k::Stack, pdw: &PodcastWidget) { + let old = stack.get_child_by_name("widget").unwrap(); + stack.remove(&old); + stack.add_named(&pdw.container, "widget"); + old.destroy(); +} + +fn replace_podcasts(stack: >k::Stack, pop: &PopulatedView) { + let old = stack.get_child_by_name("podcasts").unwrap(); + stack.remove(&old); + stack.add_named(&pop.container, "podcasts"); + old.destroy(); +} + +// This won't ever be needed probably +// pub fn replace_empty(stack: >k::Stack, emp: &EmptyView ) { +// let old = stack.get_child_by_name("empty").unwrap(); +// stack.remove(&old); +// stack.add_named(&emp.container, "empty"); +// old.destroy(); +// } + +pub fn show_widget(stack: >k::Stack) { + stack.set_visible_child_name("widget") +} + +pub fn show_podcasts(stack: >k::Stack) { + stack.set_visible_child_name("podcasts") +} + +pub fn show_empty(stack: >k::Stack) { + stack.set_visible_child_name("empty") +} + +pub fn update_podcasts(stack: >k::Stack) { + let pods = PopulatedView::new_initialized(stack); + + if pods.flowbox.get_children().is_empty() { + show_empty(stack) + } + + 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); +} + +pub fn on_home_button_activate(stack: >k::Stack) { + let vis = stack.get_visible_child_name().unwrap(); + + if vis != "widget" { + update_podcasts(stack); + } + + show_podcasts(stack); +} diff --git a/hammond-gtk/src/headerbar.rs b/hammond-gtk/src/headerbar.rs index 7e13ffa..20f491e 100644 --- a/hammond-gtk/src/headerbar.rs +++ b/hammond-gtk/src/headerbar.rs @@ -4,8 +4,8 @@ use gtk::prelude::*; use hammond_data::Source; use hammond_data::utils::url_cleaner; -use views::podcasts::update_podcasts_view; use utils; +use content; #[derive(Debug)] pub struct Header { @@ -61,11 +61,7 @@ impl Header { // TODO: make it a back arrow button, that will hide when appropriate, // and add a StackSwitcher when more views are added. self.home.connect_clicked(clone!(stack => move |_| { - let vis = stack.get_visible_child_name().unwrap(); - stack.set_visible_child_name("podcasts"); - if vis != "widget" { - update_podcasts_view(&stack); - } + content::on_home_button_activate(&stack); })); // FIXME: There appears to be a memmory leak here. diff --git a/hammond-gtk/src/utils.rs b/hammond-gtk/src/utils.rs index e0aff6c..14e8b46 100644 --- a/hammond-gtk/src/utils.rs +++ b/hammond-gtk/src/utils.rs @@ -10,7 +10,7 @@ use std::{thread, time}; use std::cell::RefCell; use std::sync::mpsc::{channel, Receiver}; -use views::podcasts; +use content; type Foo = RefCell)>>; @@ -57,7 +57,7 @@ fn refresh_podcasts_view() -> glib::Continue { GLOBAL.with(|global| { if let Some((ref stack, ref reciever)) = *global.borrow() { if reciever.try_recv().is_ok() { - podcasts::update_podcasts_view(stack); + content::update_podcasts_preserve_vis(stack); } } }); diff --git a/hammond-gtk/src/views/podcasts.rs b/hammond-gtk/src/views/podcasts.rs index 0d906a4..711be87 100644 --- a/hammond-gtk/src/views/podcasts.rs +++ b/hammond-gtk/src/views/podcasts.rs @@ -6,9 +6,10 @@ use diesel::associations::Identifiable; use hammond_data::dbqueries; use hammond_data::Podcast; -use widgets::podcast::*; use utils::get_pixbuf_from_path; +use content; + #[derive(Debug, Clone)] pub struct PopulatedView { pub container: gtk::Box, @@ -139,39 +140,5 @@ impl PodcastChild { } fn on_flowbox_child_activate(stack: >k::Stack, parent: &Podcast) { - let old = stack.get_child_by_name("widget").unwrap(); - let pdw = PodcastWidget::new(); - pdw.init(stack, parent); - - stack.remove(&old); - stack.add_named(&pdw.container, "widget"); - stack.set_visible_child_full("widget", gtk::StackTransitionType::SlideLeft); - - // aggresive memory cleanup - // probably not needed - old.destroy(); -} - -pub fn update_podcasts_view(stack: >k::Stack) { - let vis = stack.get_visible_child_name().unwrap(); - let old = stack.get_child_by_name("podcasts").unwrap(); - stack.remove(&old); - - let pdw = PopulatedView::new(); - pdw.init(stack); - stack.add_named(&pdw.container, "podcasts"); - let flowbox = &pdw.flowbox; - - if vis == "empty" && !flowbox.get_children().is_empty() { - stack.set_visible_child_name("podcasts"); - } else if vis == "podcasts" && flowbox.get_children().is_empty() { - stack.set_visible_child_name("empty"); - } else { - // preserve the visible widget - stack.set_visible_child_name(&vis); - }; - - // aggresive memory cleanup - // probably not needed - old.destroy(); + content::on_podcasts_child_activate(stack, parent) } diff --git a/hammond-gtk/src/widgets/podcast.rs b/hammond-gtk/src/widgets/podcast.rs index 116e221..140e313 100644 --- a/hammond-gtk/src/widgets/podcast.rs +++ b/hammond-gtk/src/widgets/podcast.rs @@ -8,8 +8,8 @@ use hammond_data::Podcast; use hammond_downloader::downloader; use widgets::episode::episodes_listbox; -use views::podcasts::update_podcasts_view; use utils::get_pixbuf_from_path; +use content; #[derive(Debug)] pub struct PodcastWidget { @@ -46,6 +46,12 @@ impl PodcastWidget { } } + pub fn new_initialized(stack: >k::Stack, pd: &Podcast) -> PodcastWidget { + let pdw = PodcastWidget::new(); + pdw.init(stack, pd); + pdw + } + pub fn init(&self, stack: >k::Stack, pd: &Podcast) { // TODO: should spawn a thread to avoid locking the UI probably. self.unsub.connect_clicked(clone!(stack, pd => move |bttn| { @@ -86,7 +92,6 @@ impl PodcastWidget { } } -// Note: Stack manipulation fn on_unsub_button_clicked(stack: >k::Stack, pd: &Podcast, unsub_button: >k::Button) { let res = dbqueries::remove_feed(pd); if res.is_ok() { @@ -103,25 +108,12 @@ fn on_unsub_button_clicked(stack: >k::Stack, pd: &Podcast, unsub_button: >k: } }; } - stack.set_visible_child_name("podcasts"); - update_podcasts_view(stack); + content::show_podcasts(stack); + content::update_podcasts(stack); } fn on_played_button_clicked(stack: >k::Stack, pd: &Podcast) { let _ = dbqueries::update_none_to_played_now(pd); - update_podcast_widget(stack, pd); -} - -// Note: Stack manipulation -pub fn update_podcast_widget(stack: >k::Stack, pd: &Podcast) { - let old = stack.get_child_by_name("widget").unwrap(); - let pdw = PodcastWidget::new(); - pdw.init(stack, pd); - let vis = stack.get_visible_child_name().unwrap(); - - stack.remove(&old); - stack.add_named(&pdw.container, "widget"); - stack.set_visible_child_name(&vis); - old.destroy(); + content::update_widget_preserve_vis(stack, pd); } diff --git a/org.gnome.Hammond.json b/org.gnome.Hammond.json index a3638e5..459b13c 100644 --- a/org.gnome.Hammond.json +++ b/org.gnome.Hammond.json @@ -30,7 +30,7 @@ { "type": "git", "url": "https://gitlab.gnome.org/alatiera/Hammond.git", - "branch": "client-rework" + "branch": "master" } ] }