diff options
author | 2021-02-13 05:00:52 +0800 | |
---|---|---|
committer | 2021-02-12 22:00:52 +0100 | |
commit | 9f5c2eb0c43daa61b19769322cf3692b29e0ac0f (patch) | |
tree | 2486e4c8b690800d4d9a2bb3c62f14b4f5156ba5 /examples/download_progress/src/download.rs | |
parent | 9e453843b26f2f73228316334298a4c608b2f050 (diff) | |
download | iced-9f5c2eb0c43daa61b19769322cf3692b29e0ac0f.tar.gz iced-9f5c2eb0c43daa61b19769322cf3692b29e0ac0f.tar.bz2 iced-9f5c2eb0c43daa61b19769322cf3692b29e0ac0f.zip |
Improve download_progress example (#283)
* Add advanced download example
* Rename to task fields and variables
* Cargo fmt advanced_download/src/download.rs
* Add progress bar for advanced download example
* Merge two download examples to single one
* Apply great review suggestions
* Change to url::Url instead of plain String
* Simplify `download_progress` example
* Update `README` of `download_progress` example
Co-authored-by: Héctor Ramón Jiménez <hector0193@gmail.com>
Diffstat (limited to 'examples/download_progress/src/download.rs')
-rw-r--r-- | examples/download_progress/src/download.rs | 46 |
1 files changed, 31 insertions, 15 deletions
diff --git a/examples/download_progress/src/download.rs b/examples/download_progress/src/download.rs index f46a01f7..08805f13 100644 --- a/examples/download_progress/src/download.rs +++ b/examples/download_progress/src/download.rs @@ -1,37 +1,46 @@ use iced_futures::futures; +use std::hash::{Hash, Hasher}; // Just a little utility function -pub fn file<T: ToString>(url: T) -> iced::Subscription<Progress> { +pub fn file<I: 'static + Hash + Copy + Send, T: ToString>( + id: I, + url: T, +) -> iced::Subscription<(I, Progress)> { iced::Subscription::from_recipe(Download { + id, url: url.to_string(), }) } -pub struct Download { +pub struct Download<I> { + id: I, url: String, } // Make sure iced can use our download stream -impl<H, I> iced_native::subscription::Recipe<H, I> for Download +impl<H, I, T> iced_native::subscription::Recipe<H, I> for Download<T> where - H: std::hash::Hasher, + T: 'static + Hash + Copy + Send, + H: Hasher, { - type Output = Progress; + type Output = (T, Progress); fn hash(&self, state: &mut H) { - use std::hash::Hash; + struct Marker; + std::any::TypeId::of::<Marker>().hash(state); - std::any::TypeId::of::<Self>().hash(state); - self.url.hash(state); + self.id.hash(state); } fn stream( self: Box<Self>, _input: futures::stream::BoxStream<'static, I>, ) -> futures::stream::BoxStream<'static, Self::Output> { + let id = self.id; + Box::pin(futures::stream::unfold( State::Ready(self.url), - |state| async move { + move |state| async move { match state { State::Ready(url) => { let response = reqwest::get(&url).await; @@ -40,7 +49,7 @@ where Ok(response) => { if let Some(total) = response.content_length() { Some(( - Progress::Started, + (id, Progress::Started), State::Downloading { response, total, @@ -48,11 +57,14 @@ where }, )) } else { - Some((Progress::Errored, State::Finished)) + Some(( + (id, Progress::Errored), + State::Finished, + )) } } Err(_) => { - Some((Progress::Errored, State::Finished)) + Some(((id, Progress::Errored), State::Finished)) } } } @@ -68,7 +80,7 @@ where (downloaded as f32 / total as f32) * 100.0; Some(( - Progress::Advanced(percentage), + (id, Progress::Advanced(percentage)), State::Downloading { response, total, @@ -76,8 +88,12 @@ where }, )) } - Ok(None) => Some((Progress::Finished, State::Finished)), - Err(_) => Some((Progress::Errored, State::Finished)), + Ok(None) => { + Some(((id, Progress::Finished), State::Finished)) + } + Err(_) => { + Some(((id, Progress::Errored), State::Finished)) + } }, State::Finished => { // We do not let the stream die, as it would start a |