refactor and improve room list

This commit is contained in:
Henry Hiles 2026-10-03 21:58:01 -04:00
commit e480e6193b
Signed by: Henry-Hiles
SSH key fingerprint: SHA256:VKQUdS31Q90KvX7EkKMHMBpUspcmItAh86a+v7PGiIs
2 changed files with 251 additions and 254 deletions

View file

@ -0,0 +1,8 @@
import "package:fast_immutable_collections/fast_immutable_collections.dart";
import "package:nexus/models/room.dart";
import "package:nexus/models/space.dart";
extension GetAllChildRooms on Space {
IList<Room> get allChildRooms =>
children.addAll(subSpaces.expand((s) => s.children));
}

View file

@ -1,6 +1,9 @@
import "dart:math";
import "package:hooks_riverpod/hooks_riverpod.dart"; import "package:hooks_riverpod/hooks_riverpod.dart";
import "package:material_ui/material_ui.dart"; import "package:material_ui/material_ui.dart";
import "package:navigation_rail_m3e/navigation_rail_m3e.dart"; import "package:navigation_rail_m3e/navigation_rail_m3e.dart";
import "package:nexus/helpers/extensions/get_all_child_rooms.dart";
import "package:nexus/models/nav_page.dart"; import "package:nexus/models/nav_page.dart";
import "package:nexus/widgets/avatar_or_hash.dart"; import "package:nexus/widgets/avatar_or_hash.dart";
import "package:nexus/widgets/divider_widget.dart"; import "package:nexus/widgets/divider_widget.dart";
@ -17,9 +20,26 @@ import "package:fast_immutable_collections/fast_immutable_collections.dart";
// Needed for navigation_rail_m3e (#65). // Needed for navigation_rail_m3e (#65).
import "package:flutter/material.dart" as old_mat; import "package:flutter/material.dart" as old_mat;
class const RoomList({super.key}) List<NavigationRailM3EDestination> roomDestinations(
extends HookConsumerWidget IList<Room> rooms, {
implements NavPage { required bool isDm,
}) => [
for (final room in rooms)
.new(
label: room.metadata?.name ?? "Unnamed Room",
badgeCount: switch (room.metadata?.unreadNotifications) {
0 || null => room.metadata?.unreadMessages == 0 ? null : 0,
int unread => unread,
},
icon: AvatarOrHash(
room.metadata?.avatar,
room.metadata?.name ?? "Unnamed Room",
fallback: isDm ? null : const Icon(Icons.numbers),
),
),
];
class const RoomList({super.key}) extends ConsumerWidget implements NavPage {
@override @override
String get title => "Rooms"; String get title => "Rooms";
@ -32,21 +52,8 @@ class const RoomList({super.key})
@override @override
Widget build(BuildContext context, WidgetRef ref) { Widget build(BuildContext context, WidgetRef ref) {
final selectedSpaceProvider = KeyController.provider(
KeyController.spaceKey,
);
final selectedSpaceId = ref.watch(selectedSpaceProvider).requireValue;
final selectedSpaceIdNotifier = ref.watch(selectedSpaceProvider.notifier);
final selectedRoomController = KeyController.provider(
KeyController.roomKey,
);
final selectedRoomId = ref.watch(selectedRoomController).requireValue;
final selectedRoomIdNotifier = ref.watch(selectedRoomController.notifier);
final spacesAsync = ref.watch(SpacesController.provider); final spacesAsync = ref.watch(SpacesController.provider);
final spaces = spacesAsync.value; if (spacesAsync.value == null) {
if (spaces == null) {
return switch (spacesAsync) { return switch (spacesAsync) {
AsyncError(:final error, :final stackTrace) => ErrorDialog( AsyncError(:final error, :final stackTrace) => ErrorDialog(
error, error,
@ -56,48 +63,27 @@ class const RoomList({super.key})
}; };
} }
final indexOfSelected = spaces.indexWhere( return const Row(
(space) => space.id == selectedSpaceId, children: [
SpacesRail(),
Expanded(child: RoomsPane()),
],
);
}
}
class const SpacesRail({super.key}) extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final spaces = ref.watch(
SpacesController.provider.select((async) => async.requireValue),
);
final selectedSpaceId = ref.watch(
KeyController.provider(KeyController.spaceKey)
.select((async) => async.requireValue),
); );
final selectedIndex = indexOfSelected == -1 ? 0 : indexOfSelected; return MaterialUiCompatibilityBridge(
final selectedSpace =
spaces.firstWhereOrNull((space) => space.id == selectedSpaceId) ??
spaces.first;
final indexOfSelectedRoom = selectedSpace.children
.addAll(
selectedSpace.subSpaces.map((element) => element.children).flattened,
)
.indexWhere((room) => room.metadata?.id == selectedRoomId);
final selectedRoomIndex = indexOfSelectedRoom == -1
? null
: indexOfSelectedRoom;
List<NavigationRailM3EDestination> roomsToDestinations(IList<Room> rooms) =>
rooms
.map(
(room) => NavigationRailM3EDestination(
label: room.metadata?.name ?? "Unnamed Room",
badgeCount: switch (room.metadata?.unreadNotifications) {
0 || null => room.metadata?.unreadMessages == 0 ? null : 0,
int unread => unread,
},
icon: AvatarOrHash(
room.metadata?.avatar,
room.metadata?.name ?? "Unnamed Room",
fallback: selectedSpaceId == "dms"
? null
: Icon(Icons.numbers),
),
),
)
.toList();
return Row(
children: [
MaterialUiCompatibilityBridge(
child: Builder( child: Builder(
builder: (context) => old_mat.Theme( builder: (context) => old_mat.Theme(
data: old_mat.Theme.of(context).copyWith( data: old_mat.Theme.of(context).copyWith(
@ -110,69 +96,56 @@ class const RoomList({super.key})
), ),
child: Container( child: Container(
color: NavigationRailTokensAdapter(context).containerColor, color: NavigationRailTokensAdapter(context).containerColor,
padding: EdgeInsets.only(top: 16), padding: .only(top: 16),
child: NavigationRailM3E( child: NavigationRailM3E(
type: .alwaysCollapse, type: .alwaysCollapse,
labelBehavior: .alwaysHide, labelBehavior: .alwaysHide,
scrollable: true, scrollable: true,
onDestinationSelected: (value) { onDestinationSelected: (value) {
selectedSpaceIdNotifier.set(spaces[value].id); ref
selectedRoomIdNotifier.set( .read(
spaces[value].children.firstOrNull?.metadata?.id, KeyController.provider(KeyController.spaceKey).notifier,
); )
.set(spaces[value].id);
ref
.read(
KeyController.provider(KeyController.roomKey).notifier,
)
.set(spaces[value].children.firstOrNull?.metadata?.id);
}, },
sections: [ sections: [
.new( .new(
destinations: spaces destinations: spaces.map((space) {
.map( final notifications = space.allChildRooms.fold(
(space) => NavigationRailM3EDestination(
badgeCount: switch (space.children
.addAll(
space.subSpaces
.map((element) => element.children)
.flattened,
)
.fold(
0,
(previousValue, room) =>
previousValue +
(room.metadata?.unreadNotifications ??
0),
)) {
0 =>
space.children
.addAll(
space.subSpaces
.map(
(element) => element.children,
)
.flattened,
)
.any(
(room) =>
room.metadata?.unreadMessages !=
0, 0,
(sum, room) =>
sum + (room.metadata?.unreadNotifications ?? 0),
);
return NavigationRailM3EDestination(
badgeCount: notifications > 0
? notifications
: space.allChildRooms.any(
(room) => room.metadata?.unreadMessages != 0,
) )
? 0 ? 0
: null, : null,
int badgeCount => badgeCount,
},
short: true, short: true,
icon: AvatarOrHash( icon: AvatarOrHash(
dimension: 28, dimension: 28,
space.room?.metadata?.avatar, space.room?.metadata?.avatar,
fallback: space.icon == null fallback: space.icon == null ? null : Icon(space.icon),
? null
: Icon(space.icon),
space.title, space.title,
), ),
label: space.title, label: space.title,
), );
) }).toList(),
.toList(),
), ),
], ],
selectedIndex: selectedIndex, selectedIndex: max(
0,
spaces.indexWhere((space) => space.id == selectedSpaceId),
),
trailingAtBottom: true, trailingAtBottom: true,
trailing: Padding( trailing: Padding(
padding: .only(top: 8), padding: .only(top: 8),
@ -185,12 +158,12 @@ class const RoomList({super.key})
context: context, context: context,
builder: (_) => JoinDialog(ref), builder: (_) => JoinDialog(ref),
), ),
child: ListTile( child: const ListTile(
title: Text("Join an existing room (or space)"), title: Text("Join an existing room (or space)"),
leading: Icon(Icons.numbers), leading: Icon(Icons.numbers),
), ),
), ),
PopupMenuItem( const PopupMenuItem(
onTap: null, onTap: null,
child: ListTile( child: ListTile(
title: Text("Create a new room"), title: Text("Create a new room"),
@ -198,9 +171,9 @@ class const RoomList({super.key})
), ),
), ),
], ],
icon: Icon(Icons.add), icon: const Icon(Icons.add),
), ),
IconButton( const IconButton(
tooltip: "Explore other rooms", tooltip: "Explore other rooms",
onPressed: null, onPressed: null,
icon: Icon(Icons.explore), icon: Icon(Icons.explore),
@ -211,7 +184,7 @@ class const RoomList({super.key})
context: context, context: context,
builder: (_) => SettingsPage(), builder: (_) => SettingsPage(),
), ),
icon: Icon(Icons.settings), icon: const Icon(Icons.settings),
), ),
], ],
), ),
@ -220,10 +193,33 @@ class const RoomList({super.key})
), ),
), ),
), ),
), );
}
}
Expanded( class const RoomsPane({super.key}) extends ConsumerWidget {
child: Scaffold( @override
Widget build(BuildContext context, WidgetRef ref) {
final selectedSpaceId = ref.watch(
KeyController.provider(KeyController.spaceKey)
.select((async) => async.requireValue),
);
final selectedRoomId = ref.watch(
KeyController.provider(KeyController.roomKey)
.select((async) => async.requireValue),
);
final selectedSpace = ref.watch(
SpacesController.provider.select((async) {
final spaces = async.requireValue;
return spaces.firstWhereOrNull(
(space) => space.id == selectedSpaceId,
) ??
spaces.first;
}),
);
final isDm = selectedSpace.id == "dms";
return Scaffold(
backgroundColor: Colors.transparent, backgroundColor: Colors.transparent,
appBar: AppBar( appBar: AppBar(
leading: AvatarOrHash( leading: AvatarOrHash(
@ -231,20 +227,12 @@ class const RoomList({super.key})
fallback: selectedSpace.icon == null fallback: selectedSpace.icon == null
? null ? null
: Icon(selectedSpace.icon), : Icon(selectedSpace.icon),
selectedSpace.title, selectedSpace.title,
), ),
title: Text(selectedSpace.title, overflow: .ellipsis), title: Text(selectedSpace.title, overflow: .ellipsis),
backgroundColor: Colors.transparent, backgroundColor: Colors.transparent,
actions: [ actions: [
RoomMenu( RoomMenu(selectedSpace.room, children: selectedSpace.allChildRooms),
selectedSpace.room,
children: selectedSpace.children.addAll(
selectedSpace.subSpaces
.map((element) => element.children)
.flattened,
),
),
], ],
), ),
body: MaterialUiCompatibilityBridge( body: MaterialUiCompatibilityBridge(
@ -263,53 +251,57 @@ class const RoomList({super.key})
scrollable: true, scrollable: true,
background: Colors.transparent, background: Colors.transparent,
type: .alwaysExpand, type: .alwaysExpand,
selectedIndex: selectedRoomIndex ?? 0, selectedIndex: max(
0,
selectedSpace.allChildRooms.indexWhere(
(room) => room.metadata?.id == selectedRoomId,
),
),
sections: [ sections: [
.new( .new(
header: selectedSpace.room == null header: selectedSpace.room == null
? null ? null
: DividerWidget(Text("Rooms")), : const DividerWidget(Text("Rooms")),
destinations: roomsToDestinations( destinations: roomDestinations(
selectedSpace.children, selectedSpace.children,
isDm: isDm,
), ),
), ),
for (final subSpace in selectedSpace.subSpaces) for (final subSpace in selectedSpace.subSpaces)
.new( .new(
header: DividerWidget( header: DividerWidget(
Row( Row(
mainAxisSize: MainAxisSize.min, mainAxisSize: .min,
spacing: 8, spacing: 8,
children: [ children: [
if (subSpace.room.metadata?.avatar != null) if (subSpace.room.metadata?.avatar != null)
AvatarOrHash( AvatarOrHash(
subSpace.room.metadata?.avatar, subSpace.room.metadata?.avatar,
subSpace.room.metadata?.name ?? subSpace.room.metadata?.name ?? "Unnamed Room",
"Unnamed Room",
dimension: 16, dimension: 16,
), ),
Flexible( Flexible(
child: Text( child: Text(
subSpace.room.metadata?.name ?? subSpace.room.metadata?.name ?? "Unnamed Space",
"Unnamed Space",
maxLines: 1, maxLines: 1,
overflow: TextOverflow.ellipsis, overflow: .ellipsis,
), ),
), ),
], ],
), ),
), ),
destinations: roomsToDestinations(subSpace.children), destinations: roomDestinations(
subSpace.children,
isDm: isDm,
),
), ),
], ],
onDestinationSelected: (value) { onDestinationSelected: (value) {
final children = selectedSpace.children.addAll( ref
selectedSpace.subSpaces .read(
.map((element) => element.children) KeyController.provider(KeyController.roomKey).notifier,
.flattened, )
); .set(selectedSpace.allChildRooms[value].metadata?.id);
selectedRoomIdNotifier.set(
children[value].metadata?.id, //
);
if (Navigator.of(context).canPop()) { if (Navigator.of(context).canPop()) {
Navigator.of(context).pop(); Navigator.of(context).pop();
} }
@ -318,9 +310,6 @@ class const RoomList({super.key})
), ),
), ),
), ),
),
),
],
); );
} }
} }