Skip to content

Commit

Permalink
Add first preference
Browse files Browse the repository at this point in the history
  • Loading branch information
blissd committed Apr 11, 2024
1 parent eafa51f commit 017d6a0
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 18 deletions.
18 changes: 15 additions & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ mod components;

use self::components::{
about::AboutDialog,
preferences::PreferencesDialog,
preferences::{PreferencesDialog, PreferencesInput, PreferencesOutput},
album::{Album, AlbumFilter, AlbumInput, AlbumOutput},
folder_photos::{FolderPhotos, FolderPhotosInput, FolderPhotosOutput},
month_photos::{MonthPhotos, MonthPhotosInput, MonthPhotosOutput},
Expand Down Expand Up @@ -142,6 +142,9 @@ pub(super) enum AppMsg {
// Cleanup events
CleanupStarted,
CleanupCompleted,

// Preferences
PreferencesUpdated,
}

relm4::new_action_group!(pub(super) WindowActionGroup, "win");
Expand Down Expand Up @@ -320,6 +323,7 @@ impl SimpleComponent for App {
set_icon_name: "image-alt-symbolic",
},


add_child = &gtk::Box {
set_orientation: gtk::Orientation::Vertical,
container_add: model.selfie_photos.widget(),
Expand Down Expand Up @@ -485,7 +489,9 @@ impl SimpleComponent for App {

let preferences_dialog = PreferencesDialog::builder()
.launch(root.clone())
.detach();
.forward(sender.input_sender(), |msg| match msg {
PreferencesOutput::Updated => AppMsg::PreferencesUpdated,
});

let library_view_stack = adw::ViewStack::new();

Expand Down Expand Up @@ -553,7 +559,7 @@ impl SimpleComponent for App {
let preferences_action = {
let sender = model.preferences_dialog.sender().clone();
RelmAction::<PreferencesAction>::new_stateless(move |_| {
sender.send(()).unwrap();
sender.send(PreferencesInput::Present).unwrap();
})
};

Expand Down Expand Up @@ -711,6 +717,12 @@ impl SimpleComponent for App {
self.banner.set_title("Database maintenance.");
self.banner.set_revealed(true);
}

AppMsg::PreferencesUpdated => {
println!("Preferences updated.");
// TODO create a Preferences struct to hold preferences and send with update message.
self.show_selfies = AppWidgets::show_selfies();
}
}
}

Expand Down
94 changes: 79 additions & 15 deletions src/app/components/preferences.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,102 @@

use relm4::{adw, ComponentParts, ComponentSender, SimpleComponent};
use relm4::adw::prelude::AdwDialogExt;
use relm4::gtk::prelude::WidgetExt;
use relm4::gtk::prelude::SettingsExt;
use relm4::gtk::gio;
use relm4::adw::prelude::PreferencesDialogExt;
use relm4::adw::prelude::PreferencesPageExt;
use relm4::adw::prelude::PreferencesGroupExt;
use relm4::adw::prelude::ActionRowExt;
use relm4::adw::prelude::PreferencesRowExt;

use crate::config::APP_ID;


pub struct PreferencesDialog {
parent: adw::ApplicationWindow,
dialog: adw::PreferencesDialog,

// Preference values
show_selfies: bool,
}

#[derive(Debug)]
pub enum PreferencesInput {
Present,
ShowSelfies(bool),
}

#[derive(Debug)]
pub enum PreferencesOutput {
Updated,
}

#[relm4::component(pub)]
impl SimpleComponent for PreferencesDialog {
type Init = adw::ApplicationWindow;
type Widgets = adw::PreferencesDialog;
type Input = ();
type Output = ();
type Root = adw::PreferencesDialog;

fn init_root() -> Self::Root {
adw::PreferencesDialog::builder()
.build()
type Input = PreferencesInput;
type Output = PreferencesOutput;

view!{
adw::PreferencesDialog {
add = &adw::PreferencesPage {
add = &adw::PreferencesGroup {
set_title: "Views",
set_description: Some("Show or hide sidebar views"),

adw::SwitchRow {
set_title: "Selfies",
set_subtitle: "Shows a separate view for selfies taken on iOS devices. Restart Fotema to apply.",

#[watch]
set_active: model.show_selfies,

connect_active_notify[sender] => move |switch| {
sender.input_sender().send(PreferencesInput::ShowSelfies(switch.is_active())).unwrap();
},
}
}
}
}
}


fn init(
parent: Self::Init,
root: Self::Root,
_sender: ComponentSender<Self>,
dialog: Self::Root,
sender: ComponentSender<Self>,
) -> ComponentParts<Self> {
let model = Self {parent};

let widgets = root.clone();

let settings = gio::Settings::new(APP_ID);
let show_selfies = settings.boolean("show-selfies");

let model = Self {
parent,
dialog: dialog.clone(),
show_selfies,
};

let widgets = view_output!();

ComponentParts { model, widgets }
}

fn update_view(&self, dialog: &mut Self::Widgets, _sender: ComponentSender<Self>) {
dialog.present(&self.parent);
fn update(&mut self, msg: Self::Input, sender: ComponentSender<Self>) {
match msg {
PreferencesInput::Present => {
let settings = gio::Settings::new(APP_ID);
self.show_selfies = settings.boolean("show-selfies");
self.dialog.present(&self.parent);
},
PreferencesInput::ShowSelfies(visible) => {
let settings = gio::Settings::new(APP_ID);
self.show_selfies = visible;

settings.set_boolean("show-selfies", visible).expect("Update settings");

sender.output(PreferencesOutput::Updated).expect("Sending update prefs");
},
}
}
}

0 comments on commit 017d6a0

Please sign in to comment.