Use sipper in download_progress

This commit is contained in:
Héctor Ramón Jiménez 2025-02-11 00:56:14 +01:00
parent 54ffbbf043
commit 05618ea9b3
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
2 changed files with 37 additions and 38 deletions

View file

@ -1,16 +1,14 @@
use iced::futures::{SinkExt, Stream, StreamExt};
use iced::stream::try_channel;
use iced::futures::StreamExt;
use iced::task::{sipper, Straw};
use std::sync::Arc;
pub fn download(
url: impl AsRef<str>,
) -> impl Stream<Item = Result<Progress, Error>> {
try_channel(1, move |mut output| async move {
pub fn download(url: impl AsRef<str>) -> impl Straw<(), Progress, Error> {
sipper(move |mut progress| async move {
let response = reqwest::get(url.as_ref()).await?;
let total = response.content_length().ok_or(Error::NoContentLength)?;
let _ = output.send(Progress::Downloading { percent: 0.0 }).await;
let _ = progress.send(Progress { percent: 0.0 }).await;
let mut byte_stream = response.bytes_stream();
let mut downloaded = 0;
@ -19,23 +17,20 @@ pub fn download(
let bytes = next_bytes?;
downloaded += bytes.len();
let _ = output
.send(Progress::Downloading {
let _ = progress
.send(Progress {
percent: 100.0 * downloaded as f32 / total as f32,
})
.await;
}
let _ = output.send(Progress::Finished).await;
Ok(())
})
}
#[derive(Debug, Clone)]
pub enum Progress {
Downloading { percent: f32 },
Finished,
pub struct Progress {
pub percent: f32,
}
#[derive(Debug, Clone)]