BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-state / src / call / calendar_settings.rs

calendar_settings.rs

calendar_settings.rs 7.8 KiB Download Raw
1//! Calendar view settings on `AppState`: local preferences plus the mailbox work hours.
2
3use app_base::ui_locale::tr;
4use gpui_kit::Context;
5use graph::MailboxWorkingHours;
6
7use super::calendar_prefs::{CalendarPrefs, WorkHours, save_calendar_prefs};
8use crate::app_state::AppState;
9
10impl AppState {
11 /// Change local calendar preferences, save them and redraw the visible range.
12 pub fn update_calendar_prefs(
13 &mut self,
14 change: impl FnOnce(&mut CalendarPrefs),
15 cx: &mut Context<Self>,
16 ) {
17 let anchor = self.calendar_week.start;
18 change(&mut self.calendar_prefs);
19 save_calendar_prefs(&self.store, &self.calendar_prefs);
20 if let Some(anchor) = anchor {
21 // Week start or work days may have moved: re-anchor on the same date.
22 self.calendar_week.start = Some(self.calendar_span_start(anchor));
23 self.request_calendar_scroll_to_now(cx);
24 self.load_calendar_week(cx);
25 }
26 cx.notify();
27 }
28
29 /// Read the mailbox work hours once the session is there and cache them.
30 pub(crate) fn load_work_hours(&mut self, cx: &mut Context<Self>) {
31 if self.mode.demo {
32 return;
33 }
34 let Some(engine) = self.engine.clone() else {
35 return;
36 };
37 let receiver = crate::runtime::spawn(async move { engine.remote().working_hours().await });
38 cx.spawn(async move |this, cx| {
39 let Ok(result) = receiver.await else {
40 return;
41 };
42 this.update(cx, |state, cx| match result {
43 Ok(mailbox) => {
44 state.mailbox_time_zone = mailbox.time_zone.clone();
45 let hours = work_hours_from(&mailbox);
46 let locale = mailbox.locale.clone();
47 let prefs = &state.calendar_prefs;
48 if hours != prefs.work_hours || locale != prefs.mailbox_locale {
49 state.update_calendar_prefs(
50 |prefs| {
51 prefs.work_hours = hours;
52 prefs.mailbox_locale = locale;
53 },
54 cx,
55 );
56 }
57 }
58 Err(error) => crate::diagnostics::record(format!(
59 "calendar: mailbox work hours not read: {error}"
60 )),
61 })
62 .ok();
63 })
64 .detach();
65 }
66
67 /// Save work days and hours to the mailbox; on failure the previous value returns.
68 pub fn set_work_hours(&mut self, hours: WorkHours, cx: &mut Context<Self>) {
69 let previous = self.calendar_prefs.work_hours.clone();
70 if previous == hours {
71 return;
72 }
73 let wanted = hours.clone();
74 self.update_calendar_prefs(|prefs| prefs.work_hours = wanted, cx);
75 if self.mode.demo {
76 return;
77 }
78 let Some(engine) = self.engine.clone() else {
79 self.revert_work_hours(previous, "no session".to_owned(), cx);
80 return;
81 };
82 let mailbox = MailboxWorkingHours {
83 days: hours.days,
84 start: hours.start,
85 end: hours.end,
86 time_zone: self.mailbox_time_zone.clone(),
87 locale: None,
88 };
89 let receiver =
90 crate::runtime::spawn(async move { engine.remote().set_working_hours(&mailbox).await });
91 cx.spawn(async move |this, cx| {
92 let result = receiver
93 .await
94 .unwrap_or_else(|_| Err(graph::Error::Decode("mailbox task cancelled".into())));
95 this.update(cx, |state, cx| {
96 if let Err(error) = result {
97 state.revert_work_hours(previous, error.to_string(), cx);
98 }
99 })
100 .ok();
101 })
102 .detach();
103 }
104
105 fn revert_work_hours(&mut self, previous: WorkHours, error: String, cx: &mut Context<Self>) {
106 crate::diagnostics::record(format!("calendar: mailbox work hours not saved: {error}"));
107 self.update_calendar_prefs(|prefs| prefs.work_hours = previous, cx);
108 self.raise_notice(tr("settings.calendar.work_hours_not_saved"), None, cx);
109 }
110}
111
112fn work_hours_from(mailbox: &MailboxWorkingHours) -> WorkHours {
113 WorkHours {
114 days: mailbox.days.clone(),
115 start: mailbox.start,
116 end: mailbox.end,
117 }
118}
119
120#[cfg(test)]
121mod tests {
122 use std::sync::Arc;
123
124 use chrono::Weekday;
125 use gpui_kit::{AppContext as _, TestAppContext};
126 use store::Store;
127
128 use crate::app_state::{AppState, Mode};
129 use crate::call::calendar_prefs::{DefaultView, WorkHours, load_calendar_prefs};
130 use crate::call::week_grid::CalendarSpan;
131
132 #[gpui_kit::test]
133 fn the_week_start_follows_the_mailbox_language(cx: &mut TestAppContext) {
134 cx.update(gpui_kit::init);
135 let store = Arc::new(Store::open_in_memory().unwrap());
136 let app = cx.update(|cx| cx.new(|_| AppState::new(store, Mode::default())));
137 cx.update(|cx| {
138 app.update(cx, |state, cx| {
139 state
140 .update_calendar_prefs(|prefs| prefs.mailbox_locale = Some("en-US".into()), cx);
141 assert_eq!(state.calendar_layout().first_day, Weekday::Sun);
142 state
143 .update_calendar_prefs(|prefs| prefs.mailbox_locale = Some("en-CH".into()), cx);
144 assert_eq!(state.calendar_layout().first_day, Weekday::Mon);
145 state.update_calendar_prefs(|prefs| prefs.first_day = Some(Weekday::Sat), cx);
146 assert_eq!(state.calendar_layout().first_day, Weekday::Sat);
147 })
148 });
149 }
150
151 #[gpui_kit::test]
152 fn the_chosen_span_survives_a_restart(cx: &mut TestAppContext) {
153 cx.update(gpui_kit::init);
154 let store = Arc::new(Store::open_in_memory().unwrap());
155 let first = cx.update(|cx| cx.new(|_| AppState::new(store.clone(), Mode::default())));
156 cx.update(|cx| {
157 first.update(cx, |state, cx| {
158 assert_eq!(state.calendar_week.span, CalendarSpan::WorkWeek);
159 state.set_calendar_span(CalendarSpan::Month, cx);
160 // A day opened from a month cell is temporary navigation.
161 let today = chrono::Local::now().date_naive();
162 state.show_calendar_day(today, cx);
163 })
164 });
165 let second = cx.update(|cx| cx.new(|_| AppState::new(store.clone(), Mode::default())));
166 cx.update(|cx| {
167 second.update(cx, |state, cx| {
168 assert_eq!(state.calendar_week.span, CalendarSpan::Month);
169 state.update_calendar_prefs(|prefs| prefs.default_view = DefaultView::Week, cx);
170 state.set_calendar_span(CalendarSpan::Day, cx);
171 })
172 });
173 let third = cx.update(|cx| cx.new(|_| AppState::new(store, Mode::default())));
174 cx.update(|cx| {
175 third.update(cx, |state, _| {
176 assert_eq!(state.calendar_week.span, CalendarSpan::Week)
177 })
178 });
179 }
180
181 #[gpui_kit::test]
182 fn work_hours_fall_back_when_the_mailbox_cannot_be_written(cx: &mut TestAppContext) {
183 cx.update(gpui_kit::init);
184 let store = Arc::new(Store::open_in_memory().unwrap());
185 let app = cx.update(|cx| cx.new(|_| AppState::new(store.clone(), Mode::default())));
186 cx.update(|cx| {
187 app.update(cx, |state, cx| {
188 let mut hours = WorkHours::default();
189 hours.days.retain(|day| *day != Weekday::Fri);
190 state.set_work_hours(hours, cx);
191 assert_eq!(state.calendar_prefs.work_hours, WorkHours::default());
192 assert!(state.notice.is_some());
193 })
194 });
195 assert_eq!(load_calendar_prefs(&store).work_hours, WorkHours::default());
196 }
197}