From 8488ce21bf920ec6b35ce66096c9ce4c56498a6f Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Wed, 6 Nov 2019 15:19:56 +0200 Subject: [PATCH] ui: move is_online() check to Context Context needs to know when an account gets online in order to get the mailbox hashes and launch the watcher threads for this account. Instead of assuming all accounts are online when launching meli, move the initialisation logic to an is_online() method on Context to do it on demand. The is_online() method is then called by ui::components::mail::Listing everytime it's drawn to check for status changes. --- ui/src/components/mail/listing.rs | 8 ++--- ui/src/state.rs | 60 ++++++++++++++++++------------- 2 files changed, 40 insertions(+), 28 deletions(-) diff --git a/ui/src/components/mail/listing.rs b/ui/src/components/mail/listing.rs index d68b87423..022872acb 100644 --- a/ui/src/components/mail/listing.rs +++ b/ui/src/components/mail/listing.rs @@ -184,8 +184,8 @@ impl fmt::Display for Listing { impl Component for Listing { fn draw(&mut self, grid: &mut CellBuffer, area: Area, context: &mut Context) { - for a in context.accounts.iter_mut() { - a.is_online(); + for i in 0..context.accounts.len() { + context.is_online(i); } if !self.is_dirty() { @@ -224,7 +224,7 @@ impl Component for Listing { self.dirty = false; if right_component_width == total_cols { - if !context.accounts[self.cursor_pos.0].is_online() { + if !context.is_online(self.cursor_pos.0) { clear_area(grid, area); write_string_to_grid( "offline", @@ -248,7 +248,7 @@ impl Component for Listing { self.draw_menu(grid, area, context); } else { self.draw_menu(grid, (upper_left, (mid, get_y(bottom_right))), context); - if !context.accounts[self.cursor_pos.0].is_online() { + if !context.is_online(self.cursor_pos.0) { clear_area(grid, (set_x(upper_left, mid + 1), bottom_right)); write_string_to_grid( "offline", diff --git a/ui/src/state.rs b/ui/src/state.rs index b50fad63a..136fdd59c 100644 --- a/ui/src/state.rs +++ b/ui/src/state.rs @@ -135,6 +135,40 @@ impl Context { } } + pub fn is_online(&mut self, account_pos: usize) -> bool { + let Context { + ref mut work_controller, + ref sender, + ref mut replies, + ref mut accounts, + ref mut mailbox_hashes, + .. + } = self; + if accounts[account_pos].is_online() { + for folder in accounts[account_pos] + .backend + .read() + .unwrap() + .folders() + .values() + { + debug!("hash & folder: {:?} {}", folder.hash(), folder.name()); + mailbox_hashes.insert(folder.hash(), account_pos); + } + /* Account::watch() needs + * - work_controller to pass `work_context` to the watcher threads and then add them + * to the controller's static thread list, + * - sender to pass a RefreshEventConsumer closure to watcher threads for them to + * inform the main binary that refresh events arrived + * - replies to report any failures to the user + */ + accounts[account_pos].watch((work_controller, sender, replies)); + true + } else { + false + } + } + pub fn work_controller(&self) -> &WorkController { &self.work_controller } @@ -246,30 +280,8 @@ impl State { s.switch_to_alternate_screen(); debug!("inserting mailbox hashes:"); - { - /* Account::watch() needs - * - work_controller to pass `work_context` to the watcher threads and then add them - * to the controller's static thread list, - * - sender to pass a RefreshEventConsumer closure to watcher threads for them to - * inform the main binary that refresh events arrived - * - replies to report any failures to the user - */ - let Context { - ref mut work_controller, - ref sender, - ref mut replies, - ref mut accounts, - ref mut mailbox_hashes, - .. - } = &mut s.context; - - for (x, account) in accounts.iter_mut().enumerate() { - for folder in account.backend.read().unwrap().folders().values() { - debug!("hash & folder: {:?} {}", folder.hash(), folder.name()); - mailbox_hashes.insert(folder.hash(), x); - } - account.watch((work_controller, sender, replies)); - } + for i in 0..s.context.accounts.len() { + s.context.is_online(i); } s.context.restore_input(); s