BlueGit

ict rusty-teams

Files

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

calendar_settings.rs

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