From 812210a63c8b88d18ccfbb556e2fc27e4cdffebb Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Thu, 31 May 2018 11:11:01 -0400 Subject: [PATCH] Modify views to observe changes when active, not just foreground. --- .../ConversationViewController.m | 24 ++++++-------- .../HomeView/HomeViewController.m | 32 ++++++++----------- SignalMessaging/Views/ThreadViewHelper.m | 24 ++++++++------ 3 files changed, 38 insertions(+), 42 deletions(-) diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m index 9ddd842c8..b9338905f 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m @@ -233,7 +233,6 @@ typedef enum : NSUInteger { @property (nonatomic) BOOL isViewCompletelyAppeared; @property (nonatomic) BOOL isViewVisible; -@property (nonatomic) BOOL isAppInBackground; @property (nonatomic) BOOL shouldObserveDBModifications; @property (nonatomic) BOOL viewHasEverAppeared; @property (nonatomic) BOOL hasUnreadMessages; @@ -594,12 +593,12 @@ typedef enum : NSUInteger { - (void)applicationWillEnterForeground:(NSNotification *)notification { [self startReadTimer]; - self.isAppInBackground = NO; + [self updateCellsVisible]; } - (void)applicationDidEnterBackground:(NSNotification *)notification { - self.isAppInBackground = YES; + [self updateCellsVisible]; if (self.hasClearedUnreadMessagesIndicator) { self.hasClearedUnreadMessagesIndicator = NO; [self.dynamicInteractions clearUnreadIndicatorState]; @@ -609,6 +608,7 @@ typedef enum : NSUInteger { - (void)applicationWillResignActive:(NSNotification *)notification { + [self updateShouldObserveDBModifications]; [self cancelVoiceMemo]; self.isUserScrolling = NO; [self saveDraft]; @@ -620,6 +620,7 @@ typedef enum : NSUInteger { - (void)applicationDidBecomeActive:(NSNotification *)notification { + [self updateShouldObserveDBModifications]; [self startReadTimer]; } @@ -1530,7 +1531,8 @@ typedef enum : NSUInteger { - (void)autoLoadMoreIfNecessary { - if (self.isUserScrolling || !self.isViewVisible || self.isAppInBackground) { + BOOL isAppInBackground = CurrentAppContext().isInBackground; + if (self.isUserScrolling || !self.isViewVisible || isAppInBackground) { return; } if (!self.showLoadMoreHeader) { @@ -4428,17 +4430,10 @@ typedef enum : NSUInteger { [self updateCellsVisible]; } -- (void)setIsAppInBackground:(BOOL)isAppInBackground -{ - _isAppInBackground = isAppInBackground; - - [self updateShouldObserveDBModifications]; - [self updateCellsVisible]; -} - - (void)updateCellsVisible { - BOOL isCellVisible = self.isViewVisible && !self.isAppInBackground; + BOOL isAppInBackground = CurrentAppContext().isInBackground; + BOOL isCellVisible = self.isViewVisible && !isAppInBackground; for (ConversationViewCell *cell in self.collectionView.visibleCells) { cell.isCellVisible = isCellVisible; } @@ -4446,7 +4441,8 @@ typedef enum : NSUInteger { - (void)updateShouldObserveDBModifications { - self.shouldObserveDBModifications = self.isViewVisible && !self.isAppInBackground; + BOOL isAppForegroundAndActive = CurrentAppContext().reportedApplicationState == UIApplicationStateActive; + self.shouldObserveDBModifications = self.isViewVisible && isAppForegroundAndActive; } - (void)setShouldObserveDBModifications:(BOOL)shouldObserveDBModifications diff --git a/Signal/src/ViewControllers/HomeView/HomeViewController.m b/Signal/src/ViewControllers/HomeView/HomeViewController.m index e68963b39..b48375ba1 100644 --- a/Signal/src/ViewControllers/HomeView/HomeViewController.m +++ b/Signal/src/ViewControllers/HomeView/HomeViewController.m @@ -51,7 +51,6 @@ NSString *const kArchivedConversationsReuseIdentifier = @"kArchivedConversations @property (nonatomic) NSSet *blockedPhoneNumberSet; @property (nonatomic, readonly) NSCache *threadViewModelCache; @property (nonatomic) BOOL isViewVisible; -@property (nonatomic) BOOL isAppInBackground; @property (nonatomic) BOOL shouldObserveDBModifications; @property (nonatomic) BOOL hasBeenPresented; @@ -131,14 +130,14 @@ NSString *const kArchivedConversationsReuseIdentifier = @"kArchivedConversations selector:@selector(applicationWillEnterForeground:) name:OWSApplicationWillEnterForegroundNotification object:nil]; - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(applicationDidEnterBackground:) - name:OWSApplicationDidEnterBackgroundNotification - object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive:) name:OWSApplicationDidBecomeActiveNotification object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(applicationWillResignActive:) + name:OWSApplicationWillResignActiveNotification + object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(yapDatabaseModified:) name:YapDatabaseModifiedNotification @@ -484,16 +483,10 @@ NSString *const kArchivedConversationsReuseIdentifier = @"kArchivedConversations [self updateShouldObserveDBModifications]; } -- (void)setIsAppInBackground:(BOOL)isAppInBackground -{ - _isAppInBackground = isAppInBackground; - - [self updateShouldObserveDBModifications]; -} - - (void)updateShouldObserveDBModifications { - self.shouldObserveDBModifications = self.isViewVisible && !self.isAppInBackground; + BOOL isAppForegroundAndActive = CurrentAppContext().reportedApplicationState == UIApplicationStateActive; + self.shouldObserveDBModifications = self.isViewVisible && isAppForegroundAndActive; } - (void)setShouldObserveDBModifications:(BOOL)shouldObserveDBModifications @@ -550,15 +543,9 @@ NSString *const kArchivedConversationsReuseIdentifier = @"kArchivedConversations - (void)applicationWillEnterForeground:(NSNotification *)notification { - self.isAppInBackground = NO; [self checkIfEmptyView]; } -- (void)applicationDidEnterBackground:(NSNotification *)notification -{ - self.isAppInBackground = YES; -} - - (BOOL)hasAnyMessagesWithTransaction:(YapDatabaseReadTransaction *)transaction { return [TSThread numberOfKeysInCollectionWithTransaction:transaction] > 0; @@ -566,6 +553,8 @@ NSString *const kArchivedConversationsReuseIdentifier = @"kArchivedConversations - (void)applicationDidBecomeActive:(NSNotification *)notification { + [self updateShouldObserveDBModifications]; + // It's possible a thread was created while we where in the background. But since we don't honor contact // requests unless the app is in the foregrond, we must check again here upon becoming active. __block BOOL hasAnyMessages; @@ -582,6 +571,11 @@ NSString *const kArchivedConversationsReuseIdentifier = @"kArchivedConversations } } +- (void)applicationWillResignActive:(NSNotification *)notification +{ + [self updateShouldObserveDBModifications]; +} + #pragma mark - startup - (NSArray *)unseenUpgradeExperiences diff --git a/SignalMessaging/Views/ThreadViewHelper.m b/SignalMessaging/Views/ThreadViewHelper.m index 3050122ce..881ad4ae5 100644 --- a/SignalMessaging/Views/ThreadViewHelper.m +++ b/SignalMessaging/Views/ThreadViewHelper.m @@ -56,25 +56,31 @@ NS_ASSUME_NONNULL_BEGIN [self.uiDatabaseConnection beginLongLivedReadTransaction]; [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(applicationWillEnterForeground:) - name:OWSApplicationWillEnterForegroundNotification + selector:@selector(applicationDidBecomeActive:) + name:OWSApplicationDidBecomeActiveNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(applicationDidEnterBackground:) - name:OWSApplicationDidEnterBackgroundNotification + selector:@selector(applicationWillResignActive:) + name:OWSApplicationWillResignActiveNotification object:nil]; - self.shouldObserveDBModifications = !CurrentAppContext().isInBackground; + [self updateShouldObserveDBModifications]; } -- (void)applicationWillEnterForeground:(NSNotification *)notification +- (void)applicationDidBecomeActive:(NSNotification *)notification { - self.shouldObserveDBModifications = YES; + [self updateShouldObserveDBModifications]; } -- (void)applicationDidEnterBackground:(NSNotification *)notification +- (void)applicationWillResignActive:(NSNotification *)notification { - self.shouldObserveDBModifications = NO; + [self updateShouldObserveDBModifications]; +} + +- (void)updateShouldObserveDBModifications +{ + BOOL isAppForegroundAndActive = CurrentAppContext().reportedApplicationState == UIApplicationStateActive; + self.shouldObserveDBModifications = isAppForegroundAndActive; } // Don't observe database change notifications when the app is in the background.