Refactor notifications processing to use max 3 calls on initial load and streamlined older loading

codemagic-setup
Hank Grabowski 2023-11-16 17:37:22 -05:00
rodzic 1787bb4f7e
commit 910e6d2c4e
1 zmienionych plików z 76 dodań i 40 usunięć

Wyświetl plik

@ -71,7 +71,9 @@ class NotificationsManager extends ChangeNotifier {
.canUseFeature(RelaticaFeatures.usingActualFollowRequests);
var hasMore = true;
var first = true;
while (hasMore) {
const maxCalls = 3;
var count = 0;
while (hasMore && count < maxCalls) {
final result =
first ? await pm.initialize(itemsPerQuery) : await pm.nextFromEnd();
@ -85,6 +87,7 @@ class NotificationsManager extends ChangeNotifier {
!useActualRequests || n.type != NotificationType.follow_request)
.forEach(notificationsFromRefresh.add);
hasMore = response.next != null;
count++;
}
// filter out connection requests if going to use the real service for that when doing the query
@ -111,7 +114,10 @@ class NotificationsManager extends ChangeNotifier {
: [
PagedResponse(
<String>[],
previous: PagingData(minId: highestId),
previous: PagingData(
minId: highestId,
limit: itemsPerQuery,
),
)
],
);
@ -130,46 +136,15 @@ class NotificationsManager extends ChangeNotifier {
FutureResult<List<UserNotification>, ExecError> loadOlderNotifications(
{bool withListenerNotification = true}) async {
final (lowestId, _) =
read.isNotEmpty ? calcLowHigh(read) : calcLowHigh(unread);
final pm = _buildPageManager(
profile,
true,
initialPages: read.isEmpty && unread.isEmpty
? []
: [
PagedResponse(
<String>[],
next: PagingData(maxId: lowestId),
)
],
);
final notifications = <UserNotification>[];
if (read.isEmpty) {
var hasReadNotification = false;
var hasMorePages = false;
do {
await (notifications.isEmpty
? pm.initialize(itemsPerQuery)
: pm.nextFromEnd())
.match(onSuccess: (r) {
notifications.addAll(r.data);
hasMorePages = r.next != null;
hasReadNotification = r.data.map((e) => e.dismissed).firstWhere(
(t) => t == true,
orElse: () => false,
);
}, onError: (e) {
hasMorePages = false;
print('Error getting older notifications: $e');
});
} while (!hasReadNotification && hasMorePages);
} else {
await pm.nextFromEnd().withResult((r) => notifications.addAll(r.data));
if (unread.isNotEmpty) {
final result =
await _loadOlderUnreadNotifications(withListenerNotification);
if (result.getValueOrElse(() => []).isNotEmpty) {
return result;
}
}
return _postFetchOperations(notifications, withListenerNotification);
return _loadOlderReadAndUnreadNotifications(withListenerNotification);
}
FutureResult<bool, ExecError> markSeen(UserNotification notification) async {
@ -367,6 +342,67 @@ class NotificationsManager extends ChangeNotifier {
..addAll(readMap.values)
..sort();
}
FutureResult<List<UserNotification>, ExecError> _loadOlderUnreadNotifications(
bool withListenerNotification) async {
final (lowestId, _) = calcLowHigh(unread);
final pm = _buildPageManager(
profile,
false,
initialPages: [
PagedResponse(
<String>[],
next: PagingData(
maxId: lowestId,
limit: itemsPerQuery,
),
)
],
);
final result = await pm
.nextFromEnd()
.andThenAsync(
(page) async =>
await _postFetchOperations(page.data, withListenerNotification),
)
.withError(
(error) => _logger.info('Error getting more updates: $error'));
return result.execErrorCast();
}
FutureResult<List<UserNotification>, ExecError>
_loadOlderReadAndUnreadNotifications(
bool withListenerNotification) async {
final (lowestId, _) =
read.isNotEmpty ? calcLowHigh(read) : calcLowHigh(unread);
final pm = _buildPageManager(
profile,
true,
initialPages: read.isEmpty && unread.isEmpty
? []
: [
PagedResponse(
<String>[],
next: PagingData(
maxId: lowestId,
limit: itemsPerQuery,
),
)
],
);
final result = await (read.isEmpty && unread.isEmpty
? pm.initialize(itemsPerQuery)
: pm.nextFromEnd())
.andThenAsync(
(page) async =>
await _postFetchOperations(page.data, withListenerNotification),
)
.withError(
(error) => _logger.info('Error getting more updates: $error'));
return result.execErrorCast();
}
}
(int lowest, int highest) calcLowHigh(List<UserNotification> notifications) {