[fractal/fractal-next] session: Move RoomList as a child of Session



commit d547b9adfcc72f8ddda39e6e99f273cfae76c266
Author: Kévin Commaille <zecakeh tedomum fr>
Date:   Wed May 26 14:19:35 2021 +0200

    session: Move RoomList as a child of Session

 src/session/categories/categories.rs      | 48 ++++++++++++++++---------------
 src/session/categories/category.rs        |  5 +---
 src/session/categories/mod.rs             |  2 --
 src/session/mod.rs                        | 34 +++++++---------------
 src/session/{categories => }/room_list.rs | 10 +++++++
 5 files changed, 47 insertions(+), 52 deletions(-)
---
diff --git a/src/session/categories/categories.rs b/src/session/categories/categories.rs
index 6ef7480f..a77a2c8c 100644
--- a/src/session/categories/categories.rs
+++ b/src/session/categories/categories.rs
@@ -1,14 +1,18 @@
 use gtk::{gio, glib, prelude::*, subclass::prelude::*};
 
-use crate::session::categories::{Category, CategoryType, RoomList};
+use crate::session::{
+    categories::{Category, CategoryType},
+    room_list::RoomList,
+};
 
 mod imp {
+    use once_cell::unsync::OnceCell;
+
     use super::*;
 
-    #[derive(Debug)]
+    #[derive(Debug, Default)]
     pub struct Categories {
-        pub list: [Category; 5],
-        pub room_list: RoomList,
+        pub list: OnceCell<[Category; 5]>,
     }
 
     #[glib::object_subclass]
@@ -17,21 +21,6 @@ mod imp {
         type Type = super::Categories;
         type ParentType = glib::Object;
         type Interfaces = (gio::ListModel,);
-
-        fn new() -> Self {
-            let room_list = RoomList::new();
-
-            Self {
-                list: [
-                    Category::new(CategoryType::Invited, &room_list),
-                    Category::new(CategoryType::Favorite, &room_list),
-                    Category::new(CategoryType::Normal, &room_list),
-                    Category::new(CategoryType::LowPriority, &room_list),
-                    Category::new(CategoryType::Left, &room_list),
-                ],
-                room_list,
-            }
-        }
     }
 
     impl ObjectImpl for Categories {}
@@ -41,11 +30,12 @@ mod imp {
             Category::static_type()
         }
         fn n_items(&self, _list_model: &Self::Type) -> u32 {
-            self.list.len() as u32
+            self.list.get().map(|l| l.len()).unwrap_or(0) as u32
         }
         fn item(&self, _list_model: &Self::Type, position: u32) -> Option<glib::Object> {
             self.list
-                .get(position as usize)
+                .get()
+                .and_then(|l| l.get(position as usize))
                 .map(glib::object::Cast::upcast_ref::<glib::Object>)
                 .cloned()
         }
@@ -68,8 +58,20 @@ impl Categories {
         glib::Object::new(&[]).expect("Failed to create Categories")
     }
 
-    pub fn room_list(&self) -> &RoomList {
+    pub fn set_room_list(&self, room_list: &RoomList) {
         let priv_ = imp::Categories::from_instance(&self);
-        &priv_.room_list
+
+        priv_
+            .list
+            .set([
+                Category::new(CategoryType::Invited, room_list),
+                Category::new(CategoryType::Favorite, room_list),
+                Category::new(CategoryType::Normal, room_list),
+                Category::new(CategoryType::LowPriority, room_list),
+                Category::new(CategoryType::Left, room_list),
+            ])
+            .unwrap();
+
+        self.items_changed(0, 0, 5);
     }
 }
diff --git a/src/session/categories/category.rs b/src/session/categories/category.rs
index 369d4c10..738e60d1 100644
--- a/src/session/categories/category.rs
+++ b/src/session/categories/category.rs
@@ -1,9 +1,6 @@
 use gtk::{gio, glib, glib::clone, prelude::*, subclass::prelude::*};
 
-use crate::session::{
-    categories::{CategoryType, RoomList},
-    room::Room,
-};
+use crate::session::{categories::CategoryType, room::Room, room_list::RoomList};
 
 mod imp {
     use once_cell::unsync::OnceCell;
diff --git a/src/session/categories/mod.rs b/src/session/categories/mod.rs
index f58f4f0a..9b644b47 100644
--- a/src/session/categories/mod.rs
+++ b/src/session/categories/mod.rs
@@ -1,9 +1,7 @@
 mod categories;
 mod category;
 mod category_type;
-mod room_list;
 
 pub use self::categories::Categories;
 pub use self::category::Category;
 pub use self::category_type::CategoryType;
-pub use self::room_list::RoomList;
diff --git a/src/session/mod.rs b/src/session/mod.rs
index af99f7ab..21b3e2b4 100644
--- a/src/session/mod.rs
+++ b/src/session/mod.rs
@@ -1,12 +1,14 @@
 mod categories;
 mod content;
 mod room;
+mod room_list;
 mod sidebar;
 mod user;
 
 use self::categories::Categories;
 use self::content::Content;
 use self::room::Room;
+use self::room_list::RoomList;
 use self::sidebar::Sidebar;
 pub use self::user::User;
 
@@ -48,6 +50,7 @@ mod imp {
         /// Contains the error if something went wrong
         pub error: RefCell<Option<matrix_sdk::Error>>,
         pub client: OnceCell<Client>,
+        pub room_list: RoomList,
         pub categories: Categories,
         pub user: OnceCell<User>,
         pub selected_room: RefCell<Option<Room>>,
@@ -128,6 +131,8 @@ mod imp {
 
         fn constructed(&self, obj: &Self::Type) {
             self.parent_constructed(obj);
+
+            self.categories.set_room_list(&self.room_list);
         }
     }
     impl WidgetImpl for Session {}
@@ -254,17 +259,19 @@ impl Session {
         match result {
             Ok((client, session)) => {
                 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();
                 }
-                self.load();
+
+                priv_.room_list.set_client(client).unwrap();
+                priv_.room_list.set_user(user).unwrap();
+                priv_.room_list.load();
+
                 self.sync();
             }
             Err(error) => {
@@ -324,13 +331,6 @@ impl Session {
         priv_.user.set(user).unwrap();
     }
 
-    // FIXME: Leaving this method for now, if it's never used it should be removed at some point.
-    #[allow(dead_code)]
-    fn user(&self) -> &User {
-        let priv_ = &imp::Session::from_instance(self);
-        priv_.user.get().unwrap()
-    }
-
     /// Sets up the required channel to receive new room events
     fn create_new_sync_response_sender(&self) -> SyncSender<SyncResponse> {
         let (sender, receiver) =
@@ -350,15 +350,6 @@ impl Session {
         sender
     }
 
-    /// Loads the state from the `Store`
-    /// Note that the `Store` currently doesn't store all events, therefore, we arn't really
-    /// loading much via this function.
-    pub fn load(&self) {
-        let priv_ = imp::Session::from_instance(self);
-
-        priv_.categories.room_list().load();
-    }
-
     /// Returns and consumes the `error` that was generated when the session failed to login,
     /// on a successful login this will be `None`.
     /// Unfortunatly it's not possible to connect the Error direclty to the `prepared` signals.
@@ -381,9 +372,6 @@ impl Session {
     fn handle_sync_response(&self, response: SyncResponse) {
         let priv_ = imp::Session::from_instance(self);
 
-        priv_
-            .categories
-            .room_list()
-            .handle_response_rooms(response.rooms);
+        priv_.room_list.handle_response_rooms(response.rooms);
     }
 }
diff --git a/src/session/categories/room_list.rs b/src/session/room_list.rs
similarity index 95%
rename from src/session/categories/room_list.rs
rename to src/session/room_list.rs
index fc9e4394..65430b1b 100644
--- a/src/session/categories/room_list.rs
+++ b/src/session/room_list.rs
@@ -50,6 +50,12 @@ 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")
@@ -119,6 +125,10 @@ impl RoomList {
         self.items_changed(position as u32, 0, added as u32);
     }
 
+    /// Loads the state from the `Store`.
+    ///
+    /// Note that the `Store` currently doesn't store all events, therefore, we aren't really
+    /// loading much via this function.
     pub fn load(&self) {
         let priv_ = imp::RoomList::from_instance(&self);
 


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