EpisodeWidget: Hide total_size if request fails

This makes the `total_bytes` of the `Progress` struct an optional
to indicate when the request failed and its not going to be
available.

Close #90
This commit is contained in:
Jordan Petridis 2018-09-04 16:37:51 +03:00
parent da361d0cb9
commit 15ce205219
No known key found for this signature in database
GPG Key ID: E8523968931763BE
3 changed files with 56 additions and 36 deletions

View File

@ -23,7 +23,7 @@ use errors::DownloadError;
pub trait DownloadProgress { pub trait DownloadProgress {
fn set_downloaded(&mut self, downloaded: u64); fn set_downloaded(&mut self, downloaded: u64);
fn set_size(&mut self, bytes: u64); fn set_size(&mut self, bytes: Option<u64>);
fn should_cancel(&self) -> bool; fn should_cancel(&self) -> bool;
} }
@ -63,6 +63,11 @@ 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(p) = progress.clone() {
let mut m = p.lock().unwrap();
m.set_size(None);
}
return Err(DownloadError::UnexpectedResponse(resp.status())); return Err(DownloadError::UnexpectedResponse(resp.status()));
} }
@ -84,7 +89,7 @@ fn download_into(
ct_len.map(|x| { ct_len.map(|x| {
if let Some(p) = progress.clone() { if let Some(p) = progress.clone() {
let mut m = p.lock().unwrap(); let mut m = p.lock().unwrap();
m.set_size(x); m.set_size(Some(x));
} }
}); });

View File

@ -15,7 +15,7 @@ use std::sync::{Arc, Mutex, RwLock};
#[derive(Debug)] #[derive(Debug)]
pub(crate) struct Progress { pub(crate) struct Progress {
total_bytes: u64, total_bytes: Option<u64>,
downloaded_bytes: u64, downloaded_bytes: u64,
cancel: bool, cancel: bool,
} }
@ -23,7 +23,7 @@ pub(crate) struct Progress {
impl Default for Progress { impl Default for Progress {
fn default() -> Self { fn default() -> Self {
Progress { Progress {
total_bytes: 0, total_bytes: Some(0),
downloaded_bytes: 0, downloaded_bytes: 0,
cancel: false, cancel: false,
} }
@ -31,18 +31,23 @@ impl Default for Progress {
} }
impl Progress { impl Progress {
pub(crate) fn get_fraction(&self) -> f64 { pub(crate) fn get_fraction(&self) -> Option<f64> {
let ratio = self.downloaded_bytes as f64 / self.total_bytes as f64; if let Some(total) = self.total_bytes {
debug!("{:?}", self); let ratio = self.downloaded_bytes as f64 / total as f64;
debug!("Ratio completed: {}", ratio); debug!("{:?}", self);
debug!("Ratio completed: {}", ratio);
if ratio >= 1.0 { if ratio >= 1.0 {
return 1.0; return Some(1.0);
}; };
ratio
return Some(ratio);
}
None
} }
pub(crate) fn get_total_size(&self) -> u64 { pub(crate) fn get_total_size(&self) -> Option<u64> {
self.total_bytes self.total_bytes
} }
@ -60,7 +65,7 @@ impl DownloadProgress for Progress {
self.downloaded_bytes = downloaded self.downloaded_bytes = downloaded
} }
fn set_size(&mut self, bytes: u64) { fn set_size(&mut self, bytes: Option<u64>) {
self.total_bytes = bytes; self.total_bytes = bytes;
} }

View File

@ -519,35 +519,39 @@ 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_1, downloaded) = match prog.try_lock() {
Ok(guard) => (guard.get_fraction(), guard.get_downloaded()), Ok(guard) => (guard.get_fraction(), guard.get_downloaded()),
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")),
}; };
// I hate floating points. if let Some(fraction) = fraction_1 {
// Update the progress_bar. // I hate floating points.
if (fraction >= 0.0) && (fraction <= 1.0) && (!fraction.is_nan()) { // Update the progress_bar.
// Update local_size label if (fraction >= 0.0) && (fraction <= 1.0) && (!fraction.is_nan()) {
let size = downloaded // Update local_size label
.file_size(SIZE_OPTS.clone()) let size = downloaded
.map_err(|err| format_err!("{}", err))?; .file_size(SIZE_OPTS.clone())
.map_err(|err| format_err!("{}", err))?;
widget.update_progress(&size, fraction); widget.update_progress(&size, fraction);
}
// info!("Fraction: {}", progress_bar.get_fraction());
// info!("Fraction: {}", fraction);
if (fraction >= 1.0) && (!fraction.is_nan()) {
return Ok(glib::Continue(false));
}
} }
// info!("Fraction: {}", progress_bar.get_fraction());
// info!("Fraction: {}", fraction);
// Check if the download is still active // Check if the download is still active
let active = match manager::ACTIVE_DOWNLOADS.read() { let active = match manager::ACTIVE_DOWNLOADS.read() {
Ok(guard) => guard.contains_key(&episode_rowid), Ok(guard) => guard.contains_key(&episode_rowid),
Err(_) => return Err(format_err!("Failed to get a lock on the mutex.")), Err(_) => return Err(format_err!("Failed to get a lock on the mutex.")),
}; };
if (fraction >= 1.0) && (!fraction.is_nan()) { if !active || fraction_1.is_none() {
Ok(glib::Continue(false))
} else if !active {
Ok(glib::Continue(false)) Ok(glib::Continue(false))
} else { } else {
Ok(glib::Continue(true)) Ok(glib::Continue(true))
@ -581,15 +585,21 @@ fn total_size_helper(
Err(TryLockError::Poisoned(_)) => return Err(format_err!("Progress Mutex is poisoned")), Err(TryLockError::Poisoned(_)) => return Err(format_err!("Progress Mutex is poisoned")),
}; };
debug!("Total Size: {}", total_bytes); if let Some(total_bytes) = total_bytes {
if total_bytes != 0 { debug!("Total Size: {}", total_bytes);
// Update the total_size label if total_bytes != 0 {
widget.info.set_size(Some(total_bytes as i32)); // Update the total_size label
widget.info.set_size(Some(total_bytes as i32));
// Do not call again the callback // Do not call again the callback
Ok(glib::Continue(false)) Ok(glib::Continue(false))
} else {
Ok(glib::Continue(true))
}
} else { } else {
Ok(glib::Continue(true)) // Request to get the total size did not succeed
widget.info.set_size(None);
Ok(glib::Continue(false))
} }
} }