refactor: Use prevBatch from server for pagination in event search
When using the searchEvent() method it was impossible to paginate to the next chunk when one chunk had 0 returned events. This fixes it by returning the prevBatch and makes it possible to insert the prevBatch again. sinceEventId is still working but now deprecated.
This commit is contained in:
parent
efa2d6f566
commit
00616f02ac
|
|
@ -546,17 +546,39 @@ class Timeline {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deprecated('Use [startSearch] instead.')
|
||||||
|
Stream<List<Event>> searchEvent({
|
||||||
|
String? searchTerm,
|
||||||
|
int requestHistoryCount = 100,
|
||||||
|
int maxHistoryRequests = 10,
|
||||||
|
String? sinceEventId,
|
||||||
|
int? limit,
|
||||||
|
bool Function(Event)? searchFunc,
|
||||||
|
}) =>
|
||||||
|
startSearch(
|
||||||
|
searchTerm: searchTerm,
|
||||||
|
requestHistoryCount: requestHistoryCount,
|
||||||
|
maxHistoryRequests: maxHistoryRequests,
|
||||||
|
// ignore: deprecated_member_use_from_same_package
|
||||||
|
sinceEventId: sinceEventId,
|
||||||
|
limit: limit,
|
||||||
|
searchFunc: searchFunc,
|
||||||
|
).map((result) => result.$1);
|
||||||
|
|
||||||
/// Searches [searchTerm] in this timeline. It first searches in the
|
/// Searches [searchTerm] in this timeline. It first searches in the
|
||||||
/// cache, then in the database and then on the server. The search can
|
/// cache, then in the database and then on the server. The search can
|
||||||
/// take a while, which is why this returns a stream so the already found
|
/// take a while, which is why this returns a stream so the already found
|
||||||
/// events can already be displayed.
|
/// events can already be displayed.
|
||||||
/// Override the [searchFunc] if you need another search. This will then
|
/// Override the [searchFunc] if you need another search. This will then
|
||||||
/// ignore [searchTerm].
|
/// ignore [searchTerm].
|
||||||
Stream<List<Event>> searchEvent({
|
/// Returns the List of Events and the next prevBatch at the end of the
|
||||||
|
/// search.
|
||||||
|
Stream<(List<Event>, String?)> startSearch({
|
||||||
String? searchTerm,
|
String? searchTerm,
|
||||||
int requestHistoryCount = 100,
|
int requestHistoryCount = 100,
|
||||||
int maxHistoryRequests = 10,
|
int maxHistoryRequests = 10,
|
||||||
String? sinceEventId,
|
String? prevBatch,
|
||||||
|
@Deprecated('Use [prevBatch] instead.') String? sinceEventId,
|
||||||
int? limit,
|
int? limit,
|
||||||
bool Function(Event)? searchFunc,
|
bool Function(Event)? searchFunc,
|
||||||
}) async* {
|
}) async* {
|
||||||
|
|
@ -569,7 +591,7 @@ class Timeline {
|
||||||
// Search locally
|
// Search locally
|
||||||
for (final event in events) {
|
for (final event in events) {
|
||||||
if (searchFunc(event)) {
|
if (searchFunc(event)) {
|
||||||
yield found..add(event);
|
yield (found..add(event), null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -586,14 +608,14 @@ class Timeline {
|
||||||
start += eventsFromStore.length;
|
start += eventsFromStore.length;
|
||||||
for (final event in eventsFromStore) {
|
for (final event in eventsFromStore) {
|
||||||
if (searchFunc(event)) {
|
if (searchFunc(event)) {
|
||||||
yield found..add(event);
|
yield (found..add(event), null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Search on the server
|
// Search on the server
|
||||||
var prevBatch = room.prev_batch;
|
prevBatch ??= room.prev_batch;
|
||||||
if (sinceEventId != null) {
|
if (sinceEventId != null) {
|
||||||
prevBatch =
|
prevBatch =
|
||||||
(await room.client.getEventContext(room.id, sinceEventId)).end;
|
(await room.client.getEventContext(room.id, sinceEventId)).end;
|
||||||
|
|
@ -622,7 +644,7 @@ class Timeline {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (searchFunc(event)) {
|
if (searchFunc(event)) {
|
||||||
yield found..add(event);
|
yield (found..add(event), resp.end);
|
||||||
if (limit != null && found.length >= limit) break;
|
if (limit != null && found.length >= limit) break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue