forked from Nexus/nexus
fixup subspace ordering
This commit is contained in:
parent
dcb160556e
commit
701024b5f6
2 changed files with 38 additions and 19 deletions
|
|
@ -256,7 +256,7 @@ class SettingsSectionsController
|
||||||
),
|
),
|
||||||
.new(
|
.new(
|
||||||
title: "Room Ordering",
|
title: "Room Ordering",
|
||||||
description: "Sort rooms 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, optionally showing mentioned or unread ones first, or keep the order their space sets.",
|
||||||
icon: Icons.sort,
|
icon: Icons.sort,
|
||||||
builder: (title, description, icon) => DialogListTile<RoomOrder>(
|
builder: (title, description, icon) => DialogListTile<RoomOrder>(
|
||||||
icon: Icon(icon),
|
icon: Icon(icon),
|
||||||
|
|
|
||||||
|
|
@ -173,31 +173,50 @@ class SpacesController extends AsyncNotifier<IList<Space>> {
|
||||||
DateTime activity(Room room) => room.metadata?.sortingTimestamp ?? .new(0);
|
DateTime activity(Room room) => room.metadata?.sortingTimestamp ?? .new(0);
|
||||||
|
|
||||||
// Higher priority first, then most recent activity first
|
// Higher priority first, then most recent activity first
|
||||||
IList<Room> sortRooms(IList<Room> rooms) => rooms.sort(
|
int compare(
|
||||||
(a, b) => switch (priority(a).compareTo(priority(b))) {
|
({int priority, DateTime activity}) a,
|
||||||
0 => activity(b).compareTo(activity(a)),
|
({int priority, DateTime activity}) b,
|
||||||
|
) => switch (a.priority.compareTo(b.priority)) {
|
||||||
|
0 => b.activity.compareTo(a.activity),
|
||||||
final byPriority => byPriority,
|
final byPriority => byPriority,
|
||||||
},
|
};
|
||||||
|
|
||||||
|
({int priority, DateTime activity}) roomKey(Room room) =>
|
||||||
|
(priority: priority(room), activity: activity(room));
|
||||||
|
|
||||||
|
({int priority, DateTime activity}) groupKey(IList<Room> rooms) => (
|
||||||
|
priority: rooms.map(priority).minOrNull ?? 2,
|
||||||
|
activity: rooms.map(activity).maxOrNull ?? .new(0),
|
||||||
);
|
);
|
||||||
|
|
||||||
IList<Room> orderRooms(Space space, IList<Room> rooms) =>
|
IList<Room> sortRooms(IList<Room> rooms) =>
|
||||||
roomOrder == .spaceOrder && space.room != null
|
rooms.sort((a, b) => compare(roomKey(a), roomKey(b)));
|
||||||
? rooms
|
|
||||||
: sortRooms(rooms);
|
|
||||||
|
|
||||||
return .new(
|
return .new(
|
||||||
allSpaces.map(
|
allSpaces.map((space) {
|
||||||
(space) => space.copyWith(
|
if (roomOrder == .spaceOrder && space.room != null) return space;
|
||||||
children: orderRooms(space, space.children),
|
|
||||||
|
final subSpaces = space.subSpaces.sort(
|
||||||
|
(a, b) => compare(groupKey(a.children), groupKey(b.children)),
|
||||||
|
);
|
||||||
|
|
||||||
|
final childrenKey = groupKey(space.children);
|
||||||
|
return space.copyWith(
|
||||||
|
children: sortRooms(space.children),
|
||||||
subSpaces: .new(
|
subSpaces: .new(
|
||||||
space.subSpaces.map(
|
subSpaces.map(
|
||||||
(subspace) => subspace.copyWith(
|
(subspace) =>
|
||||||
children: orderRooms(space, subspace.children),
|
subspace.copyWith(children: sortRooms(subspace.children)),
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
childrenIndex: subSpaces
|
||||||
|
.where(
|
||||||
|
(subspace) =>
|
||||||
|
compare(groupKey(subspace.children), childrenKey) < 0,
|
||||||
|
)
|
||||||
|
.length,
|
||||||
|
);
|
||||||
|
}),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue