From c2608785e195f6b937786450b7ca9e01cb1ccfe7 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Mon, 16 Oct 2017 12:29:22 -0400 Subject: [PATCH 1/2] Reduce relayout and reload churn; respond to dynamic type changes. // FREEBIE --- .../ConversationView/Cells/OWSMessageCell.m | 9 ++++++ .../ConversationInputToolbar.h | 2 ++ .../ConversationInputToolbar.m | 8 +++++ .../ConversationViewController.m | 31 +++++++++---------- 4 files changed, 33 insertions(+), 17 deletions(-) diff --git a/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageCell.m b/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageCell.m index 714e6fb4c..e4754fd71 100644 --- a/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageCell.m +++ b/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageCell.m @@ -125,6 +125,8 @@ NS_ASSUME_NONNULL_BEGIN [self.bubbleImageView autoPinToSuperviewEdges]; self.textView = [UITextView new]; + // Honor dynamic type in the message bodies. + self.textView.font = [self textMessageFont]; self.textView.font = [UIFont ows_regularFontWithSize:16.f]; self.textView.backgroundColor = [UIColor clearColor]; self.textView.opaque = NO; @@ -167,6 +169,11 @@ NS_ASSUME_NONNULL_BEGIN return NSStringFromClass([self class]); } +- (UIFont *)textMessageFont +{ + return [UIFont ows_dynamicTypeBodyFont]; +} + - (OWSMessageCellType)cellType { return self.viewItem.messageCellType; @@ -543,6 +550,7 @@ NS_ASSUME_NONNULL_BEGIN self.textView.text = self.textMessage; UIColor *textColor = [self textColor]; self.textView.textColor = textColor; + self.textView.font = [self textMessageFont]; // Don't link outgoing messages that haven't been sent yet, as // this interferes with "tap to retry". @@ -738,6 +746,7 @@ NS_ASSUME_NONNULL_BEGIN const int maxTextWidth = (int)floor(maxMessageWidth - (leftMargin + rightMargin)); self.textView.text = self.textMessage; + self.textView.font = [self textMessageFont]; CGSize textSize = [self.textView sizeThatFits:CGSizeMake(maxTextWidth, CGFLOAT_MAX)]; cellSize = CGSizeMake((CGFloat)ceil(textSize.width + leftMargin + rightMargin), (CGFloat)ceil(textSize.height + textVMargin * 2)); diff --git a/Signal/src/ViewControllers/ConversationView/ConversationInputToolbar.h b/Signal/src/ViewControllers/ConversationView/ConversationInputToolbar.h index 62d2ada52..760202a13 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationInputToolbar.h +++ b/Signal/src/ViewControllers/ConversationView/ConversationInputToolbar.h @@ -49,6 +49,8 @@ NS_ASSUME_NONNULL_BEGIN - (nullable NSString *)textInputPrimaryLanguage; +- (void)updateFontSizes; + #pragma mark - Voice Memo - (void)showVoiceMemoUI; diff --git a/Signal/src/ViewControllers/ConversationView/ConversationInputToolbar.m b/Signal/src/ViewControllers/ConversationView/ConversationInputToolbar.m index 4fcd80c98..91795817d 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationInputToolbar.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationInputToolbar.m @@ -83,6 +83,7 @@ static void *kConversationInputTextViewObservingContext = &kConversationInputTex _inputTextView = [ConversationInputTextView new]; self.inputTextView.textViewToolbarDelegate = self; + self.inputTextView.font = [UIFont ows_dynamicTypeBodyFont]; [self.contentView addSubview:self.inputTextView]; // We want to be permissive about taps on the send and attachment buttons, @@ -151,6 +152,13 @@ static void *kConversationInputTextViewObservingContext = &kConversationInputTex [self ensureContentConstraints]; } +- (void)updateFontSizes +{ + self.inputTextView.font = [UIFont ows_dynamicTypeBodyFont]; + + [self ensureContentConstraints]; +} + - (void)setInputTextViewDelegate:(id)value { OWSAssert(self.inputTextView); diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m index e9057eb9c..6a3a81850 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m @@ -329,8 +329,11 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) { [self setNavigationTitle]; } - // Reload all cells. - [self resetContentAndLayout]; + if (self.isGroupConversation) { + // Reload all cells if this is a group conversation, + // since we may need to update the sender names on the messages. + [self resetContentAndLayout]; + } } } @@ -633,10 +636,8 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) { - (void)resetContentAndLayout { // Avoid layout corrupt issues and out-of-date message subtitles. - [self.collectionView.collectionViewLayout - invalidateLayoutWithContext:[JSQMessagesCollectionViewFlowLayoutInvalidationContext context]]; + [self.collectionView.collectionViewLayout invalidateLayout]; [self.collectionView reloadData]; - // TODO: Should we evacuate cached cell sizes here? } @@ -1377,9 +1378,14 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) { */ - (void)didChangePreferredContentSize:(NSNotification *)notification { - // [self.collectionView.collectionViewLayout setMessageBubbleFont:[UIFont ows_dynamicTypeBodyFont]]; + DDLogInfo(@"%@ didChangePreferredContentSize", self.tag); + + // Evacuate cached cell sizes. + for (ConversationViewItem *viewItem in self.viewItems) { + [viewItem clearCachedLayoutState]; + } [self resetContentAndLayout]; - // [self reloadInputToolbarSizeIfNeeded]; + [self.inputToolbar updateFontSizes]; } #pragma mark - Actions @@ -3579,7 +3585,6 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) { [groupMemberIds addObject:[TSAccountManager localNumber]]; groupModel.groupMemberIds = [NSMutableArray arrayWithArray:[groupMemberIds allObjects]]; [self updateGroupModelTo:groupModel successCompletion:nil]; - [self resetContentAndLayout]; } - (void)popAllConversationSettingsViews @@ -3668,20 +3673,12 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) { [self toggleDefaultKeyboard]; } [self clearDraft]; - [self finishSendingMessage]; + [self.inputToolbar clearTextMessage]; if (didAddToProfileWhitelist) { [self ensureDynamicInteractions]; } } -- (void)finishSendingMessage -{ - [self.inputToolbar clearTextMessage]; - [self.collectionView.collectionViewLayout - invalidateLayoutWithContext:[JSQMessagesCollectionViewFlowLayoutInvalidationContext context]]; - [self.collectionView reloadData]; -} - - (void)voiceMemoGestureDidStart { OWSAssert([NSThread isMainThread]); From d355b45baeff3b1b527b08c71f668f0d844ace03 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Mon, 16 Oct 2017 12:36:04 -0400 Subject: [PATCH 2/2] Reduce relayout and reload churn; respond to dynamic type changes. // FREEBIE --- .../ConversationView/ConversationViewController.m | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m index 6a3a81850..b6a619c30 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m @@ -1370,7 +1370,6 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) { #pragma mark - Dynamic Text -// TODO: Fix this. /** Called whenever the user manually changes the dynamic type options inside Settings. @@ -2798,6 +2797,11 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) { // may need to extend the mapping's contents to reflect the current // range. [self updateMessageMappingRangeOptions:MessagesRangeSizeMode_Normal]; + // Calling resetContentAndLayout is a bit expensive. + // Since by definition this won't affect any cells in the previous + // range, it should be sufficient to call invalidateLayout. + // + // TODO: Investigate whether we can just call invalidateLayout. [self resetContentAndLayout]; return; }