Handle errors in download_progress
example
This commit is contained in:
parent
b92e1f9574
commit
0d719bbdf3
@ -35,15 +35,23 @@ where
|
|||||||
let response = reqwest::get(&url).await;
|
let response = reqwest::get(&url).await;
|
||||||
|
|
||||||
match response {
|
match response {
|
||||||
Ok(response) => Some((
|
Ok(response) => {
|
||||||
Progress::Started,
|
if let Some(total) = response.content_length() {
|
||||||
State::Downloading {
|
Some((
|
||||||
total: response.content_length().unwrap(),
|
Progress::Started,
|
||||||
downloaded: 0,
|
State::Downloading {
|
||||||
response,
|
response,
|
||||||
},
|
total,
|
||||||
)),
|
downloaded: 0,
|
||||||
Err(_) => None,
|
},
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
Some((Progress::Errored, State::Finished))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
Some((Progress::Errored, State::Finished))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
State::Downloading {
|
State::Downloading {
|
||||||
@ -67,9 +75,16 @@ where
|
|||||||
))
|
))
|
||||||
}
|
}
|
||||||
Ok(None) => Some((Progress::Finished, State::Finished)),
|
Ok(None) => Some((Progress::Finished, State::Finished)),
|
||||||
Err(_) => None,
|
Err(_) => Some((Progress::Errored, State::Finished)),
|
||||||
},
|
},
|
||||||
State::Finished => None,
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
))
|
))
|
||||||
@ -81,6 +96,7 @@ pub enum Progress {
|
|||||||
Started,
|
Started,
|
||||||
Advanced(f32),
|
Advanced(f32),
|
||||||
Finished,
|
Finished,
|
||||||
|
Errored,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum State {
|
pub enum State {
|
||||||
|
@ -14,6 +14,7 @@ enum Example {
|
|||||||
Idle { button: button::State },
|
Idle { button: button::State },
|
||||||
Downloading { progress: f32 },
|
Downloading { progress: f32 },
|
||||||
Finished { button: button::State },
|
Finished { button: button::State },
|
||||||
|
Errored { button: button::State },
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
@ -60,6 +61,11 @@ impl Application for Example {
|
|||||||
button: button::State::new(),
|
button: button::State::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
download::Progress::Errored => {
|
||||||
|
*self = Example::Errored {
|
||||||
|
button: button::State::new(),
|
||||||
|
};
|
||||||
|
}
|
||||||
},
|
},
|
||||||
_ => {}
|
_ => {}
|
||||||
},
|
},
|
||||||
@ -83,6 +89,7 @@ impl Application for Example {
|
|||||||
Example::Idle { .. } => 0.0,
|
Example::Idle { .. } => 0.0,
|
||||||
Example::Downloading { progress } => *progress,
|
Example::Downloading { progress } => *progress,
|
||||||
Example::Finished { .. } => 100.0,
|
Example::Finished { .. } => 100.0,
|
||||||
|
Example::Errored { .. } => 0.0,
|
||||||
};
|
};
|
||||||
|
|
||||||
let progress_bar = ProgressBar::new(0.0..=100.0, current_progress);
|
let progress_bar = ProgressBar::new(0.0..=100.0, current_progress);
|
||||||
@ -106,6 +113,15 @@ impl Application for Example {
|
|||||||
Text::new(format!("Downloading... {:.2}%", current_progress))
|
Text::new(format!("Downloading... {:.2}%", current_progress))
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
|
Example::Errored { button } => Column::new()
|
||||||
|
.spacing(10)
|
||||||
|
.align_items(Align::Center)
|
||||||
|
.push(Text::new("Something went wrong :("))
|
||||||
|
.push(
|
||||||
|
Button::new(button, Text::new("Try again"))
|
||||||
|
.on_press(Message::Download),
|
||||||
|
)
|
||||||
|
.into(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let content = Column::new()
|
let content = Column::new()
|
||||||
|
Loading…
Reference in New Issue
Block a user