diff --git a/lib/models/messagecache.dart b/lib/models/messagecache.dart index 8e67b81a..eefc6206 100644 --- a/lib/models/messagecache.dart +++ b/lib/models/messagecache.dart @@ -5,6 +5,10 @@ import 'package:flutter/foundation.dart'; import 'message.dart'; +// we only count up to 100 unread messages, if more than that we can't accurately resync message cache, just reset +// https://git.openprivacy.ca/cwtch.im/libcwtch-go/src/branch/trunk/utils/eventHandler.go#L210 +const MaxUnreadBeforeCacheReset = 100; + class MessageInfo { late MessageMetadata metadata; late String wrapper; @@ -101,7 +105,7 @@ class MessageCache extends ChangeNotifier { // if we find the last seen ID, the diff of unread count is what's unsynced for(var i = 0; i < (count+1) && i < cacheByIndex.length; i++) { if (this.cacheByIndex[i].messageId == lastSeenId) { - // we have + // we have found the matching lastSeenId so we can calculate the unsynced as the unread messages before it this._indexUnsynced = count - i; notifyListeners(); return; @@ -149,6 +153,8 @@ class MessageCache extends ChangeNotifier { void lockIndexes(int start, int end) { for (var i = start; i < end; i++) { this.cacheByIndex.insert(i, LocalIndexMessage(null, isLoading: true)); + // if there are unsynced messages on the index cache it means there are messages at the front, and by the logic in message/ByIndex/get() we will be loading those + // there for we can decrement the count as this will be one of them if (this._indexUnsynced > 0) { this._indexUnsynced--; } diff --git a/lib/models/profile.dart b/lib/models/profile.dart index 4d23ef1e..a7b746da 100644 --- a/lib/models/profile.dart +++ b/lib/models/profile.dart @@ -6,6 +6,7 @@ import 'package:flutter/widgets.dart'; import 'contact.dart'; import 'contactlist.dart'; import 'filedownloadprogress.dart'; +import 'messagecache.dart'; import 'profileservers.dart'; class ProfileInfoState extends ChangeNotifier { @@ -177,11 +178,10 @@ class ProfileInfoState extends ChangeNotifier { profileContact.status = contact["status"]; profileContact.totalMessages = contact["numMessages"]; profileContact.unreadMessages = contact["numUnread"]; - // we only count up to 100 unread messages, if more than that we can't accuratly resync message cache, just reset - if (contact["numUnread"] > 100 || (contact["numUnread"] > 0 && contact["lastSeenMessageId"] == -1)) { + + if (contact["numUnread"] > MaxUnreadBeforeCacheReset || (contact["numUnread"] > 0 && contact["lastSeenMessageId"] == -1)) { profileContact.messageCache.resetIndexCache(); } else if (contact["numUnread"] > 0) { - print("contact ${contact["name"]} with unread ${contact["numUnread"]} so addFrontIndexGap"); profileContact.messageCache.addFrontIndexGap(contact["numUnread"], contact["lastSeenMessageId"]); } profileContact.lastMessageTime = DateTime.fromMillisecondsSinceEpoch(1000 * int.parse(contact["lastMsgTime"]));