summaryrefslogtreecommitdiffstats
path: root/examples/download_progress/src/download.rs
blob: 08805f13f44135bb80c313f598bd8786c5abbb3d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use iced_futures::futures;
use std::hash::{Hash, Hasher};

// Just a little utility function
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<I> {
    id: I,
    url: String,
}

// Make sure iced can use our download stream
impl<H, I, T> iced_native::subscription::Recipe<H, I> for Download<T>
where
    T: 'static + Hash + Copy + Send,
    H: Hasher,
{
    type Output = (T, Progress);

    fn hash(&self, state: &mut H) {
        struct Marker;
        std::any::TypeId::of::<Marker>().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),
            move |state| async move {
                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))
                            }
                        }
                    }
                    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)]
pub enum Progress {
    Started,
    Advanced(f32),
    Finished,
    Errored,
}

pub enum State {
    Ready(String),
    Downloading {
        response: reqwest::Response,
        total: u64,
        downloaded: u64,
    },
    Finished,
}