[fractal] refactor: simplify how rooms are added to the roomlist
- From: Alexandre Franke <afranke src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [fractal] refactor: simplify how rooms are added to the roomlist
- Date: Wed, 2 Jan 2019 22:44:22 +0000 (UTC)
commit 0ea988a0f34bf0d4d00b7d65b1888d6cddc76be3
Author: Julian Sparber <julian sparber net>
Date: Sat Dec 22 18:12:47 2018 +0100
refactor: simplify how rooms are added to the roomlist
This removes many clones and loops which where not needed.
fractal-gtk/src/app/backend_loop.rs | 8 ++-
fractal-gtk/src/appop/login.rs | 2 +-
fractal-gtk/src/appop/mod.rs | 2 +-
fractal-gtk/src/appop/room.rs | 128 +++++++++++++++---------------------
4 files changed, 59 insertions(+), 81 deletions(-)
---
diff --git a/fractal-gtk/src/app/backend_loop.rs b/fractal-gtk/src/app/backend_loop.rs
index 726f6907..6a12f91a 100644
--- a/fractal-gtk/src/app/backend_loop.rs
+++ b/fractal-gtk/src/app/backend_loop.rs
@@ -103,11 +103,13 @@ pub fn backend_loop(rx: Receiver<BKResponse>) {
let s = Some(since);
APPOP!(synced, (s));
}
- Ok(BKResponse::Rooms(rooms, default)) => {
- APPOP!(update_rooms, (rooms, default));
+ Ok(BKResponse::Rooms(rooms, _default)) => {
+ let clear_room_list = true;
+ APPOP!(set_rooms, (rooms, clear_room_list));
}
Ok(BKResponse::NewRooms(rooms)) => {
- APPOP!(new_rooms, (rooms));
+ let clear_room_list = false;
+ APPOP!(set_rooms, (rooms, clear_room_list));
}
Ok(BKResponse::RoomDetail(room, key, value)) => {
let v = Some(value);
diff --git a/fractal-gtk/src/appop/login.rs b/fractal-gtk/src/appop/login.rs
index 1e55ec7f..e832daf1 100644
--- a/fractal-gtk/src/appop/login.rs
+++ b/fractal-gtk/src/appop/login.rs
@@ -47,7 +47,7 @@ impl AppOp {
}
pub fn bk_logout(&mut self) {
- self.set_rooms(vec![], None);
+ self.set_rooms(vec![], true);
if let Err(_) = cache::destroy() {
error!("Error removing cache file");
}
diff --git a/fractal-gtk/src/appop/mod.rs b/fractal-gtk/src/appop/mod.rs
index 34ca1986..0fa265a6 100644
--- a/fractal-gtk/src/appop/mod.rs
+++ b/fractal-gtk/src/appop/mod.rs
@@ -117,7 +117,7 @@ impl AppOp {
if let Ok(data) = cache::load() {
let r: Vec<Room> = data.rooms.values().cloned().collect();
- self.set_rooms(r, None);
+ self.set_rooms(r, true);
/* Make sure that since is never an empty string */
self.since = data.since.filter(|s| !s.is_empty());
self.username = Some(data.username);
diff --git a/fractal-gtk/src/appop/room.rs b/fractal-gtk/src/appop/room.rs
index 529c96cc..97230db7 100644
--- a/fractal-gtk/src/appop/room.rs
+++ b/fractal-gtk/src/appop/room.rs
@@ -29,96 +29,72 @@ pub enum RoomPanel {
}
impl AppOp {
- pub fn update_rooms(&mut self, rooms: Vec<Room>, default: Option<Room>) {
- let rs: Vec<Room> = rooms.iter().filter(|x| !x.left).cloned().collect();
-
- // uploading each room avatar
- for r in rooms.iter() {
- self.backend
- .send(BKCommand::GetRoomAvatar(r.id.clone()))
- .unwrap();
- }
- self.set_rooms(rs, default);
- }
-
- pub fn new_rooms(&mut self, rooms: Vec<Room>) {
- // ignoring existing rooms
- let rs: Vec<&Room> = rooms
- .iter()
- .filter(|x| !self.rooms.contains_key(&x.id) && !x.left)
- .collect();
-
- for r in rs {
- self.rooms.insert(r.id.clone(), r.clone());
- self.roomlist.add_room(r.clone());
- self.roomlist.moveup(r.id.clone());
- }
-
- // removing left rooms
- let rs: Vec<&Room> = rooms.iter().filter(|x| x.left).collect();
- for r in rs {
- if r.id == self.active_room.clone().unwrap_or_default() {
- self.really_leave_active_room();
- } else {
- self.remove_room(r.id.clone());
- }
- }
- }
-
pub fn remove_room(&mut self, id: String) {
self.rooms.remove(&id);
- self.roomlist.remove_room(id.clone());
self.unsent_messages.remove(&id);
+ self.roomlist.remove_room(id);
}
- pub fn set_rooms(&mut self, rooms: Vec<Room>, def: Option<Room>) {
- let container: gtk::Box = self
- .ui
- .builder
- .get_object("room_container")
- .expect("Couldn't find room_container in ui file.");
-
- self.rooms.clear();
- for ch in container.get_children().iter() {
- container.remove(ch);
+ pub fn set_rooms(&mut self, mut rooms: Vec<Room>, clear_room_list: bool) {
+ if clear_room_list {
+ self.rooms.clear();
}
-
- for r in rooms.iter() {
- if let None = r.name {
- // This will force the room name calculation for 1:1 rooms and other rooms with no
- // name
+ let mut roomlist = vec![];
+ while let Some(room) = rooms.pop() {
+ if room.left {
+ // removing left rooms
+ if self.active_room.as_ref().map_or(false, |x| x == &room.id) {
+ self.really_leave_active_room();
+ } else {
+ self.remove_room(room.id);
+ }
+ } else if self.rooms.contains_key(&room.id) {
+ // TODO: update the existing rooms
+ } else {
+ if room.name.is_none() {
+ // This force the room name calculation for 1:1 rooms and for rooms with no name
+ self.backend
+ .send(BKCommand::GetRoomMembers(room.id.clone()))
+ .unwrap();
+ }
+ // Download the room avatar
self.backend
- .send(BKCommand::GetRoomMembers(r.id.clone()))
+ .send(BKCommand::GetRoomAvatar(room.id.clone()))
.unwrap();
+ if clear_room_list {
+ roomlist.push(room.clone());
+ } else {
+ self.roomlist.add_room(room.clone());
+ self.roomlist.moveup(room.id.clone());
+ }
+ self.rooms.insert(room.id.clone(), room);
}
-
- self.rooms.insert(r.id.clone(), r.clone());
}
- self.roomlist = widgets::RoomList::new(Some(self.server_url.clone()));
- self.roomlist.add_rooms(rooms.iter().cloned().collect());
- container.add(self.roomlist.widget());
-
- let bk = self.backend.clone();
- self.roomlist.connect_fav(move |room, tofav| {
- bk.send(BKCommand::AddToFav(room.id.clone(), tofav))
- .unwrap();
- });
+ if clear_room_list {
+ let container: gtk::Box = self
+ .ui
+ .builder
+ .get_object("room_container")
+ .expect("Couldn't find room_container in ui file.");
- let mut godef = def;
- if let Some(aroom) = self.active_room.clone() {
- if let Some(r) = self.rooms.get(&aroom) {
- godef = Some(r.clone());
+ for ch in container.get_children().iter() {
+ container.remove(ch);
}
- }
- if let Some(d) = godef {
- self.set_active_room_by_id(d.id.clone());
- } else {
- self.set_state(AppState::NoRoom);
- self.room_panel(RoomPanel::NoRoom);
- self.active_room = None;
- self.clear_tmp_msgs();
+ self.roomlist = widgets::RoomList::new(Some(self.server_url.clone()));
+ self.roomlist.add_rooms(roomlist);
+ container.add(self.roomlist.widget());
+
+ let bk = self.backend.clone();
+ self.roomlist.connect_fav(move |room, tofav| {
+ bk.send(BKCommand::AddToFav(room.id.clone(), tofav))
+ .unwrap();
+ });
+ // Select active room in the sidebar
+ if let Some(ref active_room) = self.active_room {
+ self.roomlist.select(active_room);
+ }
}
self.cache_rooms();
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]