mirror of
https://github.com/matrix-org/matrix-rust-sdk.git
synced 2026-04-26 10:11:10 -04:00
refactor(sdk): Move day_divider constructor to TimelineItem
This commit is contained in:
committed by
Jonas Platte
parent
271e925adc
commit
237edcd747
@@ -474,15 +474,13 @@ impl<'a, 'i> TimelineEventHandler<'a, 'i> {
|
||||
if let Some(day_divider_item) =
|
||||
maybe_create_day_divider_from_timestamps(old_ts, *timestamp)
|
||||
{
|
||||
self.timeline_items
|
||||
.push_cloned(Arc::new(TimelineItem::Virtual(day_divider_item)));
|
||||
self.timeline_items.push_cloned(Arc::new(day_divider_item));
|
||||
}
|
||||
} else {
|
||||
// If there is no event item, there is no day divider yet.
|
||||
let (year, month, day) = timestamp_to_ymd(*timestamp);
|
||||
self.timeline_items.push_cloned(Arc::new(TimelineItem::Virtual(
|
||||
VirtualTimelineItem::day_divider(year, month, day),
|
||||
)));
|
||||
self.timeline_items
|
||||
.push_cloned(Arc::new(TimelineItem::day_divider(year, month, day)));
|
||||
}
|
||||
|
||||
self.timeline_items.push_cloned(item);
|
||||
@@ -539,19 +537,15 @@ impl<'a, 'i> TimelineEventHandler<'a, 'i> {
|
||||
(*year, *month, *day),
|
||||
timestamp_to_ymd(*origin_server_ts),
|
||||
) {
|
||||
self.timeline_items.insert_cloned(
|
||||
offset,
|
||||
Arc::new(TimelineItem::Virtual(day_divider_item)),
|
||||
);
|
||||
self.timeline_items
|
||||
.insert_cloned(offset, Arc::new(day_divider_item));
|
||||
}
|
||||
} else {
|
||||
// The list must always start with a day divider.
|
||||
let (year, month, day) = timestamp_to_ymd(*origin_server_ts);
|
||||
self.timeline_items.insert_cloned(
|
||||
offset,
|
||||
Arc::new(TimelineItem::Virtual(VirtualTimelineItem::day_divider(
|
||||
year, month, day,
|
||||
))),
|
||||
Arc::new(TimelineItem::day_divider(year, month, day)),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -567,15 +561,13 @@ impl<'a, 'i> TimelineEventHandler<'a, 'i> {
|
||||
if let Some(day_divider_item) =
|
||||
maybe_create_day_divider_from_timestamps(old_ts, *origin_server_ts)
|
||||
{
|
||||
self.timeline_items
|
||||
.push_cloned(Arc::new(TimelineItem::Virtual(day_divider_item)));
|
||||
self.timeline_items.push_cloned(Arc::new(day_divider_item));
|
||||
}
|
||||
} else {
|
||||
// If there is not event item, there is no day divider yet.
|
||||
let (year, month, day) = timestamp_to_ymd(*origin_server_ts);
|
||||
self.timeline_items.push_cloned(Arc::new(TimelineItem::Virtual(
|
||||
VirtualTimelineItem::day_divider(year, month, day),
|
||||
)));
|
||||
self.timeline_items
|
||||
.push_cloned(Arc::new(TimelineItem::day_divider(year, month, day)));
|
||||
}
|
||||
|
||||
self.timeline_items.push_cloned(item)
|
||||
@@ -662,7 +654,7 @@ fn timestamp_to_ymd(ts: MilliSecondsSinceUnixEpoch) -> (i32, u32, u32) {
|
||||
fn maybe_create_day_divider_from_timestamps(
|
||||
old_ts: MilliSecondsSinceUnixEpoch,
|
||||
new_ts: MilliSecondsSinceUnixEpoch,
|
||||
) -> Option<VirtualTimelineItem> {
|
||||
) -> Option<TimelineItem> {
|
||||
maybe_create_day_divider_from_ymd(timestamp_to_ymd(old_ts), timestamp_to_ymd(new_ts))
|
||||
}
|
||||
|
||||
@@ -671,11 +663,11 @@ fn maybe_create_day_divider_from_timestamps(
|
||||
fn maybe_create_day_divider_from_ymd(
|
||||
old_ymd: (i32, u32, u32),
|
||||
new_ymd: (i32, u32, u32),
|
||||
) -> Option<VirtualTimelineItem> {
|
||||
) -> Option<TimelineItem> {
|
||||
let (old_year, old_month, old_day) = old_ymd;
|
||||
let (new_year, new_month, new_day) = new_ymd;
|
||||
if old_year != new_year || old_month != new_month || old_day != new_day {
|
||||
Some(VirtualTimelineItem::day_divider(new_year, new_month, new_day))
|
||||
Some(TimelineItem::day_divider(new_year, new_month, new_day))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
||||
@@ -330,6 +330,11 @@ impl TimelineItem {
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new day divider from the given year, month and day.
|
||||
fn day_divider(year: i32, month: u32, day: u32) -> Self {
|
||||
Self::Virtual(VirtualTimelineItem::DayDivider { year, month, day })
|
||||
}
|
||||
|
||||
fn read_marker() -> Self {
|
||||
Self::Virtual(VirtualTimelineItem::ReadMarker)
|
||||
}
|
||||
|
||||
@@ -35,11 +35,3 @@ pub enum VirtualTimelineItem {
|
||||
/// A loading indicator for a pagination request.
|
||||
LoadingIndicator,
|
||||
}
|
||||
|
||||
impl VirtualTimelineItem {
|
||||
/// Creates a new `VirtualTimelineItem::DayDivider` from the given year,
|
||||
/// month and day.
|
||||
pub(crate) fn day_divider(year: i32, month: u32, day: u32) -> Self {
|
||||
Self::DayDivider { year, month, day }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user