From 784f3ff7f4808988ba101c78e2761509e66a2961 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Thu, 24 Aug 2023 20:52:27 -0400 Subject: [PATCH] Be able to update the library path in the core --- kifu/core/src/api.rs | 48 +++++++++++++++++++++++---------------- kifu/core/src/lib.rs | 3 ++- kifu/core/src/types.rs | 2 +- kifu/gtk/src/main.rs | 18 ++++----------- kifu/gtk/src/ui/config.rs | 11 +++++---- 5 files changed, 43 insertions(+), 39 deletions(-) diff --git a/kifu/core/src/api.rs b/kifu/core/src/api.rs index 9ac63a0..76d5264 100644 --- a/kifu/core/src/api.rs +++ b/kifu/core/src/api.rs @@ -1,15 +1,19 @@ use crate::{ - types::{AppState, Config, DatabasePath, GameState, Player, Rank}, + types::{AppState, Config, ConfigOption, DatabasePath, GameState, Player, Rank}, ui::{configuration, home, playing_field, ConfigurationView, HomeView, PlayingFieldView}, }; use serde::{Deserialize, Serialize}; -use std::sync::{Arc, RwLock}; +use std::{ + path::PathBuf, + sync::{Arc, RwLock}, +}; use typeshare::typeshare; #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[typeshare] #[serde(tag = "type", content = "content")] pub enum CoreRequest { + ChangeSetting(ChangeSettingRequest), CreateGame(CreateGameRequest), Home, OpenConfiguration, @@ -18,6 +22,13 @@ pub enum CoreRequest { StartGame, } +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[typeshare] +#[serde(tag = "type", content = "content")] +pub enum ChangeSettingRequest { + LibraryPath(String), +} + #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] #[typeshare] pub struct PlayStoneRequest { @@ -61,11 +72,12 @@ pub enum CoreResponse { ConfigurationView(ConfigurationView), HomeView(HomeView), PlayingFieldView(PlayingFieldView), + UpdatedConfigurationView(ConfigurationView), } #[derive(Clone, Debug)] pub struct CoreApp { - config: Config, + config: Arc>, state: Arc>, } @@ -76,25 +88,23 @@ impl CoreApp { let db_path: DatabasePath = config.get().unwrap(); let state = Arc::new(RwLock::new(AppState::new(db_path))); - Self { config, state } + Self { + config: Arc::new(RwLock::new(config)), + state, + } } pub async fn dispatch(&self, request: CoreRequest) -> CoreResponse { match request { - /* - CoreRequest::LaunchScreen => { - let app_state = self.state.read().unwrap(); - - At launch, I want to either show a list of games in progress, the current game, or the game creation screen. - - if a live game is in progress, immmediately go to that game. Such a game will be classified at game creation, so it should be persisted to the state. - - if no live games are in progress, but there are slow games in progress, show a list of the slow games and let the player choose which one to jump into. - - if no games are in progress, show only the game creation screen - - game creation menu should be present both when there are only slow games and when there are no games - - the UI returned here will always be available in other places, such as when the user is viewing a game and wants to return to this page - - For the initial version, I want only to show the game creation screen. Then I will backtrack record application state so that the only decisions can be made. - } - */ + CoreRequest::ChangeSetting(request) => match request { + ChangeSettingRequest::LibraryPath(path) => { + let mut config = self.config.write().unwrap(); + config.set(ConfigOption::DatabasePath(DatabasePath(PathBuf::from( + path, + )))); + CoreResponse::UpdatedConfigurationView(configuration(&config)) + } + }, CoreRequest::CreateGame(create_request) => { let mut app_state = self.state.write().unwrap(); let white_player = { @@ -119,7 +129,7 @@ impl CoreApp { CoreResponse::HomeView(home(self.state.read().unwrap().database.all_games())) } CoreRequest::OpenConfiguration => { - CoreResponse::ConfigurationView(configuration(&self.config)) + CoreResponse::ConfigurationView(configuration(&self.config.read().unwrap())) } CoreRequest::PlayingField => { let app_state = self.state.read().unwrap(); diff --git a/kifu/core/src/lib.rs b/kifu/core/src/lib.rs index b817d92..ceda4df 100644 --- a/kifu/core/src/lib.rs +++ b/kifu/core/src/lib.rs @@ -3,7 +3,8 @@ extern crate config_derive; mod api; pub use api::{ - CoreApp, CoreRequest, CoreResponse, CreateGameRequest, HotseatPlayerRequest, PlayerInfoRequest, + ChangeSettingRequest, CoreApp, CoreRequest, CoreResponse, CreateGameRequest, + HotseatPlayerRequest, PlayerInfoRequest, }; mod board; diff --git a/kifu/core/src/types.rs b/kifu/core/src/types.rs index 28ff8f3..5754c42 100644 --- a/kifu/core/src/types.rs +++ b/kifu/core/src/types.rs @@ -16,7 +16,7 @@ define_config! { } #[derive(Clone, Debug, PartialEq, Serialize, Deserialize, ConfigOption)] -pub struct DatabasePath(PathBuf); +pub struct DatabasePath(pub PathBuf); impl std::ops::Deref for DatabasePath { type Target = PathBuf; diff --git a/kifu/gtk/src/main.rs b/kifu/gtk/src/main.rs index 9cf9198..3217134 100644 --- a/kifu/gtk/src/main.rs +++ b/kifu/gtk/src/main.rs @@ -11,20 +11,7 @@ fn handle_response(api: CoreApi, layout: Layout, message: CoreResponse) { let playing_field = Arc::new(RwLock::new(None)); match message { CoreResponse::ConfigurationView(view) => perftrace("ConfigurationView", || { - /* - let config_group = adw::PreferencesGroup::builder().build(); - config_group.add( - &adw::EntryRow::builder() - .name("library") - .title("Library path") - .build(), - ); - let config_page = adw::PreferencesPage::new(); - config_page.set_name(Some("preferences")); - config_page.add(&config_group); - */ - - let config_page = ConfigurationPage::new(view); + let config_page = ConfigurationPage::new(api, view); let window = adw::PreferencesWindow::new(); window.add(&config_page); @@ -51,6 +38,9 @@ fn handle_response(api: CoreApi, layout: Layout, message: CoreResponse) { playing_field.as_ref().map(|field| field.update_view(view)); } }), + CoreResponse::UpdatedConfigurationView(view) => perftrace("UpdatedConfiguration", || { + println!("updated configuration: {:?}", view); + }), } } diff --git a/kifu/gtk/src/ui/config.rs b/kifu/gtk/src/ui/config.rs index b854839..7dcb80d 100644 --- a/kifu/gtk/src/ui/config.rs +++ b/kifu/gtk/src/ui/config.rs @@ -1,6 +1,7 @@ +use crate::CoreApi; use adw::{prelude::*, subclass::prelude::*}; use glib::Object; -use kifu_core::ui::ConfigurationView; +use kifu_core::{ui::ConfigurationView, ChangeSettingRequest, CoreRequest}; #[derive(Default)] pub struct ConfigurationPagePrivate {} @@ -23,7 +24,7 @@ glib::wrapper! { } impl ConfigurationPage { - pub fn new(view: ConfigurationView) -> Self { + pub fn new(api: CoreApi, view: ConfigurationView) -> Self { let s: Self = Object::builder().build(); let group = adw::PreferencesGroup::builder().build(); @@ -36,8 +37,10 @@ impl ConfigurationPage { if let Some(path) = view.library.value { library_entry.set_text(&path); } - library_entry.connect_apply(|entry| { - println!("Set the library path to {}", entry.text()); + library_entry.connect_apply(move |entry| { + api.dispatch(CoreRequest::ChangeSetting( + ChangeSettingRequest::LibraryPath(entry.text().into()), + )); }); group.add(library_entry);