BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-call-model / src / week_grid.rs

week_grid.rs

week_grid.rs 7.9 KiB Download Raw
1use chatsvc::{CalendarEvent, ResponseType};
2use chrono::{DateTime, Datelike, Duration, FixedOffset, NaiveDate, Utc, Weekday};
3
4pub const WORK_WEEK_DAYS: i64 = 5;
5pub const MINUTES_PER_DAY: i64 = 24 * 60;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub struct WeekBlock {
9 pub event_index: usize,
10 pub start_minute: i64,
11 pub end_minute: i64,
12 pub lane: usize,
13 pub lanes: usize,
14}
15
16#[derive(Debug, Clone, PartialEq, Eq)]
17pub struct WeekDay {
18 pub date: NaiveDate,
19 pub all_day: Vec<usize>,
20 pub blocks: Vec<WeekBlock>,
21}
22
23pub fn week_start(date: NaiveDate) -> NaiveDate {
24 date - Duration::days(i64::from(date.weekday().num_days_from_monday()))
25}
26
27/// On a weekend the work week view shows the coming week, as Outlook does.
28pub fn work_week_for(today: NaiveDate) -> NaiveDate {
29 match today.weekday() {
30 Weekday::Sat | Weekday::Sun => week_start(today) + Duration::weeks(1),
31 _ => week_start(today),
32 }
33}
34
35pub fn local_midnight(date: NaiveDate, offset: FixedOffset) -> DateTime<Utc> {
36 date.and_hms_opt(0, 0, 0)
37 .and_then(|local| local.and_local_timezone(offset).single())
38 .map(|local| local.with_timezone(&Utc))
39 .unwrap_or_else(|| date.and_hms_opt(0, 0, 0).unwrap_or_default().and_utc())
40}
41
42pub fn week_range(
43 start: NaiveDate,
44 days: i64,
45 offset: FixedOffset,
46) -> (DateTime<Utc>, DateTime<Utc>) {
47 (
48 local_midnight(start, offset),
49 local_midnight(start + Duration::days(days), offset),
50 )
51}
52
53pub fn shown_in_calendar(event: &CalendarEvent) -> bool {
54 event.response != ResponseType::Declined
55}
56
57pub fn week_days(
58 events: &[CalendarEvent],
59 start: NaiveDate,
60 days: i64,
61 offset: FixedOffset,
62) -> Vec<WeekDay> {
63 (0..days)
64 .map(|day| {
65 let date = start + Duration::days(day);
66 let (day_start, day_end) = week_range(date, 1, offset);
67 let mut all_day = Vec::new();
68 let mut spans = Vec::new();
69 for (index, event) in events.iter().enumerate() {
70 if !shown_in_calendar(event) || event.end <= day_start || event.start >= day_end {
71 continue;
72 }
73 if event.all_day {
74 all_day.push(index);
75 continue;
76 }
77 let start_minute = (event.start.max(day_start) - day_start).num_minutes();
78 let end_minute = (event.end.min(day_end) - day_start).num_minutes();
79 spans.push((index, start_minute, end_minute.max(start_minute + 1)));
80 }
81 WeekDay {
82 date,
83 all_day,
84 blocks: lay_out(spans),
85 }
86 })
87 .collect()
88}
89
90fn lay_out(mut spans: Vec<(usize, i64, i64)>) -> Vec<WeekBlock> {
91 spans.sort_by_key(|&(index, start, end)| (start, std::cmp::Reverse(end), index));
92 let mut blocks: Vec<WeekBlock> = Vec::with_capacity(spans.len());
93 let mut cluster_start = 0;
94 let mut cluster_end = i64::MIN;
95 let mut lane_ends: Vec<i64> = Vec::new();
96 for (index, start, end) in spans {
97 if start >= cluster_end {
98 close_cluster(&mut blocks[cluster_start..], lane_ends.len());
99 cluster_start = blocks.len();
100 lane_ends.clear();
101 }
102 let lane = match lane_ends.iter().position(|&lane_end| lane_end <= start) {
103 Some(free) => {
104 lane_ends[free] = end;
105 free
106 }
107 None => {
108 lane_ends.push(end);
109 lane_ends.len() - 1
110 }
111 };
112 cluster_end = cluster_end.max(end);
113 blocks.push(WeekBlock {
114 event_index: index,
115 start_minute: start,
116 end_minute: end,
117 lane,
118 lanes: 1,
119 });
120 }
121 close_cluster(&mut blocks[cluster_start..], lane_ends.len());
122 blocks
123}
124
125fn close_cluster(blocks: &mut [WeekBlock], lanes: usize) {
126 for block in blocks {
127 block.lanes = lanes.max(1);
128 }
129}
130
131pub fn range_label(start: NaiveDate, days: i64) -> String {
132 let end = start + Duration::days(days - 1);
133 if start.month() == end.month() {
134 start.format("%B %Y").to_string()
135 } else if start.year() == end.year() {
136 format!("{} - {}", start.format("%B"), end.format("%B %Y"))
137 } else {
138 format!("{} - {}", start.format("%B %Y"), end.format("%B %Y"))
139 }
140}
141
142pub fn hour_label(hour: u32) -> String {
143 match hour {
144 0 => "12 AM".to_owned(),
145 12 => "12 PM".to_owned(),
146 hour if hour < 12 => format!("{hour} AM"),
147 hour => format!("{} PM", hour - 12),
148 }
149}
150
151#[cfg(test)]
152mod tests {
153 use chrono::TimeZone;
154
155 use super::*;
156
157 fn event(start_hour: u32, start_minute: u32, minutes: i64) -> CalendarEvent {
158 let start = Utc
159 .with_ymd_and_hms(2026, 11, 9, start_hour, start_minute, 0)
160 .unwrap();
161 CalendarEvent {
162 object_id: format!("{start_hour}:{start_minute}"),
163 subject: "Sync".into(),
164 start,
165 end: start + Duration::minutes(minutes),
166 all_day: false,
167 cancelled: false,
168 online: true,
169 join_url: None,
170 thread_id: None,
171 organizer_name: None,
172 organizer_address: None,
173 ical_uid: None,
174 response: ResponseType::Accepted,
175 }
176 }
177
178 fn utc() -> FixedOffset {
179 FixedOffset::east_opt(0).unwrap()
180 }
181
182 #[test]
183 fn the_week_starts_on_monday() {
184 let sunday = NaiveDate::from_ymd_opt(2026, 11, 15).unwrap();
185 assert_eq!(
186 week_start(sunday),
187 NaiveDate::from_ymd_opt(2026, 11, 9).unwrap()
188 );
189 assert_eq!(week_start(week_start(sunday)), week_start(sunday));
190 assert_eq!(
191 work_week_for(sunday),
192 NaiveDate::from_ymd_opt(2026, 11, 16).unwrap()
193 );
194 let friday = NaiveDate::from_ymd_opt(2026, 11, 13).unwrap();
195 assert_eq!(
196 work_week_for(friday),
197 NaiveDate::from_ymd_opt(2026, 11, 9).unwrap()
198 );
199 }
200
201 #[test]
202 fn overlapping_meetings_share_the_column_in_lanes() {
203 let events = vec![
204 event(9, 0, 60),
205 event(9, 30, 60),
206 event(10, 0, 30),
207 event(13, 0, 30),
208 ];
209 let monday = NaiveDate::from_ymd_opt(2026, 11, 9).unwrap();
210 let days = week_days(&events, monday, WORK_WEEK_DAYS, utc());
211 assert_eq!(days.len(), 5);
212 let lanes: Vec<(usize, usize, usize)> = days[0]
213 .blocks
214 .iter()
215 .map(|block| (block.event_index, block.lane, block.lanes))
216 .collect();
217 assert_eq!(lanes, [(0, 0, 2), (1, 1, 2), (2, 0, 2), (3, 0, 1)]);
218 assert_eq!(days[0].blocks[0].start_minute, 9 * 60);
219 assert!(days[1].blocks.is_empty());
220 }
221
222 #[test]
223 fn declined_and_other_days_are_left_out_and_all_day_is_separate() {
224 let mut declined = event(9, 0, 30);
225 declined.response = ResponseType::Declined;
226 let mut all_day = event(0, 0, 24 * 60);
227 all_day.all_day = true;
228 let late = event(23, 30, 60);
229 let monday = NaiveDate::from_ymd_opt(2026, 11, 9).unwrap();
230 let days = week_days(&[declined, all_day, late], monday, 2, utc());
231 assert!(days[0].blocks.iter().all(|block| block.event_index != 0));
232 assert_eq!(days[0].all_day, [1]);
233 assert_eq!(days[0].blocks[0].end_minute, MINUTES_PER_DAY);
234 assert_eq!(
235 (days[1].blocks[0].start_minute, days[1].blocks[0].end_minute),
236 (0, 30)
237 );
238 }
239
240 #[test]
241 fn labels_read_like_teams() {
242 let monday = NaiveDate::from_ymd_opt(2026, 11, 30).unwrap();
243 assert_eq!(range_label(monday, 5), "November - December 2026");
244 assert_eq!(
245 range_label(NaiveDate::from_ymd_opt(2026, 11, 9).unwrap(), 5),
246 "November 2026"
247 );
248 assert_eq!(hour_label(0), "12 AM");
249 assert_eq!(hour_label(13), "1 PM");
250 }
251}