forked from Nexus/nexus
parent
1e25cbf822
commit
dcb160556e
4 changed files with 55 additions and 43 deletions
|
|
@ -256,7 +256,7 @@ class SettingsSectionsController
|
||||||
),
|
),
|
||||||
.new(
|
.new(
|
||||||
title: "Room Ordering",
|
title: "Room Ordering",
|
||||||
description: "Sort rooms and subspaces by recent activity, optionally showing mentioned or unread ones first.",
|
description: "Sort rooms 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),
|
||||||
|
|
@ -268,6 +268,7 @@ class SettingsSectionsController
|
||||||
.unreadFirst => "Mentioned, then unread",
|
.unreadFirst => "Mentioned, then unread",
|
||||||
.mentionedFirst => "Mentioned only",
|
.mentionedFirst => "Mentioned only",
|
||||||
.recentActivity => "Recent activity only",
|
.recentActivity => "Recent activity only",
|
||||||
|
.spaceOrder => "Space order",
|
||||||
},
|
},
|
||||||
onChanged: (value) => ref
|
onChanged: (value) => ref
|
||||||
.read(SettingsController.provider.notifier)
|
.read(SettingsController.provider.notifier)
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ import "package:nexus/controllers/space_edges.dart";
|
||||||
import "package:nexus/models/room.dart";
|
import "package:nexus/models/room.dart";
|
||||||
import "package:nexus/models/room_metadata.dart";
|
import "package:nexus/models/room_metadata.dart";
|
||||||
import "package:nexus/models/space.dart";
|
import "package:nexus/models/space.dart";
|
||||||
|
import "package:nexus/models/space_edge.dart";
|
||||||
import "package:nexus/models/subspace.dart";
|
import "package:nexus/models/subspace.dart";
|
||||||
|
|
||||||
class SpacesController extends AsyncNotifier<IList<Space>> {
|
class SpacesController extends AsyncNotifier<IList<Space>> {
|
||||||
|
|
@ -20,9 +21,27 @@ class SpacesController extends AsyncNotifier<IList<Space>> {
|
||||||
final spaceEdges = ref.watch(SpaceEdgesController.provider);
|
final spaceEdges = ref.watch(SpaceEdgesController.provider);
|
||||||
final accountData = ref.watch(AccountDataController.provider);
|
final accountData = ref.watch(AccountDataController.provider);
|
||||||
|
|
||||||
|
bool hasValidOrder(SpaceEdge edge) =>
|
||||||
|
edge.order.isNotEmpty &&
|
||||||
|
edge.order.length <= 50 &&
|
||||||
|
edge.order.codeUnits.every((unit) => unit >= 0x20 && unit <= 0x7E);
|
||||||
|
|
||||||
|
int compareEdges(SpaceEdge a, SpaceEdge b) =>
|
||||||
|
switch ((hasValidOrder(a), hasValidOrder(b))) {
|
||||||
|
(true, false) => -1,
|
||||||
|
(false, true) => 1,
|
||||||
|
(true, true) when a.order != b.order => a.order.compareTo(b.order),
|
||||||
|
_ => switch (a.childEventRowId.compareTo(b.childEventRowId)) {
|
||||||
|
0 => a.childId.compareTo(b.childId),
|
||||||
|
final byRowId => byRowId,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
final childrenById = spaceEdges.map(
|
final childrenById = spaceEdges.map(
|
||||||
(spaceId, edges) =>
|
(spaceId, edges) => .new(
|
||||||
MapEntry(spaceId, edges.map((edge) => edge.childId).toList()),
|
spaceId,
|
||||||
|
edges.sort(compareEdges).map((edge) => edge.childId).toList(),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
/// Everything under [spaceId], however deep, skipping any already
|
/// Everything under [spaceId], however deep, skipping any already
|
||||||
|
|
@ -70,6 +89,10 @@ class SpacesController extends AsyncNotifier<IList<Space>> {
|
||||||
)
|
)
|
||||||
.nonNulls,
|
.nonNulls,
|
||||||
),
|
),
|
||||||
|
childrenIndex: directChildrenIds
|
||||||
|
.where(rooms.containsKey)
|
||||||
|
.takeWhile(childrenById.containsKey)
|
||||||
|
.length,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -140,7 +163,7 @@ class SpacesController extends AsyncNotifier<IList<Space>> {
|
||||||
);
|
);
|
||||||
|
|
||||||
int priority(Room room) => switch ((roomOrder, room.metadata)) {
|
int priority(Room room) => switch ((roomOrder, room.metadata)) {
|
||||||
(.recentActivity, _) => 0,
|
(.recentActivity || .spaceOrder, _) => 0,
|
||||||
(_, RoomMetadata(unreadHighlights: > 0)) => 0,
|
(_, RoomMetadata(unreadHighlights: > 0)) => 0,
|
||||||
(.mentionedFirst, _) => 1,
|
(.mentionedFirst, _) => 1,
|
||||||
(.unreadFirst, RoomMetadata(unreadNotifications: > 0)) ||
|
(.unreadFirst, RoomMetadata(unreadNotifications: > 0)) ||
|
||||||
|
|
@ -150,46 +173,32 @@ 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
|
||||||
int compare(
|
IList<Room> sortRooms(IList<Room> rooms) => rooms.sort(
|
||||||
({int priority, DateTime activity}) a,
|
(a, b) => switch (priority(a).compareTo(priority(b))) {
|
||||||
({int priority, DateTime activity}) b,
|
0 => activity(b).compareTo(activity(a)),
|
||||||
) => 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> sortRooms(IList<Room> rooms) =>
|
IList<Room> orderRooms(Space space, IList<Room> rooms) =>
|
||||||
rooms.sort((a, b) => compare(roomKey(a), roomKey(b)));
|
roomOrder == .spaceOrder && space.room != null
|
||||||
|
? rooms
|
||||||
|
: sortRooms(rooms);
|
||||||
|
|
||||||
return allSpaces.map((space) {
|
return .new(
|
||||||
final subSpaces = space.subSpaces
|
allSpaces.map(
|
||||||
.map(
|
(space) => space.copyWith(
|
||||||
(subspace) =>
|
children: orderRooms(space, space.children),
|
||||||
subspace.copyWith(children: sortRooms(subspace.children)),
|
subSpaces: .new(
|
||||||
)
|
space.subSpaces.map(
|
||||||
.toIList()
|
(subspace) => subspace.copyWith(
|
||||||
.sort((a, b) => compare(groupKey(a.children), groupKey(b.children)));
|
children: orderRooms(space, subspace.children),
|
||||||
|
),
|
||||||
final childrenKey = groupKey(space.children);
|
),
|
||||||
return space.copyWith(
|
),
|
||||||
children: sortRooms(space.children),
|
),
|
||||||
subSpaces: subSpaces,
|
),
|
||||||
childrenIndex: subSpaces
|
|
||||||
.where(
|
|
||||||
(subspace) =>
|
|
||||||
compare(groupKey(subspace.children), childrenKey) < 0,
|
|
||||||
)
|
|
||||||
.length,
|
|
||||||
);
|
);
|
||||||
}).toIList();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static final provider = AsyncNotifierProvider<SpacesController, IList<Space>>(
|
static final provider = AsyncNotifierProvider<SpacesController, IList<Space>>(
|
||||||
|
|
|
||||||
|
|
@ -1 +1 @@
|
||||||
enum RoomOrder { unreadFirst, mentionedFirst, recentActivity }
|
enum RoomOrder { unreadFirst, mentionedFirst, recentActivity, spaceOrder }
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,8 @@ part "space_edge.g.dart";
|
||||||
@JsonSerializable()
|
@JsonSerializable()
|
||||||
class const SpaceEdge({
|
class const SpaceEdge({
|
||||||
required final String childId,
|
required final String childId,
|
||||||
|
@JsonKey(name: "child_event_rowid") final int childEventRowId = 0,
|
||||||
|
final String order = "",
|
||||||
final bool suggested = false,
|
final bool suggested = false,
|
||||||
}) with _$SpaceEdge {
|
}) with _$SpaceEdge {
|
||||||
Map<String, Object?> toJson() => _$SpaceEdgeToJson(this);
|
Map<String, Object?> toJson() => _$SpaceEdgeToJson(this);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue