I hate global mutable state..

This commit is contained in:
Jordan Petridis 2018-01-07 05:51:29 +02:00
parent da459707be
commit 6bd391d89e
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
3 changed files with 45 additions and 68 deletions

View File

@ -7,18 +7,13 @@ use gio::{ActionMapExt, ApplicationExt, ApplicationExtManual, SimpleActionExt};
use hammond_data::utils::checkup; use hammond_data::utils::checkup;
use hammond_data::Source; use hammond_data::Source;
use manager::Manager;
use headerbar::Header; use headerbar::Header;
use content::Content; use content::Content;
use utils; use utils;
use std::sync::{Arc, Mutex}; use std::sync::Arc;
use std::sync::mpsc::{channel, Receiver, Sender}; use std::sync::mpsc::{channel, Receiver, Sender};
lazy_static! {
pub static ref DOWNLOADS_MANAGER: Arc<Mutex<Manager>> = Arc::new(Mutex::new(Manager::new()));
}
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum Action { pub enum Action {
UpdateSources(Option<Source>), UpdateSources(Option<Source>),

View File

@ -4,72 +4,58 @@ use hammond_downloader::downloader::get_episode;
use app::Action; use app::Action;
use std::collections::HashSet; use std::collections::{HashMap, HashSet};
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex, RwLock};
use std::sync::mpsc::Sender; use std::sync::mpsc::Sender;
// use std::sync::atomic::AtomicUsize;
// use std::path::PathBuf; // use std::path::PathBuf;
use std::thread; use std::thread;
// struct DonwloadInstance { #[derive(Debug)]
// uri: String, pub struct Progress {
// // FIXME: MAKE ME A PATHBUF total_bytes: u64,
// local_uri: Option<String>, downloaded_bytes: u64,
// downloaded_bytes: u64,
// total_bytes: u64,
// }
// impl DonwloadInstance {
// fn new(url: &str, total_bytes: u64) -> Self {
// DonwloadInstance {
// uri: url.into(),
// local_uri: None,
// downloaded_bytes: 0,
// total_bytes,
// }
// }
// }
#[derive(Debug, Clone)]
// FIXME: privacy stuff
pub struct Manager {
pub active: Arc<Mutex<HashSet<i32>>>,
} }
impl Default for Manager { impl Progress {
fn default() -> Self { pub fn new(size: u64) -> Self {
Manager { Progress {
active: Arc::new(Mutex::new(HashSet::new())), total_bytes: size,
downloaded_bytes: 0,
} }
} }
} }
impl Manager { lazy_static! {
pub fn new() -> Self { pub static ref ACTIVE_DOWNLOADS: Arc<RwLock<HashSet<i32>>> = {
Manager::default() Arc::new(RwLock::new(HashSet::new()))
};
pub static ref ACTIVE_PROGRESS: Arc<RwLock<HashMap<i32, Mutex<Progress>>>> = {
Arc::new(RwLock::new(HashMap::new()))
};
}
pub fn add(id: i32, directory: &str, sender: Sender<Action>) {
{
let mut m = ACTIVE_DOWNLOADS.write().unwrap();
m.insert(id);
} }
pub fn add(&self, id: i32, directory: &str, sender: Sender<Action>) { let dir = directory.to_owned();
thread::spawn(move || {
let episode = dbqueries::get_episode_from_rowid(id).unwrap();
let e = get_episode(&mut episode.into(), dir.as_str());
if let Err(err) = e {
error!("Error: {}", err);
};
{ {
let mut m = self.active.lock().unwrap(); let mut m = ACTIVE_DOWNLOADS.write().unwrap();
m.insert(id); m.remove(&id);
} }
sender.send(Action::RefreshViews).unwrap();
let dir = directory.to_owned(); });
let list = self.active.clone();
thread::spawn(move || {
let episode = dbqueries::get_episode_from_rowid(id).unwrap();
let e = get_episode(&mut episode.into(), dir.as_str());
if let Err(err) = e {
error!("Error: {}", err);
};
{
let mut m = list.lock().unwrap();
m.remove(&id);
}
sender.send(Action::RefreshViews).unwrap();
});
}
} }
#[cfg(test)] #[cfg(test)]
@ -117,9 +103,8 @@ mod tests {
let (sender, _rx) = channel(); let (sender, _rx) = channel();
let manager = Manager::new();
let download_fold = downloader::get_download_folder(&pd.title()).unwrap(); let download_fold = downloader::get_download_folder(&pd.title()).unwrap();
manager.add(episode.rowid(), download_fold.as_str(), sender); add(episode.rowid(), download_fold.as_str(), sender);
// Give it soem time to download the file // Give it soem time to download the file
thread::sleep(time::Duration::from_secs(40)); thread::sleep(time::Duration::from_secs(40));

View File

@ -13,8 +13,8 @@ use hammond_data::{EpisodeWidgetQuery, Podcast};
use hammond_data::errors::*; use hammond_data::errors::*;
use hammond_downloader::downloader; use hammond_downloader::downloader;
use app::DOWNLOADS_MANAGER;
use app::Action; use app::Action;
use manager;
use std::sync::mpsc::Sender; use std::sync::mpsc::Sender;
use std::path::Path; use std::path::Path;
@ -99,9 +99,8 @@ impl EpisodeWidget {
self.show_buttons(episode.local_uri()); self.show_buttons(episode.local_uri());
{ {
let m = DOWNLOADS_MANAGER.lock().unwrap(); let m = manager::ACTIVE_DOWNLOADS.read().unwrap();
let list = m.active.lock().unwrap(); if m.contains(&episode.rowid()) {
if list.contains(&episode.rowid()) {
self.show_progess_bar() self.show_progess_bar()
}; };
} }
@ -237,10 +236,8 @@ fn on_download_clicked(
progress.show(); progress.show();
download_bttn.hide(); download_bttn.hide();
let download_fold = downloader::get_download_folder(&pd_title).unwrap(); let download_fold = downloader::get_download_folder(&pd_title).unwrap();
{
let man = DOWNLOADS_MANAGER.lock().unwrap(); manager::add(ep.rowid(), &download_fold, sender.clone());
man.add(ep.rowid(), &download_fold, sender.clone());
}
sender.send(Action::RefreshEpisodesViewBGR).unwrap(); sender.send(Action::RefreshEpisodesViewBGR).unwrap();
} }