refactor: Implement on history received in timeline

In order to make delta updates
for GUIs displaying the
timeline events easier, the
onHistoryReceived callback
has been introduced.
This commit is contained in:
Christian Pauly 2022-02-03 09:38:39 +01:00
parent e30c0f7fa2
commit 01d13e7e2c
2 changed files with 13 additions and 10 deletions

View File

@ -983,7 +983,8 @@ class Room {
/// Request more previous events from the server. [historyCount] defines how much events should /// 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** /// be received maximum. When the request is answered, [onHistoryReceived] will be triggered **before**
/// the historical events will be published in the onEvent stream. /// the historical events will be published in the onEvent stream.
Future<void> requestHistory( /// Returns the actual count of received timeline events.
Future<int> requestHistory(
{int historyCount = defaultHistoryCount, {int historyCount = defaultHistoryCount,
void Function()? onHistoryReceived}) async { void Function()? onHistoryReceived}) async {
final prev_batch = this.prev_batch; final prev_batch = this.prev_batch;
@ -1044,6 +1045,8 @@ class Room {
} else { } else {
await loadFn(); await loadFn();
} }
return resp.chunk?.length ?? 0;
} }
/// Sets this room as a direct chat for this user if not already. /// 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 /// 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] /// just want to update the whole timeline on every change, use the [onUpdate]
/// callback. For updating only the parts that have changed, use the /// 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<Timeline> getTimeline({ Future<Timeline> getTimeline({
void Function(int index)? onChange, void Function(int index)? onChange,
void Function(int index)? onRemove, void Function(int index)? onRemove,
void Function(int insertID)? onInsert, void Function(int insertID)? onInsert,
void Function()? onUpdate, void Function()? onUpdate,
void Function(int count)? onHistoryReceived,
}) async { }) async {
await postLoad(); await postLoad();
var events; var events;
@ -1166,6 +1170,7 @@ class Room {
onRemove: onRemove, onRemove: onRemove,
onInsert: onInsert, onInsert: onInsert,
onUpdate: onUpdate, onUpdate: onUpdate,
onHistoryReceived: onHistoryReceived,
); );
if (client.database == null) { if (client.database == null) {
await requestHistory(historyCount: 10); await requestHistory(historyCount: 10);

View File

@ -36,6 +36,7 @@ class Timeline {
final void Function(int index)? onChange; final void Function(int index)? onChange;
final void Function(int index)? onInsert; final void Function(int index)? onInsert;
final void Function(int index)? onRemove; final void Function(int index)? onRemove;
final void Function(int count)? onHistoryReceived;
StreamSubscription<EventUpdate>? sub; StreamSubscription<EventUpdate>? sub;
StreamSubscription<SyncUpdate>? roomSub; StreamSubscription<SyncUpdate>? roomSub;
@ -86,19 +87,16 @@ class Timeline {
); );
if (eventsFromStore != null && eventsFromStore.isNotEmpty) { if (eventsFromStore != null && eventsFromStore.isNotEmpty) {
events.addAll(eventsFromStore); events.addAll(eventsFromStore);
final startIndex = events.length - eventsFromStore.length; onHistoryReceived?.call(eventsFromStore.length);
final endIndex = events.length;
for (var i = startIndex; i < endIndex; i++) {
onInsert?.call(i);
}
} else { } else {
Logs().v('No more events found in the store. Request from server...'); Logs().v('No more events found in the store. Request from server...');
await room.requestHistory( final count = await room.requestHistory(
historyCount: historyCount, historyCount: historyCount,
onHistoryReceived: () { onHistoryReceived: () {
_collectHistoryUpdates = true; _collectHistoryUpdates = true;
}, },
); );
onHistoryReceived?.call(count);
} }
} finally { } finally {
_collectHistoryUpdates = false; _collectHistoryUpdates = false;
@ -114,6 +112,7 @@ class Timeline {
this.onChange, this.onChange,
this.onInsert, this.onInsert,
this.onRemove, this.onRemove,
this.onHistoryReceived,
}) : events = events ?? [] { }) : events = events ?? [] {
sub = room.client.onEvent.stream.listen(_handleEventUpdate); sub = room.client.onEvent.stream.listen(_handleEventUpdate);
@ -331,11 +330,10 @@ class Timeline {
} else { } else {
index = events.firstIndexWhereNotError; index = events.firstIndexWhereNotError;
events.insert(index, newEvent); events.insert(index, newEvent);
onInsert?.call(index);
} }
addAggregatedEvent(newEvent); addAggregatedEvent(newEvent);
onInsert?.call(index);
} }
} }
if (update && !_collectHistoryUpdates) { if (update && !_collectHistoryUpdates) {