From a0c147722e3a07412cb356c7712f42ba086a3fa0 Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Fri, 8 Apr 2016 12:59:05 -0400 Subject: [PATCH] Delete last message updates thread preview (signal ios#833) (#3) * fix thread preview when deleting last message because we are doing internal accounting of what our "last" message id is, when we delete the last message, other things get out of whack. Caching is hard, don't do it! We can efficiently get the last object using our existing Yap DB view. //FREEBIE * remove lastMessageId in favor of lastMessage for cleaner code //FREEBIE --- src/Contacts/TSThread.m | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/src/Contacts/TSThread.m b/src/Contacts/TSThread.m index 72dae348a..56c5a64c5 100644 --- a/src/Contacts/TSThread.m +++ b/src/Contacts/TSThread.m @@ -19,8 +19,10 @@ @property (nonatomic, retain) NSDate *creationDate; @property (nonatomic, copy) NSDate *archivalDate; @property (nonatomic, retain) NSDate *lastMessageDate; -@property (nonatomic, copy) NSString *latestMessageId; @property (nonatomic, copy) NSString *messageDraft; + +- (TSInteraction *) lastInteraction; + @end @implementation TSThread @@ -34,7 +36,6 @@ if (self) { _archivalDate = nil; - _latestMessageId = nil; _lastMessageDate = nil; _creationDate = [NSDate date]; _messageDraft = nil; @@ -62,15 +63,12 @@ #pragma mark Read Status - (BOOL)hasUnreadMessages { - __block TSInteraction *interaction; - __block BOOL hasUnread = NO; - [[TSStorageManager sharedManager] - .dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { - interaction = [TSInteraction fetchObjectWithUniqueID:self.latestMessageId transaction:transaction]; - if ([interaction isKindOfClass:[TSIncomingMessage class]]) { - hasUnread = ![(TSIncomingMessage *)interaction wasRead]; - } - }]; + TSInteraction *interaction = self.lastInteraction; + BOOL hasUnread = NO; + + if ([interaction isKindOfClass:[TSIncomingMessage class]]) { + hasUnread = ![(TSIncomingMessage *)interaction wasRead]; + } return hasUnread; } @@ -93,6 +91,14 @@ #pragma mark Last Interactions +- (TSInteraction *) lastInteraction { + __block TSInteraction *last; + [TSStorageManager.sharedManager.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction){ + last = [[transaction ext:TSMessageDatabaseViewExtensionName] lastObjectInGroup:self.uniqueId]; + }]; + return (TSInteraction *)last; +} + - (NSDate *)lastMessageDate { if (_lastMessageDate) { return _lastMessageDate; @@ -102,12 +108,11 @@ } - (NSString *)lastMessageLabel { - __block TSInteraction *interaction; - [[TSStorageManager sharedManager] - .dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { - interaction = [TSInteraction fetchObjectWithUniqueID:self.latestMessageId transaction:transaction]; - }]; - return interaction.description; + if (self.lastInteraction == nil) { + return @""; + } else { + return self.lastInteraction.description; + } } - (void)updateWithLastMessage:(TSInteraction *)lastMessage transaction:(YapDatabaseReadWriteTransaction *)transaction { @@ -119,7 +124,6 @@ } if (!_lastMessageDate || [lastMessageDate timeIntervalSinceDate:self.lastMessageDate] > 0) { - _latestMessageId = lastMessage.uniqueId; _lastMessageDate = lastMessageDate; [self saveWithTransaction:transaction];