diff --git a/lib/cwtch/cwtchNotifier.dart b/lib/cwtch/cwtchNotifier.dart index 72d68f26..185ed735 100644 --- a/lib/cwtch/cwtchNotifier.dart +++ b/lib/cwtch/cwtchNotifier.dart @@ -35,7 +35,7 @@ class CwtchNotifier { } void handleMessage(String type, dynamic data) { - EnvironmentConfig.debugLog("NewEvent $type $data"); + //EnvironmentConfig.debugLog("NewEvent $type $data"); switch (type) { case "CwtchStarted": appState.SetCwtchInit(); @@ -144,7 +144,7 @@ class CwtchNotifier { profileCN.getProfile(data["ProfileOnion"])?.contactList.getContact(identifier)!.newMarker++; } profileCN.getProfile(data["ProfileOnion"])?.contactList.updateLastMessageTime(identifier, DateTime.now()); - profileCN.getProfile(data["ProfileOnion"])?.contactList.getContact(identifier)!.updateMessageCache(identifier, messageID, timestamp, senderHandle, senderImage, data["Data"], ""); + profileCN.getProfile(data["ProfileOnion"])?.contactList.getContact(identifier)!.updateMessageCache(identifier, messageID, timestamp, senderHandle, senderImage, data["Data"]); profileCN.getProfile(data["ProfileOnion"])?.contactList.getContact(identifier)!.totalMessages++; // We only ever see messages from authenticated peers. @@ -170,17 +170,17 @@ class CwtchNotifier { if (key == null) break; try { var message = Provider.of(key.currentContext!, listen: false); - if (message == null) break; + message.ackd = true; + // We only ever see acks from authenticated peers. // If the contact is marked as offline then override this - can happen when the contact is removed from the front // end during syncing. if (profileCN.getProfile(data["ProfileOnion"])?.contactList.getContact(conversation)!.isOnline() == false) { profileCN.getProfile(data["ProfileOnion"])?.contactList.getContact(conversation)!.status = "Authenticated"; } - message.ackd = true; + profileCN.getProfile(data["ProfileOnion"])?.contactList.getContact(conversation)!.ackCache(messageID); } catch (e) { - // ignore, we received an ack for a message that hasn't loaded onto the screen yet... - // the protocol was faster than the ui....yay? + // ignore, most likely cause is the key got optimized out... } break; case "NewMessageFromGroup": @@ -194,11 +194,7 @@ class CwtchNotifier { // Only bother to do anything if we know about the group and the provided index is greater than our current total... if (currentTotal != null && idx >= currentTotal) { - profileCN - .getProfile(data["ProfileOnion"]) - ?.contactList - .getContact(identifier)! - .updateMessageCache(identifier, idx, timestampSent, senderHandle, senderImage, data["Data"], data["Signature"]); + profileCN.getProfile(data["ProfileOnion"])?.contactList.getContact(identifier)!.updateMessageCache(identifier, idx, timestampSent, senderHandle, senderImage, data["Data"]); profileCN.getProfile(data["ProfileOnion"])?.contactList.getContact(identifier)!.totalMessages++; //if not currently open diff --git a/lib/model.dart b/lib/model.dart index f08113cf..4c94fb8e 100644 --- a/lib/model.dart +++ b/lib/model.dart @@ -687,11 +687,16 @@ class ContactInfoState extends ChangeNotifier { return ret; } - void updateMessageCache(int conversation, int messageID, DateTime timestamp, String senderHandle, String senderImage, String data, String signature) { - this.messageCache.insert(0, MessageCache(MessageMetadata(profileOnion, conversation, messageID, timestamp, senderHandle, senderImage, signature, {}, false, false), data)); + void updateMessageCache(int conversation, int messageID, DateTime timestamp, String senderHandle, String senderImage, String data) { + this.messageCache.insert(0, MessageCache(MessageMetadata(profileOnion, conversation, messageID, timestamp, senderHandle, senderImage, "", {}, false, false), data)); } void bumpMessageCache() { this.messageCache.insert(0, null); } + + void ackCache(int messageID) { + this.messageCache.firstWhere((element) => element?.metadata.messageID == messageID)?.metadata.ackd = true; + notifyListeners(); + } } diff --git a/lib/models/message.dart b/lib/models/message.dart index 0fab66e9..4df9eace 100644 --- a/lib/models/message.dart +++ b/lib/models/message.dart @@ -58,8 +58,8 @@ Message compileOverlay(MessageMetadata metadata, String messageData) { } Future messageHandler(BuildContext context, String profileOnion, int conversationIdentifier, int index, {bool byID = false}) { - var cache = Provider.of(context, listen: false).contactList.getContact(conversationIdentifier)!.messageCache; - if (cache.length > index) { + var cache = Provider.of(context).contactList.getContact(conversationIdentifier)?.messageCache; + if (cache != null && cache.length > index) { if (cache[index] != null) { return Future.value(compileOverlay(cache[index]!.metadata, cache[index]!.wrapper)); } diff --git a/lib/models/messages/filemessage.dart b/lib/models/messages/filemessage.dart index 37562aca..9254c6d2 100644 --- a/lib/models/messages/filemessage.dart +++ b/lib/models/messages/filemessage.dart @@ -35,7 +35,7 @@ class FileMessage extends Message { return MessageRow(MalformedBubble()); } - return MessageRow(FileBubble(nameSuggestion, rootHash, nonce, fileSize)); + return MessageRow(FileBubble(nameSuggestion, rootHash, nonce, fileSize), key: key); }); } diff --git a/lib/models/messages/invitemessage.dart b/lib/models/messages/invitemessage.dart index 3b243a05..149ba5e0 100644 --- a/lib/models/messages/invitemessage.dart +++ b/lib/models/messages/invitemessage.dart @@ -37,11 +37,10 @@ class InviteMessage extends Message { inviteTarget = jsonObj['GroupID']; inviteNick = jsonObj['GroupName']; } else { - return MessageRow(MalformedBubble()); + return MessageRow(MalformedBubble(), key: key); } } - var lrt = Provider.of(bcontext).lastMessageTime; - return MessageRow(InvitationBubble(overlay, inviteTarget, inviteNick, invite)); + return MessageRow(InvitationBubble(overlay, inviteTarget, inviteNick, invite), key: key); }); } diff --git a/lib/models/messages/malformedmessage.dart b/lib/models/messages/malformedmessage.dart index ea9896ab..0ebf2281 100644 --- a/lib/models/messages/malformedmessage.dart +++ b/lib/models/messages/malformedmessage.dart @@ -11,10 +11,9 @@ class MalformedMessage extends Message { @override Widget getWidget(BuildContext context, Key key) { return ChangeNotifierProvider.value( - key: key, value: this.metadata, builder: (context, child) { - return MessageRow(MalformedBubble()); + return MessageRow(MalformedBubble(), key: key); }); } diff --git a/lib/models/messages/quotedmessage.dart b/lib/models/messages/quotedmessage.dart index 9fbbc5cc..5a69ca91 100644 --- a/lib/models/messages/quotedmessage.dart +++ b/lib/models/messages/quotedmessage.dart @@ -89,15 +89,16 @@ class QuotedMessage extends Message { }); return ChangeNotifierProvider.value( - key: key, value: this.metadata, builder: (bcontext, child) { - return MessageRow(QuotedMessageBubble(message["body"], quotedMessage.then((LocallyIndexedMessage? localIndex) { - if (localIndex != null) { - return messageHandler(context, metadata.profileOnion, metadata.conversationIdentifier, localIndex.index); - } - return MalformedMessage(this.metadata); - }))); + return MessageRow( + QuotedMessageBubble(message["body"], quotedMessage.then((LocallyIndexedMessage? localIndex) { + if (localIndex != null) { + return messageHandler(context, metadata.profileOnion, metadata.conversationIdentifier, localIndex.index); + } + return MalformedMessage(this.metadata); + })), + key: key); }); } catch (e) { return MalformedMessage(this.metadata).getWidget(context, key); diff --git a/lib/models/messages/textmessage.dart b/lib/models/messages/textmessage.dart index a94c86f6..a8d7f6af 100644 --- a/lib/models/messages/textmessage.dart +++ b/lib/models/messages/textmessage.dart @@ -33,13 +33,12 @@ class TextMessage extends Message { @override Widget getWidget(BuildContext context, Key key) { return ChangeNotifierProvider.value( - key: key, value: this.metadata, builder: (bcontext, child) { - var lrt = Provider.of(bcontext).lastMessageTime; - // var key = Provider.of(bcontext).getMessageKey(this.metadata.conversationIdentifier, this.metadata.messageID, lrt); - - return MessageRow(MessageBubble(this.content)); + return MessageRow( + MessageBubble(this.content), + key: key, + ); }); } } diff --git a/lib/widgets/messagerow.dart b/lib/widgets/messagerow.dart index 311fc3a1..55884aa2 100644 --- a/lib/widgets/messagerow.dart +++ b/lib/widgets/messagerow.dart @@ -204,7 +204,9 @@ class MessageRowState extends State with SingleTickerProviderStateMi children: widgetRow, ))))); var mark = Provider.of(context).newMarker; - if (mark > 0 && Provider.of(context).messageCache[mark]?.metadata.messageID == Provider.of(context).messageID) { + if (mark > 0 && + Provider.of(context).messageCache.length > mark && + Provider.of(context).messageCache[mark]?.metadata.messageID == Provider.of(context).messageID) { return Column(crossAxisAlignment: fromMe ? CrossAxisAlignment.end : CrossAxisAlignment.start, children: [Align(alignment: Alignment.center, child: _bubbleNew()), mr]); } else { return mr;