visible load status, etc

main
aprzn 1 year ago
parent f87c61e382
commit fa1f3d218d

@ -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<String> },
}
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<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")
.set("User-Agent", "")
.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 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))
.call()?
.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!()
}

Loading…
Cancel
Save