From a6722dbfc5c3a2d3f593e509acb04d6966757d78 Mon Sep 17 00:00:00 2001 From: Daniel Garcia Moreno Date: Tue, 14 May 2019 06:47:40 +0000 Subject: [PATCH] roomlist: flatten sibling_id function The sibling_id function has a complex if-else, to flatten that I've split the functionality in three different functions with one if-else in each one so the code is easier to read. --- fractal-gtk/src/widgets/roomlist.rs | 52 ++++++++++++++++++----------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/fractal-gtk/src/widgets/roomlist.rs b/fractal-gtk/src/widgets/roomlist.rs index c9132444..3f58349a 100644 --- a/fractal-gtk/src/widgets/roomlist.rs +++ b/fractal-gtk/src/widgets/roomlist.rs @@ -524,8 +524,8 @@ impl RoomList { //FIXME don't use to_string(), pass &str run_in_group!(self, &r.to_string(), set_selected, Some(r.to_string())); } - - fn sibling_id(&self, unread_only: bool, direction: i32) -> Option { + + fn sibling_id_inv(&self, unread_only: bool, direction: i32) -> Option { let (room, _, next) = self.inv.get().sibling_id(unread_only, direction); if let Some(room) = room { @@ -533,28 +533,40 @@ impl RoomList { } else if next { self.fav.get().first_id(unread_only) } else { - let (room, prev, next) = self.fav.get().sibling_id(unread_only, direction); - - if let Some(room) = room { - Some(room) - } else if prev { - self.inv.get().last_id(unread_only) - } else if next { - self.rooms.get().first_id(unread_only) - } else { - let (room, prev, _) = self.rooms.get().sibling_id(unread_only, direction); + self.sibling_id_fav(unread_only, direction) + } + } - if let Some(room) = room { - Some(room) - } else if prev { - self.fav.get().last_id(unread_only) - } else { - None - } - } + fn sibling_id_fav(&self, unread_only: bool, direction: i32) -> Option { + let (room, prev, next) = self.fav.get().sibling_id(unread_only, direction); + + if let Some(room) = room { + Some(room) + } else if prev { + self.inv.get().last_id(unread_only) + } else if next { + self.rooms.get().first_id(unread_only) + } else { + self.sibling_id_rooms(unread_only, direction) } } + fn sibling_id_rooms(&self, unread_only: bool, direction: i32) -> Option { + let (room, prev, _) = self.rooms.get().sibling_id(unread_only, direction); + + if let Some(room) = room { + Some(room) + } else if prev { + self.fav.get().last_id(unread_only) + } else { + None + } + } + + fn sibling_id(&self, unread_only: bool, direction: i32) -> Option { + self.sibling_id_inv(unread_only, direction) + } + pub fn next_id(&self) -> Option { self.sibling_id(false, 1) }