From b5f5599774cc9edc036240bd171e48c44893801f Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Wed, 31 May 2017 16:38:29 -0400 Subject: [PATCH] Fix edge cases in the unseen indicator. // FREEBIE --- .../ViewControllers/MessagesViewController.m | 4 ++-- Signal/src/util/ThreadUtil.m | 20 ++++++++++--------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/Signal/src/ViewControllers/MessagesViewController.m b/Signal/src/ViewControllers/MessagesViewController.m index 7477ba335..f879ad95e 100644 --- a/Signal/src/ViewControllers/MessagesViewController.m +++ b/Signal/src/ViewControllers/MessagesViewController.m @@ -1095,8 +1095,6 @@ typedef enum : NSUInteger { selector:@selector(scrollToDefaultPosition) userInfo:nil repeats:NO]; - - [self markVisibleMessagesAsRead]; } - (NSIndexPath *_Nullable)indexPathOfUnreadMessagesIndicator @@ -1344,6 +1342,8 @@ typedef enum : NSUInteger { [self updateNavigationBarSubtitleLabel]; [MarkIdentityAsSeenJob runWithThread:self.thread]; [ProfileFetcherJob runWithThread:self.thread networkManager:self.networkManager]; + + [self markVisibleMessagesAsRead]; } - (void)viewWillDisappear:(BOOL)animated diff --git a/Signal/src/util/ThreadUtil.m b/Signal/src/util/ThreadUtil.m index 63beae03e..78e20397d 100644 --- a/Signal/src/util/ThreadUtil.m +++ b/Signal/src/util/ThreadUtil.m @@ -167,17 +167,15 @@ NS_ASSUME_NONNULL_BEGIN // have been marked as read. // // IFF this variable is non-null, there are unseen messages in the thread. - NSNumber *firstUnseenInteractionTimestamp = nil; if (firstUnseenInteractionTimestampParameter) { - firstUnseenInteractionTimestamp = firstUnseenInteractionTimestampParameter; + result.firstUnseenInteractionTimestamp = firstUnseenInteractionTimestampParameter; } else { TSInteraction *firstUnseenInteraction = [[transaction ext:TSUnseenDatabaseViewExtensionName] firstObjectInGroup:thread.uniqueId]; if (firstUnseenInteraction) { - firstUnseenInteractionTimestamp = @(firstUnseenInteraction.timestampForSorting); + result.firstUnseenInteractionTimestamp = @(firstUnseenInteraction.timestampForSorting); } } - result.firstUnseenInteractionTimestamp = firstUnseenInteractionTimestamp; TSIncomingMessage *firstIncomingMessage = [[transaction ext:TSThreadIncomingMessageDatabaseViewExtensionName] firstObjectInGroup:thread.uniqueId]; @@ -196,7 +194,7 @@ NS_ASSUME_NONNULL_BEGIN __block BOOL hasMoreUnseenMessages = NO; __block TSInteraction *interactionAfterUnreadIndicator = nil; NSUInteger missingUnseenSafetyNumberChangeCount = 0; - if (firstUnseenInteractionTimestamp != nil) { + if (result.firstUnseenInteractionTimestamp != nil) { [[transaction ext:TSMessageDatabaseViewExtensionName] enumerateRowsInGroup:thread.uniqueId withOptions:NSEnumerationReverse @@ -220,7 +218,7 @@ NS_ASSUME_NONNULL_BEGIN } if (interaction.timestampForSorting - < firstUnseenInteractionTimestamp.unsignedLongLongValue) { + < result.firstUnseenInteractionTimestamp.unsignedLongLongValue) { // By default we want the unread indicator to appear just before // the first unread message. *stop = YES; @@ -246,7 +244,7 @@ NS_ASSUME_NONNULL_BEGIN NSMutableSet *missingUnseenSafetyNumberChanges = [NSMutableSet set]; for (TSInvalidIdentityKeyErrorMessage *safetyNumberChange in blockingSafetyNumberChanges) { BOOL isUnseen = safetyNumberChange.timestampForSorting - >= firstUnseenInteractionTimestamp.unsignedLongLongValue; + >= result.firstUnseenInteractionTimestamp.unsignedLongLongValue; if (!isUnseen) { continue; } @@ -264,10 +262,11 @@ NS_ASSUME_NONNULL_BEGIN = (missingUnseenSafetyNumberChanges.count + nonBlockingSafetyNumberChanges.count); } } - if (hasMoreUnseenMessages) { + if (result.firstUnseenInteractionTimestamp) { // The unread indicator is _before_ the last visible unseen message. result.unreadIndicatorPosition = @(visibleUnseenMessageCount); } + OWSAssert((result.firstUnseenInteractionTimestamp != nil) == (result.unreadIndicatorPosition != nil)); TSMessage *firstMessage = firstIncomingMessage; if (!firstMessage @@ -399,7 +398,10 @@ NS_ASSUME_NONNULL_BEGIN missingUnseenSafetyNumberChangeCount:missingUnseenSafetyNumberChangeCount]; [indicator saveWithTransaction:transaction]; - DDLogInfo(@"%@ Creating TSUnreadIndicatorInteraction: %@", self.tag, indicator.uniqueId); + DDLogInfo(@"%@ Creating TSUnreadIndicatorInteraction: %@ (%llu)", + self.tag, + indicator.uniqueId, + indicator.timestampForSorting); } } }];