Use sipper in download_progress
This commit is contained in:
parent
54ffbbf043
commit
05618ea9b3
2 changed files with 37 additions and 38 deletions
|
|
@ -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)]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue