From fa1f3d218d406f2ae4ea12ab6b6c6578df8a2417 Mon Sep 17 00:00:00 2001 From: aprzn Date: Sun, 27 Aug 2023 08:46:40 -0400 Subject: [PATCH] visible load status, etc --- NONGfisher/src/main.rs | 47 +++++++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/NONGfisher/src/main.rs b/NONGfisher/src/main.rs index 2a9ed59..659a3cd 100644 --- a/NONGfisher/src/main.rs +++ b/NONGfisher/src/main.rs @@ -45,10 +45,11 @@ struct SfhResponse { enum Event { IdSubmit { id: String }, Error { description: String }, + LoadStatusUpdate { description: String }, } enum State { - IdSelect { search_string: String, enabled: bool }, + IdSelect { search_string: String, loading: Option }, } struct App { @@ -64,7 +65,7 @@ impl App { Self { state: State::IdSelect { search_string: String::new(), - enabled: true, + loading: None, }, error: None, event_tx, @@ -103,17 +104,18 @@ impl App { (Some(desc), _) => { }, - (None, State::IdSelect { search_string, enabled }) => { + (None, State::IdSelect { search_string, loading }) => { ui.label("Enter level ID:"); - ui.add_enabled_ui(*enabled, |ui| { + ui.add_enabled_ui(loading.is_none(), |ui| { ui.text_edit_singleline(search_string).request_focus(); }); - if *enabled { - if ui.button("submit").clicked() { - self.event_tx.send(Event::IdSubmit { id: search_string.clone() }); - } - } else { - ui.label("loading..."); + match loading { + None => if ui.button("submit").clicked() { + self.event_tx.send(Event::IdSubmit { id: search_string.clone() }).unwrap(); + }, + Some(loading) => { + ui.label(loading.as_str()); + }, } } } @@ -124,22 +126,27 @@ impl App { match event { Event::IdSubmit { id } => { let tx = self.event_tx.clone(); - self.state = State::IdSelect { search_string: id.clone(), enabled: false }; + self.state = State::IdSelect { search_string: id.clone(), loading: Some("loading...".into()) }; thread::spawn(move || { match query_level_id(&tx, id) { - Ok(Some(song_desc)) => todo!(), - Ok(None) => todo!(), + Ok(_) => todo!(), Err(e) => todo!(), } }); }, + Event::LoadStatusUpdate { description } => { + match &mut self.state { + State::IdSelect { search_string, loading } => *loading = Some(description), + } + }, Event::Error { description } => self.error = Some(description), } } } } -fn query_level_id(tx: &mpsc::Sender, id: String) -> Result> { +fn query_level_id(tx: &mpsc::Sender, id: String) -> Result<()> { + tx.send(Event::LoadStatusUpdate { description: "Getting level info...".into() })?; let level_desc = ureq::post("http://www.boomlings.com/database/downloadGJLevel22.php") .set("User-Agent", "") .send_form(&[ @@ -157,11 +164,23 @@ fn query_level_id(tx: &mpsc::Sender, id: String) -> Result