From 585079de2165a445568e2c64e269113f78c08e0a Mon Sep 17 00:00:00 2001 From: Frederic Jacobs Date: Sat, 31 Jan 2015 01:00:58 -1000 Subject: [PATCH] Fixes #553 --- .../view controllers/MessagesViewController.m | 53 ++++++++++++++----- 1 file changed, 40 insertions(+), 13 deletions(-) diff --git a/Signal/src/view controllers/MessagesViewController.m b/Signal/src/view controllers/MessagesViewController.m index c7ee75546..0029867e9 100644 --- a/Signal/src/view controllers/MessagesViewController.m +++ b/Signal/src/view controllers/MessagesViewController.m @@ -103,6 +103,8 @@ typedef enum : NSUInteger { @property NSUInteger page; +@property BOOL isVisible; + @end @implementation MessagesViewController @@ -143,11 +145,12 @@ typedef enum : NSUInteger { else if(![self isTextSecureReachable] ){ [self inputToolbar].hidden= YES; // only RedPhone self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"btnPhone--white"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(callAction)];; - } } + - (void)viewDidLoad { [super viewDidLoad]; + _isVisible = NO; [self.navigationController.navigationBar setTranslucent:NO]; _showFingerprintDisplay = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(showFingerprint)]; @@ -185,6 +188,7 @@ typedef enum : NSUInteger { [self updateRangeOptionsForPage:self.page]; + [self.uiDatabaseConnection beginLongLivedReadTransaction]; [self.uiDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { [self.messageMappings updateWithTransaction:transaction]; }]; @@ -216,7 +220,8 @@ typedef enum : NSUInteger { -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; - + + [self.collectionView reloadData]; NSInteger numberOfMessages = (NSInteger)[self.messageMappings numberOfItemsInGroup:self.thread.uniqueId]; if (numberOfMessages > 0) { @@ -237,6 +242,7 @@ typedef enum : NSUInteger { - (void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; [self startReadTimer]; + _isVisible = YES; } - (void)viewWillDisappear:(BOOL)animated{ @@ -274,6 +280,10 @@ typedef enum : NSUInteger { } +- (void)viewDidDisappear:(BOOL)animated{ + _isVisible = NO; +} + - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @@ -449,9 +459,6 @@ typedef enum : NSUInteger { return [PhoneNumber tryParsePhoneNumberFromUserSpecifiedText:contactId]; } - - - -(void)callAction { if ([self isRedPhoneReachable]) { @@ -1251,24 +1258,44 @@ typedef enum : NSUInteger { [self initializeToolbars]; }]; } - // Process the notification(s), - // and get the change-set(s) as applies to my view and mappings configuration. + NSArray *notifications = [self.uiDatabaseConnection beginLongLivedReadTransaction]; + + if ( ![[self.uiDatabaseConnection ext:TSMessageDatabaseViewExtensionName] hasChangesForNotifications:notifications]) + { + [self.uiDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction *transaction){ + [self.messageMappings updateWithTransaction:transaction]; + }]; + return; + } + + if (!_isVisible) + { + // Since we moved our databaseConnection to a new commit, + // we need to update the mappings too. + [self.uiDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction *transaction){ + [self.messageMappings updateWithTransaction:transaction]; + }]; + return; + } + NSArray *messageRowChanges = nil; - - [[self.uiDatabaseConnection ext:TSMessageDatabaseViewExtensionName] getSectionChanges:nil + NSArray *sectionChanges = nil; + + + [[self.uiDatabaseConnection ext:TSMessageDatabaseViewExtensionName] getSectionChanges:§ionChanges rowChanges:&messageRowChanges forNotifications:notifications withMappings:self.messageMappings]; __block BOOL scrollToBottom = NO; - - if (!messageRowChanges) { + + if ([sectionChanges count] == 0 & [messageRowChanges count] == 0) + { return; } - + [self.collectionView performBatchUpdates:^{ - for (YapDatabaseViewRowChange *rowChange in messageRowChanges) { switch (rowChange.type)