From 15ce205219b455220063dae1e657deb03b0984eb Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Tue, 4 Sep 2018 16:37:51 +0300 Subject: [PATCH] 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 --- podcasts-downloader/src/downloader.rs | 9 ++++- podcasts-gtk/src/manager.rs | 29 ++++++++------ podcasts-gtk/src/widgets/episode.rs | 54 ++++++++++++++++----------- 3 files changed, 56 insertions(+), 36 deletions(-) diff --git a/podcasts-downloader/src/downloader.rs b/podcasts-downloader/src/downloader.rs index 86e44ba..bd054db 100644 --- a/podcasts-downloader/src/downloader.rs +++ b/podcasts-downloader/src/downloader.rs @@ -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); 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)); } }); diff --git a/podcasts-gtk/src/manager.rs b/podcasts-gtk/src/manager.rs index af932f9..8442efe 100644 --- a/podcasts-gtk/src/manager.rs +++ b/podcasts-gtk/src/manager.rs @@ -15,7 +15,7 @@ use std::sync::{Arc, Mutex, RwLock}; #[derive(Debug)] pub(crate) struct Progress { - total_bytes: u64, + total_bytes: Option, 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 { + 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 { 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) { self.total_bytes = bytes; } diff --git a/podcasts-gtk/src/widgets/episode.rs b/podcasts-gtk/src/widgets/episode.rs index 7dd9bf9..2335cf2 100644 --- a/podcasts-gtk/src/widgets/episode.rs +++ b/podcasts-gtk/src/widgets/episode.rs @@ -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)) } }