refactor: Simplify room sorting logic to make invite sorting more obvious

It wasn't quite obvious that invites were always sorted at the top, if
you just looked at the sort function. This makes it more explicit and
also makes invites always sort before favourited rooms.
This commit is contained in:
Nicolas Werner 2023-07-31 10:55:27 +02:00
parent 4f8fce34d5
commit 334c9e3b78
No known key found for this signature in database
2 changed files with 15 additions and 8 deletions

View File

@ -2245,12 +2245,21 @@ class Client extends MatrixApi {
/// The compare function how the rooms should be sorted internally. By default /// The compare function how the rooms should be sorted internally. By default
/// rooms are sorted by timestamp of the last m.room.message event or the last /// rooms are sorted by timestamp of the last m.room.message event or the last
/// event if there is no known message. /// event if there is no known message.
RoomSorter get sortRoomsBy => (a, b) => (a.isFavourite != b.isFavourite) RoomSorter get sortRoomsBy => (a, b) {
? (a.isFavourite ? -1 : 1) if (pinInvitedRooms &&
: (pinUnreadRooms && a.notificationCount != b.notificationCount) a.membership != b.membership &&
? b.notificationCount.compareTo(a.notificationCount) [a.membership, b.membership].any((m) => m == Membership.invite)) {
: b.timeCreated.millisecondsSinceEpoch return a.membership == Membership.invite ? -1 : 1;
} else if (a.isFavourite != b.isFavourite) {
return a.isFavourite ? -1 : 1;
} else if (pinUnreadRooms &&
a.notificationCount != b.notificationCount) {
return b.notificationCount.compareTo(a.notificationCount);
} else {
return b.timeCreated.millisecondsSinceEpoch
.compareTo(a.timeCreated.millisecondsSinceEpoch); .compareTo(a.timeCreated.millisecondsSinceEpoch);
}
};
void _sortRooms() { void _sortRooms() {
if (_sortLock || rooms.length < 2) return; if (_sortLock || rooms.length < 2) return;

View File

@ -599,9 +599,7 @@ class Room {
} }
/// Returns true if this room has a m.favourite tag. /// Returns true if this room has a m.favourite tag.
bool get isFavourite => bool get isFavourite => tags[TagType.favourite] != null;
tags[TagType.favourite] != null ||
(client.pinInvitedRooms && membership == Membership.invite);
/// Sets the m.favourite tag for this room. /// Sets the m.favourite tag for this room.
Future<void> setFavourite(bool favourite) => Future<void> setFavourite(bool favourite) =>