From a6feac8d4f3b5712902ec28924ad016a68a4cfcc Mon Sep 17 00:00:00 2001 From: Mike Dilger Date: Fri, 23 Dec 2022 19:55:41 +1300 Subject: [PATCH] Settings page setup and plumbed --- src/overlord/mod.rs | 13 ++++- src/ui/feed.rs | 4 +- src/ui/mod.rs | 7 +-- src/ui/settings.rs | 118 +++++++++++++++++++++++++++++++++++--------- 4 files changed, 114 insertions(+), 28 deletions(-) diff --git a/src/overlord/mod.rs b/src/overlord/mod.rs index be016ef1..eb1e7dfa 100644 --- a/src/overlord/mod.rs +++ b/src/overlord/mod.rs @@ -294,7 +294,18 @@ impl Overlord { // FIXME: Handle EventKind::ContactList } } - "minion_is_ready" => {} + "minion_is_ready" => {}, + "save_settings" => { + let settings: Settings = serde_json::from_str(&bus_message.json_payload)?; + + // Save to database + settings.save().await?; // to database + + // Update in globals + *GLOBALS.settings.lock().await = settings; + + debug!("Settings saved."); + }, _ => {} }, _ => {} diff --git a/src/ui/feed.rs b/src/ui/feed.rs index a859639c..4881fe01 100644 --- a/src/ui/feed.rs +++ b/src/ui/feed.rs @@ -106,7 +106,7 @@ fn render_post( ui.horizontal(|ui| { // Indents first (if threaded) - if app.threaded { + if app.settings.view_threaded { for _ in 0..indent { ui.add_space(8.0); ui.separator(); @@ -170,7 +170,7 @@ fn render_post( ui.separator(); - if app.threaded { + if app.settings.view_threaded { for reply_id in fevent.replies { render_post(app, _ctx, _frame, ui, reply_id, indent + 1); } diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 3e2d8001..842c6e90 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -10,6 +10,7 @@ mod you; use crate::about::About; use crate::error::Error; use crate::globals::GLOBALS; +use crate::settings::Settings; use eframe::{egui, IconData, Theme}; use egui::{ColorImage, Context, ImageData, TextureHandle, TextureOptions}; @@ -59,7 +60,7 @@ struct GossipUi { icon: TextureHandle, placeholder_avatar: TextureHandle, draft: String, - threaded: bool, + settings: Settings, } impl GossipUi { @@ -102,7 +103,7 @@ impl GossipUi { ) }; - let threaded = GLOBALS.settings.blocking_lock().view_threaded; + let settings = GLOBALS.settings.blocking_lock().clone(); GossipUi { page: Page::Feed, @@ -110,7 +111,7 @@ impl GossipUi { icon: icon_texture_handle, placeholder_avatar: placeholder_avatar_texture_handle, draft: "".to_owned(), - threaded, + settings: settings, } } } diff --git a/src/ui/settings.rs b/src/ui/settings.rs index 29797ea7..bf741711 100644 --- a/src/ui/settings.rs +++ b/src/ui/settings.rs @@ -1,10 +1,12 @@ +use crate::GLOBALS; +use crate::comms::BusMessage; use super::GossipUi; use eframe::egui; -use egui::widgets::Button; -use egui::{Context, Ui}; +use egui::{Align, Context, Layout, Ui}; +use egui::widgets::{Button, Slider}; pub(super) fn update( - _app: &mut GossipUi, + app: &mut GossipUi, _ctx: &Context, _frame: &mut eframe::Frame, ui: &mut Ui, @@ -12,24 +14,96 @@ pub(super) fn update( ) { ui.heading("Settings"); - #[allow(clippy::collapsible_else_if)] - if darkmode { - if ui - .add(Button::new("☀ Light")) - .on_hover_text("Switch to light mode") - .clicked() - { - ui.ctx().set_visuals(super::style::light_mode_visuals()); - } - } else { - if ui - .add(Button::new("🌙 Dark")) - .on_hover_text("Switch to dark mode") - .clicked() - { - ui.ctx().set_visuals(super::style::dark_mode_visuals()); - } - } + ui.separator(); + + ui.add_space(24.0); + ui.heading("How Many Posts to Load"); + + ui.horizontal(|ui| { + ui.label("Feed Chunk: ").on_hover_text("This is the amount of time backwards from now that we will load events from. You'll eventually be able to load more, one chunk at a time. Mostly takes effect on restart."); + ui.add(Slider::new(&mut app.settings.feed_chunk, 600..=86400).text("seconds, ")); + ui.label(secs_to_string(app.settings.feed_chunk)); + }); + + ui.horizontal(|ui| { + ui.label("Overlap: ").on_hover_text("If we recently loaded events up to time T, but restarted, we will now load events starting from time T minus overlap. Takes effect on restart."); + ui.add(Slider::new(&mut app.settings.overlap, 0..=3600).text("seconds, ")); + ui.label(secs_to_string(app.settings.overlap)); + }); + + ui.add_space(24.0); + ui.heading("Feed Style / Order"); + + ui.checkbox(&mut app.settings.view_threaded, "Threaded feed") + .on_hover_text("If selected, replies are under what they reply to and the newest replied-to thread comes first. Otherwise all posts are independent and in time order."); + + ui.add_space(24.0); + ui.heading("What Posts to Include"); + + ui.checkbox(&mut app.settings.view_posts_referred_to, "View posts referred to by people you follow (not yet implemented)") + .on_hover_text("Recommended, otherwise it's hard to understand what the person is talking about."); + + ui.checkbox(&mut app.settings.view_posts_referring_to, "View posts referring to posts by people you follow (not yet implemented)") + .on_hover_text("Not recommended, as anyone can reply to them and you'll certainly encounter spam this way."); + + ui.add_space(24.0); + ui.heading("Miscellaneous"); + + ui.checkbox(&mut app.settings.autofollow, "Autofollow everybody (not yet implemented)") + .on_hover_text("Definately not recommended. In fact we may remove this soon."); + + ui.add_space(24.0); + ui.heading("Style"); + + ui.horizontal(|ui| { + ui.label("Switch to"); + + #[allow(clippy::collapsible_else_if)] + if darkmode { + if ui + .add(Button::new("☀ Light")) + .on_hover_text("Switch to light mode") + .clicked() + { + ui.ctx().set_visuals(super::style::light_mode_visuals()); + } + } else { + if ui + .add(Button::new("🌙 Dark")) + .on_hover_text("Switch to dark mode") + .clicked() + { + ui.ctx().set_visuals(super::style::dark_mode_visuals()); + } + } + }); + + ui.add_space(32.0); + ui.with_layout(Layout::top_down(Align::Center), |ui| { + if ui.button("SAVE CHANGES").clicked() { + let tx = GLOBALS.to_overlord.clone(); + let _ = tx.send(BusMessage { + target: "overlord".to_string(), + kind: "save_settings".to_string(), + json_payload: serde_json::to_string(&app.settings).unwrap() + }); + } + }); - ui.label("SETTINGS PAGE - Coming Soon".to_string()); +} + +fn secs_to_string(secs: u64) -> String { + + let days = secs / 86400; + let remainder = secs % 86400; + let hours = remainder / 3600; + let remainder = remainder % 3600; + let minutes = remainder / 60; + let seconds = remainder % 60; + let mut output: String = String::new(); + if days>0 { output.push_str(&format!(" {} days", days)); } + if hours>0 { output.push_str(&format!(" {} hours", hours)); } + if minutes>0 { output.push_str(&format!(" {} minutes", minutes)); } + output.push_str(&format!(" {} seconds", seconds)); + output }