| 1 | use chatsvc::{CalendarEvent, ResponseType, ShowAs}; |
| 2 | use chrono::{DateTime, Datelike, Duration, FixedOffset, Months, NaiveDate, Utc, Weekday}; |
| 3 | |
| 4 | pub const WORK_WEEK_DAYS: i64 = 5; |
| 5 | pub const MINUTES_PER_DAY: i64 = 24 * 60; |
| 6 | pub const MONTH_GRID_DAYS: i64 = 42; |
| 7 | |
| 8 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 9 | pub struct WeekBlock { |
| 10 | pub event_index: usize, |
| 11 | pub start_minute: i64, |
| 12 | pub end_minute: i64, |
| 13 | pub lane: usize, |
| 14 | pub lanes: usize, |
| 15 | } |
| 16 | |
| 17 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 18 | pub struct WeekDay { |
| 19 | pub date: NaiveDate, |
| 20 | pub all_day: Vec<usize>, |
| 21 | pub blocks: Vec<WeekBlock>, |
| 22 | } |
| 23 | |
| 24 | pub fn week_start(date: NaiveDate) -> NaiveDate { |
| 25 | date - Duration::days(i64::from(date.weekday().num_days_from_monday())) |
| 26 | } |
| 27 | |
| 28 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 29 | pub enum CalendarSpan { |
| 30 | Day, |
| 31 | #[default] |
| 32 | WorkWeek, |
| 33 | Week, |
| 34 | Month, |
| 35 | } |
| 36 | |
| 37 | impl CalendarSpan { |
| 38 | pub const ALL: [CalendarSpan; 4] = [ |
| 39 | CalendarSpan::Day, |
| 40 | CalendarSpan::WorkWeek, |
| 41 | CalendarSpan::Week, |
| 42 | CalendarSpan::Month, |
| 43 | ]; |
| 44 | |
| 45 | pub fn label(self) -> &'static str { |
| 46 | match self { |
| 47 | CalendarSpan::Day => "Day", |
| 48 | CalendarSpan::WorkWeek => "Work week", |
| 49 | CalendarSpan::Week => "Week", |
| 50 | CalendarSpan::Month => "Month", |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | pub fn days(self) -> i64 { |
| 55 | match self { |
| 56 | CalendarSpan::Day => 1, |
| 57 | CalendarSpan::WorkWeek => WORK_WEEK_DAYS, |
| 58 | CalendarSpan::Week => 7, |
| 59 | CalendarSpan::Month => MONTH_GRID_DAYS, |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | pub fn start_for(self, date: NaiveDate) -> NaiveDate { |
| 64 | match self { |
| 65 | CalendarSpan::Day => date, |
| 66 | CalendarSpan::WorkWeek => work_week_for(date), |
| 67 | CalendarSpan::Week => week_start(date), |
| 68 | CalendarSpan::Month => month_grid_start(date), |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | pub fn shifted(self, start: NaiveDate, steps: i32) -> NaiveDate { |
| 73 | match self { |
| 74 | CalendarSpan::Day => start + Duration::days(i64::from(steps)), |
| 75 | CalendarSpan::WorkWeek | CalendarSpan::Week => start + Duration::weeks(i64::from(steps)), |
| 76 | CalendarSpan::Month => { |
| 77 | let month = month_shown(start); |
| 78 | let moved = if steps >= 0 { |
| 79 | month.checked_add_months(Months::new(steps.unsigned_abs())) |
| 80 | } else { |
| 81 | month.checked_sub_months(Months::new(steps.unsigned_abs())) |
| 82 | }; |
| 83 | month_grid_start(moved.unwrap_or(month)) |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | pub fn title(self, start: NaiveDate) -> String { |
| 89 | match self { |
| 90 | CalendarSpan::Month => month_shown(start).format("%B %Y").to_string(), |
| 91 | _ => range_label(start, self.days()), |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | pub fn first_of_month(date: NaiveDate) -> NaiveDate { |
| 97 | date.with_day(1).unwrap_or(date) |
| 98 | } |
| 99 | |
| 100 | pub fn month_grid_start(date: NaiveDate) -> NaiveDate { |
| 101 | week_start(first_of_month(date)) |
| 102 | } |
| 103 | |
| 104 | /// The grid starts up to six days before the 1st, so a week in is always inside the shown month. |
| 105 | pub fn month_shown(grid_start: NaiveDate) -> NaiveDate { |
| 106 | first_of_month(grid_start + Duration::weeks(1)) |
| 107 | } |
| 108 | |
| 109 | /// On a weekend the work week view shows the coming week, as Outlook does. |
| 110 | pub fn work_week_for(today: NaiveDate) -> NaiveDate { |
| 111 | match today.weekday() { |
| 112 | Weekday::Sat | Weekday::Sun => week_start(today) + Duration::weeks(1), |
| 113 | _ => week_start(today), |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | pub fn local_midnight(date: NaiveDate, offset: FixedOffset) -> DateTime<Utc> { |
| 118 | date.and_hms_opt(0, 0, 0) |
| 119 | .and_then(|local| local.and_local_timezone(offset).single()) |
| 120 | .map(|local| local.with_timezone(&Utc)) |
| 121 | .unwrap_or_else(|| date.and_hms_opt(0, 0, 0).unwrap_or_default().and_utc()) |
| 122 | } |
| 123 | |
| 124 | pub fn week_range( |
| 125 | start: NaiveDate, |
| 126 | days: i64, |
| 127 | offset: FixedOffset, |
| 128 | ) -> (DateTime<Utc>, DateTime<Utc>) { |
| 129 | ( |
| 130 | local_midnight(start, offset), |
| 131 | local_midnight(start + Duration::days(days), offset), |
| 132 | ) |
| 133 | } |
| 134 | |
| 135 | pub fn shown_in_calendar(event: &CalendarEvent) -> bool { |
| 136 | event.response != ResponseType::Declined |
| 137 | } |
| 138 | |
| 139 | pub fn week_days( |
| 140 | events: &[CalendarEvent], |
| 141 | start: NaiveDate, |
| 142 | days: i64, |
| 143 | offset: FixedOffset, |
| 144 | ) -> Vec<WeekDay> { |
| 145 | (0..days) |
| 146 | .map(|day| { |
| 147 | let date = start + Duration::days(day); |
| 148 | let (day_start, day_end) = week_range(date, 1, offset); |
| 149 | let mut all_day = Vec::new(); |
| 150 | let mut spans = Vec::new(); |
| 151 | for (index, event) in events.iter().enumerate() { |
| 152 | if !shown_in_calendar(event) || event.end <= day_start || event.start >= day_end { |
| 153 | continue; |
| 154 | } |
| 155 | if event.all_day { |
| 156 | all_day.push(index); |
| 157 | continue; |
| 158 | } |
| 159 | let start_minute = (event.start.max(day_start) - day_start).num_minutes(); |
| 160 | let end_minute = (event.end.min(day_end) - day_start).num_minutes(); |
| 161 | spans.push((index, start_minute, end_minute.max(start_minute + 1))); |
| 162 | } |
| 163 | WeekDay { |
| 164 | date, |
| 165 | all_day, |
| 166 | blocks: lay_out(spans), |
| 167 | } |
| 168 | }) |
| 169 | .collect() |
| 170 | } |
| 171 | |
| 172 | fn lay_out(mut spans: Vec<(usize, i64, i64)>) -> Vec<WeekBlock> { |
| 173 | spans.sort_by_key(|&(index, start, end)| (start, std::cmp::Reverse(end), index)); |
| 174 | let mut blocks: Vec<WeekBlock> = Vec::with_capacity(spans.len()); |
| 175 | let mut cluster_start = 0; |
| 176 | let mut cluster_end = i64::MIN; |
| 177 | let mut lane_ends: Vec<i64> = Vec::new(); |
| 178 | for (index, start, end) in spans { |
| 179 | if start >= cluster_end { |
| 180 | close_cluster(&mut blocks[cluster_start..], lane_ends.len()); |
| 181 | cluster_start = blocks.len(); |
| 182 | lane_ends.clear(); |
| 183 | } |
| 184 | let lane = match lane_ends.iter().position(|&lane_end| lane_end <= start) { |
| 185 | Some(free) => { |
| 186 | lane_ends[free] = end; |
| 187 | free |
| 188 | } |
| 189 | None => { |
| 190 | lane_ends.push(end); |
| 191 | lane_ends.len() - 1 |
| 192 | } |
| 193 | }; |
| 194 | cluster_end = cluster_end.max(end); |
| 195 | blocks.push(WeekBlock { |
| 196 | event_index: index, |
| 197 | start_minute: start, |
| 198 | end_minute: end, |
| 199 | lane, |
| 200 | lanes: 1, |
| 201 | }); |
| 202 | } |
| 203 | close_cluster(&mut blocks[cluster_start..], lane_ends.len()); |
| 204 | blocks |
| 205 | } |
| 206 | |
| 207 | fn close_cluster(blocks: &mut [WeekBlock], lanes: usize) { |
| 208 | for block in blocks { |
| 209 | block.lanes = lanes.max(1); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | pub fn range_label(start: NaiveDate, days: i64) -> String { |
| 214 | app_base::format::calendar_range_label(start, start + Duration::days(days - 1)) |
| 215 | } |
| 216 | |
| 217 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 218 | pub enum EventLook { |
| 219 | Busy, |
| 220 | Free, |
| 221 | Tentative, |
| 222 | } |
| 223 | |
| 224 | pub fn event_look(event: &CalendarEvent) -> EventLook { |
| 225 | match event.show_as { |
| 226 | ShowAs::Free => EventLook::Free, |
| 227 | ShowAs::Tentative => EventLook::Tentative, |
| 228 | ShowAs::Busy | ShowAs::OutOfOffice | ShowAs::WorkingElsewhere => EventLook::Busy, |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | pub fn hour_label(hour: u32, twelve_hour: bool) -> String { |
| 233 | if !twelve_hour { |
| 234 | return hour.to_string(); |
| 235 | } |
| 236 | match hour { |
| 237 | 0 => "12 AM".to_owned(), |
| 238 | 12 => "12 PM".to_owned(), |
| 239 | hour if hour < 12 => format!("{hour} AM"), |
| 240 | hour => format!("{} PM", hour - 12), |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | #[cfg(test)] |
| 245 | mod tests { |
| 246 | use chrono::TimeZone; |
| 247 | |
| 248 | use super::*; |
| 249 | |
| 250 | fn event(start_hour: u32, start_minute: u32, minutes: i64) -> CalendarEvent { |
| 251 | let start = Utc |
| 252 | .with_ymd_and_hms(2026, 11, 9, start_hour, start_minute, 0) |
| 253 | .unwrap(); |
| 254 | CalendarEvent { |
| 255 | object_id: format!("{start_hour}:{start_minute}"), |
| 256 | subject: "Sync".into(), |
| 257 | start, |
| 258 | end: start + Duration::minutes(minutes), |
| 259 | all_day: false, |
| 260 | cancelled: false, |
| 261 | online: true, |
| 262 | join_url: None, |
| 263 | thread_id: None, |
| 264 | organizer_name: None, |
| 265 | organizer_address: None, |
| 266 | ical_uid: None, |
| 267 | show_as: Default::default(), |
| 268 | dial_in: None, |
| 269 | response: ResponseType::Accepted, |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | fn utc() -> FixedOffset { |
| 274 | FixedOffset::east_opt(0).unwrap() |
| 275 | } |
| 276 | |
| 277 | #[test] |
| 278 | fn month_view_steps_whole_months_from_a_monday_grid() { |
| 279 | let date = |day| NaiveDate::from_ymd_opt(2026, 11, day).unwrap(); |
| 280 | let start = CalendarSpan::Month.start_for(date(18)); |
| 281 | assert_eq!(start, NaiveDate::from_ymd_opt(2026, 10, 26).unwrap()); |
| 282 | assert_eq!(CalendarSpan::Month.title(start), "November 2026"); |
| 283 | let next = CalendarSpan::Month.shifted(start, 1); |
| 284 | assert_eq!(next, NaiveDate::from_ymd_opt(2026, 11, 30).unwrap()); |
| 285 | assert_eq!(CalendarSpan::Month.title(next), "December 2026"); |
| 286 | let back = CalendarSpan::Month.shifted(start, -11); |
| 287 | assert_eq!(CalendarSpan::Month.title(back), "December 2025"); |
| 288 | assert_eq!(CalendarSpan::Week.shifted(date(2), -1), date(2) - Duration::weeks(1)); |
| 289 | } |
| 290 | |
| 291 | #[test] |
| 292 | fn the_week_starts_on_monday() { |
| 293 | let sunday = NaiveDate::from_ymd_opt(2026, 11, 15).unwrap(); |
| 294 | assert_eq!( |
| 295 | week_start(sunday), |
| 296 | NaiveDate::from_ymd_opt(2026, 11, 9).unwrap() |
| 297 | ); |
| 298 | assert_eq!(week_start(week_start(sunday)), week_start(sunday)); |
| 299 | assert_eq!( |
| 300 | work_week_for(sunday), |
| 301 | NaiveDate::from_ymd_opt(2026, 11, 16).unwrap() |
| 302 | ); |
| 303 | let friday = NaiveDate::from_ymd_opt(2026, 11, 13).unwrap(); |
| 304 | assert_eq!( |
| 305 | work_week_for(friday), |
| 306 | NaiveDate::from_ymd_opt(2026, 11, 9).unwrap() |
| 307 | ); |
| 308 | } |
| 309 | |
| 310 | #[test] |
| 311 | fn overlapping_meetings_share_the_column_in_lanes() { |
| 312 | let events = vec![ |
| 313 | event(9, 0, 60), |
| 314 | event(9, 30, 60), |
| 315 | event(10, 0, 30), |
| 316 | event(13, 0, 30), |
| 317 | ]; |
| 318 | let monday = NaiveDate::from_ymd_opt(2026, 11, 9).unwrap(); |
| 319 | let days = week_days(&events, monday, WORK_WEEK_DAYS, utc()); |
| 320 | assert_eq!(days.len(), 5); |
| 321 | let lanes: Vec<(usize, usize, usize)> = days[0] |
| 322 | .blocks |
| 323 | .iter() |
| 324 | .map(|block| (block.event_index, block.lane, block.lanes)) |
| 325 | .collect(); |
| 326 | assert_eq!(lanes, [(0, 0, 2), (1, 1, 2), (2, 0, 2), (3, 0, 1)]); |
| 327 | assert_eq!(days[0].blocks[0].start_minute, 9 * 60); |
| 328 | assert!(days[1].blocks.is_empty()); |
| 329 | } |
| 330 | |
| 331 | #[test] |
| 332 | fn declined_and_other_days_are_left_out_and_all_day_is_separate() { |
| 333 | let mut declined = event(9, 0, 30); |
| 334 | declined.response = ResponseType::Declined; |
| 335 | let mut all_day = event(0, 0, 24 * 60); |
| 336 | all_day.all_day = true; |
| 337 | let late = event(23, 30, 60); |
| 338 | let monday = NaiveDate::from_ymd_opt(2026, 11, 9).unwrap(); |
| 339 | let days = week_days(&[declined, all_day, late], monday, 2, utc()); |
| 340 | assert!(days[0].blocks.iter().all(|block| block.event_index != 0)); |
| 341 | assert_eq!(days[0].all_day, [1]); |
| 342 | assert_eq!(days[0].blocks[0].end_minute, MINUTES_PER_DAY); |
| 343 | assert_eq!( |
| 344 | (days[1].blocks[0].start_minute, days[1].blocks[0].end_minute), |
| 345 | (0, 30) |
| 346 | ); |
| 347 | } |
| 348 | |
| 349 | #[test] |
| 350 | fn spans_start_and_step_like_teams() { |
| 351 | let wednesday = NaiveDate::from_ymd_opt(2026, 11, 11).unwrap(); |
| 352 | let monday = NaiveDate::from_ymd_opt(2026, 11, 9).unwrap(); |
| 353 | assert_eq!(CalendarSpan::Day.start_for(wednesday), wednesday); |
| 354 | assert_eq!(CalendarSpan::WorkWeek.start_for(wednesday), monday); |
| 355 | assert_eq!(CalendarSpan::Week.start_for(wednesday), monday); |
| 356 | let sunday = NaiveDate::from_ymd_opt(2026, 11, 15).unwrap(); |
| 357 | assert_eq!(CalendarSpan::Week.start_for(sunday), monday); |
| 358 | assert_eq!(CalendarSpan::Day.shifted(wednesday, 1), wednesday + Duration::days(1)); |
| 359 | assert_eq!(CalendarSpan::Week.days(), 7); |
| 360 | } |
| 361 | |
| 362 | #[test] |
| 363 | fn labels_read_like_teams() { |
| 364 | let monday = NaiveDate::from_ymd_opt(2026, 11, 30).unwrap(); |
| 365 | assert_eq!(range_label(monday, 5), "November 30 - December 4, 2026"); |
| 366 | assert_eq!( |
| 367 | range_label(NaiveDate::from_ymd_opt(2026, 11, 9).unwrap(), 5), |
| 368 | "November 9-13, 2026" |
| 369 | ); |
| 370 | assert_eq!( |
| 371 | range_label(NaiveDate::from_ymd_opt(2026, 12, 28).unwrap(), 7), |
| 372 | "December 28, 2026 - January 3, 2027" |
| 373 | ); |
| 374 | assert_eq!( |
| 375 | range_label(NaiveDate::from_ymd_opt(2026, 11, 9).unwrap(), 1), |
| 376 | "November 9, 2026" |
| 377 | ); |
| 378 | assert_eq!(hour_label(0, true), "12 AM"); |
| 379 | assert_eq!(hour_label(13, true), "1 PM"); |
| 380 | assert_eq!(hour_label(13, false), "13"); |
| 381 | } |
| 382 | |
| 383 | #[test] |
| 384 | fn free_and_tentative_meetings_look_like_teams() { |
| 385 | let mut following = event(9, 0, 30); |
| 386 | following.show_as = ShowAs::Free; |
| 387 | following.response = ResponseType::Tentative; |
| 388 | let mut tentative = event(9, 0, 30); |
| 389 | tentative.show_as = ShowAs::Tentative; |
| 390 | let mut away = event(9, 0, 30); |
| 391 | away.show_as = ShowAs::OutOfOffice; |
| 392 | assert_eq!(event_look(&following), EventLook::Free); |
| 393 | assert_eq!(event_look(&tentative), EventLook::Tentative); |
| 394 | assert_eq!(event_look(&away), EventLook::Busy); |
| 395 | assert_eq!(event_look(&event(9, 0, 30)), EventLook::Busy); |
| 396 | } |
| 397 | } |