budctlc7666c2
budctl-gui: try again when the window gets focus after a failure
If the buds were not connected (or the channel was busy, or a command failed), getting focus reads them again, as an old reading already did: open the case, click the window, and it connects. No new dependencies.
+22 −1
1 file changed
- M crates/budctl-gui/src/main.rs +22 −1
| @@ -400,7 +400,11 @@ impl App { | |||
| 400 | 400 | Message::Worker(event) => self.answer(event), | |
| 401 | 401 | Message::Focused => { | |
| 402 | 402 | self.refresh_palette(); | |
| 403 | − | if self.read_at.is_some_and(|at| at.elapsed() >= STALE) && self.in_flight.is_empty() { | |
| 403 | + | // The phone may have changed something, or the buds may be back: read again | |
| 404 | + | // when the reading is old or the last command failed. | |
| 405 | + | let stale = self.read_at.is_some_and(|at| at.elapsed() >= STALE); | |
| 406 | + | let failed = matches!(self.link, Link::Failed { .. }); | |
| 407 | + | if (stale || failed) && self.in_flight.is_empty() { | |
| 404 | 408 | self.refresh(); | |
| 405 | 409 | } | |
| 406 | 410 | } | |
| @@ -1490,6 +1494,23 @@ mod tests { | |||
| 1490 | 1494 | assert_eq!(app.items().len(), 10); | |
| 1491 | 1495 | } | |
| 1492 | 1496 | ||
| 1497 | + | #[test] | |
| 1498 | + | fn focus_tries_again_after_a_failure() { | |
| 1499 | + | let (mut app, commands) = live(); | |
| 1500 | + | let _ = commands.try_recv(); | |
| 1501 | + | let _ = app.update(Message::Worker(Event::Failed { | |
| 1502 | + | kind: Kind::NotFound, | |
| 1503 | + | message: "realme Buds T310 is paired but not connected".to_owned(), | |
| 1504 | + | })); | |
| 1505 | + | let _ = app.update(Message::Focused); | |
| 1506 | + | assert!(matches!(commands.try_recv(), Ok(Command::Read))); | |
| 1507 | + | assert_eq!(app.link, Link::Connecting); | |
| 1508 | + | // A fresh reading is not read again on focus. | |
| 1509 | + | let _ = app.update(Message::Worker(Event::Read(Box::new(reading())))); | |
| 1510 | + | let _ = app.update(Message::Focused); | |
| 1511 | + | assert!(commands.try_recv().is_err()); | |
| 1512 | + | } | |
| 1513 | + | ||
| 1493 | 1514 | #[test] | |
| 1494 | 1515 | fn the_preset_in_use_is_not_written_again() { | |
| 1495 | 1516 | let (mut app, commands) = live(); | |