parent
47645ea327
commit
7f7bdbf525
1 changed files with 1477 additions and 577 deletions
37
lib/controllers/thread_list.dart
Normal file
37
lib/controllers/thread_list.dart
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import "package:flutter_riverpod/flutter_riverpod.dart";
|
||||
import "package:nexus/controllers/client.dart";
|
||||
import "package:nexus/controllers/rooms.dart";
|
||||
|
||||
class ThreadListController(final String roomId) extends AsyncNotifier<String?> {
|
||||
@override
|
||||
Future<String?> build() => fetch(null);
|
||||
|
||||
Future<String?> fetch(String? since) async {
|
||||
final threads = await ref
|
||||
.read(ClientController.provider.notifier)
|
||||
.getThreads(.new(roomId: roomId, since: since));
|
||||
ref
|
||||
.read(RoomsController.provider.notifier)
|
||||
.storeEvents(threads.events.addAll(threads.relatedEvents));
|
||||
|
||||
return threads.nextBatch;
|
||||
}
|
||||
|
||||
Future<void> loadMore() async {
|
||||
final since = await future;
|
||||
if (state.isLoading || since == null) return;
|
||||
|
||||
state = .loading();
|
||||
try {
|
||||
state = .data(await fetch(since));
|
||||
} catch (_) {
|
||||
state = .data(since);
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
static final provider = AsyncNotifierProvider.family
|
||||
.autoDispose<ThreadListController, String?, String>(
|
||||
ThreadListController.new,
|
||||
);
|
||||
}
|
||||
Loading…
Reference in a new issue