Commit 6bd391d8 authored by Jordan Petridis's avatar Jordan Petridis 🌱

I hate global mutable state..

parent da459707
Pipeline #1868 passed with stages
in 27 minutes and 52 seconds
......@@ -7,18 +7,13 @@ use gio::{ActionMapExt, ApplicationExt, ApplicationExtManual, SimpleActionExt};
use hammond_data::utils::checkup;
use hammond_data::Source;
use manager::Manager;
use headerbar::Header;
use content::Content;
use utils;
use std::sync::{Arc, Mutex};
use std::sync::Arc;
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)]
pub enum Action {
UpdateSources(Option<Source>),
......
......@@ -4,72 +4,58 @@ use hammond_downloader::downloader::get_episode;
use app::Action;
use std::collections::HashSet;
use std::sync::{Arc, Mutex};
use std::collections::{HashMap, HashSet};
use std::sync::{Arc, Mutex, RwLock};
use std::sync::mpsc::Sender;
// use std::sync::atomic::AtomicUsize;
// use std::path::PathBuf;
use std::thread;
// struct DonwloadInstance {
// uri: String,
// // FIXME: MAKE ME A PATHBUF
// local_uri: Option<String>,
// 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>>>,
#[derive(Debug)]
pub struct Progress {
total_bytes: u64,
downloaded_bytes: u64,
}
impl Default for Manager {
fn default() -> Self {
Manager {
active: Arc::new(Mutex::new(HashSet::new())),
impl Progress {
pub fn new(size: u64) -> Self {
Progress {
total_bytes: size,
downloaded_bytes: 0,
}
}
}
impl Manager {
pub fn new() -> Self {
Manager::default()
lazy_static! {
pub static ref ACTIVE_DOWNLOADS: Arc<RwLock<HashSet<i32>>> = {
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();
m.insert(id);
let mut m = ACTIVE_DOWNLOADS.write().unwrap();
m.remove(&id);
}
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();
});
}
sender.send(Action::RefreshViews).unwrap();
});
}
#[cfg(test)]
......@@ -117,9 +103,8 @@ mod tests {
let (sender, _rx) = channel();
let manager = Manager::new();
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
thread::sleep(time::Duration::from_secs(40));
......
......@@ -13,8 +13,8 @@ use hammond_data::{EpisodeWidgetQuery, Podcast};
use hammond_data::errors::*;
use hammond_downloader::downloader;
use app::DOWNLOADS_MANAGER;
use app::Action;
use manager;
use std::sync::mpsc::Sender;
use std::path::Path;
......@@ -99,9 +99,8 @@ impl EpisodeWidget {
self.show_buttons(episode.local_uri());
{
let m = DOWNLOADS_MANAGER.lock().unwrap();
let list = m.active.lock().unwrap();
if list.contains(&episode.rowid()) {
let m = manager::ACTIVE_DOWNLOADS.read().unwrap();
if m.contains(&episode.rowid()) {
self.show_progess_bar()
};
}
......@@ -237,10 +236,8 @@ fn on_download_clicked(
progress.show();
download_bttn.hide();
let download_fold = downloader::get_download_folder(&pd_title).unwrap();
{
let man = DOWNLOADS_MANAGER.lock().unwrap();
man.add(ep.rowid(), &download_fold, sender.clone());
}
manager::add(ep.rowid(), &download_fold, sender.clone());
sender.send(Action::RefreshEpisodesViewBGR).unwrap();
}
......
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