Merge pull request #2681 from xosxos/patch-1

fix: add an initial state to the `system_information` example
This commit is contained in:
Héctor 2024-12-03 01:35:41 +01:00 committed by GitHub
commit 9c93341089
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -7,7 +7,7 @@ pub fn main() -> iced::Result {
Example::update, Example::update,
Example::view, Example::view,
) )
.run() .run_with(Example::new)
} }
#[derive(Default)] #[derive(Default)]
@ -28,20 +28,28 @@ enum Message {
} }
impl Example { impl Example {
fn new() -> (Self, Task<Message>) {
(
Self::Loading,
system::fetch_information().map(Message::InformationReceived),
)
}
fn update(&mut self, message: Message) -> Task<Message> { fn update(&mut self, message: Message) -> Task<Message> {
match message { match message {
Message::Refresh => { Message::Refresh => {
*self = Self::Loading; let (state, refresh) = Self::new();
return system::fetch_information() *self = state;
.map(Message::InformationReceived);
refresh
} }
Message::InformationReceived(information) => { Message::InformationReceived(information) => {
*self = Self::Loaded { information }; *self = Self::Loaded { information };
Task::none()
} }
} }
Task::none()
} }
fn view(&self) -> Element<Message> { fn view(&self) -> Element<Message> {