|
|
|
@ -3,6 +3,7 @@
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#import "ThreadViewHelper.h"
|
|
|
|
|
#import <SignalServiceKit/AppContext.h>
|
|
|
|
|
#import <SignalServiceKit/TSDatabaseView.h>
|
|
|
|
|
#import <SignalServiceKit/TSStorageManager.h>
|
|
|
|
|
#import <SignalServiceKit/TSThread.h>
|
|
|
|
@ -16,9 +17,12 @@ NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
|
|
|
|
|
@property (nonatomic) YapDatabaseConnection *uiDatabaseConnection;
|
|
|
|
|
@property (nonatomic) YapDatabaseViewMappings *threadMappings;
|
|
|
|
|
@property (nonatomic) BOOL shouldObserveDBModifications;
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
|
|
|
|
|
@implementation ThreadViewHelper
|
|
|
|
|
|
|
|
|
|
- (instancetype)init
|
|
|
|
@ -48,49 +52,117 @@ NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
[[YapDatabaseViewMappings alloc] initWithGroups:@[ grouping ] view:TSThreadDatabaseViewExtensionName];
|
|
|
|
|
[self.threadMappings setIsReversed:YES forGroup:grouping];
|
|
|
|
|
|
|
|
|
|
__weak ThreadViewHelper *weakSelf = self;
|
|
|
|
|
[self.uiDatabaseConnection asyncReadWithBlock:^(YapDatabaseReadTransaction *transaction) {
|
|
|
|
|
[self.threadMappings updateWithTransaction:transaction];
|
|
|
|
|
self.uiDatabaseConnection = [TSStorageManager.sharedManager newDatabaseConnection];
|
|
|
|
|
[self.uiDatabaseConnection beginLongLivedReadTransaction];
|
|
|
|
|
|
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
|
[weakSelf updateThreads];
|
|
|
|
|
[weakSelf.delegate threadListDidChange];
|
|
|
|
|
});
|
|
|
|
|
}];
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
|
selector:@selector(applicationWillEnterForeground:)
|
|
|
|
|
name:UIApplicationWillEnterForegroundNotification
|
|
|
|
|
object:nil];
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
|
selector:@selector(applicationDidEnterBackground:)
|
|
|
|
|
name:UIApplicationDidEnterBackgroundNotification
|
|
|
|
|
object:nil];
|
|
|
|
|
|
|
|
|
|
self.shouldObserveDBModifications
|
|
|
|
|
= !(CurrentAppContext().isMainApp && CurrentAppContext().mainApplicationState == UIApplicationStateBackground);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#pragma mark - Database
|
|
|
|
|
- (void)applicationWillEnterForeground:(NSNotification *)notification
|
|
|
|
|
{
|
|
|
|
|
self.shouldObserveDBModifications = YES;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (YapDatabaseConnection *)uiDatabaseConnection
|
|
|
|
|
- (void)applicationDidEnterBackground:(NSNotification *)notification
|
|
|
|
|
{
|
|
|
|
|
self.shouldObserveDBModifications = NO;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Don't observe database change notifications when the app is in the background.
|
|
|
|
|
//
|
|
|
|
|
// Instead, rebuild model state when app enters foreground.
|
|
|
|
|
- (void)setShouldObserveDBModifications:(BOOL)shouldObserveDBModifications
|
|
|
|
|
{
|
|
|
|
|
NSAssert([NSThread isMainThread], @"Must access uiDatabaseConnection on main thread!");
|
|
|
|
|
if (!_uiDatabaseConnection) {
|
|
|
|
|
YapDatabase *database = TSStorageManager.sharedManager.database;
|
|
|
|
|
_uiDatabaseConnection = [database newConnection];
|
|
|
|
|
[_uiDatabaseConnection beginLongLivedReadTransaction];
|
|
|
|
|
if (_shouldObserveDBModifications == shouldObserveDBModifications) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_shouldObserveDBModifications = shouldObserveDBModifications;
|
|
|
|
|
|
|
|
|
|
if (shouldObserveDBModifications) {
|
|
|
|
|
[self.uiDatabaseConnection beginLongLivedReadTransaction];
|
|
|
|
|
[self.uiDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
|
|
|
|
|
[self.threadMappings updateWithTransaction:transaction];
|
|
|
|
|
}];
|
|
|
|
|
[self updateThreads];
|
|
|
|
|
[self.delegate threadListDidChange];
|
|
|
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
|
selector:@selector(yapDatabaseModified:)
|
|
|
|
|
name:YapDatabaseModifiedNotification
|
|
|
|
|
object:database];
|
|
|
|
|
object:nil];
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
|
selector:@selector(yapDatabaseModified:)
|
|
|
|
|
selector:@selector(yapDatabaseModifiedExternally:)
|
|
|
|
|
name:YapDatabaseModifiedExternallyNotification
|
|
|
|
|
object:database];
|
|
|
|
|
object:nil];
|
|
|
|
|
} else {
|
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self name:YapDatabaseModifiedNotification object:nil];
|
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self
|
|
|
|
|
name:YapDatabaseModifiedExternallyNotification
|
|
|
|
|
object:nil];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#pragma mark - Database
|
|
|
|
|
|
|
|
|
|
- (YapDatabaseConnection *)uiDatabaseConnection
|
|
|
|
|
{
|
|
|
|
|
OWSAssert([NSThread isMainThread]);
|
|
|
|
|
|
|
|
|
|
return _uiDatabaseConnection;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)yapDatabaseModifiedExternally:(NSNotification *)notification
|
|
|
|
|
{
|
|
|
|
|
OWSAssert([NSThread isMainThread]);
|
|
|
|
|
|
|
|
|
|
DDLogVerbose(@"%@ %s", self.logTag, __PRETTY_FUNCTION__);
|
|
|
|
|
|
|
|
|
|
// External database modifications can't be converted into incremental updates,
|
|
|
|
|
// so rebuild everything. This is expensive and usually isn't necessary, but
|
|
|
|
|
// there's no alternative.
|
|
|
|
|
[self.uiDatabaseConnection beginLongLivedReadTransaction];
|
|
|
|
|
[self.uiDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
|
|
|
|
|
[self.threadMappings updateWithTransaction:transaction];
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
|
|
[self updateThreads];
|
|
|
|
|
[self.delegate threadListDidChange];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)yapDatabaseModified:(NSNotification *)notification
|
|
|
|
|
{
|
|
|
|
|
OWSAssert([NSThread isMainThread]);
|
|
|
|
|
|
|
|
|
|
DDLogVerbose(@"%@ %s", self.logTag, __PRETTY_FUNCTION__);
|
|
|
|
|
|
|
|
|
|
NSArray *notifications = [self.uiDatabaseConnection beginLongLivedReadTransaction];
|
|
|
|
|
|
|
|
|
|
if (!
|
|
|
|
|
[[self.uiDatabaseConnection ext:TSMessageDatabaseViewExtensionName] hasChangesForNotifications:notifications]) {
|
|
|
|
|
[self.uiDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
|
|
|
|
|
[self.threadMappings updateWithTransaction:transaction];
|
|
|
|
|
}];
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NSArray *sectionChanges = nil;
|
|
|
|
|
NSArray *rowChanges = nil;
|
|
|
|
|
[[self.uiDatabaseConnection ext:TSThreadDatabaseViewExtensionName] getSectionChanges:§ionChanges
|
|
|
|
|
rowChanges:&rowChanges
|
|
|
|
|
forNotifications:notifications
|
|
|
|
|
withMappings:self.threadMappings];
|
|
|
|
|
|
|
|
|
|
if (sectionChanges.count == 0 && rowChanges.count == 0) {
|
|
|
|
|
// Ignore irrelevant modifications.
|
|
|
|
|
return;
|
|
|
|
|