BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / last_views.rs

last_views.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 2 weeks ago
a6592765 History
last_views.rs 2.8 KiB Download Raw
1use super::sidebar::SidebarTab;
2use crate::app_state::Selection;
3
4#[derive(Debug, Default, Clone, PartialEq, Eq)]
5pub struct LastViews {
6 chat: Option<String>,
7 channel: Option<String>,
8}
9
10impl LastViews {
11 pub fn remember(&mut self, selection: &Selection) {
12 match selection {
13 Selection::Chat(chat_id) => self.chat = Some(chat_id.clone()),
14 Selection::Channel(channel_id) => self.channel = Some(channel_id.clone()),
15 }
16 }
17
18 pub fn restore(
19 &self,
20 tab: SidebarTab,
21 current: Option<&Selection>,
22 first_chat_id: Option<&str>,
23 first_channel_id: Option<&str>,
24 ) -> Option<Selection> {
25 match (tab, current) {
26 (SidebarTab::Chats, Some(Selection::Chat(_))) => None,
27 (SidebarTab::Channels, Some(Selection::Channel(_))) => None,
28 (SidebarTab::Chats, _) => self
29 .chat
30 .clone()
31 .or_else(|| first_chat_id.map(str::to_owned))
32 .map(Selection::Chat),
33 (SidebarTab::Channels, _) => self
34 .channel
35 .clone()
36 .or_else(|| first_channel_id.map(str::to_owned))
37 .map(Selection::Channel),
38 _ => None,
39 }
40 }
41}
42
43#[cfg(test)]
44mod tests {
45 use super::*;
46
47 #[test]
48 fn each_app_gets_back_its_own_conversation() {
49 let mut last = LastViews::default();
50 let chat = Selection::Chat("19:chat".to_owned());
51 let channel = Selection::Channel("19:channel".to_owned());
52 last.remember(&chat);
53 last.remember(&channel);
54 assert_eq!(
55 last.restore(SidebarTab::Chats, Some(&channel), Some("19:first"), None),
56 Some(chat.clone())
57 );
58 assert_eq!(
59 last.restore(SidebarTab::Channels, Some(&chat), None, Some("19:general")),
60 Some(channel.clone())
61 );
62 assert_eq!(
63 last.restore(SidebarTab::Chats, Some(&chat), None, None),
64 None
65 );
66 assert_eq!(
67 last.restore(SidebarTab::Calendar, Some(&channel), None, None),
68 None
69 );
70 }
71
72 #[test]
73 fn each_app_falls_back_to_its_first_conversation() {
74 let last = LastViews::default();
75 let channel = Selection::Channel("19:channel".to_owned());
76 assert_eq!(
77 last.restore(SidebarTab::Chats, Some(&channel), Some("19:first"), None),
78 Some(Selection::Chat("19:first".to_owned()))
79 );
80 let chat = Selection::Chat("19:chat".to_owned());
81 assert_eq!(
82 last.restore(SidebarTab::Channels, Some(&chat), None, Some("19:general")),
83 Some(Selection::Channel("19:general".to_owned()))
84 );
85 assert_eq!(
86 last.restore(SidebarTab::Channels, Some(&chat), None, None),
87 None
88 );
89 }
90}