fix: timeline pagination

This commit is contained in:
Erwan Leboucher 2026-08-10 09:02:17 +02:00
commit 2f3bd7a8ac
No known key found for this signature in database
GPG key ID: E191CC9EB650480E

View file

@ -17,27 +17,30 @@ import "package:nexus/models/room.dart";
class RoomChatController extends AsyncNotifier<IList<Event>?> {
final String roomId;
Future<bool>? _loadingOlder;
RoomChatController(this.roomId);
@override
Future<IList<Event>?> build() async {
final client = ref.watch(ClientController.provider.notifier);
final room = ref.watch(
final initialRoom = ref.watch(
RoomsController.provider.select((rooms) => rooms[roomId]),
);
if (room == null) return null;
if (initialRoom == null) return null;
Room room = initialRoom;
if (!room.hasFetchedState) {
final state = await client.getRoomState(.new(roomId: roomId));
await ref.read(RoomsController.provider.notifier).addState(roomId, state);
room = ref.read(RoomsController.provider)[roomId] ?? room;
}
// While there are under 20 events, try to load more
// until there's no more or the conditions are met.
// Load one more page when the initial timeline is short.
if (room.hasMore && room.timeline.length < 20) {
loadOlder();
await loadOlder();
room = ref.read(RoomsController.provider)[roomId] ?? room;
}
return room.timeline
@ -74,18 +77,22 @@ class RoomChatController extends AsyncNotifier<IList<Event>?> {
),
);
Future<bool> loadOlder() async {
final timelineKeys = ref
.read(RoomsController.provider.select((value) => value[roomId]))
?.timeline
.keys;
Future<bool> loadOlder() => _loadingOlder ??= _loadOlder().whenComplete(() {
_loadingOlder = null;
});
Future<bool> _loadOlder() async {
final room = ref.read(RoomsController.provider)[roomId];
if (room == null || !room.hasMore) return false;
final timelineKeys = room.timeline.keys;
final response = await ref
.watch(ClientController.provider.notifier)
.paginate(
.new(
roomId: roomId,
maxTimelineId: timelineKeys?.isNotEmpty == true
? timelineKeys?.reduce(min)
maxTimelineId: timelineKeys.isNotEmpty
? timelineKeys.reduce(min)
: null,
),
);