Added a probably terrible way to update podcast_view async.

This commit is contained in:
Jordan Petridis 2017-10-21 22:34:20 +03:00
parent f58ad6bd8c
commit 7b0a8f0e25
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
5 changed files with 38 additions and 16 deletions

View File

@ -9,7 +9,7 @@ use utils;
use std::sync::{Arc, Mutex};
pub fn get_headerbar(db: &Arc<Mutex<SqliteConnection>>, stack: &gtk::Stack) -> gtk::HeaderBar {
pub fn get_headerbar(db: Arc<Mutex<SqliteConnection>>, stack: gtk::Stack) -> gtk::HeaderBar {
let builder = include_str!("../gtk/headerbar.ui");
let builder = gtk::Builder::new_from_string(builder);
@ -65,7 +65,7 @@ pub fn get_headerbar(db: &Arc<Mutex<SqliteConnection>>, stack: &gtk::Stack) -> g
let db_clone = db.clone();
// FIXME: There appears to be a memmory leak here.
refresh_button.connect_clicked(move |_| {
utils::refresh_db(&db_clone, &stack_clone);
utils::refresh_db(db_clone.clone(), stack_clone.clone());
});
header

View File

@ -1,10 +1,9 @@
#![cfg_attr(feature = "cargo-clippy", allow(clone_on_ref_ptr))]
// extern crate glib;
extern crate gdk;
extern crate gdk_pixbuf;
extern crate gio;
extern crate glib;
extern crate gtk;
extern crate diesel;
@ -55,7 +54,7 @@ fn build_ui(app: &gtk::Application) {
});
// Get the headerbar
let header = headerbar::get_headerbar(&db, &stack);
let header = headerbar::get_headerbar(db, stack);
// Uncomment this when etag implementation is fixed and refesh_db thread is non blocking.
// utils::refresh_db(&db, &stack);
window.set_titlebar(&header);

View File

@ -1,6 +1,6 @@
#![cfg_attr(feature = "cargo-clippy", allow(clone_on_ref_ptr))]
// use glib;
use glib;
use gtk;
// use gtk::prelude::*;
@ -11,25 +11,37 @@ use hammond_data::models::Source;
use diesel::prelude::SqliteConnection;
use std::thread;
use std::cell::RefCell;
use std::sync::{Arc, Mutex};
use std::sync::mpsc::{channel, Receiver};
use views::podcasts_view;
pub fn refresh_db(db: &Arc<Mutex<SqliteConnection>>, stack: &gtk::Stack) {
thread_local!(
static GLOBAL: RefCell<Option<(Arc<Mutex<SqliteConnection>>,
gtk::Stack,
Receiver<bool>)>> = RefCell::new(None));
pub fn refresh_db(db: Arc<Mutex<SqliteConnection>>, stack: gtk::Stack) {
let (sender, receiver) = channel();
let db_clone = db.clone();
let handle = thread::spawn(move || {
GLOBAL.with(move |global| {
*global.borrow_mut() = Some((db_clone, stack, receiver));
});
// The implementation of how this is done is probably terrible but it works!.
let db_clone = db.clone();
thread::spawn(move || {
let t = hammond_data::index_feed::index_loop(&db_clone, false);
if t.is_err() {
error!("Error While trying to update the database.");
error!("Error msg: {}", t.unwrap_err());
};
});
// FIXME: atm freezing the ui till update is done.
// Make it instead emmit a signal on update completion.
// TODO: emit a signal in order to update the podcast widget.
let _ = handle.join();
sender.send(true).expect("Couldn't send data to channel");;
podcasts_view::update_podcasts_view(db, stack);
glib::idle_add(receive);
});
}
pub fn refresh_feed(db: &Arc<Mutex<SqliteConnection>>, stack: &gtk::Stack, source: &mut Source) {
@ -54,7 +66,6 @@ pub fn refresh_feed(db: &Arc<Mutex<SqliteConnection>>, stack: &gtk::Stack, sourc
// Make it instead emmit a signal on update completion.
// TODO: emit a signal in order to update the podcast widget.
let _ = handle.join();
podcasts_view::update_podcasts_view(db, stack);
}
@ -77,3 +88,14 @@ pub fn refresh_feed(db: &Arc<Mutex<SqliteConnection>>, stack: &gtk::Stack, sourc
// info!("{}", markup);
// markup
// }
fn receive() -> glib::Continue {
GLOBAL.with(|global| {
if let Some((ref db, ref stack, ref reciever)) = *global.borrow() {
if let Ok(_) = reciever.try_recv() {
podcasts_view::update_podcasts_view(db, stack);
}
}
});
glib::Continue(false)
}

View File

@ -100,6 +100,7 @@ fn epidose_widget(
ep
}
pub fn episodes_listbox(connection: &Arc<Mutex<SqliteConnection>>, pd_title: &str) -> gtk::ListBox {
// TODO: handle unwraps.
let m = connection.lock().unwrap();

View File

@ -29,7 +29,7 @@ pub fn podcast_widget(
if let Some(t) = title {
title_label.set_text(t);
let listbox = episodes_listbox(&connection.clone(), t);
let listbox = episodes_listbox(&connection, t);
view.add(&listbox);
}