diff --git a/lib/src/room.dart b/lib/src/room.dart index 5f0b1b65..8db6686a 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -983,7 +983,8 @@ class Room { /// Request more previous events from the server. [historyCount] defines how much events should /// be received maximum. When the request is answered, [onHistoryReceived] will be triggered **before** /// the historical events will be published in the onEvent stream. - Future requestHistory( + /// Returns the actual count of received timeline events. + Future requestHistory( {int historyCount = defaultHistoryCount, void Function()? onHistoryReceived}) async { final prev_batch = this.prev_batch; @@ -1044,6 +1045,8 @@ class Room { } else { await loadFn(); } + + return resp.chunk?.length ?? 0; } /// Sets this room as a direct chat for this user if not already. @@ -1131,12 +1134,13 @@ class Room { /// Creates a timeline from the store. Returns a [Timeline] object. If you /// just want to update the whole timeline on every change, use the [onUpdate] /// callback. For updating only the parts that have changed, use the - /// [onChange], [onRemove] and the [onInsert] callbacks. + /// [onChange], [onRemove], [onInsert] and the [onHistoryReceived] callbacks. Future getTimeline({ void Function(int index)? onChange, void Function(int index)? onRemove, void Function(int insertID)? onInsert, void Function()? onUpdate, + void Function(int count)? onHistoryReceived, }) async { await postLoad(); var events; @@ -1166,6 +1170,7 @@ class Room { onRemove: onRemove, onInsert: onInsert, onUpdate: onUpdate, + onHistoryReceived: onHistoryReceived, ); if (client.database == null) { await requestHistory(historyCount: 10); diff --git a/lib/src/timeline.dart b/lib/src/timeline.dart index 4232dcf4..4fc20b6c 100644 --- a/lib/src/timeline.dart +++ b/lib/src/timeline.dart @@ -36,6 +36,7 @@ class Timeline { final void Function(int index)? onChange; final void Function(int index)? onInsert; final void Function(int index)? onRemove; + final void Function(int count)? onHistoryReceived; StreamSubscription? sub; StreamSubscription? roomSub; @@ -86,19 +87,16 @@ class Timeline { ); if (eventsFromStore != null && eventsFromStore.isNotEmpty) { events.addAll(eventsFromStore); - final startIndex = events.length - eventsFromStore.length; - final endIndex = events.length; - for (var i = startIndex; i < endIndex; i++) { - onInsert?.call(i); - } + onHistoryReceived?.call(eventsFromStore.length); } else { Logs().v('No more events found in the store. Request from server...'); - await room.requestHistory( + final count = await room.requestHistory( historyCount: historyCount, onHistoryReceived: () { _collectHistoryUpdates = true; }, ); + onHistoryReceived?.call(count); } } finally { _collectHistoryUpdates = false; @@ -114,6 +112,7 @@ class Timeline { this.onChange, this.onInsert, this.onRemove, + this.onHistoryReceived, }) : events = events ?? [] { sub = room.client.onEvent.stream.listen(_handleEventUpdate); @@ -331,11 +330,10 @@ class Timeline { } else { index = events.firstIndexWhereNotError; events.insert(index, newEvent); + onInsert?.call(index); } addAggregatedEvent(newEvent); - - onInsert?.call(index); } } if (update && !_collectHistoryUpdates) {