Groupd stack manipulation functions into content module.

This commit is contained in:
Jordan Petridis 2017-12-02 08:58:28 +02:00
parent 7727bc5ec3
commit 8bd48a09a6
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
6 changed files with 97 additions and 63 deletions

View File

@ -3,6 +3,7 @@ use gtk::prelude::*;
// use gdk_pixbuf::Pixbuf; // use gdk_pixbuf::Pixbuf;
// use diesel::associations::Identifiable; // use diesel::associations::Identifiable;
use hammond_data::Podcast;
use widgets::podcast::PodcastWidget; use widgets::podcast::PodcastWidget;
use views::podcasts::PopulatedView; use views::podcasts::PopulatedView;
@ -65,3 +66,81 @@ impl Content {
} }
} }
} }
fn replace_widget(stack: &gtk::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: &gtk::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: &gtk::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: &gtk::Stack) {
stack.set_visible_child_name("widget")
}
pub fn show_podcasts(stack: &gtk::Stack) {
stack.set_visible_child_name("podcasts")
}
pub fn show_empty(stack: &gtk::Stack) {
stack.set_visible_child_name("empty")
}
pub fn update_podcasts(stack: &gtk::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: &gtk::Stack, pd: &Podcast) {
let pdw = PodcastWidget::new_initialized(stack, pd);
replace_widget(stack, &pdw);
}
pub fn update_podcasts_preserve_vis(stack: &gtk::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: &gtk::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: &gtk::Stack, pd: &Podcast) {
update_widget(stack, pd);
stack.set_visible_child_full("widget", gtk::StackTransitionType::SlideLeft);
}
pub fn on_home_button_activate(stack: &gtk::Stack) {
let vis = stack.get_visible_child_name().unwrap();
if vis != "widget" {
update_podcasts(stack);
}
show_podcasts(stack);
}

View File

@ -4,8 +4,8 @@ use gtk::prelude::*;
use hammond_data::Source; use hammond_data::Source;
use hammond_data::utils::url_cleaner; use hammond_data::utils::url_cleaner;
use views::podcasts::update_podcasts_view;
use utils; use utils;
use content;
#[derive(Debug)] #[derive(Debug)]
pub struct Header { pub struct Header {
@ -61,11 +61,7 @@ impl Header {
// TODO: make it a back arrow button, that will hide when appropriate, // TODO: make it a back arrow button, that will hide when appropriate,
// and add a StackSwitcher when more views are added. // and add a StackSwitcher when more views are added.
self.home.connect_clicked(clone!(stack => move |_| { self.home.connect_clicked(clone!(stack => move |_| {
let vis = stack.get_visible_child_name().unwrap(); content::on_home_button_activate(&stack);
stack.set_visible_child_name("podcasts");
if vis != "widget" {
update_podcasts_view(&stack);
}
})); }));
// FIXME: There appears to be a memmory leak here. // FIXME: There appears to be a memmory leak here.

View File

@ -10,7 +10,7 @@ use std::{thread, time};
use std::cell::RefCell; use std::cell::RefCell;
use std::sync::mpsc::{channel, Receiver}; use std::sync::mpsc::{channel, Receiver};
use views::podcasts; use content;
type Foo = RefCell<Option<(gtk::Stack, Receiver<bool>)>>; type Foo = RefCell<Option<(gtk::Stack, Receiver<bool>)>>;
@ -57,7 +57,7 @@ fn refresh_podcasts_view() -> glib::Continue {
GLOBAL.with(|global| { GLOBAL.with(|global| {
if let Some((ref stack, ref reciever)) = *global.borrow() { if let Some((ref stack, ref reciever)) = *global.borrow() {
if reciever.try_recv().is_ok() { if reciever.try_recv().is_ok() {
podcasts::update_podcasts_view(stack); content::update_podcasts_preserve_vis(stack);
} }
} }
}); });

View File

@ -6,9 +6,10 @@ use diesel::associations::Identifiable;
use hammond_data::dbqueries; use hammond_data::dbqueries;
use hammond_data::Podcast; use hammond_data::Podcast;
use widgets::podcast::*;
use utils::get_pixbuf_from_path; use utils::get_pixbuf_from_path;
use content;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct PopulatedView { pub struct PopulatedView {
pub container: gtk::Box, pub container: gtk::Box,
@ -139,39 +140,5 @@ impl PodcastChild {
} }
fn on_flowbox_child_activate(stack: &gtk::Stack, parent: &Podcast) { fn on_flowbox_child_activate(stack: &gtk::Stack, parent: &Podcast) {
let old = stack.get_child_by_name("widget").unwrap(); content::on_podcasts_child_activate(stack, parent)
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: &gtk::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();
} }

View File

@ -8,8 +8,8 @@ use hammond_data::Podcast;
use hammond_downloader::downloader; use hammond_downloader::downloader;
use widgets::episode::episodes_listbox; use widgets::episode::episodes_listbox;
use views::podcasts::update_podcasts_view;
use utils::get_pixbuf_from_path; use utils::get_pixbuf_from_path;
use content;
#[derive(Debug)] #[derive(Debug)]
pub struct PodcastWidget { pub struct PodcastWidget {
@ -46,6 +46,12 @@ impl PodcastWidget {
} }
} }
pub fn new_initialized(stack: &gtk::Stack, pd: &Podcast) -> PodcastWidget {
let pdw = PodcastWidget::new();
pdw.init(stack, pd);
pdw
}
pub fn init(&self, stack: &gtk::Stack, pd: &Podcast) { pub fn init(&self, stack: &gtk::Stack, pd: &Podcast) {
// TODO: should spawn a thread to avoid locking the UI probably. // TODO: should spawn a thread to avoid locking the UI probably.
self.unsub.connect_clicked(clone!(stack, pd => move |bttn| { self.unsub.connect_clicked(clone!(stack, pd => move |bttn| {
@ -86,7 +92,6 @@ impl PodcastWidget {
} }
} }
// Note: Stack manipulation
fn on_unsub_button_clicked(stack: &gtk::Stack, pd: &Podcast, unsub_button: &gtk::Button) { fn on_unsub_button_clicked(stack: &gtk::Stack, pd: &Podcast, unsub_button: &gtk::Button) {
let res = dbqueries::remove_feed(pd); let res = dbqueries::remove_feed(pd);
if res.is_ok() { if res.is_ok() {
@ -103,25 +108,12 @@ fn on_unsub_button_clicked(stack: &gtk::Stack, pd: &Podcast, unsub_button: &gtk:
} }
}; };
} }
stack.set_visible_child_name("podcasts"); content::show_podcasts(stack);
update_podcasts_view(stack); content::update_podcasts(stack);
} }
fn on_played_button_clicked(stack: &gtk::Stack, pd: &Podcast) { fn on_played_button_clicked(stack: &gtk::Stack, pd: &Podcast) {
let _ = dbqueries::update_none_to_played_now(pd); let _ = dbqueries::update_none_to_played_now(pd);
update_podcast_widget(stack, pd); content::update_widget_preserve_vis(stack, pd);
}
// Note: Stack manipulation
pub fn update_podcast_widget(stack: &gtk::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();
} }

View File

@ -30,7 +30,7 @@
{ {
"type": "git", "type": "git",
"url": "https://gitlab.gnome.org/alatiera/Hammond.git", "url": "https://gitlab.gnome.org/alatiera/Hammond.git",
"branch": "client-rework" "branch": "master"
} }
] ]
} }