InAppNotif: Refactor to infer the undo state

If we use an Optional instead of passing empty closures, we
can infer if the Undo button needs to be shown.
This commit is contained in:
Jordan Petridis 2018-08-13 08:31:52 +03:00
parent 866fa6a758
commit 01efbf5c79
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
4 changed files with 22 additions and 18 deletions

View File

@ -100,7 +100,6 @@ Tobias Bernard
<child>
<object class="GtkButton" id="undo">
<property name="label" translatable="yes">Undo</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="halign">center</property>

View File

@ -17,7 +17,7 @@ use settings::{self, WindowGeometry};
use stacks::{Content, PopulatedState};
use utils;
use widgets::about_dialog;
use widgets::appnotif::{InAppNotification, UndoState};
use widgets::appnotif::InAppNotification;
use widgets::player;
use widgets::show_menu::{mark_all_notif, remove_show_notif, ShowMenu};
@ -306,7 +306,8 @@ impl App {
Action::ErrorNotification(err) => {
error!("An error notification was triggered: {}", err);
let callback = || glib::Continue(false);
let notif = InAppNotification::new(&err, callback, || {}, UndoState::Hidden);
let undo_cb: Option<fn()> = None;
let notif = InAppNotification::new(&err, callback, undo_cb);
notif.show(&self.overlay);
}
Action::InitEpisode(rowid) => {

View File

@ -6,6 +6,7 @@ use std::cell::RefCell;
use std::rc::Rc;
#[derive(Debug, Clone, Copy)]
#[allow(dead_code)]
pub(crate) enum UndoState {
Shown,
Hidden,
@ -38,12 +39,7 @@ impl Default for InAppNotification {
}
impl InAppNotification {
pub(crate) fn new<F, U>(
text: &str,
mut callback: F,
undo_callback: U,
show_undo: UndoState,
) -> Self
pub(crate) fn new<F, U>(text: &str, mut callback: F, undo_callback: Option<U>) -> Self
where
F: FnMut() -> glib::Continue + 'static,
U: Fn() + 'static,
@ -58,6 +54,10 @@ impl InAppNotification {
});
let id = Rc::new(RefCell::new(Some(id)));
if undo_callback.is_some() {
notif.set_undo_state(UndoState::Shown)
};
// Cancel the callback
let revealer = notif.revealer.clone();
notif.undo.connect_clicked(move |_| {
@ -66,7 +66,9 @@ impl InAppNotification {
glib::source::source_remove(id);
}
undo_callback();
if let Some(ref f) = undo_callback {
f();
}
// Hide the notification
revealer.set_reveal_child(false);
@ -78,11 +80,6 @@ impl InAppNotification {
revealer.set_reveal_child(false);
});
match show_undo {
UndoState::Shown => (),
UndoState::Hidden => notif.undo.hide(),
}
notif
}
@ -96,4 +93,11 @@ impl InAppNotification {
// so there will be a nice animation.
self.revealer.set_reveal_child(true);
}
pub(crate) fn set_undo_state(&self, state: UndoState) {
match state {
UndoState::Shown => self.undo.show(),
UndoState::Hidden => self.undo.hide(),
}
}
}

View File

@ -13,7 +13,7 @@ use podcasts_data::Show;
use app::Action;
use utils;
use widgets::appnotif::{InAppNotification, UndoState};
use widgets::appnotif::InAppNotification;
use std::sync::Arc;
@ -145,7 +145,7 @@ pub(crate) fn mark_all_notif(pd: Arc<Show>, sender: &Sender<Action>) -> InAppNot
let undo_callback = clone!(sender => move || sender.send(Action::RefreshWidgetIfSame(id)));
let text = i18n("Marked all episodes as listened");
InAppNotification::new(&text, callback, undo_callback, UndoState::Shown)
InAppNotification::new(&text, callback, Some(undo_callback))
}
pub(crate) fn remove_show_notif(pd: Arc<Show>, sender: Sender<Action>) -> InAppNotification {
@ -177,5 +177,5 @@ pub(crate) fn remove_show_notif(pd: Arc<Show>, sender: Sender<Action>) -> InAppN
sender.send(Action::RefreshEpisodesView);
};
InAppNotification::new(&text, callback, undo_callback, UndoState::Shown)
InAppNotification::new(&text, callback, Some(undo_callback))
}