From 1a0bcdb2f68b63d8c01d823205d85f7d51bc88bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Tue, 10 Sep 2024 19:24:30 +0200 Subject: [PATCH 1/2] Fix `download_progress` and make it work on Wasm Co-authored-by: Skygrango --- examples/download_progress/Cargo.toml | 2 +- examples/download_progress/src/download.rs | 103 ++++++++------------- examples/download_progress/src/main.rs | 20 ++-- 3 files changed, 48 insertions(+), 77 deletions(-) diff --git a/examples/download_progress/Cargo.toml b/examples/download_progress/Cargo.toml index f78df529dd..61a1b2572c 100644 --- a/examples/download_progress/Cargo.toml +++ b/examples/download_progress/Cargo.toml @@ -12,4 +12,4 @@ iced.features = ["tokio"] [dependencies.reqwest] version = "0.12" default-features = false -features = ["rustls-tls"] +features = ["stream", "rustls-tls"] diff --git a/examples/download_progress/src/download.rs b/examples/download_progress/src/download.rs index bdf57290fe..a8e7b404b6 100644 --- a/examples/download_progress/src/download.rs +++ b/examples/download_progress/src/download.rs @@ -1,91 +1,62 @@ -use iced::futures; +use iced::futures::{SinkExt, Stream, StreamExt}; +use iced::stream::try_channel; use iced::Subscription; use std::hash::Hash; +use std::sync::Arc; // Just a little utility function pub fn file( id: I, url: T, -) -> iced::Subscription<(I, Progress)> { +) -> iced::Subscription<(I, Result)> { Subscription::run_with_id( id, - futures::stream::unfold(State::Ready(url.to_string()), move |state| { - use iced::futures::FutureExt; - - download(id, state).map(Some) - }), + download(url.to_string()).map(move |progress| (id, progress)), ) } -async fn download(id: I, state: State) -> ((I, Progress), State) { - match state { - State::Ready(url) => { - let response = reqwest::get(&url).await; +fn download(url: String) -> impl Stream> { + try_channel(1, move |mut output| async move { + let response = reqwest::get(&url).await?; + let total = response.content_length().ok_or(Error::NoContentLength)?; - match response { - Ok(response) => { - if let Some(total) = response.content_length() { - ( - (id, Progress::Started), - State::Downloading { - response, - total, - downloaded: 0, - }, - ) - } else { - ((id, Progress::Errored), State::Finished) - } - } - Err(_) => ((id, Progress::Errored), State::Finished), - } - } - State::Downloading { - mut response, - total, - downloaded, - } => match response.chunk().await { - Ok(Some(chunk)) => { - let downloaded = downloaded + chunk.len() as u64; + let _ = output.send(Progress::Downloading { percent: 0.0 }).await; + + let mut byte_stream = response.bytes_stream(); + let mut downloaded = 0; - let percentage = (downloaded as f32 / total as f32) * 100.0; + while let Some(next_bytes) = byte_stream.next().await { + let bytes = next_bytes?; + downloaded += bytes.len(); - ( - (id, Progress::Advanced(percentage)), - State::Downloading { - response, - total, - downloaded, - }, - ) - } - Ok(None) => ((id, Progress::Finished), State::Finished), - Err(_) => ((id, Progress::Errored), State::Finished), - }, - State::Finished => { - // We do not let the stream die, as it would start a - // new download repeatedly if the user is not careful - // in case of errors. - iced::futures::future::pending().await + let _ = output + .send(Progress::Downloading { + percent: 100.0 * downloaded as f32 / total as f32, + }) + .await; } - } + + let _ = output.send(Progress::Finished).await; + + Ok(()) + }) } #[derive(Debug, Clone)] pub enum Progress { - Started, - Advanced(f32), + Downloading { percent: f32 }, Finished, - Errored, } -pub enum State { - Ready(String), - Downloading { - response: reqwest::Response, - total: u64, - downloaded: u64, - }, - Finished, +#[derive(Debug, Clone)] +pub enum Error { + RequestFailed(Arc), + NoContentLength, +} + +impl From for Error { + fn from(error: reqwest::Error) -> Self { + Error::RequestFailed(Arc::new(error)) + } } diff --git a/examples/download_progress/src/main.rs b/examples/download_progress/src/main.rs index 667fb448ec..bcc01606f3 100644 --- a/examples/download_progress/src/main.rs +++ b/examples/download_progress/src/main.rs @@ -23,7 +23,7 @@ struct Example { pub enum Message { Add, Download(usize), - DownloadProgressed((usize, download::Progress)), + DownloadProgressed((usize, Result)), } impl Example { @@ -114,19 +114,19 @@ impl Download { } } - pub fn progress(&mut self, new_progress: download::Progress) { + pub fn progress( + &mut self, + new_progress: Result, + ) { if let State::Downloading { progress } = &mut self.state { match new_progress { - download::Progress::Started => { - *progress = 0.0; + Ok(download::Progress::Downloading { percent }) => { + *progress = percent; } - download::Progress::Advanced(percentage) => { - *progress = percentage; - } - download::Progress::Finished => { + Ok(download::Progress::Finished) => { self.state = State::Finished; } - download::Progress::Errored => { + Err(_error) => { self.state = State::Errored; } } @@ -136,7 +136,7 @@ impl Download { pub fn subscription(&self) -> Subscription { match self.state { State::Downloading { .. } => { - download::file(self.id, "https://speed.hetzner.de/100MB.bin?") + download::file(self.id, "https://huggingface.co/mattshumer/Reflection-Llama-3.1-70B/resolve/main/model-00001-of-00162.safetensors") .map(Message::DownloadProgressed) } _ => Subscription::none(), From 0053cc03f9cf92f4f476bdd52e5aca1ac630dfbf Mon Sep 17 00:00:00 2001 From: Skygrango Date: Fri, 3 May 2024 12:06:35 +0800 Subject: [PATCH 2/2] Add `index.html` to `download_progress` example --- examples/download_progress/index.html | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 examples/download_progress/index.html diff --git a/examples/download_progress/index.html b/examples/download_progress/index.html new file mode 100644 index 0000000000..c79e32c109 --- /dev/null +++ b/examples/download_progress/index.html @@ -0,0 +1,12 @@ + + + + + + Download_Progress - Iced + + + + + +