EpisodeWidget: Hide total_size if request fails

This moves the rest of the methods of Progress struct to the downloader
trait and cancels the Progress if the request does nto succed.

Close #90
This commit is contained in:
Jordan Petridis 2018-09-04 22:29:21 +03:00
parent b40c12efbd
commit e42cb49cbe
No known key found for this signature in database
GPG Key ID: E8523968931763BE
3 changed files with 46 additions and 19 deletions

View File

@ -22,9 +22,12 @@ use errors::DownloadError;
// with / or not. // with / or not.
pub trait DownloadProgress { pub trait DownloadProgress {
fn get_downloaded(&self) -> u64;
fn set_downloaded(&mut self, downloaded: u64); fn set_downloaded(&mut self, downloaded: u64);
fn get_size(&self) -> u64;
fn set_size(&mut self, bytes: u64); fn set_size(&mut self, bytes: u64);
fn should_cancel(&self) -> bool; fn should_cancel(&self) -> bool;
fn cancel(&mut self);
} }
// Adapted from https://github.com/mattgathu/rget . // Adapted from https://github.com/mattgathu/rget .
@ -63,6 +66,12 @@ fn download_into(
info!("Status Resp: {}", resp.status()); info!("Status Resp: {}", resp.status());
if !resp.status().is_success() { if !resp.status().is_success() {
if let Some(ref prog) = progress {
if let Ok(mut m) = prog.lock() {
m.cancel();
}
}
return Err(DownloadError::UnexpectedResponse(resp.status())); return Err(DownloadError::UnexpectedResponse(resp.status()));
} }
@ -82,10 +91,11 @@ fn download_into(
let out_file = format!("{}/temp.part", tempdir.path().to_str().unwrap(),); let out_file = format!("{}/temp.part", tempdir.path().to_str().unwrap(),);
ct_len.map(|x| { ct_len.map(|x| {
if let Some(p) = progress.clone() { if let Some(ref p) = progress {
let mut m = p.lock().unwrap(); if let Ok(mut m) = p.lock() {
m.set_size(x); m.set_size(x);
} }
}
}); });
// Save requested content into the file. // Save requested content into the file.

View File

@ -41,21 +41,13 @@ impl Progress {
}; };
ratio ratio
} }
pub(crate) fn get_total_size(&self) -> u64 {
self.total_bytes
}
pub(crate) fn get_downloaded(&self) -> u64 {
self.downloaded_bytes
}
pub(crate) fn cancel(&mut self) {
self.cancel = true;
}
} }
impl DownloadProgress for Progress { impl DownloadProgress for Progress {
fn get_downloaded(&self) -> u64 {
self.downloaded_bytes
}
fn set_downloaded(&mut self, downloaded: u64) { fn set_downloaded(&mut self, downloaded: u64) {
self.downloaded_bytes = downloaded self.downloaded_bytes = downloaded
} }
@ -64,9 +56,17 @@ impl DownloadProgress for Progress {
self.total_bytes = bytes; self.total_bytes = bytes;
} }
fn get_size(&self) -> u64 {
self.total_bytes
}
fn should_cancel(&self) -> bool { fn should_cancel(&self) -> bool {
self.cancel self.cancel
} }
fn cancel(&mut self) {
self.cancel = true;
}
} }
lazy_static! { lazy_static! {

View File

@ -13,6 +13,7 @@ use open;
use podcasts_data::dbqueries; use podcasts_data::dbqueries;
use podcasts_data::utils::get_download_folder; use podcasts_data::utils::get_download_folder;
use podcasts_data::EpisodeWidgetModel; use podcasts_data::EpisodeWidgetModel;
use podcasts_downloader::downloader::DownloadProgress;
use app::Action; use app::Action;
use manager; use manager;
@ -519,8 +520,12 @@ fn progress_bar_helper(
None => return Ok(glib::Continue(false)), None => return Ok(glib::Continue(false)),
}; };
let (fraction, downloaded) = match prog.try_lock() { let (fraction, downloaded, cancel) = match prog.try_lock() {
Ok(guard) => (guard.get_fraction(), guard.get_downloaded()), Ok(guard) => (
guard.get_fraction(),
guard.get_downloaded(),
guard.should_cancel(),
),
Err(TryLockError::WouldBlock) => return Ok(glib::Continue(true)), Err(TryLockError::WouldBlock) => return Ok(glib::Continue(true)),
Err(TryLockError::Poisoned(_)) => return Err(format_err!("Progress Mutex is poisoned")), Err(TryLockError::Poisoned(_)) => return Err(format_err!("Progress Mutex is poisoned")),
}; };
@ -547,7 +552,19 @@ fn progress_bar_helper(
if (fraction >= 1.0) && (!fraction.is_nan()) { if (fraction >= 1.0) && (!fraction.is_nan()) {
Ok(glib::Continue(false)) Ok(glib::Continue(false))
} else if !active { } else if !active || cancel {
// if the total size is not a number, hide it
if widget
.info
.total_size
.get_text()
.as_ref()
.map(|s| s.trim_right_matches(" MB"))
.and_then(|s| s.parse::<i32>().ok())
.is_none()
{
widget.info.total_size.hide();
}
Ok(glib::Continue(false)) Ok(glib::Continue(false))
} else { } else {
Ok(glib::Continue(true)) Ok(glib::Continue(true))
@ -576,7 +593,7 @@ fn total_size_helper(
// Get the total_bytes. // Get the total_bytes.
let total_bytes = match prog.try_lock() { let total_bytes = match prog.try_lock() {
Ok(guard) => guard.get_total_size(), Ok(guard) => guard.get_size(),
Err(TryLockError::WouldBlock) => return Ok(glib::Continue(true)), Err(TryLockError::WouldBlock) => return Ok(glib::Continue(true)),
Err(TryLockError::Poisoned(_)) => return Err(format_err!("Progress Mutex is poisoned")), Err(TryLockError::Poisoned(_)) => return Err(format_err!("Progress Mutex is poisoned")),
}; };