Use subscription::run for download_progress example
This commit is contained in:
parent
35e4f30759
commit
75348c5b8c
1 changed files with 70 additions and 85 deletions
|
|
@ -1,112 +1,97 @@
|
||||||
|
use futures::Stream;
|
||||||
use iced_futures::futures;
|
use iced_futures::futures;
|
||||||
use std::hash::{Hash, Hasher};
|
use iced_native::subscription;
|
||||||
|
|
||||||
|
use std::hash::Hash;
|
||||||
|
|
||||||
// Just a little utility function
|
// Just a little utility function
|
||||||
pub fn file<I: 'static + Hash + Copy + Send, T: ToString>(
|
pub fn file<I: 'static + Hash + Copy + Send, T: ToString>(
|
||||||
id: I,
|
id: I,
|
||||||
url: T,
|
url: T,
|
||||||
) -> iced::Subscription<(I, Progress)> {
|
) -> iced::Subscription<(I, Progress)> {
|
||||||
iced::Subscription::from_recipe(Download {
|
subscription::run(
|
||||||
id,
|
Download {
|
||||||
url: url.to_string(),
|
id,
|
||||||
})
|
url: url.to_string(),
|
||||||
|
},
|
||||||
|
download,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Hash, Clone)]
|
||||||
pub struct Download<I> {
|
pub struct Download<I> {
|
||||||
id: I,
|
id: I,
|
||||||
url: String,
|
url: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make sure iced can use our download stream
|
fn download<I: Copy>(
|
||||||
impl<H, I, T> iced_native::subscription::Recipe<H, I> for Download<T>
|
download: Download<I>,
|
||||||
where
|
) -> impl Stream<Item = (I, Progress)> {
|
||||||
T: 'static + Hash + Copy + Send,
|
let id = download.id;
|
||||||
H: Hasher,
|
|
||||||
{
|
|
||||||
type Output = (T, Progress);
|
|
||||||
|
|
||||||
fn hash(&self, state: &mut H) {
|
futures::stream::unfold(
|
||||||
struct Marker;
|
State::Ready(download.url),
|
||||||
std::any::TypeId::of::<Marker>().hash(state);
|
move |state| async move {
|
||||||
|
match state {
|
||||||
|
State::Ready(url) => {
|
||||||
|
let response = reqwest::get(&url).await;
|
||||||
|
|
||||||
self.id.hash(state);
|
match response {
|
||||||
}
|
Ok(response) => {
|
||||||
|
if let Some(total) = response.content_length() {
|
||||||
fn stream(
|
Some((
|
||||||
self: Box<Self>,
|
(id, Progress::Started),
|
||||||
_input: futures::stream::BoxStream<'static, I>,
|
State::Downloading {
|
||||||
) -> futures::stream::BoxStream<'static, Self::Output> {
|
response,
|
||||||
let id = self.id;
|
total,
|
||||||
|
downloaded: 0,
|
||||||
Box::pin(futures::stream::unfold(
|
},
|
||||||
State::Ready(self.url),
|
))
|
||||||
move |state| async move {
|
} else {
|
||||||
match state {
|
|
||||||
State::Ready(url) => {
|
|
||||||
let response = reqwest::get(&url).await;
|
|
||||||
|
|
||||||
match response {
|
|
||||||
Ok(response) => {
|
|
||||||
if let Some(total) = response.content_length() {
|
|
||||||
Some((
|
|
||||||
(id, Progress::Started),
|
|
||||||
State::Downloading {
|
|
||||||
response,
|
|
||||||
total,
|
|
||||||
downloaded: 0,
|
|
||||||
},
|
|
||||||
))
|
|
||||||
} else {
|
|
||||||
Some((
|
|
||||||
(id, Progress::Errored),
|
|
||||||
State::Finished,
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(_) => {
|
|
||||||
Some(((id, Progress::Errored), State::Finished))
|
Some(((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 percentage =
|
|
||||||
(downloaded as f32 / total as f32) * 100.0;
|
|
||||||
|
|
||||||
Some((
|
|
||||||
(id, Progress::Advanced(percentage)),
|
|
||||||
State::Downloading {
|
|
||||||
response,
|
|
||||||
total,
|
|
||||||
downloaded,
|
|
||||||
},
|
|
||||||
))
|
|
||||||
}
|
|
||||||
Ok(None) => {
|
|
||||||
Some(((id, Progress::Finished), State::Finished))
|
|
||||||
}
|
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
Some(((id, Progress::Errored), State::Finished))
|
Some(((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.
|
|
||||||
let _: () = iced::futures::future::pending().await;
|
|
||||||
|
|
||||||
None
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
State::Downloading {
|
||||||
))
|
mut response,
|
||||||
}
|
total,
|
||||||
|
downloaded,
|
||||||
|
} => match response.chunk().await {
|
||||||
|
Ok(Some(chunk)) => {
|
||||||
|
let downloaded = downloaded + chunk.len() as u64;
|
||||||
|
|
||||||
|
let percentage =
|
||||||
|
(downloaded as f32 / total as f32) * 100.0;
|
||||||
|
|
||||||
|
Some((
|
||||||
|
(id, Progress::Advanced(percentage)),
|
||||||
|
State::Downloading {
|
||||||
|
response,
|
||||||
|
total,
|
||||||
|
downloaded,
|
||||||
|
},
|
||||||
|
))
|
||||||
|
}
|
||||||
|
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
|
||||||
|
// new download repeatedly if the user is not careful
|
||||||
|
// in case of errors.
|
||||||
|
let _: () = iced::futures::future::pending().await;
|
||||||
|
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue