From 76df8431a8997162c86c267e8b91cad3dacf49c2 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Tue, 30 May 2017 12:19:17 -0400 Subject: [PATCH] Reset the unread indicator state if possible while app is in the background. * Clean up timer usage. // FREEBIE --- .../ViewControllers/MessagesViewController.m | 32 +++++++++++++++---- .../OWSAudioAttachmentPlayer.m | 1 + .../OWSLinkedDevicesTableViewController.m | 11 ++++--- .../SignalsNavigationController.m | 14 ++++---- Signal/src/util/ThreadUtil.m | 2 +- 5 files changed, 42 insertions(+), 18 deletions(-) diff --git a/Signal/src/ViewControllers/MessagesViewController.m b/Signal/src/ViewControllers/MessagesViewController.m index 3aa3da3af..fa8ba8ab6 100644 --- a/Signal/src/ViewControllers/MessagesViewController.m +++ b/Signal/src/ViewControllers/MessagesViewController.m @@ -585,6 +585,7 @@ typedef enum : NSUInteger { } }]; + [self.voiceMemoUpdateTimer invalidate]; self.voiceMemoUpdateTimer = [NSTimer weakScheduledTimerWithTimeInterval:0.1f target:self selector:@selector(updateVoiceMemo) @@ -971,6 +972,10 @@ typedef enum : NSUInteger { selector:@selector(applicationWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(applicationDidEnterBackground:) + name:UIApplicationDidEnterBackgroundNotification + object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification @@ -1006,6 +1011,16 @@ typedef enum : NSUInteger { [self ensureThreadOffersAndIndicators]; } +- (void)applicationDidEnterBackground:(NSNotification *)notification +{ + // self.hasEnteredBackground = YES; + + if (self.hasClearedUnreadMessagesIndicator) { + self.hasClearedUnreadMessagesIndicator = NO; + self.offersAndIndicators = nil; + } +} + - (void)applicationWillResignActive:(NSNotification *)notification { [self cancelVoiceMemo]; @@ -1279,11 +1294,12 @@ typedef enum : NSUInteger { } - (void)startReadTimer { - self.readTimer = [NSTimer scheduledTimerWithTimeInterval:1 - target:self - selector:@selector(readTimerDidFire) - userInfo:nil - repeats:YES]; + [self.readTimer invalidate]; + self.readTimer = [NSTimer weakScheduledTimerWithTimeInterval:2 + target:self + selector:@selector(readTimerDidFire) + userInfo:nil + repeats:YES]; } - (void)readTimerDidFire @@ -2972,7 +2988,11 @@ typedef enum : NSUInteger { // make sure we don't show it again. self.hasClearedUnreadMessagesIndicator = YES; - [self ensureThreadOffersAndIndicators]; + if (self.offersAndIndicators.unreadIndicatorPosition) { + // If we've just cleared the "unread messages" indicator, + // update the dynamic interactions. + [self ensureThreadOffersAndIndicators]; + } } #pragma mark - Attachment Picking: Documents diff --git a/Signal/src/ViewControllers/OWSAudioAttachmentPlayer.m b/Signal/src/ViewControllers/OWSAudioAttachmentPlayer.m index 740a950a7..26ca60eb7 100644 --- a/Signal/src/ViewControllers/OWSAudioAttachmentPlayer.m +++ b/Signal/src/ViewControllers/OWSAudioAttachmentPlayer.m @@ -127,6 +127,7 @@ NS_ASSUME_NONNULL_BEGIN [self.audioPlayer prepareToPlay]; [self.audioPlayer play]; + [self.audioPlayerPoller invalidate]; self.audioPlayerPoller = [NSTimer weakScheduledTimerWithTimeInterval:.05f target:self selector:@selector(audioPlayerUpdated:) diff --git a/Signal/src/ViewControllers/OWSLinkedDevicesTableViewController.m b/Signal/src/ViewControllers/OWSLinkedDevicesTableViewController.m index 001deb86c..ee8e25a88 100644 --- a/Signal/src/ViewControllers/OWSLinkedDevicesTableViewController.m +++ b/Signal/src/ViewControllers/OWSLinkedDevicesTableViewController.m @@ -6,6 +6,7 @@ #import "OWSDeviceTableViewCell.h" #import "OWSLinkDeviceViewController.h" #import "UIViewController+CameraPermissions.h" +#import #import #import #import @@ -100,11 +101,11 @@ int const OWSLinkedDevicesTableViewControllerSectionAddDevice = 1; self.editing = NO; __weak typeof(self) wself = self; - self.pollingRefreshTimer = [NSTimer scheduledTimerWithTimeInterval:(10.0) - target:wself - selector:@selector(refreshDevices) - userInfo:nil - repeats:YES]; + [self.pollingRefreshTimer invalidate]; + self.pollingRefreshTimer = [NSTimer weakScheduledTimerWithTimeInterval:(10.0)target:wself + selector:@selector(refreshDevices) + userInfo:nil + repeats:YES]; NSString *progressText = NSLocalizedString(@"WAITING_TO_COMPLETE_DEVICE_LINK_TEXT", @"Activity indicator title, shown upon returning to the device " diff --git a/Signal/src/ViewControllers/SignalsNavigationController.m b/Signal/src/ViewControllers/SignalsNavigationController.m index e8310f160..ea7c0bd65 100644 --- a/Signal/src/ViewControllers/SignalsNavigationController.m +++ b/Signal/src/ViewControllers/SignalsNavigationController.m @@ -4,6 +4,7 @@ #import "SignalsNavigationController.h" #import "UIUtil.h" +#import #import #import @@ -87,12 +88,13 @@ static double const STALLED_PROGRESS = 0.9; case SocketManagerStateClosed: if (_socketStatusView == nil) { [self initializeSocketStatusBar]; - _updateStatusTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 - target:self - selector:@selector(updateProgress) - userInfo:nil - repeats:YES]; - + [_updateStatusTimer invalidate]; + _updateStatusTimer = [NSTimer weakScheduledTimerWithTimeInterval:0.5 + target:self + selector:@selector(updateProgress) + userInfo:nil + repeats:YES]; + } else if (_socketStatusView.progress >= STALLED_PROGRESS) { [_updateStatusTimer invalidate]; } diff --git a/Signal/src/util/ThreadUtil.m b/Signal/src/util/ThreadUtil.m index a62a8c06c..0c46e2930 100644 --- a/Signal/src/util/ThreadUtil.m +++ b/Signal/src/util/ThreadUtil.m @@ -155,7 +155,7 @@ NS_ASSUME_NONNULL_BEGIN // have been marked as read. // // IFF this variable is non-null, there are unseen messages in the thread. - __block NSNumber *firstUnseenInteractionTimestamp = nil; + NSNumber *firstUnseenInteractionTimestamp = nil; if (firstUnseenInteractionTimestampParameter) { firstUnseenInteractionTimestamp = firstUnseenInteractionTimestampParameter; } else {