From 915028c32a6a62abb9dd917d232e84270cdc5bb0 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Tue, 27 Feb 2024 22:36:35 -0500 Subject: [PATCH] Set up the notifications pattern for view models --- kifu/core/src/api.rs | 25 +++++---- kifu/core/src/lib.rs | 2 +- kifu/gtk/src/lib.rs | 4 +- kifu/gtk/src/main.rs | 9 +--- kifu/gtk/src/view_models/game_view_model.rs | 57 +++++++++++---------- 5 files changed, 48 insertions(+), 49 deletions(-) diff --git a/kifu/core/src/api.rs b/kifu/core/src/api.rs index 6ffecc4..e656707 100644 --- a/kifu/core/src/api.rs +++ b/kifu/core/src/api.rs @@ -9,6 +9,11 @@ use std::{ path::PathBuf, sync::{Arc, RwLock}, }; + +pub trait Observable { + fn subscribe(&self) -> Receiver; +} + #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub enum CoreRequest { ChangeSetting(ChangeSettingRequest), @@ -92,15 +97,6 @@ impl Core { } } - pub fn subscribe(&self) -> Receiver { - let mut subscribers = self.subscribers.write().unwrap(); - - let (sender, receiver) = async_std::channel::unbounded(); - subscribers.push(sender); - - receiver - } - /* pub async fn dispatch(&self, request: CoreRequest) -> CoreResponse { match request { @@ -168,3 +164,14 @@ impl Core { // pub async fn run(&self) {} } + +impl Observable for Core { + fn subscribe(&self) -> Receiver { + let mut subscribers = self.subscribers.write().unwrap(); + + let (sender, receiver) = async_std::channel::unbounded(); + subscribers.push(sender); + + receiver + } +} diff --git a/kifu/core/src/lib.rs b/kifu/core/src/lib.rs index 2a62b0b..855fe82 100644 --- a/kifu/core/src/lib.rs +++ b/kifu/core/src/lib.rs @@ -3,7 +3,7 @@ extern crate config_derive; mod api; pub use api::{ ChangeSettingRequest, Core, CoreNotification, CoreRequest, CoreResponse, CreateGameRequest, - HotseatPlayerRequest, PlayerInfoRequest, + HotseatPlayerRequest, Observable, PlayerInfoRequest, }; mod board; diff --git a/kifu/gtk/src/lib.rs b/kifu/gtk/src/lib.rs index e0428c0..0a96a10 100644 --- a/kifu/gtk/src/lib.rs +++ b/kifu/gtk/src/lib.rs @@ -3,14 +3,12 @@ pub mod ui; mod view_models; mod views; - use kifu_core::{Core, CoreRequest, CoreResponse}; use std::sync::Arc; -use tokio::{spawn, runtime::Runtime}; +use tokio::{runtime::Runtime, spawn}; #[derive(Clone)] pub struct CoreApi { - pub notification_tx: async_channel::Sender, pub rt: Arc, pub core: Core, } diff --git a/kifu/gtk/src/main.rs b/kifu/gtk/src/main.rs index 02d471b..4601569 100644 --- a/kifu/gtk/src/main.rs +++ b/kifu/gtk/src/main.rs @@ -1,5 +1,5 @@ use adw::prelude::*; -use kifu_core::{Core, CoreRequest, CoreResponse, DatabasePath, Config, ConfigOption}; +use kifu_core::{Config, ConfigOption, Core, CoreRequest, CoreResponse, DatabasePath}; use kifu_gtk::{ perftrace, ui::{AppWindow, ConfigurationPage, Home, PlayingField}, @@ -104,16 +104,9 @@ fn main() { app.connect_activate({ let runtime = runtime.clone(); move |app| { - let (notification_tx, notification_rx) = async_channel::unbounded::(); - /* - let (gtk_tx, gtk_rx) = - gtk::glib::MainContext::channel::(gtk::glib::Priority::DEFAULT); - */ - let app_window = AppWindow::new(app); let api = CoreApi { - notification_tx, rt: runtime.clone(), core: core.clone(), }; diff --git a/kifu/gtk/src/view_models/game_view_model.rs b/kifu/gtk/src/view_models/game_view_model.rs index e8ac87a..8a10bcf 100644 --- a/kifu/gtk/src/view_models/game_view_model.rs +++ b/kifu/gtk/src/view_models/game_view_model.rs @@ -14,13 +14,12 @@ 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, yield_now, JoinHandle}; -use kifu_core::{Color, Core, CoreNotification, Goban, Player}; -use std::{ - sync::{Arc, RwLock}, - time::Duration, +use async_std::{ + channel::Receiver, + task::{spawn, yield_now, JoinHandle}, }; +use kifu_core::{Color, Core, CoreNotification, Goban, Observable, Player}; +use std::{cell::RefCell, rc::Rc, time::Duration}; pub struct GameState { goban: Goban, @@ -41,9 +40,8 @@ struct GameViewModelPrivate { /// The Game View Model manages the current state of the game. It shows the two player cards, the board, the current capture count, the current player, and it maintains the UI for the clock (bearing in mind that the real clock is managed in the core). This view model should only be created once the details of the game, whether a game in progress or a new game (this view model won't know the difference) is known. pub struct GameViewModel { core: Core, - notification_handler: JoinHandle<()>, widget: gtk::Box, - data: Arc>, + data: Rc>, } impl GameViewModelPrivate { @@ -52,34 +50,37 @@ impl GameViewModelPrivate { impl GameViewModel { pub fn new(white: Player, black: Player, game: GameState, core: Core) -> Self { - let data = Arc::new(RwLock::new(GameViewModelPrivate { + let data = Rc::new(RefCell::new(GameViewModelPrivate { white, black, state: game, })); - let handler = spawn({ - let core = core.clone(); - let data = data.clone(); - async move { - let notifications = core.subscribe(); - loop { - match notifications.recv().await { - Ok(msg) => data.write().unwrap().handle(msg), - Err(err) => { - unimplemented!("Should display an error message in the UI: {}", err) - } - } - yield_now().await; - } - } - }); - - Self { + let s = Self { core, - notification_handler: handler, widget: gtk::Box::new(gtk::Orientation::Horizontal, 0), data, + }; + + let notifications = s.core.subscribe(); + let data = s.data.clone(); + glib::spawn_future_local(Self::listen(notifications, data)); + + s + } + + async fn listen( + notifications: Receiver, + data: Rc>, + ) { + loop { + match notifications.recv().await { + Ok(msg) => data.borrow_mut().handle(msg), + Err(err) => { + unimplemented!("Should display an error message in the UI: {}", err) + } + } + yield_now().await; } } }