Removed code duplication and combined refresh_feed and refresh_db.

This commit is contained in:
Jordan Petridis
2017-11-04 13:36:25 +02:00
parent 7e8f39119d
commit 7a1b272d9e
8 changed files with 34 additions and 45 deletions
+1
View File
@@ -15,6 +15,7 @@ loggerv = "0.4"
log = "0.3"
open = "1.2"
dissolve = "0.2"
rayon = "0.8"
hammond-data = {path = "../hammond-data"}
hammond-downloader = {path = "../hammond-downloader"}
+3 -3
View File
@@ -49,7 +49,7 @@ pub fn get_headerbar(db: &Database, stack: &gtk::Stack) -> gtk::HeaderBar {
// FIXME: There appears to be a memmory leak here.
refresh_button.connect_clicked(clone!(stack, db => move |_| {
utils::refresh_db(&db, &stack);
utils::refresh_feed(&db, &stack, None);
}));
header
@@ -62,9 +62,9 @@ fn on_add_bttn_clicked(db: &Database, stack: &gtk::Stack, url: &str) {
};
info!("{:?} feed added", url);
if let Ok(mut s) = source {
if let Ok(s) = source {
// update the db
utils::refresh_feed(db, stack, &mut s);
utils::refresh_feed(db, stack, Some(Box::new(vec![s])));
} else {
error!("Expected Error, feed probably already exists.");
error!("Error: {:?}", source.unwrap_err());
+1
View File
@@ -12,6 +12,7 @@ extern crate hammond_downloader;
extern crate log;
extern crate loggerv;
extern crate open;
extern crate rayon;
use log::LogLevel;
use hammond_data::dbcheckup;
+17 -35
View File
@@ -1,9 +1,8 @@
use glib;
use gtk;
// use gtk::prelude::*;
use rayon::prelude::*;
use hammond_data;
use hammond_data::index_feed::Feed;
use hammond_data::index_feed;
use hammond_data::models::Source;
use hammond_data::index_feed::Database;
@@ -19,48 +18,31 @@ thread_local!(
gtk::Stack,
Receiver<bool>)>> = RefCell::new(None));
pub fn refresh_db(db: &Database, stack: &gtk::Stack) {
// Create a async channel.
pub fn refresh_feed(db: &Database, stack: &gtk::Stack, source: Option<Box<Vec<Source>>>) {
let (sender, receiver) = channel();
// Pass the desired arguments into the Local Thread Storage.
GLOBAL.with(clone!(db, stack => move |global| {
*global.borrow_mut() = Some((db, stack, receiver));
}));
// The implementation of how this is done is probably terrible but it works!.
// TODO: add timeout option and error reporting.
thread::spawn(clone!(db => move || {
let t = hammond_data::index_feed::index_loop(&db);
if t.is_err() {
error!("Error While trying to update the database.");
error!("Error msg: {}", t.unwrap_err());
let feeds = {
if let Some(mut boxed_vec) = source {
let f = boxed_vec
.par_iter_mut()
.filter_map(|mut s| {
index_feed::refresh_source(&db, &mut s).ok()
})
.collect();
Ok(f)
} else {
index_feed::fetch_feeds(&db)
}
};
sender.send(true).expect("Couldn't send data to channel");;
// http://gtk-rs.org/docs/glib/source/fn.idle_add.html
glib::idle_add(refresh_podcasts_view);
}));
}
pub fn refresh_feed(db: &Database, stack: &gtk::Stack, source: &mut Source) {
let (sender, receiver) = channel();
GLOBAL.with(clone!(db, stack => move |global| {
*global.borrow_mut() = Some((db, stack, receiver));
}));
// TODO: add timeout option and error reporting.
thread::spawn(clone!(db, source => move || {
let foo_ = hammond_data::index_feed::refresh_source(&db, &mut source.clone());
if let Ok(x) = foo_ {
let Feed(mut req, s) = x;
let s = hammond_data::index_feed::complete_index_from_source(&mut req, &s, &db);
if s.is_err() {
error!("Error While trying to update the database.");
error!("Error msg: {}", s.unwrap_err());
};
if let Ok(mut x) = feeds {
index_feed::index_feed(&db, &mut x);
sender.send(true).expect("Couldn't send data to channel");;
glib::idle_add(refresh_podcasts_view);
+1 -1
View File
@@ -199,7 +199,7 @@ fn receive() -> glib::Continue {
pub fn episodes_listbox(db: &Database, pd: &Podcast) -> Result<gtk::ListBox> {
let conn = db.lock().unwrap();
let mut episodes = dbqueries::get_pd_episodes(&conn, &pd)?;
let mut episodes = dbqueries::get_pd_episodes(&conn, pd)?;
drop(conn);
let list = gtk::ListBox::new();
+1 -1
View File
@@ -30,7 +30,7 @@ fn podcast_widget(db: &Database, stack: &gtk::Stack, pd: &Podcast) -> gtk::Box {
}));
title_label.set_text(pd.title());
let listbox = episodes_listbox(db, &pd);
let listbox = episodes_listbox(db, pd);
if let Ok(l) = listbox {
view.add(&l);
}