diff --git a/ui/src/components/mail/accounts.rs b/ui/src/components/mail/accounts.rs index c3684a4ba..42ee3f8d1 100644 --- a/ui/src/components/mail/accounts.rs +++ b/ui/src/components/mail/accounts.rs @@ -38,71 +38,7 @@ impl fmt::Display for AccountsPanel { impl Component for AccountsPanel { fn draw(&mut self, grid: &mut CellBuffer, area: Area, context: &mut Context) { if self.dirty { - write_string_to_grid( - "Accounts", - &mut self.content, - Color::Default, - Color::Default, - ((2, 3), (120 - 1, 3)), - true, - ); - - for (i, a) in context.accounts.iter().enumerate() { - create_box(&mut self.content, ((2, 5 + i * 10), (120 - 1, 15 + i * 10))); - let (x, y) = write_string_to_grid( - a.name(), - &mut self.content, - Color::Default, - Color::Default, - ((3, 5 + i * 10), (120 - 2, 5 + i * 10)), - true, - ); - write_string_to_grid( - " ▒██▒ ", - &mut self.content, - Color::Byte(32), - Color::Default, - ((x, y), (120 - 2, 5 + i * 10)), - true, - ); - write_string_to_grid( - &a.runtime_settings.account().identity, - &mut self.content, - Color::Default, - Color::Default, - ((4, y + 2), (120 - 2, y + 2)), - true, - ); - if i == self.cursor { - for h in 1..8 { - self.content[(2, h + y + 1)].set_ch('*'); - } - } - write_string_to_grid( - "- Settings", - &mut self.content, - Color::Default, - Color::Default, - ((5, y + 3), (120 - 2, y + 3)), - true, - ); - write_string_to_grid( - "- Contacts", - &mut self.content, - Color::Default, - Color::Default, - ((5, y + 4), (120 - 2, y + 4)), - true, - ); - write_string_to_grid( - "- Mailing Lists", - &mut self.content, - Color::Default, - Color::Default, - ((5, y + 5), (120 - 2, y + 5)), - true, - ); - } + self.initialize(context); self.dirty = false; } clear_area(grid, area); @@ -134,6 +70,9 @@ impl Component for AccountsPanel { }); return true; } + UIEventType::MailboxUpdate(_) => { + self.dirty = true; + } _ => {} } @@ -157,4 +96,94 @@ impl AccountsPanel { dirty: true, } } + fn initialize(&mut self, context: &Context) { + write_string_to_grid( + "Accounts", + &mut self.content, + Color::Default, + Color::Default, + ((2, 3), (120 - 1, 3)), + true, + ); + + for (i, a) in context.accounts.iter().enumerate() { + for x in 2..(120 - 1) { + set_and_join_box(&mut self.content, (x, 5 + i * 10), HORZ_BOUNDARY); + } + //create_box(&mut self.content, ((2, 5 + i * 10), (120 - 1, 15 + i * 10))); + let (x, y) = write_string_to_grid( + a.name(), + &mut self.content, + Color::Default, + Color::Default, + ((3, 5 + i * 10), (120 - 2, 5 + i * 10)), + true, + ); + write_string_to_grid( + " ▒██▒ ", + &mut self.content, + Color::Byte(32), + Color::Default, + ((x, y), (120 - 2, 5 + i * 10)), + true, + ); + write_string_to_grid( + &a.runtime_settings.account().identity, + &mut self.content, + Color::Default, + Color::Default, + ((4, y + 2), (120 - 2, y + 2)), + true, + ); + if i == self.cursor { + for h in 1..8 { + self.content[(2, h + y + 1)].set_ch('*'); + } + } else { + for h in 1..8 { + self.content[(2, h + y + 1)].set_ch(' '); + } + } + let (x, _) = write_string_to_grid( + "- Settings", + &mut self.content, + Color::Default, + Color::Default, + ((5, y + 3), (120 - 2, y + 3)), + true, + ); + write_string_to_grid( + &format!( + "total {}", + a.iter_mailboxes().fold(0, |mut acc, m| { + acc += m + .map(|m| m.collection.values().filter(|e| !e.is_seen()).count()) + .unwrap_or(0); + acc + }) + ), + &mut self.content, + Color::Default, + Color::Default, + ((10 + x, y + 3), (120 - 2, y + 3)), + true, + ); + write_string_to_grid( + "- Contacts", + &mut self.content, + Color::Default, + Color::Default, + ((5, y + 4), (120 - 2, y + 4)), + true, + ); + write_string_to_grid( + "- Mailing Lists", + &mut self.content, + Color::Default, + Color::Default, + ((5, y + 5), (120 - 2, y + 5)), + true, + ); + } + } } diff --git a/ui/src/conf/accounts.rs b/ui/src/conf/accounts.rs index 562092993..05bf89976 100644 --- a/ui/src/conf/accounts.rs +++ b/ui/src/conf/accounts.rs @@ -82,6 +82,44 @@ impl Drop for Account { } } +pub struct MailboxIterator<'a> { + folders: &'a [Option>], + pos: usize, +} + +impl<'a> Iterator for MailboxIterator<'a> { + type Item = Option<&'a Mailbox>; + + fn next(&mut self) -> Option> { + eprintln!( + "self.pos = {}, iter.len {}", + self.pos, + self.folders[self.pos..] + .iter() + .collect::>>>() + .len() + ); + if self.pos == self.folders.len() { + return None; + } + for f in self.folders[self.pos..].iter() { + if self.pos == self.folders.len() { + return None; + } + + self.pos += 1; + if let Some(Err(_)) = f { + return Some(None); + } + if let None = f { + return Some(None); + } + return Some(Some(f.as_ref().unwrap().as_ref().unwrap())); + } + return None; + } +} + impl Account { pub fn new(name: String, settings: AccountConf, map: &Backends, notify_fn: NotifyFn) -> Self { let mut backend = map.get(settings.account().format())(settings.account()); @@ -313,6 +351,12 @@ impl Account { self.backend .save(&finalize.as_bytes(), &self.settings.conf.draft_folder) } + pub fn iter_mailboxes<'a>(&'a self) -> MailboxIterator<'a> { + MailboxIterator { + folders: &self.folders, + pos: 0, + } + } } impl Index for Account {