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 {
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;
}
@ -63,6 +63,11 @@ fn download_into(
info!("Status Resp: {}", resp.status());
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()));
}
@ -84,7 +89,7 @@ fn download_into(
ct_len.map(|x| {
if let Some(p) = progress.clone() {
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)]
pub(crate) struct Progress {
total_bytes: u64,
total_bytes: Option<u64>,
downloaded_bytes: u64,
cancel: bool,
}
@ -23,7 +23,7 @@ pub(crate) struct Progress {
impl Default for Progress {
fn default() -> Self {
Progress {
total_bytes: 0,
total_bytes: Some(0),
downloaded_bytes: 0,
cancel: false,
}
@ -31,18 +31,23 @@ impl Default for Progress {
}
impl Progress {
pub(crate) fn get_fraction(&self) -> f64 {
let ratio = self.downloaded_bytes as f64 / self.total_bytes as f64;
debug!("{:?}", self);
debug!("Ratio completed: {}", ratio);
pub(crate) fn get_fraction(&self) -> Option<f64> {
if let Some(total) = self.total_bytes {
let ratio = self.downloaded_bytes as f64 / total as f64;
debug!("{:?}", self);
debug!("Ratio completed: {}", ratio);
if ratio >= 1.0 {
return 1.0;
};
ratio
if ratio >= 1.0 {
return Some(1.0);
};
return Some(ratio);
}
None
}
pub(crate) fn get_total_size(&self) -> u64 {
pub(crate) fn get_total_size(&self) -> Option<u64> {
self.total_bytes
}
@ -60,7 +65,7 @@ impl DownloadProgress for Progress {
self.downloaded_bytes = downloaded
}
fn set_size(&mut self, bytes: u64) {
fn set_size(&mut self, bytes: Option<u64>) {
self.total_bytes = bytes;
}

View File

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