Fix the "is contact" test.

// FREEBIE
This commit is contained in:
Matthew Chen 2017-10-02 11:47:46 -04:00 committed by Michael Kirk
parent 50ec55c313
commit 10c00501fd
5 changed files with 49 additions and 19 deletions

View File

@ -837,6 +837,7 @@ static NSString *const kURLHostVerifyPrefix = @"verify";
[OWSProfileManager.sharedManager fetchLocalUsersProfile];
[[OWSReadReceiptManager sharedManager] prepareCachedValues];
[[Environment getCurrent].contactsManager loadLastKnownContactRecipientIds];
}
- (void)registrationStateDidChange

View File

@ -423,8 +423,8 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) {
name:kNSNotificationName_ProfileWhitelistDidChange
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(contactsDidChange:)
name:OWSContactsManagerContactListDidChangeNotification
selector:@selector(signalAccountsDidChange:)
name:OWSContactsManagerSignalAccountsDidChangeNotification
object:nil];
}
@ -446,7 +446,7 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];
}
- (void)contactsDidChange:(NSNotification *)notification
- (void)signalAccountsDidChange:(NSNotification *)notification
{
OWSAssert([NSThread isMainThread]);

View File

@ -8,9 +8,6 @@
NS_ASSUME_NONNULL_BEGIN
extern NSString *const OWSContactsManagerSignalAccountsDidChangeNotification;
// Fired when the list of contact phone numbers changes, not when
// other contact properties change.
extern NSString *const OWSContactsManagerContactListDidChangeNotification;
@class ImageCache;
@class SignalAccount;
@ -39,10 +36,27 @@ extern NSString *const OWSContactsManagerContactListDidChangeNotification;
@property (atomic, readonly) NSDictionary<NSString *, SignalAccount *> *signalAccountMap;
@property (atomic, readonly) NSArray<SignalAccount *> *signalAccounts;
// This value is cached and is available immediately, before system contacts
// fetch or contacts intersection.
//
// In some cases, its better if our UI reflects these values
// which haven't been updated yet rather than assume that
// we have no contacts until the first contacts intersection
// successfully completes.
//
// This significantly improves the user experience when:
//
// * No contacts intersection has completed because the app has just launched.
// * Contacts intersection can't complete due to an unreliable connection or
// the contacts intersection rate limit.
@property (atomic, readonly) NSArray<NSString *> *lastKnownContactRecipientIds;
- (nullable SignalAccount *)signalAccountForRecipientId:(NSString *)recipientId;
- (Contact *)getOrBuildContactForPhoneIdentifier:(NSString *)identifier;
- (void)loadLastKnownContactRecipientIds;
#pragma mark - System Contact Fetching
// Must call `requestSystemContactsOnce` before accessing this method

View File

@ -16,14 +16,14 @@
@import Contacts;
NSString *const OWSContactsManagerSignalAccountsDidChangeNotification =
@"OWSContactsManagerSignalAccountsDidChangeNotification";
NSString *const OWSContactsManagerContactListDidChangeNotification =
@"OWSContactsManagerContactListDidChangeNotification";
NSString *const OWSContactsManagerSignalAccountsDidChangeNotification
= @"OWSContactsManagerSignalAccountsDidChangeNotification";
NSString *const kTSStorageManager_AccountDisplayNames = @"kTSStorageManager_AccountDisplayNames";
NSString *const kTSStorageManager_AccountFirstNames = @"kTSStorageManager_AccountFirstNames";
NSString *const kTSStorageManager_AccountLastNames = @"kTSStorageManager_AccountLastNames";
NSString *const kTSStorageManager_OWSContactsManager = @"kTSStorageManager_OWSContactsManager";
NSString *const kTSStorageManager_lastKnownContactRecipientIds = @"lastKnownContactRecipientIds";
@interface OWSContactsManager () <SystemContactsFetcherDelegate>
@ -36,6 +36,7 @@ NSString *const kTSStorageManager_AccountLastNames = @"kTSStorageManager_Account
@property (atomic) NSDictionary<NSString *, Contact *> *allContactsMap;
@property (atomic) NSArray<SignalAccount *> *signalAccounts;
@property (atomic) NSDictionary<NSString *, SignalAccount *> *signalAccountMap;
@property (atomic) NSArray<NSString *> *lastKnownContactRecipientIds;
@property (nonatomic, readonly) SystemContactsFetcher *systemContactsFetcher;
@property (atomic) NSDictionary<NSString *, NSString *> *cachedAccountNameMap;
@ -58,6 +59,7 @@ NSString *const kTSStorageManager_AccountLastNames = @"kTSStorageManager_Account
_allContactsMap = @{};
_signalAccountMap = @{};
_signalAccounts = @[];
_lastKnownContactRecipientIds = @[];
_systemContactsFetcher = [SystemContactsFetcher new];
_systemContactsFetcher.delegate = self;
@ -68,6 +70,18 @@ NSString *const kTSStorageManager_AccountLastNames = @"kTSStorageManager_Account
return self;
}
- (void)loadLastKnownContactRecipientIds
{
[TSStorageManager.sharedManager.newDatabaseConnection readWithBlock:^(
YapDatabaseReadTransaction *_Nonnull transaction) {
NSArray<NSString *> *_Nullable value = [transaction objectForKey:kTSStorageManager_lastKnownContactRecipientIds
inCollection:kTSStorageManager_OWSContactsManager];
if (value) {
self.lastKnownContactRecipientIds = value;
}
}];
}
#pragma mark - System Contact Fetching
// Request contacts access if you haven't asked recently.
@ -178,8 +192,6 @@ NSString *const kTSStorageManager_AccountLastNames = @"kTSStorageManager_Account
}
dispatch_async(dispatch_get_main_queue(), ^{
BOOL didContactListChange = ![self.allContactsMap isEqual:allContactsMap];
self.allContacts = contacts;
self.allContactsMap = [allContactsMap copy];
@ -190,12 +202,6 @@ NSString *const kTSStorageManager_AccountLastNames = @"kTSStorageManager_Account
[self updateSignalAccounts];
[self updateCachedDisplayNames];
if (didContactListChange) {
[[NSNotificationCenter defaultCenter]
postNotificationNameAsync:OWSContactsManagerContactListDidChangeNotification
object:nil];
}
});
});
}
@ -237,7 +243,16 @@ NSString *const kTSStorageManager_AccountLastNames = @"kTSStorageManager_Account
}
}
NSArray<NSString *> *lastKnownContactRecipientIds = [signalAccountMap allKeys];
[TSStorageManager.sharedManager.newDatabaseConnection
readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) {
[transaction setObject:lastKnownContactRecipientIds
forKey:kTSStorageManager_lastKnownContactRecipientIds
inCollection:kTSStorageManager_OWSContactsManager];
}];
dispatch_async(dispatch_get_main_queue(), ^{
self.lastKnownContactRecipientIds = lastKnownContactRecipientIds;
self.signalAccountMap = [signalAccountMap copy];
self.signalAccounts = [signalAccounts copy];

View File

@ -385,7 +385,7 @@ NS_ASSUME_NONNULL_BEGIN
shouldHaveAddToProfileWhitelistOffer = NO;
}
BOOL isContact = contactsManager.allContactsMap[recipientId] != nil;
BOOL isContact = [contactsManager.lastKnownContactRecipientIds containsObject:recipientId];
if (isContact) {
// Only create "add to contacts" offers for non-contacts.
shouldHaveAddToContactsOffer = NO;