Skip to content

Commit

Permalink
fix: make recording saved message more specific to each case
Browse files Browse the repository at this point in the history
  • Loading branch information
SeaDve committed Apr 24, 2023
1 parent fba03df commit 211130a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
1 change: 1 addition & 0 deletions po/POTFILES.in
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ src/about.rs
src/core/date_time.rs
src/main.rs
src/preferences_window.rs
src/recognizer/mod.rs
src/recognizer/provider/error.rs
src/window/external_link_tile.rs
src/window/history_view.rs
Expand Down
49 changes: 41 additions & 8 deletions src/recognizer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod recording;
mod recordings;

use anyhow::{ensure, Context, Result};
use gettextrs::gettext;
use gst::prelude::*;
use gtk::{
gio::{self, prelude::*},
Expand Down Expand Up @@ -84,7 +85,9 @@ mod imp {
Signal::builder("song-recognized")
.param_types([Song::static_type()])
.build(),
Signal::builder("recording-saved").build(),
Signal::builder("recording-saved")
.param_types([String::static_type()])
.build(),
]
});

Expand Down Expand Up @@ -115,6 +118,10 @@ impl Recognizer {
)
}

fn emit_recording_peak_changed(&self, peak: f64) {
self.emit_by_name::<()>("recording-peak-changed", &[&peak]);
}

pub fn connect_song_recognized<F>(&self, f: F) -> glib::SignalHandlerId
where
F: Fn(&Self, &Song) + 'static,
Expand All @@ -128,19 +135,27 @@ impl Recognizer {
)
}

fn emit_song_recognized(&self, song: &Song) {
self.emit_by_name::<()>("song-recognized", &[song]);
}

pub fn connect_recording_saved<F>(&self, f: F) -> glib::SignalHandlerId
where
F: Fn(&Self) + 'static,
F: Fn(&Self, &str) + 'static,
{
self.connect_closure(
"recording-saved",
true,
closure_local!(|obj: &Self| {
f(obj);
closure_local!(|obj: &Self, message: &str| {
f(obj, message);
}),
)
}

fn emit_recording_saved(&self, message: &str) {
self.emit_by_name::<()>("recording-saved", &[&message]);
}

pub fn bind_saved_recordings(&self, recordings: &Recordings) {
self.imp()
.saved_recordings
Expand Down Expand Up @@ -256,7 +271,7 @@ impl Recognizer {
.start(
Some(&device_name),
clone!(@weak self as obj => move |peak| {
obj.emit_by_name::<()>("recording-peak-changed", &[&peak]);
obj.emit_recording_peak_changed(peak);
}),
)
.context("Failed to start recording")?;
Expand Down Expand Up @@ -289,7 +304,9 @@ impl Recognizer {
self.saved_recordings()
.insert(Recording::new(&recording_bytes, &recorded_time))
.context("Failed to insert recording")?;
self.emit_by_name::<()>("recording-saved", &[]);
self.emit_recording_saved(&gettext(
"The result will be available when you're back online.",
));
tracing::debug!("Offline mode is active; saved recording for later recognition");
return Ok(());
}
Expand All @@ -305,7 +322,7 @@ impl Recognizer {
Ok(song) => {
song.set_last_heard(recorded_time);

self.emit_by_name::<()>("song-recognized", &[&song]);
self.emit_song_recognized(&song);
}
Err(err) => {
if err.is_permanent() {
Expand All @@ -315,7 +332,23 @@ impl Recognizer {
self.saved_recordings()
.insert(Recording::new(&recording_bytes, &recorded_time))
.context("Failed to insert recording")?;
self.emit_by_name::<()>("recording-saved", &[]);
let message = match err.kind() {
RecognizeErrorKind::Connection => {
gettext("The result will be available when your connection is restored.")
}
RecognizeErrorKind::TokenLimitReached => {
gettext("The result will be available when your token limit is reset.")
}
RecognizeErrorKind::InvalidToken => {
gettext("The result will be available when your token is replaced.")
}
RecognizeErrorKind::NoMatches
| RecognizeErrorKind::Fingerprint
| RecognizeErrorKind::OtherPermanent => {
unreachable!("permanent errors should have been returned")
}
};
self.emit_recording_saved(&message);
tracing::debug!("Recognition failed with non-permanent error `{:?}`; saved recording for later recognition", err);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,10 @@ impl Window {
main_view.scroll_to_top();
}));
imp.recognizer
.connect_recording_saved(clone!(@weak self as obj => move |_| {
.connect_recording_saved(clone!(@weak self as obj => move |_, message| {
let dialog = adw::MessageDialog::builder()
.heading(gettext("Recording saved"))
.body(gettext("The result will be available when you're back online."))
.body(message)
.default_response("ok")
.transient_for(&obj)
.modal(true)
Expand Down

0 comments on commit 211130a

Please sign in to comment.