Skip to content

Commit ccf11ad

Browse files
committed
feat(ui): latest_event sorter handles LatestEventValue::LocalHasBeenSent.
This patch changes the semantics of the Room List `latest_event` sorter by changing “is local” to “is remote like”, to include the new `LatestEventValue::LocalHasBeenSent` variant.
1 parent 631671f commit ccf11ad

File tree

4 files changed

+100
-86
lines changed

4 files changed

+100
-86
lines changed

crates/matrix-sdk-base/src/latest_event.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ impl LatestEventValue {
6565
}
6666
}
6767

68+
/// Check whether the [`LatestEventValue`] represents an unsent event, i.e.
69+
/// is [`LocalIsSending`] nor [`LocalCannotBeSent`].
70+
///
71+
/// [`LocalIsSending`]: LatestEventValue::LocalIsSending
72+
/// [`LocalCannotBeSent`]: LatestEventValue::LocalCannotBeSent
73+
pub fn is_unsent(&self) -> bool {
74+
match self {
75+
Self::LocalIsSending(_) | Self::LocalCannotBeSent(_) => true,
76+
Self::LocalHasBeenSent(_) | Self::Remote(_) | Self::None => false,
77+
}
78+
}
79+
6880
/// Check whether the [`LatestEventValue`] is not set, i.e. [`None`].
6981
///
7082
/// [`None`]: LatestEventValue::None

crates/matrix-sdk-base/src/room/latest_event.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ impl Room {
3131
self.info.read().latest_event_value.timestamp()
3232
}
3333

34-
/// Return the value of [`LatestEventValue::is_local`].
35-
pub fn latest_event_is_local(&self) -> bool {
36-
self.info.read().latest_event_value.is_local()
34+
/// Return the value of [`LatestEventValue::is_unsent`].
35+
pub fn latest_event_is_unsent(&self) -> bool {
36+
self.info.read().latest_event_value.is_unsent()
3737
}
3838
}

