Introduce ConversationSnapshot.

This commit is contained in:
Matthew Chen 2019-02-19 17:49:40 -05:00
parent 586b362b89
commit 56e5feca46
4 changed files with 59 additions and 23 deletions

View File

@ -451,11 +451,11 @@ typedef enum : NSUInteger {
NSString *_Nullable recipientId = notification.userInfo[kNSNotificationKey_ProfileRecipientId];
NSData *_Nullable groupId = notification.userInfo[kNSNotificationKey_ProfileGroupId];
if (recipientId.length > 0 && [self.thread.recipientIdentifiers containsObject:recipientId]) {
[self.conversationViewModel ensureDynamicInteractions];
[self.conversationViewModel ensureDynamicInteractionsAndUpdateIfNecessary:YES];
} else if (groupId.length > 0 && self.thread.isGroupThread) {
TSGroupThread *groupThread = (TSGroupThread *)self.thread;
if ([groupThread.groupModel.groupId isEqualToData:groupId]) {
[self.conversationViewModel ensureDynamicInteractions];
[self.conversationViewModel ensureDynamicInteractionsAndUpdateIfNecessary:YES];
[self ensureBannerState];
}
}
@ -869,6 +869,7 @@ typedef enum : NSUInteger {
// Avoid layout corrupt issues and out-of-date message subtitles.
self.lastReloadDate = [NSDate new];
[self.conversationViewModel viewDidResetContentAndLayout];
[self tryToUpdateConversationSnapshot];
[self.collectionView.collectionViewLayout invalidateLayout];
[self.collectionView reloadData];
@ -2446,7 +2447,7 @@ typedef enum : NSUInteger {
- (void)contactsViewHelperDidUpdateContacts
{
[self.conversationViewModel ensureDynamicInteractions];
[self.conversationViewModel ensureDynamicInteractionsAndUpdateIfNecessary:YES];
}
- (void)createConversationScrollButtons
@ -2484,7 +2485,7 @@ typedef enum : NSUInteger {
_hasUnreadMessages = hasUnreadMessages;
self.scrollDownButton.hasUnreadMessages = hasUnreadMessages;
[self.conversationViewModel ensureDynamicInteractions];
[self.conversationViewModel ensureDynamicInteractionsAndUpdateIfNecessary:YES];
}
- (void)scrollDownButtonTapped
@ -2629,7 +2630,7 @@ typedef enum : NSUInteger {
[self showApprovalDialogForAttachment:attachment];
[ThreadUtil addThreadToProfileWhitelistIfEmptyContactThread:self.thread];
[self.conversationViewModel ensureDynamicInteractions];
[self.conversationViewModel ensureDynamicInteractionsAndUpdateIfNecessary:YES];
}
- (void)messageWasSent:(TSOutgoingMessage *)message
@ -2989,7 +2990,7 @@ typedef enum : NSUInteger {
[self messageWasSent:message];
if (didAddToProfileWhitelist) {
[self.conversationViewModel ensureDynamicInteractions];
[self.conversationViewModel ensureDynamicInteractionsAndUpdateIfNecessary:YES];
}
}];
}
@ -3641,7 +3642,7 @@ typedef enum : NSUInteger {
[self messageWasSent:message];
if (didAddToProfileWhitelist) {
[self.conversationViewModel ensureDynamicInteractions];
[self.conversationViewModel ensureDynamicInteractionsAndUpdateIfNecessary:YES];
}
});
}
@ -4031,7 +4032,7 @@ typedef enum : NSUInteger {
[self clearDraft];
if (didAddToProfileWhitelist) {
[self.conversationViewModel ensureDynamicInteractions];
[self.conversationViewModel ensureDynamicInteractionsAndUpdateIfNecessary:YES];
}
}
@ -4935,6 +4936,15 @@ typedef enum : NSUInteger {
#pragma mark - Conversation Snapshot
- (void)tryToUpdateConversationSnapshot
{
if (!self.isObservingVMUpdates) {
return;
}
[self updateConversationSnapshot];
}
- (void)updateConversationSnapshot
{
ConversationSnapshot *conversationSnapshot = [ConversationSnapshot new];

View File

@ -96,7 +96,7 @@ typedef NS_ENUM(NSUInteger, ConversationUpdateItemType) {
focusMessageIdOnOpen:(nullable NSString *)focusMessageIdOnOpen
delegate:(id<ConversationViewModelDelegate>)delegate NS_DESIGNATED_INITIALIZER;
- (void)ensureDynamicInteractions;
- (void)ensureDynamicInteractionsAndUpdateIfNecessary:(BOOL)updateIfNecessary;
- (void)clearUnreadMessagesIndicator;

View File

@ -273,7 +273,7 @@ static const int kYapDatabaseRangeMaxLength = 25000;
{
OWSAssertIsOnMainThread();
[self ensureDynamicInteractions];
[self ensureDynamicInteractionsAndUpdateIfNecessary:YES];
}
- (void)profileWhitelistDidChange:(NSNotification *)notification
@ -308,7 +308,7 @@ static const int kYapDatabaseRangeMaxLength = 25000;
self.typingIndicatorsSender = [self.typingIndicators typingRecipientIdForThread:self.thread];
self.collapseCutoffDate = [NSDate new];
[self ensureDynamicInteractions];
[self ensureDynamicInteractionsAndUpdateIfNecessary:NO];
[self.primaryStorage updateUIDatabaseConnectionToLatest];
[self createNewMessageMapping];
@ -464,21 +464,32 @@ static const int kYapDatabaseRangeMaxLength = 25000;
self.collapseCutoffDate = [NSDate new];
}
- (void)ensureDynamicInteractions
- (void)ensureDynamicInteractionsAndUpdateIfNecessary:(BOOL)updateIfNecessary
{
OWSAssertIsOnMainThread();
const int currentMaxRangeSize = (int)self.messageMapping.desiredLength;
const int maxRangeSize = MAX(kConversationInitialMaxRangeSize, currentMaxRangeSize);
self.dynamicInteractions = [ThreadUtil ensureDynamicInteractionsForThread:self.thread
contactsManager:self.contactsManager
blockingManager:self.blockingManager
dbConnection:self.editingDatabaseConnection
hideUnreadMessagesIndicator:self.hasClearedUnreadMessagesIndicator
lastUnreadIndicator:self.dynamicInteractions.unreadIndicator
focusMessageId:self.focusMessageIdOnOpen
maxRangeSize:maxRangeSize];
ThreadDynamicInteractions *dynamicInteractions =
[ThreadUtil ensureDynamicInteractionsForThread:self.thread
contactsManager:self.contactsManager
blockingManager:self.blockingManager
dbConnection:self.editingDatabaseConnection
hideUnreadMessagesIndicator:self.hasClearedUnreadMessagesIndicator
lastUnreadIndicator:self.dynamicInteractions.unreadIndicator
focusMessageId:self.focusMessageIdOnOpen
maxRangeSize:maxRangeSize];
BOOL didChange = ![NSObject isNullableObject:self.dynamicInteractions equalTo:dynamicInteractions];
self.dynamicInteractions = dynamicInteractions;
if (didChange && updateIfNecessary) {
if (![self reloadViewItems]) {
OWSFailDebug(@"Failed to reload view items.");
}
[self.delegate conversationViewModelDidUpdate:ConversationUpdate.reloadUpdate];
}
}
- (nullable id<ConversationViewItem>)viewItemForUnreadMessagesIndicator
@ -519,7 +530,7 @@ static const int kYapDatabaseRangeMaxLength = 25000;
if (self.dynamicInteractions.unreadIndicator) {
// If we've just cleared the "unread messages" indicator,
// update the dynamic interactions.
[self ensureDynamicInteractions];
[self ensureDynamicInteractionsAndUpdateIfNecessary:YES];
}
}
@ -968,7 +979,7 @@ static const int kYapDatabaseRangeMaxLength = 25000;
self.collapseCutoffDate = [NSDate new];
[self ensureDynamicInteractions];
[self ensureDynamicInteractionsAndUpdateIfNecessary:NO];
if (![self reloadViewItems]) {
OWSFailDebug(@"failed to reload view items in resetMapping.");
@ -1590,7 +1601,7 @@ static const int kYapDatabaseRangeMaxLength = 25000;
self.collapseCutoffDate = [NSDate new];
[self ensureDynamicInteractions];
[self ensureDynamicInteractionsAndUpdateIfNecessary:NO];
if (![self reloadViewItems]) {
OWSFailDebug(@"failed to reload view items in resetMapping.");

View File

@ -46,6 +46,21 @@ NS_ASSUME_NONNULL_BEGIN
self.unreadIndicator = nil;
}
- (BOOL)isEqual:(id)object
{
if (self == object) {
return YES;
}
if (![object isKindOfClass:[ThreadDynamicInteractions class]]) {
return NO;
}
ThreadDynamicInteractions *other = (ThreadDynamicInteractions *)object;
return ([NSObject isNullableObject:self.focusMessagePosition equalTo:other.focusMessagePosition] &&
[NSObject isNullableObject:self.unreadIndicator equalTo:other.unreadIndicator]);
}
@end
#pragma mark -