Skip to content

Commit

Permalink
chore(ui): move date and timestamp functionality to `timeline/date_di…
Browse files Browse the repository at this point in the history
…viders.rs`

`util.rs` files are… not the best thing. These types and functions were
only used by the date dividers file, so let's move them there.
  • Loading branch information
bnjbvr committed Jan 9, 2025
1 parent 17607bf commit 7dcdc92
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 30 deletions.
30 changes: 28 additions & 2 deletions crates/matrix-sdk-ui/src/timeline/date_dividers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,41 @@
use std::{fmt::Display, sync::Arc};

use chrono::{Datelike, Local, TimeZone};
use ruma::MilliSecondsSinceUnixEpoch;
use tracing::{error, event_enabled, instrument, trace, warn, Level};

use super::{
controller::{ObservableItemsTransaction, TimelineMetadata},
util::timestamp_to_date,
DateDividerMode, TimelineItem, TimelineItemKind, VirtualTimelineItem,
};

#[derive(Debug, PartialEq)]
struct Date {
year: i32,
month: u32,
day: u32,
}

impl Date {
fn is_same_month_as(&self, date: Date) -> bool {
self.year == date.year && self.month == date.month
}
}

/// Converts a timestamp since Unix Epoch to a year, month and day.
fn timestamp_to_date(ts: MilliSecondsSinceUnixEpoch) -> Date {
let datetime = Local
.timestamp_millis_opt(ts.0.into())
// Only returns `None` if date is after Dec 31, 262143 BCE.
.single()
// Fallback to the current date to avoid issues with malicious
// homeservers.
.unwrap_or_else(Local::now);

Date { year: datetime.year(), month: datetime.month(), day: datetime.day() }
}

/// Algorithm ensuring that date dividers are adjusted correctly, according to
/// new items that have been inserted.
pub(super) struct DateDividerAdjuster {
Expand Down Expand Up @@ -618,8 +644,8 @@ mod tests {
use super::{super::controller::ObservableItems, DateDividerAdjuster};
use crate::timeline::{
controller::TimelineMetadata,
date_dividers::timestamp_to_date,
event_item::{EventTimelineItemKind, RemoteEventTimelineItem},
util::timestamp_to_date,
DateDividerMode, EventTimelineItem, TimelineItemContent, VirtualTimelineItem,
};

Expand Down
29 changes: 1 addition & 28 deletions crates/matrix-sdk-ui/src/timeline/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@

use std::{ops::Deref, sync::Arc};

use chrono::{Datelike, Local, TimeZone};
use imbl::Vector;
use ruma::{EventId, MilliSecondsSinceUnixEpoch};
use ruma::EventId;

#[cfg(doc)]
use super::controller::TimelineMetadata;
Expand Down Expand Up @@ -124,29 +123,3 @@ pub(super) enum RelativePosition {
/// Event B is before (older than) event A.
Before,
}

#[derive(Debug, PartialEq)]
pub(super) struct Date {
year: i32,
month: u32,
day: u32,
}

impl Date {
pub fn is_same_month_as(&self, date: Date) -> bool {
self.year == date.year && self.month == date.month
}
}

/// Converts a timestamp since Unix Epoch to a year, month and day.
pub(super) fn timestamp_to_date(ts: MilliSecondsSinceUnixEpoch) -> Date {
let datetime = Local
.timestamp_millis_opt(ts.0.into())
// Only returns `None` if date is after Dec 31, 262143 BCE.
.single()
// Fallback to the current date to avoid issues with malicious
// homeservers.
.unwrap_or_else(Local::now);

Date { year: datetime.year(), month: datetime.month(), day: datetime.day() }
}

0 comments on commit 7dcdc92

Please sign in to comment.