crates/matrix-sdk-ui/src/room_list_service/room_list.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -423,8 +423,8 @@ pub struct RoomListItem {
423423
/// Cache of `Room::latest_event_timestamp`.
424424
pub(super) cached_latest_event_timestamp: Option<MilliSecondsSinceUnixEpoch>,
425425

426-
/// Cache of `Room::latest_event_is_local`.
427-
pub(super) cached_latest_event_is_local: bool,
426+
/// Cache of `Room::latest_event_is_unsent`.
427+
pub(super) cached_latest_event_is_unsent: bool,
428428

429429
/// Cache of `Room::recency_stamp`.
430430
pub(super) cached_recency_stamp: Option<RoomRecencyStamp>,
@@ -448,7 +448,7 @@ impl RoomListItem {
448448
/// Refresh the cached data.
449449
pub(super) fn refresh_cached_data(&mut self) {
450450
self.cached_latest_event_timestamp = self.inner.latest_event_timestamp();
451-
self.cached_latest_event_is_local = self.inner.latest_event_is_local();
451+
self.cached_latest_event_is_unsent = self.inner.latest_event_is_unsent();
452452
self.cached_recency_stamp = self.inner.recency_stamp();
453453
self.cached_display_name = self.inner.cached_display_name().map(|name| name.to_string());
454454
self.cached_is_space = self.inner.is_space();
@@ -459,7 +459,7 @@ impl RoomListItem {
459459
impl From<Room> for RoomListItem {
460460
fn from(inner: Room) -> Self {
461461
let cached_latest_event_timestamp = inner.latest_event_timestamp();
462-
let cached_latest_event_is_local = inner.latest_event_is_local();
462+
let cached_latest_event_is_unsent = inner.latest_event_is_unsent();
463463
let cached_recency_stamp = inner.recency_stamp();
464464
let cached_display_name = inner.cached_display_name().map(|name| name.to_string());
465465
let cached_is_space = inner.is_space();
@@ -468,7 +468,7 @@ impl From<Room> for RoomListItem {
468468
Self {
469469
inner,
470470
cached_latest_event_timestamp,
471-
cached_latest_event_is_local,
471+
cached_latest_event_is_unsent,
472472
cached_recency_stamp,
473473
cached_display_name,
474474
cached_is_space,

crates/matrix-sdk-ui/src/room_list_service/sorters/latest_event.rs

Lines changed: 80 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@ use std::cmp::Ordering;
1616

1717
use super::{RoomListItem, Sorter};
1818

19-
fn cmp<F>(are_latest_events_locals: F, left: &RoomListItem, right: &RoomListItem) -> Ordering
19+
fn cmp<F>(are_latest_events_unsent: F, left: &RoomListItem, right: &RoomListItem) -> Ordering
2020
where
2121
F: Fn(&RoomListItem, &RoomListItem) -> (bool, bool),
2222
{
23-
// We want local latest event to come first. When there is a remote latest event
24-
// or no latest event, we don't want to sort them.
23+
// We want latest events representing unsent events to come firsts. When
24+
// there is a remote latest event or no latest event, we don't want to sort
25+
// them.
2526
// NOTE: This is the same as a.cmp(b).reverse() for booleans.
26-
match are_latest_events_locals(left, right) {
27+
match are_latest_events_unsent(left, right) {
2728
// `false` and `false`, i.e.:
2829
// - `None` == `None`.
2930
// - `None` == `Remote`.
@@ -48,22 +49,15 @@ where
4849
}
4950

5051
/// Create a new sorter that will sort two [`RoomListItem`] by their latest
51-
/// events' state: latest events representing a local event
52-
/// ([`LatestEventValue::LocalIsSending`] or
53-
/// [`LatestEventValue::LocalCannotBeSent`]) come first, and latest event
54-
/// representing a remote event ([`LatestEventValue::Remote`]) come last.
55-
///
56-
/// [`LatestEventValue::LocalIsSending`]: matrix_sdk_base::latest_event::LatestEventValue::LocalIsSending
57-
/// [`LatestEventValue::LocalCannotBeSent`]: matrix_sdk_base::latest_event::LatestEventValue::LocalCannotBeSent
58-
/// [`LatestEventValue::Remote`]: matrix_sdk_base::latest_event::LatestEventValue::Remote
52+
/// events' state: latest events representing unsent events come firsts.
5953
pub fn new_sorter() -> impl Sorter {
6054
let latest_events = |left: &RoomListItem, right: &RoomListItem| {
61-
// Be careful. This method is called **a lot** in the context of a sorter. Using
62-
// `Room::latest_event` would be dramatic as it returns a clone of the
63-
// `LatestEventValue`. It's better to use the more specific method
64-
// `Room::latest_event_is_local`, where the value is cached in this module's
65-
// `Room` type.
66-
(left.cached_latest_event_is_local, right.cached_latest_event_is_local)
55+
// Be careful. This method is called **a lot** in the context of a
56+
// sorter. Using `Room::latest_event` would be dramatic as it returns a
57+
// clone of the `LatestEventValue`. It's better to use the more specific
58+
// method `Room::latest_event_is_unsent`, where the value is cached
59+
// in `RoomListItem`.
60+
(left.cached_latest_event_is_unsent, right.cached_latest_event_is_unsent)
6761
};
6862

6963
move |left, right| -> Ordering { cmp(latest_events, left, right) }
@@ -119,6 +113,16 @@ mod tests {
119113
})
120114
}
121115

116+
fn local_has_been_sent() -> LatestEventValue {
117+
LatestEventValue::LocalHasBeenSent(LocalLatestEventValue {
118+
timestamp: MilliSecondsSinceUnixEpoch(uint!(42)),
119+
content: SerializableEventContent::new(&AnyMessageLikeEventContent::RoomMessage(
120+
RoomMessageEventContent::text_plain("raclette"),
121+
))
122+
.unwrap(),
123+
})
124+
}
125+
122126
fn local_cannot_be_sent() -> LatestEventValue {
123127
LatestEventValue::LocalCannotBeSent(LocalLatestEventValue {
124128
timestamp: MilliSecondsSinceUnixEpoch(uint!(42)),
@@ -129,6 +133,10 @@ mod tests {
129133
})
130134
}
131135

136+
fn pair(left: LatestEventValue, right: LatestEventValue) -> (bool, bool) {
137+
(left.is_unsent(), right.is_unsent())
138+
}
139+
132140
#[async_test]
133141
async fn test_none_or_remote_and_none_or_remote() {
134142
let (client, server) = logged_in_client_with_server().await;
@@ -138,34 +146,22 @@ mod tests {
138146

139147
// `None` and `None`.
140148
{
141-
assert_eq!(
142-
cmp(|_, _| (none().is_local(), none().is_local()), &room_a, &room_b),
143-
Ordering::Equal
144-
);
149+
assert_eq!(cmp(|_, _| pair(none(), none()), &room_a, &room_b), Ordering::Equal);
145150
}
146151

147152
// `None` and `Remote`.
148153
{
149-
assert_eq!(
150-
cmp(|_, _| (none().is_local(), remote().is_local()), &room_a, &room_b),
151-
Ordering::Equal
152-
);
154+
assert_eq!(cmp(|_, _| pair(none(), remote()), &room_a, &room_b), Ordering::Equal);
153155
}
154156

155157
// `Remote` and `None`.
156158
{
157-
assert_eq!(
158-
cmp(|_, _| (remote().is_local(), none().is_local()), &room_a, &room_b),
159-
Ordering::Equal
160-
);
159+
assert_eq!(cmp(|_, _| pair(remote(), none()), &room_a, &room_b), Ordering::Equal);
161160
}
162161

163162
// `Remote` and `None`.
164163
{
165-
assert_eq!(
166-
cmp(|_, _| (remote().is_local(), remote().is_local()), &room_a, &room_b),
167-
Ordering::Equal
168-
);
164+
assert_eq!(cmp(|_, _| pair(remote(), remote()), &room_a, &room_b), Ordering::Equal);
169165
}
170166
}
171167

@@ -179,31 +175,31 @@ mod tests {
179175
// `None` and `Local*`.
180176
{
181177
assert_eq!(
182-
cmp(|_, _| (none().is_local(), local_is_sending().is_local()), &room_a, &room_b),
178+
cmp(|_, _| pair(none(), local_is_sending()), &room_a, &room_b),
183179
Ordering::Greater
184180
);
185181
assert_eq!(
186-
cmp(
187-
|_, _| (none().is_local(), local_cannot_be_sent().is_local()),
188-
&room_a,
189-
&room_b
190-
),
182+
cmp(|_, _| pair(none(), local_has_been_sent()), &room_a, &room_b),
183+
Ordering::Equal
184+
);
185+
assert_eq!(
186+
cmp(|_, _| pair(none(), local_cannot_be_sent()), &room_a, &room_b),
191187
Ordering::Greater
192188
);
193189
}
194190

195191
// `Remote` and `Local*`.
196192
{
197193
assert_eq!(
198-
cmp(|_, _| (remote().is_local(), local_is_sending().is_local()), &room_a, &room_b),
194+
cmp(|_, _| pair(remote(), local_is_sending()), &room_a, &room_b),
199195
Ordering::Greater
200196
);
201197
assert_eq!(
202-
cmp(
203-
|_, _| (remote().is_local(), local_cannot_be_sent().is_local()),
204-
&room_a,
205-
&room_b
206-
),
198+
cmp(|_, _| pair(remote(), local_has_been_sent()), &room_a, &room_b),
199+
Ordering::Equal
200+
);
201+
assert_eq!(
202+
cmp(|_, _| pair(remote(), local_cannot_be_sent()), &room_a, &room_b),
207203
Ordering::Greater
208204
);
209205
}
@@ -219,31 +215,31 @@ mod tests {
219215
// `Local*` and `None`.
220216
{
221217
assert_eq!(
222-
cmp(|_, _| (local_is_sending().is_local(), none().is_local()), &room_a, &room_b),
218+
cmp(|_, _| pair(local_is_sending(), none()), &room_a, &room_b),
223219
Ordering::Less
224220
);
225221
assert_eq!(
226-
cmp(
227-
|_, _| (local_cannot_be_sent().is_local(), none().is_local()),
228-
&room_a,
229-
&room_b
230-
),
222+
cmp(|_, _| pair(local_has_been_sent(), none()), &room_a, &room_b),
223+
Ordering::Equal
224+
);
225+
assert_eq!(
226+
cmp(|_, _| pair(local_cannot_be_sent(), none()), &room_a, &room_b),
231227
Ordering::Less
232228
);
233229
}
234230

235231
// `Local*` and `Remote`.
236232
{
237233
assert_eq!(
238-
cmp(|_, _| (local_is_sending().is_local(), remote().is_local()), &room_a, &room_b),
234+
cmp(|_, _| pair(local_is_sending(), remote()), &room_a, &room_b),
239235
Ordering::Less
240236
);
241237
assert_eq!(
242-
cmp(
243-
|_, _| (local_cannot_be_sent().is_local(), remote().is_local()),
244-
&room_a,
245-
&room_b
246-
),
238+
cmp(|_, _| pair(local_has_been_sent(), remote()), &room_a, &room_b),
239+
Ordering::Equal
240+
);
241+
assert_eq!(
242+
cmp(|_, _| pair(local_cannot_be_sent(), remote()), &room_a, &room_b),
247243
Ordering::Less
248244
);
249245
}
@@ -259,35 +255,41 @@ mod tests {
259255
// `Local*` and `Local*`.
260256
{
261257
assert_eq!(
262-
cmp(
263-
|_, _| (local_is_sending().is_local(), local_is_sending().is_local()),
264-
&room_a,
265-
&room_b
266-
),
258+
cmp(|_, _| pair(local_is_sending(), local_is_sending()), &room_a, &room_b),
267259
Ordering::Equal
268260
);
269261
assert_eq!(
270-
cmp(
271-
|_, _| (local_is_sending().is_local(), local_cannot_be_sent().is_local()),
272-
&room_a,
273-
&room_b
274-
),
262+
cmp(|_, _| pair(local_is_sending(), local_has_been_sent()), &room_a, &room_b),
263+
Ordering::Less
264+
);
265+
assert_eq!(
266+
cmp(|_, _| pair(local_is_sending(), local_cannot_be_sent()), &room_a, &room_b),
275267
Ordering::Equal
276268
);
269+
277270
assert_eq!(
278-
cmp(
279-
|_, _| (local_cannot_be_sent().is_local(), local_is_sending().is_local()),
280-
&room_a,
281-
&room_b
282-
),
271+
cmp(|_, _| pair(local_has_been_sent(), local_is_sending()), &room_a, &room_b),
272+
Ordering::Greater
273+
);
274+
assert_eq!(
275+
cmp(|_, _| pair(local_has_been_sent(), local_has_been_sent()), &room_a, &room_b),
276+
Ordering::Equal
277+
);
278+
assert_eq!(
279+
cmp(|_, _| pair(local_has_been_sent(), local_cannot_be_sent()), &room_a, &room_b),
280+
Ordering::Greater
281+
);
282+
283+
assert_eq!(
284+
cmp(|_, _| pair(local_cannot_be_sent(), local_is_sending()), &room_a, &room_b),
283285
Ordering::Equal
284286
);
285287
assert_eq!(
286-
cmp(
287-
|_, _| (local_cannot_be_sent().is_local(), local_cannot_be_sent().is_local()),
288-
&room_a,
289-
&room_b
290-
),
288+
cmp(|_, _| pair(local_cannot_be_sent(), local_has_been_sent()), &room_a, &room_b),
289+
Ordering::Less
290+
);
291+
assert_eq!(
292+
cmp(|_, _| pair(local_cannot_be_sent(), local_cannot_be_sent()), &room_a, &room_b),
291293
Ordering::Equal
292294
);
293295
}

0 commit comments

Comments
 (0)