update ordering settings
This commit is contained in:
parent
701024b5f6
commit
f6b9deee86
4 changed files with 56 additions and 42 deletions
|
|
@ -256,7 +256,7 @@ class SettingsSectionsController
|
|||
),
|
||||
.new(
|
||||
title: "Room Ordering",
|
||||
description: "Sort rooms and subspaces by recent activity, optionally showing mentioned or unread ones first, or keep the order their space sets.",
|
||||
description: "Sort rooms and subspaces by recent activity or the order their space sets, optionally showing mentioned and unread ones first.",
|
||||
icon: Icons.sort,
|
||||
builder: (title, description, icon) => DialogListTile<RoomOrder>(
|
||||
icon: Icon(icon),
|
||||
|
|
@ -265,10 +265,11 @@ class SettingsSectionsController
|
|||
value: settings.roomOrder,
|
||||
options: RoomOrder.values,
|
||||
getName: (option) => switch (option) {
|
||||
.unreadFirst => "Mentioned, then unread",
|
||||
.mentionedFirst => "Mentioned only",
|
||||
.unreadFirst => "Mentioned, then unread, then recent",
|
||||
.unreadFirstSpaceOrder =>
|
||||
"Mentioned, then unread, then space order",
|
||||
.recentActivity => "Recent activity only",
|
||||
.spaceOrder => "Space order",
|
||||
.spaceOrder => "Space order only",
|
||||
},
|
||||
onChanged: (value) => ref
|
||||
.read(SettingsController.provider.notifier)
|
||||
|
|
|
|||
|
|
@ -13,6 +13,8 @@ import "package:nexus/models/space.dart";
|
|||
import "package:nexus/models/space_edge.dart";
|
||||
import "package:nexus/models/subspace.dart";
|
||||
|
||||
typedef _SortKey = ({int priority, DateTime activity, int index});
|
||||
|
||||
class SpacesController extends AsyncNotifier<IList<Space>> {
|
||||
@override
|
||||
Future<IList<Space>> build() async {
|
||||
|
|
@ -162,58 +164,63 @@ class SpacesController extends AsyncNotifier<IList<Space>> {
|
|||
SettingsController.provider.selectAsync((data) => data.roomOrder),
|
||||
);
|
||||
|
||||
int priority(Room room) => switch ((roomOrder, room.metadata)) {
|
||||
(.recentActivity || .spaceOrder, _) => 0,
|
||||
(_, RoomMetadata(unreadHighlights: > 0)) => 0,
|
||||
(.mentionedFirst, _) => 1,
|
||||
(.unreadFirst, RoomMetadata(unreadNotifications: > 0)) ||
|
||||
(.unreadFirst, RoomMetadata(unreadMessages: > 0)) => 1,
|
||||
(.unreadFirst, _) => 2,
|
||||
int priority(Room? room) => switch (room?.metadata) {
|
||||
_ when !roomOrder.byUnread => 0,
|
||||
RoomMetadata(unreadHighlights: > 0) => 0,
|
||||
RoomMetadata(unreadNotifications: > 0) ||
|
||||
RoomMetadata(unreadMessages: > 0) => 1,
|
||||
_ => 2,
|
||||
};
|
||||
DateTime activity(Room room) => room.metadata?.sortingTimestamp ?? .new(0);
|
||||
|
||||
// Higher priority first, then most recent activity first
|
||||
int compare(
|
||||
({int priority, DateTime activity}) a,
|
||||
({int priority, DateTime activity}) b,
|
||||
) => switch (a.priority.compareTo(b.priority)) {
|
||||
return .new(
|
||||
allSpaces.map((space) {
|
||||
final bySpace = roomOrder.bySpace && space.room != null;
|
||||
int compare(_SortKey a, _SortKey b) =>
|
||||
switch (a.priority.compareTo(b.priority)) {
|
||||
0 when bySpace => a.index.compareTo(b.index),
|
||||
0 => b.activity.compareTo(a.activity),
|
||||
final byPriority => byPriority,
|
||||
};
|
||||
|
||||
({int priority, DateTime activity}) roomKey(Room room) =>
|
||||
(priority: priority(room), activity: activity(room));
|
||||
_SortKey roomKey(int index, Room room) =>
|
||||
(priority: priority(room), activity: activity(room), index: index);
|
||||
|
||||
({int priority, DateTime activity}) groupKey(IList<Room> rooms) => (
|
||||
priority: rooms.map(priority).minOrNull ?? 2,
|
||||
_SortKey groupKey(int index, IList<Room> rooms) => (
|
||||
priority: rooms.map(priority).minOrNull ?? priority(null),
|
||||
activity: rooms.map(activity).maxOrNull ?? .new(0),
|
||||
index: index,
|
||||
);
|
||||
|
||||
IList<Room> sortRooms(IList<Room> rooms) =>
|
||||
rooms.sort((a, b) => compare(roomKey(a), roomKey(b)));
|
||||
|
||||
return .new(
|
||||
allSpaces.map((space) {
|
||||
if (roomOrder == .spaceOrder && space.room != null) return space;
|
||||
|
||||
final subSpaces = space.subSpaces.sort(
|
||||
(a, b) => compare(groupKey(a.children), groupKey(b.children)),
|
||||
IList<Room> sortRooms(IList<Room> rooms) => .new(
|
||||
rooms.indexed
|
||||
.sorted(
|
||||
(a, b) => compare(roomKey(a.$1, a.$2), roomKey(b.$1, b.$2)),
|
||||
)
|
||||
.map((entry) => entry.$2),
|
||||
);
|
||||
|
||||
final childrenKey = groupKey(space.children);
|
||||
final subSpaces = space.subSpaces.indexed
|
||||
.map(
|
||||
(entry) => (
|
||||
subspace: entry.$2,
|
||||
key: groupKey(entry.$1, entry.$2.children),
|
||||
),
|
||||
)
|
||||
.sorted((a, b) => compare(a.key, b.key));
|
||||
|
||||
final childrenKey = groupKey(space.childrenIndex, space.children);
|
||||
return space.copyWith(
|
||||
children: sortRooms(space.children),
|
||||
subSpaces: .new(
|
||||
subSpaces.map(
|
||||
(subspace) =>
|
||||
subspace.copyWith(children: sortRooms(subspace.children)),
|
||||
(entry) => entry.subspace.copyWith(
|
||||
children: sortRooms(entry.subspace.children),
|
||||
),
|
||||
),
|
||||
),
|
||||
childrenIndex: subSpaces
|
||||
.where(
|
||||
(subspace) =>
|
||||
compare(groupKey(subspace.children), childrenKey) < 0,
|
||||
)
|
||||
.where((entry) => compare(entry.key, childrenKey) < 0)
|
||||
.length,
|
||||
);
|
||||
}),
|
||||
|
|
|
|||
|
|
@ -1 +1,6 @@
|
|||
enum RoomOrder { unreadFirst, mentionedFirst, recentActivity, spaceOrder }
|
||||
enum RoomOrder({required final bool byUnread, required final bool bySpace}) {
|
||||
unreadFirst(byUnread: true, bySpace: false),
|
||||
unreadFirstSpaceOrder(byUnread: true, bySpace: true),
|
||||
recentActivity(byUnread: false, bySpace: false),
|
||||
spaceOrder(byUnread: false, bySpace: true),
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ class const Settings({
|
|||
final bool swipeToReply = false,
|
||||
final bool hideMembershipEvents = false,
|
||||
final bool hideServerAclEvents = true,
|
||||
@JsonKey(unknownEnumValue: RoomOrder.unreadFirst)
|
||||
final RoomOrder roomOrder = .unreadFirst,
|
||||
final NameColor nameColor = .powerLevel,
|
||||
final ShowAndSend typingIndicators = .showAndSend,
|
||||
|
|
|
|||
Loading…
Reference in a new issue