Downloader: Update the progress index of downloads.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
// use hammond_data::Episode;
|
||||
use hammond_data::dbqueries;
|
||||
use hammond_downloader::downloader::get_episode;
|
||||
use hammond_downloader::downloader::DownloadProgress;
|
||||
|
||||
use app::Action;
|
||||
|
||||
@@ -18,15 +19,33 @@ pub struct Progress {
|
||||
}
|
||||
|
||||
impl Progress {
|
||||
pub fn new(size: u64) -> Self {
|
||||
pub fn get_fraction(&self) -> f64 {
|
||||
info!("Progress: {:?}", self);
|
||||
let ratio = self.downloaded_bytes as f64 / self.total_bytes as f64;
|
||||
|
||||
if ratio >= 1.0 {
|
||||
return 1.0;
|
||||
};
|
||||
ratio
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Progress {
|
||||
fn default() -> Self {
|
||||
Progress {
|
||||
total_bytes: size,
|
||||
total_bytes: 0,
|
||||
downloaded_bytes: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_fraction(&self) -> f64 {
|
||||
self.downloaded_bytes as f64 / self.total_bytes as f64
|
||||
impl DownloadProgress for Progress {
|
||||
fn set_downloaded(&mut self, downloaded: u64) {
|
||||
self.downloaded_bytes = downloaded
|
||||
}
|
||||
|
||||
fn set_size(&mut self, bytes: u64) {
|
||||
self.total_bytes = bytes;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,17 +55,23 @@ lazy_static! {
|
||||
};
|
||||
}
|
||||
|
||||
pub fn add(id: i32, directory: &str, sender: Sender<Action>, prog: Arc<Mutex<Progress>>) {
|
||||
pub fn add(id: i32, directory: &str, sender: Sender<Action>) {
|
||||
// Create a new `Progress` struct to keep track of dl progress.
|
||||
let prog = Arc::new(Mutex::new(Progress::default()));
|
||||
|
||||
{
|
||||
let mut m = ACTIVE_DOWNLOADS.write().unwrap();
|
||||
m.insert(id, prog.clone());
|
||||
}
|
||||
{
|
||||
let m = ACTIVE_DOWNLOADS.read().unwrap();
|
||||
info!("ACTIVE DOWNLOADS: {:#?}", m);
|
||||
}
|
||||
|
||||
let dir = directory.to_owned();
|
||||
thread::spawn(move || {
|
||||
info!("{:?}", prog); // just checking that it compiles
|
||||
let episode = dbqueries::get_episode_from_rowid(id).unwrap();
|
||||
let e = get_episode(&mut episode.into(), dir.as_str());
|
||||
let e = get_episode(&mut episode.into(), dir.as_str(), Some(prog));
|
||||
if let Err(err) = e {
|
||||
error!("Error: {}", err);
|
||||
};
|
||||
@@ -105,10 +130,9 @@ mod tests {
|
||||
};
|
||||
|
||||
let (sender, _rx) = channel();
|
||||
let prog = Arc::new(Mutex::new(Progress::new(42)));
|
||||
|
||||
let download_fold = downloader::get_download_folder(&pd.title()).unwrap();
|
||||
add(episode.rowid(), download_fold.as_str(), sender, prog);
|
||||
add(episode.rowid(), download_fold.as_str(), sender);
|
||||
|
||||
// Give it soem time to download the file
|
||||
thread::sleep(time::Duration::from_secs(40));
|
||||
|
||||
@@ -17,7 +17,6 @@ use app::Action;
|
||||
use manager;
|
||||
|
||||
use std::sync::mpsc::Sender;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::path::Path;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -80,8 +79,6 @@ impl EpisodeWidget {
|
||||
widget
|
||||
}
|
||||
|
||||
// TODO: calculate lenght.
|
||||
// TODO: wire the progress_bar to the downloader.
|
||||
// TODO: wire the cancel button.
|
||||
fn init(&self, episode: &mut EpisodeWidgetQuery, sender: Sender<Action>) {
|
||||
WidgetExt::set_name(&self.container, &episode.rowid().to_string());
|
||||
@@ -218,13 +215,24 @@ impl EpisodeWidget {
|
||||
let m = prog.lock().unwrap();
|
||||
m.get_fraction()
|
||||
};
|
||||
progress_bar.set_fraction(fraction);
|
||||
// info!("Fraction: {}", progress_bar.get_fraction());
|
||||
|
||||
if fraction != 1.0{
|
||||
glib::Continue(true)
|
||||
} else {
|
||||
// I hate floating points.
|
||||
if (fraction >= 0.0) && (fraction <= 1.0) && (!fraction.is_nan()) {
|
||||
progress_bar.set_fraction(fraction);
|
||||
}
|
||||
// info!("Fraction: {}", progress_bar.get_fraction());
|
||||
// info!("Fraction: {}", fraction);
|
||||
let active = {
|
||||
let m = manager::ACTIVE_DOWNLOADS.read().unwrap();
|
||||
m.contains_key(&id)
|
||||
};
|
||||
|
||||
if (fraction >= 1.0) && (!fraction.is_nan()){
|
||||
glib::Continue(false)
|
||||
} else if !active {
|
||||
glib::Continue(false)
|
||||
}else {
|
||||
glib::Continue(true)
|
||||
}
|
||||
}),
|
||||
);
|
||||
@@ -236,10 +244,8 @@ fn on_download_clicked(ep: &EpisodeWidgetQuery, sender: Sender<Action>) {
|
||||
let pd = dbqueries::get_podcast_from_id(ep.podcast_id()).unwrap();
|
||||
let download_fold = downloader::get_download_folder(&pd.title().to_owned()).unwrap();
|
||||
|
||||
// Create a new `Progress` struct to keep track of dl progress.
|
||||
let prog = Arc::new(Mutex::new(manager::Progress::new(42)));
|
||||
// Start a new download.
|
||||
manager::add(ep.rowid(), &download_fold, sender.clone(), prog.clone());
|
||||
manager::add(ep.rowid(), &download_fold, sender.clone());
|
||||
|
||||
// Update Views
|
||||
sender.send(Action::RefreshEpisodesView).unwrap();
|
||||
|
||||
Reference in New Issue
Block a user