mirror of
https://gitlab.com/mysocialportal/relatica
synced 2024-10-18 15:53:32 +00:00
Refactor notifications processing to use max 3 calls on initial load and streamlined older loading
This commit is contained in:
parent
1787bb4f7e
commit
910e6d2c4e
1 changed files with 76 additions and 40 deletions
|
@ -71,7 +71,9 @@ class NotificationsManager extends ChangeNotifier {
|
||||||
.canUseFeature(RelaticaFeatures.usingActualFollowRequests);
|
.canUseFeature(RelaticaFeatures.usingActualFollowRequests);
|
||||||
var hasMore = true;
|
var hasMore = true;
|
||||||
var first = true;
|
var first = true;
|
||||||
while (hasMore) {
|
const maxCalls = 3;
|
||||||
|
var count = 0;
|
||||||
|
while (hasMore && count < maxCalls) {
|
||||||
final result =
|
final result =
|
||||||
first ? await pm.initialize(itemsPerQuery) : await pm.nextFromEnd();
|
first ? await pm.initialize(itemsPerQuery) : await pm.nextFromEnd();
|
||||||
|
|
||||||
|
@ -85,6 +87,7 @@ class NotificationsManager extends ChangeNotifier {
|
||||||
!useActualRequests || n.type != NotificationType.follow_request)
|
!useActualRequests || n.type != NotificationType.follow_request)
|
||||||
.forEach(notificationsFromRefresh.add);
|
.forEach(notificationsFromRefresh.add);
|
||||||
hasMore = response.next != null;
|
hasMore = response.next != null;
|
||||||
|
count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// filter out connection requests if going to use the real service for that when doing the query
|
// 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(
|
PagedResponse(
|
||||||
<String>[],
|
<String>[],
|
||||||
previous: PagingData(minId: highestId),
|
previous: PagingData(
|
||||||
|
minId: highestId,
|
||||||
|
limit: itemsPerQuery,
|
||||||
|
),
|
||||||
)
|
)
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
@ -130,46 +136,15 @@ class NotificationsManager extends ChangeNotifier {
|
||||||
|
|
||||||
FutureResult<List<UserNotification>, ExecError> loadOlderNotifications(
|
FutureResult<List<UserNotification>, ExecError> loadOlderNotifications(
|
||||||
{bool withListenerNotification = true}) async {
|
{bool withListenerNotification = true}) async {
|
||||||
final (lowestId, _) =
|
if (unread.isNotEmpty) {
|
||||||
read.isNotEmpty ? calcLowHigh(read) : calcLowHigh(unread);
|
final result =
|
||||||
final pm = _buildPageManager(
|
await _loadOlderUnreadNotifications(withListenerNotification);
|
||||||
profile,
|
if (result.getValueOrElse(() => []).isNotEmpty) {
|
||||||
true,
|
return result;
|
||||||
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));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return _postFetchOperations(notifications, withListenerNotification);
|
return _loadOlderReadAndUnreadNotifications(withListenerNotification);
|
||||||
}
|
}
|
||||||
|
|
||||||
FutureResult<bool, ExecError> markSeen(UserNotification notification) async {
|
FutureResult<bool, ExecError> markSeen(UserNotification notification) async {
|
||||||
|
@ -367,6 +342,67 @@ class NotificationsManager extends ChangeNotifier {
|
||||||
..addAll(readMap.values)
|
..addAll(readMap.values)
|
||||||
..sort();
|
..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) {
|
(int lowest, int highest) calcLowHigh(List<UserNotification> notifications) {
|
||||||
|
|
Loading…
Reference in a new issue