Commit f30c6455 authored by Jordan Petridis's avatar Jordan Petridis 🌱

hammond_gtk: Refactor refresh_feed func to use the Application channel.

parent 67bc3e52
......@@ -12,7 +12,7 @@ use content::Content;
use utils;
use std::rc::Rc;
use std::sync::mpsc::{channel, Receiver};
use std::sync::mpsc::{channel, Receiver, Sender};
use std::time::Duration;
#[derive(Clone, Debug)]
......@@ -31,6 +31,7 @@ pub struct App {
header: Rc<Header>,
content: Rc<Content>,
receiver: Receiver<Action>,
sender: Sender<Action>,
}
impl App {
......@@ -59,7 +60,7 @@ impl App {
let content = Content::new(sender.clone());
// Create the headerbar
let header = Header::new(content.clone(), sender);
let header = Header::new(content.clone(), sender.clone());
// Add the Headerbar to the window.
window.set_titlebar(&header.container);
......@@ -72,44 +73,39 @@ impl App {
header,
content,
receiver,
sender,
}
}
pub fn setup_actions(&self) {
// Updates the database and refreshes every view.
let update = gio::SimpleAction::new("update", None);
let content = self.content.clone();
let header = self.header.clone();
let sender = self.sender.clone();
update.connect_activate(move |_, _| {
utils::refresh_feed(content.clone(), header.clone(), None);
utils::refresh_feed(header.clone(), None, sender.clone());
});
self.app_instance.add_action(&update);
}
pub fn setup_timed_callbacks(&self) {
let content = self.content.clone();
let header = self.header.clone();
let sender = self.sender.clone();
// Update the feeds right after the Application is initialized.
gtk::timeout_add_seconds(
2,
clone!(content => move || {
utils::refresh_feed(content.clone(), header.clone(), None);
gtk::timeout_add_seconds(2, move || {
utils::refresh_feed(header.clone(), None, sender.clone());
glib::Continue(false)
}),
);
});
let content = self.content.clone();
let header = self.header.clone();
let sender = self.sender.clone();
// Auto-updater, runs every hour.
// TODO: expose the interval in which it run to a user setting.
// TODO: show notifications.
gtk::timeout_add_seconds(
3600,
clone!(content => move || {
utils::refresh_feed(content.clone(), header.clone(), None);
gtk::timeout_add_seconds(3600, move || {
utils::refresh_feed(header.clone(), None, sender.clone());
glib::Continue(true)
}),
);
});
// Run a database checkup once the application is initialized.
gtk::timeout_add(300, || {
......@@ -130,11 +126,12 @@ impl App {
let receiver = self.receiver;
let content = self.content.clone();
let headerbar = self.header.clone();
let sender = self.sender.clone();
gtk::idle_add(clone!(content, headerbar => move || {
match receiver.recv_timeout(Duration::from_millis(5)) {
Ok(Action::UpdateSources(source)) => {
if let Some(s) = source {
utils::refresh_feed(content.clone(), headerbar.clone(), Some(vec!(s)))
utils::refresh_feed(headerbar.clone(), Some(vec!(s)), sender.clone())
}
}
Ok(Action::RefreshViews) => {
......
......@@ -8,15 +8,15 @@ use hammond_downloader::downloader;
use std::thread;
use std::cell::RefCell;
use std::sync::mpsc::{channel, Receiver};
use std::sync::mpsc::{channel, Receiver, Sender};
use std::sync::Mutex;
use std::rc::Rc;
use std::collections::HashMap;
use content::Content;
use headerbar::Header;
use app::Action;
type Foo = RefCell<Option<(Rc<Content>, Rc<Header>, Receiver<bool>)>>;
type Foo = RefCell<Option<(Rc<Header>, Receiver<bool>)>>;
// Create a thread local storage that will store the arguments to be transfered.
thread_local!(static GLOBAL: Foo = RefCell::new(None));
......@@ -25,15 +25,19 @@ thread_local!(static GLOBAL: Foo = RefCell::new(None));
/// If `source` is None, Fetches all the `Source` entries in the database and updates them.
/// `delay` represents the desired time in seconds for the thread to sleep before executing.
/// When It's done,it queues up a `podcast_view` refresh.
pub fn refresh_feed(content: Rc<Content>, headerbar: Rc<Header>, source: Option<Vec<Source>>) {
pub fn refresh_feed(
headerbar: Rc<Header>,
source: Option<Vec<Source>>,
app_sender: Sender<Action>,
) {
headerbar.show_update_notification();
// Create a async channel.
let (sender, receiver) = channel();
// Pass the desired arguments into the Local Thread Storage.
GLOBAL.with(clone!(content, headerbar => move |global| {
*global.borrow_mut() = Some((content.clone(), headerbar.clone(), receiver));
GLOBAL.with(clone!(headerbar => move |global| {
*global.borrow_mut() = Some((headerbar.clone(), receiver));
}));
thread::spawn(move || {
......@@ -47,16 +51,17 @@ pub fn refresh_feed(content: Rc<Content>, headerbar: Rc<Header>, source: Option<
};
};
sender.send(true).expect("Couldn't send data to channel");;
glib::idle_add(refresh_everything);
app_sender.send(Action::RefreshViews).unwrap();
let _ = sender.send(true);
glib::idle_add(hide_update_indicator);
});
}
fn refresh_everything() -> glib::Continue {
fn hide_update_indicator() -> glib::Continue {
GLOBAL.with(|global| {
if let Some((ref content, ref headerbar, ref reciever)) = *global.borrow() {
if let Some((ref headerbar, ref reciever)) = *global.borrow() {
if reciever.try_recv().is_ok() {
content.update();
headerbar.hide_update_notification();
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment