Browse Source

account-settings: Refactor NotificationsPage

fractal-12
Kévin Commaille 10 months ago committed by Kévin Commaille
parent
commit
59b6740ce5
  1. 174
      src/session/view/account_settings/notifications_page.rs
  2. 4
      src/session/view/account_settings/notifications_page.ui

174
src/session/view/account_settings/notifications_page.rs

@ -157,46 +157,56 @@ mod imp {
self.obj().notify_notifications_settings();
}
/// The global notifications setting, as a string.
fn global_setting(&self) -> String {
/// Update the account row.
fn update_account(&self) {
let Some(settings) = self.notifications_settings.obj() else {
return String::new();
return;
};
settings.global_setting().to_string()
}
let checked = settings.account_enabled();
self.account_row.set_is_active(checked);
self.account_row.set_sensitive(!self.account_loading.get());
/// Set the global notifications setting, as a string.
fn set_global_setting(&self, default: &str) {
let Ok(default) = default.parse::<NotificationsGlobalSetting>() else {
error!("Invalid value to set global default notifications setting: {default}");
return;
};
// Other sections will be disabled or not.
self.update_session();
}
spawn!(clone!(
#[weak(rename_to = imp)]
self,
async move {
imp.global_setting_changed(default).await;
}
));
/// Set the loading state of the account row.
fn set_account_loading(&self, loading: bool) {
self.account_loading.set(loading);
self.obj().notify_account_loading();
}
/// Update the section about the account.
fn update_account(&self) {
/// Set the account setting.
#[template_callback]
async fn set_account_enabled(&self) {
let Some(settings) = self.notifications_settings.obj() else {
return;
};
let checked = settings.account_enabled();
self.account_row.set_is_active(checked);
self.account_row.set_sensitive(!self.account_loading.get());
let enabled = self.account_row.is_active();
if enabled == settings.account_enabled() {
// Nothing to do.
return;
}
// Other sections will be disabled or not.
self.update_session();
self.account_row.set_sensitive(false);
self.set_account_loading(true);
if settings.set_account_enabled(enabled).await.is_err() {
let msg = if enabled {
gettext("Could not enable account notifications")
} else {
gettext("Could not disable account notifications")
};
toast!(self.obj(), msg);
}
self.set_account_loading(false);
self.update_account();
}
/// Update the section about the session.
/// Update the session row.
fn update_session(&self) {
let Some(settings) = self.notifications_settings.obj() else {
return;
@ -210,7 +220,26 @@ mod imp {
self.update_keywords();
}
/// Update the section about global.
/// Set the session setting.
#[template_callback]
fn set_session_enabled(&self) {
let Some(settings) = self.notifications_settings.obj() else {
return;
};
settings.set_session_enabled(self.session_row.is_active());
}
/// The global notifications setting, as a string.
fn global_setting(&self) -> String {
let Some(settings) = self.notifications_settings.obj() else {
return String::new();
};
settings.global_setting().to_string()
}
/// Update the global section.
fn update_global(&self) {
let Some(settings) = self.notifications_settings.obj() else {
return;
@ -225,67 +254,48 @@ mod imp {
self.global.set_sensitive(sensitive);
}
/// Update the section about keywords.
#[template_callback]
fn update_keywords(&self) {
let Some(settings) = self.notifications_settings.obj() else {
/// Set the global setting, as a string.
fn set_global_setting(&self, default: &str) {
let Ok(default) = default.parse::<NotificationsGlobalSetting>() else {
error!("Invalid value to set global default notifications setting: {default}");
return;
};
let sensitive = settings.account_enabled() && settings.session_enabled();
self.keywords.set_sensitive(sensitive);
if !sensitive {
// Nothing else to update.
return;
}
self.keywords_add_row
.set_inhibit_add(!self.can_add_keyword());
}
fn set_account_loading(&self, loading: bool) {
self.account_loading.set(loading);
self.obj().notify_account_loading();
spawn!(clone!(
#[weak(rename_to = imp)]
self,
async move {
imp.set_global_setting_inner(default).await;
}
));
}
#[template_callback]
async fn account_switched(&self) {
/// Propagate the global setting.
async fn set_global_setting_inner(&self, setting: NotificationsGlobalSetting) {
let Some(settings) = self.notifications_settings.obj() else {
return;
};
let enabled = self.account_row.is_active();
if enabled == settings.account_enabled() {
if setting == settings.global_setting() {
// Nothing to do.
return;
}
self.account_row.set_sensitive(false);
self.set_account_loading(true);
self.global.set_sensitive(false);
self.set_global_loading(true, setting);
if settings.set_account_enabled(enabled).await.is_err() {
let msg = if enabled {
gettext("Could not enable account notifications")
} else {
gettext("Could not disable account notifications")
};
toast!(self.obj(), msg);
if settings.set_global_setting(setting).await.is_err() {
toast!(
self.obj(),
gettext("Could not change global notifications setting"),
);
}
self.set_account_loading(false);
self.update_account();
}
#[template_callback]
fn session_switched(&self) {
let Some(settings) = self.notifications_settings.obj() else {
return;
};
settings.set_session_enabled(self.session_row.is_active());
self.set_global_loading(false, setting);
self.update_global();
}
/// Set the loading state of the global section.
fn set_global_loading(&self, loading: bool, setting: NotificationsGlobalSetting) {
// Only show the spinner on the selected one.
self.global_all_row
@ -300,29 +310,23 @@ mod imp {
self.obj().notify_global_loading();
}
/// Update the section about keywords.
#[template_callback]
async fn global_setting_changed(&self, setting: NotificationsGlobalSetting) {
fn update_keywords(&self) {
let Some(settings) = self.notifications_settings.obj() else {
return;
};
if setting == settings.global_setting() {
// Nothing to do.
return;
}
self.global.set_sensitive(false);
self.set_global_loading(true, setting);
let sensitive = settings.account_enabled() && settings.session_enabled();
self.keywords.set_sensitive(sensitive);
if settings.set_global_setting(setting).await.is_err() {
toast!(
self.obj(),
gettext("Could not change global notifications setting"),
);
if !sensitive {
// Nothing else to update.
return;
}
self.set_global_loading(false, setting);
self.update_global();
self.keywords_add_row
.set_inhibit_add(!self.can_add_keyword());
}
/// Create a row in the keywords list for the given item.

4
src/session/view/account_settings/notifications_page.ui

@ -10,14 +10,14 @@
<object class="SwitchLoadingRow" id="account_row">
<property name="title" translatable="yes">Enable for This Account</property>
<property name="is-loading" bind-source="NotificationsPage" bind-property="account-loading" bind-flags="sync-create"/>
<signal name="notify::is-active" handler="account_switched" swapped="true"/>
<signal name="notify::is-active" handler="set_account_enabled" swapped="true"/>
</object>
</child>
<child>
<object class="AdwSwitchRow" id="session_row">
<property name="selectable">False</property>
<property name="title" translatable="yes">Enable for This Session</property>
<signal name="notify::active" handler="session_switched" swapped="true"/>
<signal name="notify::active" handler="set_session_enabled" swapped="true"/>
</object>
</child>
</object>

Loading…
Cancel
Save