[fractal/fractal-next] session: Provide Client and User to RoomList



commit cc3b0e5b2ea9e40b667ffc1cc647017c3c005252
Author: Kévin Commaille <zecakeh tedomum fr>
Date:   Wed May 26 08:02:59 2021 +0200

    session: Provide Client and User to RoomList

 src/session/categories/room_list.rs | 34 ++++++++++++++++++----------------
 src/session/mod.rs                  |  9 +++++++--
 2 files changed, 25 insertions(+), 18 deletions(-)
---
diff --git a/src/session/categories/room_list.rs b/src/session/categories/room_list.rs
index d2848c11..3cc09788 100644
--- a/src/session/categories/room_list.rs
+++ b/src/session/categories/room_list.rs
@@ -1,16 +1,20 @@
 use gtk::{gio, glib, glib::clone, prelude::*, subclass::prelude::*};
 use indexmap::map::IndexMap;
-use matrix_sdk::identifiers::RoomId;
+use matrix_sdk::{identifiers::RoomId, Client};
 
-use crate::session::room::Room;
+use crate::session::{room::Room, user::User};
 
 mod imp {
-    use super::*;
+    use once_cell::unsync::OnceCell;
     use std::cell::RefCell;
 
-    #[derive(Debug)]
+    use super::*;
+
+    #[derive(Debug, Default)]
     pub struct RoomList {
         pub list: RefCell<IndexMap<RoomId, Room>>,
+        pub client: OnceCell<Client>,
+        pub user: OnceCell<User>,
     }
 
     #[glib::object_subclass]
@@ -19,12 +23,6 @@ mod imp {
         type Type = super::RoomList;
         type ParentType = glib::Object;
         type Interfaces = (gio::ListModel,);
-
-        fn new() -> Self {
-            Self {
-                list: Default::default(),
-            }
-        }
     }
 
     impl ObjectImpl for RoomList {}
@@ -52,17 +50,21 @@ glib::wrapper! {
         @implements gio::ListModel;
 }
 
-impl Default for RoomList {
-    fn default() -> Self {
-        Self::new()
-    }
-}
-
 impl RoomList {
     pub fn new() -> Self {
         glib::Object::new(&[]).expect("Failed to create RoomList")
     }
 
+    pub fn set_client(&self, client: Client) -> Result<(), Client> {
+        let priv_ = imp::RoomList::from_instance(&self);
+        priv_.client.set(client)
+    }
+
+    pub fn set_user(&self, user: User) -> Result<(), User> {
+        let priv_ = imp::RoomList::from_instance(&self);
+        priv_.user.set(user)
+    }
+
     pub fn get(&self, room_id: &RoomId) -> Option<Room> {
         let priv_ = imp::RoomList::from_instance(&self);
         priv_.list.borrow().get(room_id).cloned()
diff --git a/src/session/mod.rs b/src/session/mod.rs
index 6cef602e..4e48e929 100644
--- a/src/session/mod.rs
+++ b/src/session/mod.rs
@@ -258,8 +258,13 @@ impl Session {
         let priv_ = imp::Session::from_instance(self);
         match result {
             Ok((client, session)) => {
-                priv_.client.set(client).unwrap();
-                self.set_user(User::new(&session.user_id));
+                priv_.client.set(client.clone()).unwrap();
+                priv_.categories.room_list().set_client(client).unwrap();
+
+                let user = User::new(&session.user_id);
+                self.set_user(user.clone());
+                priv_.categories.room_list().set_user(user).unwrap();
+
                 if store_session {
                     // TODO: report secret service errors
                     secret::store_session(session).unwrap();


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]