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:
parent
da361d0cb9
commit
15ce205219
@ -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));
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@ -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;
|
||||
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;
|
||||
return Some(1.0);
|
||||
};
|
||||
ratio
|
||||
|
||||
return Some(ratio);
|
||||
}
|
||||
|
||||
pub(crate) fn get_total_size(&self) -> u64 {
|
||||
None
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@ -519,12 +519,13 @@ 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")),
|
||||
};
|
||||
|
||||
if let Some(fraction) = fraction_1 {
|
||||
// I hate floating points.
|
||||
// Update the progress_bar.
|
||||
if (fraction >= 0.0) && (fraction <= 1.0) && (!fraction.is_nan()) {
|
||||
@ -539,15 +540,18 @@ fn progress_bar_helper(
|
||||
// info!("Fraction: {}", progress_bar.get_fraction());
|
||||
// info!("Fraction: {}", fraction);
|
||||
|
||||
if (fraction >= 1.0) && (!fraction.is_nan()) {
|
||||
return Ok(glib::Continue(false));
|
||||
}
|
||||
}
|
||||
|
||||
// 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,6 +585,7 @@ fn total_size_helper(
|
||||
Err(TryLockError::Poisoned(_)) => return Err(format_err!("Progress Mutex is poisoned")),
|
||||
};
|
||||
|
||||
if let Some(total_bytes) = total_bytes {
|
||||
debug!("Total Size: {}", total_bytes);
|
||||
if total_bytes != 0 {
|
||||
// Update the total_size label
|
||||
@ -591,6 +596,11 @@ fn total_size_helper(
|
||||
} else {
|
||||
Ok(glib::Continue(true))
|
||||
}
|
||||
} else {
|
||||
// Request to get the total size did not succeed
|
||||
widget.info.set_size(None);
|
||||
Ok(glib::Continue(false))
|
||||
}
|
||||
}
|
||||
|
||||
// fn on_delete_bttn_clicked(episode_id: i32) -> Result<(), Error> {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user