From 6f94e15c5d16d66b2ead796d37bd73316f6c7c91 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Tue, 27 Feb 2024 08:34:17 -0500 Subject: [PATCH] Create the placeholder for the database view model --- Cargo.lock | 1 + kifu/core/src/api.rs | 4 +- kifu/core/src/lib.rs | 4 +- kifu/gtk/Cargo.toml | 1 + kifu/gtk/src/lib.rs | 9 ++- kifu/gtk/src/main.rs | 5 +- .../src/view_models/database_view_model.rs | 59 +++++++++++++++++++ kifu/gtk/src/view_models/mod.rs | 25 ++++++++ kifu/gtk/src/views/mod.rs | 0 9 files changed, 99 insertions(+), 9 deletions(-) create mode 100644 kifu/gtk/src/view_models/database_view_model.rs create mode 100644 kifu/gtk/src/view_models/mod.rs create mode 100644 kifu/gtk/src/views/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 1ac6e92..484471d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2240,6 +2240,7 @@ name = "kifu-gtk" version = "0.1.0" dependencies = [ "async-channel 2.1.1", + "async-std", "cairo-rs", "gio", "glib", diff --git a/kifu/core/src/api.rs b/kifu/core/src/api.rs index 99dea43..f96bb8c 100644 --- a/kifu/core/src/api.rs +++ b/kifu/core/src/api.rs @@ -80,13 +80,13 @@ pub enum CoreNotification { } #[derive(Clone, Debug)] -pub struct CoreApp { +pub struct Core { // config: Arc>, // state: Arc>, database: Arc>>, } -impl CoreApp { +impl Core { pub fn new(_config: Config) -> Self { // let config = Config::from_path(config_path).expect("configuration to open"); diff --git a/kifu/core/src/lib.rs b/kifu/core/src/lib.rs index d5d377c..aa31343 100644 --- a/kifu/core/src/lib.rs +++ b/kifu/core/src/lib.rs @@ -2,8 +2,8 @@ extern crate config_derive; mod api; pub use api::{ - ChangeSettingRequest, CoreApp, CoreRequest, CoreResponse, CreateGameRequest, - HotseatPlayerRequest, PlayerInfoRequest, + ChangeSettingRequest, Core, CoreRequest, CoreResponse, CreateGameRequest, + HotseatPlayerRequest, PlayerInfoRequest, CoreNotification, }; mod board; diff --git a/kifu/gtk/Cargo.toml b/kifu/gtk/Cargo.toml index e96b3de..4167479 100644 --- a/kifu/gtk/Cargo.toml +++ b/kifu/gtk/Cargo.toml @@ -11,6 +11,7 @@ screenplay = [] [dependencies] adw = { version = "0.5", package = "libadwaita", features = [ "v1_2" ] } async-channel = { version = "2" } +async-std = { version = "1" } cairo-rs = { version = "0.18" } gio = { version = "0.18" } glib = { version = "0.18" } diff --git a/kifu/gtk/src/lib.rs b/kifu/gtk/src/lib.rs index e4dae37..e0428c0 100644 --- a/kifu/gtk/src/lib.rs +++ b/kifu/gtk/src/lib.rs @@ -1,5 +1,10 @@ pub mod ui; -use kifu_core::{CoreApp, CoreRequest, CoreResponse}; + +mod view_models; +mod views; + + +use kifu_core::{Core, CoreRequest, CoreResponse}; use std::sync::Arc; use tokio::{spawn, runtime::Runtime}; @@ -7,7 +12,7 @@ use tokio::{spawn, runtime::Runtime}; pub struct CoreApi { pub notification_tx: async_channel::Sender, pub rt: Arc, - pub core: CoreApp, + pub core: Core, } impl CoreApi { diff --git a/kifu/gtk/src/main.rs b/kifu/gtk/src/main.rs index 4edecc6..02d471b 100644 --- a/kifu/gtk/src/main.rs +++ b/kifu/gtk/src/main.rs @@ -1,5 +1,5 @@ use adw::prelude::*; -use kifu_core::{CoreApp, CoreRequest, CoreResponse, DatabasePath, Config, ConfigOption}; +use kifu_core::{Core, CoreRequest, CoreResponse, DatabasePath, Config, ConfigOption}; use kifu_gtk::{ perftrace, ui::{AppWindow, ConfigurationPage, Home, PlayingField}, @@ -7,7 +7,6 @@ use kifu_gtk::{ }; use std::sync::{Arc, RwLock}; - const APP_ID_DEV: &str = "com.luminescent-dreams.kifu-gtk.dev"; const APP_ID_PROD: &str = "com.luminescent-dreams.kifu-gtk"; @@ -86,7 +85,7 @@ fn main() { .expect("no config path could be found"); */ - let core = CoreApp::new(config); + let core = Core::new(config); /* let core_handle = runtime.spawn({ diff --git a/kifu/gtk/src/view_models/database_view_model.rs b/kifu/gtk/src/view_models/database_view_model.rs new file mode 100644 index 0000000..7582ff4 --- /dev/null +++ b/kifu/gtk/src/view_models/database_view_model.rs @@ -0,0 +1,59 @@ +/* +Copyright 2024, Savanni D'Gerinel + +This file is part of Kifu. + +Kifu is free software: you can redistribute it and/or modify it under the terms of the GNU +General Public License as published by the Free Software Foundation, either version 3 of the +License, or (at your option) any later version. + +Kifu is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without +even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License along with Kifu. If not, see . +*/ + +use async_channel::Receiver; +use async_std::task::{spawn, spawn_blocking, yield_now, JoinHandle}; +use kifu_core::{Core, CoreNotification}; + +/// DatabaseViewModel controls the view that the user sees when starting the application if the application has been configured and if there are no games in progress. It provides a window into the database, showing a list of recently recorded games (whether from this app or from a main database). It also provides the UI for starting a new game. This will render an empty database view if the user hasn't configured a database yet. +pub struct DatabaseViewModel { + core: Core, + notification_handler: JoinHandle<()>, + widget: gtk::Box, +} + +impl DatabaseViewModel { + fn new(core: Core, notifications: Receiver) -> Self { + let handler = spawn(async move { + loop { + let message = notifications.recv().await; + println!("Message received in DatabaseViewModel: {:?}", message); + yield_now().await; + } + }); + + Self { + core, + notification_handler: handler, + widget: gtk::Box::new(gtk::Orientation::Horizontal, 0), + } + } + + /// Create a new game with the given parameters. + fn new_game(&self) { + unimplemented!() + } + + /// Select a game from the database to show in detail. This will require a transition away from this view model into a different one. + fn select_game(&self) { + unimplemented!() + } + + /// Delete a game from the database. + fn delete_game(&self) { + unimplemented!() + } +} diff --git a/kifu/gtk/src/view_models/mod.rs b/kifu/gtk/src/view_models/mod.rs new file mode 100644 index 0000000..0082a78 --- /dev/null +++ b/kifu/gtk/src/view_models/mod.rs @@ -0,0 +1,25 @@ +/* +Copyright 2024, Savanni D'Gerinel + +This file is part of Kifu. + +Kifu is free software: you can redistribute it and/or modify it under the terms of the GNU +General Public License as published by the Free Software Foundation, either version 3 of the +License, or (at your option) any later version. + +Kifu is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without +even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License along with Kifu. If not, see . +*/ + + +/* +Every view model requires a reference to the app so that it can call functions on the core, and a notification receiver so that it can receive messages from the core. + +The view model is primary over the view. It will construct the view, it can make major changes to the view or even swap for another related view. It must listen for all messages from the core, discarding those that aren't relevant to it. It will also convert requests from sync to async. +*/ + +mod database_view_model; +pub use database_view_model::DatabaseViewModel; \ No newline at end of file diff --git a/kifu/gtk/src/views/mod.rs b/kifu/gtk/src/views/mod.rs new file mode 100644 index 0000000..e69de29