From 7d32491968bc97e5539b5964755ca98484e158f7 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Wed, 30 Aug 2017 16:18:42 -0400 Subject: [PATCH] Preserve scroll state across conversation view layout changes, if possible. // FREEBIE --- Podfile | 4 +-- Podfile.lock | 10 +++---- .../ConversationView/MessagesViewController.m | 26 +++++++++++++++++-- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/Podfile b/Podfile index 070f1f1e2..9f44466e1 100644 --- a/Podfile +++ b/Podfile @@ -5,8 +5,8 @@ target 'Signal' do pod 'ATAppUpdater' pod 'AxolotlKit', git: 'https://github.com/WhisperSystems/SignalProtocolKit.git' #pod 'AxolotlKit', path: '../SignalProtocolKit' - pod 'JSQMessagesViewController', git: 'https://github.com/WhisperSystems/JSQMessagesViewController.git', branch: 'signal-master' - #pod 'JSQMessagesViewController', path: '../JSQMessagesViewController' + #pod 'JSQMessagesViewController', git: 'https://github.com/WhisperSystems/JSQMessagesViewController.git', branch: 'signal-master' + pod 'JSQMessagesViewController', path: '../JSQMessagesViewController' pod 'PureLayout' pod 'OpenSSL', git: 'https://github.com/WhisperSystems/OpenSSL-Pod' pod 'Reachability' diff --git a/Podfile.lock b/Podfile.lock index deb87985c..9cbb804ae 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -114,7 +114,7 @@ PODS: DEPENDENCIES: - ATAppUpdater - AxolotlKit (from `https://github.com/WhisperSystems/SignalProtocolKit.git`) - - JSQMessagesViewController (from `https://github.com/WhisperSystems/JSQMessagesViewController.git`, branch `signal-master`) + - JSQMessagesViewController (from `../JSQMessagesViewController`) - OpenSSL (from `https://github.com/WhisperSystems/OpenSSL-Pod`) - PureLayout - Reachability @@ -125,8 +125,7 @@ EXTERNAL SOURCES: AxolotlKit: :git: https://github.com/WhisperSystems/SignalProtocolKit.git JSQMessagesViewController: - :branch: signal-master - :git: https://github.com/WhisperSystems/JSQMessagesViewController.git + :path: ../JSQMessagesViewController OpenSSL: :git: https://github.com/WhisperSystems/OpenSSL-Pod SignalServiceKit: @@ -138,9 +137,6 @@ CHECKOUT OPTIONS: AxolotlKit: :commit: 6e9d5e8c3dc87e397b4b7485172507b0990a22dd :git: https://github.com/WhisperSystems/SignalProtocolKit.git - JSQMessagesViewController: - :commit: 868cf70fae0f3b54c3cfa7773ef03578d209b668 - :git: https://github.com/WhisperSystems/JSQMessagesViewController.git OpenSSL: :commit: b2d3c149102032a09aefbfb470885db4aa83efad :git: https://github.com/WhisperSystems/OpenSSL-Pod @@ -171,6 +167,6 @@ SPEC CHECKSUMS: UnionFind: c33be5adb12983981d6e827ea94fc7f9e370f52d YapDatabase: cd911121580ff16675f65ad742a9eb0ab4d9e266 -PODFILE CHECKSUM: 2f847bb25e70d1d376f38cf21ae08624fa6ed67d +PODFILE CHECKSUM: 4a6db311736b5ab36b660af813dcdf0af62da48c COCOAPODS: 1.2.1 diff --git a/Signal/src/ViewControllers/ConversationView/MessagesViewController.m b/Signal/src/ViewControllers/ConversationView/MessagesViewController.m index 74510dc4e..5a50a253d 100644 --- a/Signal/src/ViewControllers/ConversationView/MessagesViewController.m +++ b/Signal/src/ViewControllers/ConversationView/MessagesViewController.m @@ -169,7 +169,8 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) { UIDocumentPickerDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, - UITextViewDelegate> + UITextViewDelegate, + JSQLayoutDelegate> @property (nonatomic) TSThread *thread; @property (nonatomic) TSMessageAdapter *lastDeliveredMessage; @@ -245,6 +246,7 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) { @property (nonatomic) BOOL shouldObserveDBModifications; @property (nonatomic) BOOL viewHasEverAppeared; @property (nonatomic) BOOL wasScrolledToBottomBeforeKeyboardShow; +@property (nonatomic) BOOL wasScrolledToBottomBeforeLayoutChange; @end @@ -404,7 +406,7 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) { // We only want to update scroll state for non-empty show events. BOOL isPresenting = endValue.size.height > 0.f; - if (isPresenting) { + if (isPresenting && self.wasScrolledToBottomBeforeKeyboardShow) { [self scrollToBottomImmediately]; } } @@ -543,6 +545,8 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) { - (void)viewDidLoad { + self.collectionView.layoutDelegate = self; + [super viewDidLoad]; [self.navigationController.navigationBar setTranslucent:NO]; @@ -4461,6 +4465,24 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) { [self updateNavigationBarSubtitleLabel]; } +#pragma mark - JSQLayoutDelegate + +- (void)jsqWillChangeLayout +{ + OWSAssert([NSThread isMainThread]); + + self.wasScrolledToBottomBeforeLayoutChange = [self isScrolledToBottom]; +} + +- (void)jsqDidChangeLayout +{ + OWSAssert([NSThread isMainThread]); + + if (self.wasScrolledToBottomBeforeLayoutChange) { + [self scrollToBottomImmediately]; + } +} + #pragma mark - Class methods + (UINib *)nib