From 334c9e3b784a5d710d5db7c75760cf250496819b Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Mon, 31 Jul 2023 10:55:27 +0200 Subject: [PATCH] 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. --- lib/src/client.dart | 19 ++++++++++++++----- lib/src/room.dart | 4 +--- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/lib/src/client.dart b/lib/src/client.dart index e21458a3..7922d5c2 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -2245,12 +2245,21 @@ class Client extends MatrixApi { /// 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 /// event if there is no known message. - RoomSorter get sortRoomsBy => (a, b) => (a.isFavourite != b.isFavourite) - ? (a.isFavourite ? -1 : 1) - : (pinUnreadRooms && a.notificationCount != b.notificationCount) - ? b.notificationCount.compareTo(a.notificationCount) - : b.timeCreated.millisecondsSinceEpoch + RoomSorter get sortRoomsBy => (a, b) { + if (pinInvitedRooms && + a.membership != b.membership && + [a.membership, b.membership].any((m) => m == Membership.invite)) { + 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); + } + }; void _sortRooms() { if (_sortLock || rooms.length < 2) return; diff --git a/lib/src/room.dart b/lib/src/room.dart index c29cad81..e057dcc9 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -599,9 +599,7 @@ class Room { } /// Returns true if this room has a m.favourite tag. - bool get isFavourite => - tags[TagType.favourite] != null || - (client.pinInvitedRooms && membership == Membership.invite); + bool get isFavourite => tags[TagType.favourite] != null; /// Sets the m.favourite tag for this room. Future setFavourite(bool favourite) =>