visible load status, etc

main
aprzn 1 year ago
parent f87c61e382
commit fa1f3d218d

@ -45,10 +45,11 @@ struct SfhResponse {
enum Event { enum Event {
IdSubmit { id: String }, IdSubmit { id: String },
Error { description: String }, Error { description: String },
LoadStatusUpdate { description: String },
} }
enum State { enum State {
IdSelect { search_string: String, enabled: bool }, IdSelect { search_string: String, loading: Option<String> },
} }
struct App { struct App {
@ -64,7 +65,7 @@ impl App {
Self { Self {
state: State::IdSelect { state: State::IdSelect {
search_string: String::new(), search_string: String::new(),
enabled: true, loading: None,
}, },
error: None, error: None,
event_tx, event_tx,
@ -103,17 +104,18 @@ impl App {
(Some(desc), _) => { (Some(desc), _) => {
}, },
(None, State::IdSelect { search_string, enabled }) => { (None, State::IdSelect { search_string, loading }) => {
ui.label("Enter level ID:"); 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(); ui.text_edit_singleline(search_string).request_focus();
}); });
if *enabled { match loading {
if ui.button("submit").clicked() { None => if ui.button("submit").clicked() {
self.event_tx.send(Event::IdSubmit { id: search_string.clone() }); self.event_tx.send(Event::IdSubmit { id: search_string.clone() }).unwrap();
} },
} else { Some(loading) => {
ui.label("loading..."); ui.label(loading.as_str());
},
} }
} }
} }
@ -124,22 +126,27 @@ impl App {
match event { match event {
Event::IdSubmit { id } => { Event::IdSubmit { id } => {
let tx = self.event_tx.clone(); 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 || { thread::spawn(move || {
match query_level_id(&tx, id) { match query_level_id(&tx, id) {
Ok(Some(song_desc)) => todo!(), Ok(_) => todo!(),
Ok(None) => todo!(),
Err(e) => 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), Event::Error { description } => self.error = Some(description),
} }
} }
} }
} }
fn query_level_id(tx: &mpsc::Sender<Event>, id: String) -> Result<Option<SfhSong>> { fn query_level_id(tx: &mpsc::Sender<Event>, id: String) -> Result<()> {
tx.send(Event::LoadStatusUpdate { description: "Getting level info...".into() })?;
let level_desc = ureq::post("http://www.boomlings.com/database/downloadGJLevel22.php") let level_desc = ureq::post("http://www.boomlings.com/database/downloadGJLevel22.php")
.set("User-Agent", "") .set("User-Agent", "")
.send_form(&[ .send_form(&[
@ -157,11 +164,23 @@ fn query_level_id(tx: &mpsc::Sender<Event>, id: String) -> Result<Option<SfhSong
} }
let song_id = map.get(&35).unwrap(); let song_id = map.get(&35).unwrap();
let level_name = map.get(&2).unwrap();
tx.send(Event::LoadStatusUpdate { description: "searching SongFileHub for song...".into() })?;
let sfh_response: SfhResponse = ureq::get(&format!("https://songfilehub.com/api/v1/nongs?id={}", song_id)) let sfh_response: SfhResponse = ureq::get(&format!("https://songfilehub.com/api/v1/nongs?id={}", song_id))
.call()? .call()?
.into_json()?; .into_json()?;
let sfh_descriptor_optional = sfh_response.songs.into_iter().find(|song| song.level_name_caps == level_name.to_uppercase());
tx.send(Event::LoadStatusUpdate { description:
format!("searching SongFileHub for song... {}", if sfh_descriptor_optional.is_some() {"Found!"}
else {"Not found; falling back to download watcher"})
})?;
if let Some(song) = sfh_descriptor_optional {
let song_reader = ureq::get(&song.download_url)
.call()?.into_reader();
}
todo!() todo!()
} }

Loading…
Cancel
Save