@ -27,6 +27,7 @@ typedef BOOL (^ContactSearchBlock)(id, NSUInteger, BOOL*);
}
return self ;
}
- ( void ) doAfterEnvironmentInitSetup {
[ self setupAddressBook ] ;
[ observableContactsController watchLatestValueOnArbitraryThread : ^( NSArray * latestContacts ) {
@ -46,6 +47,12 @@ typedef BOOL (^ContactSearchBlock)(id, NSUInteger, BOOL*);
[ life cancel ] ;
}
- ( void ) verifyABPermission {
if ( !addressBookReference ) {
[ self setupAddressBook ] ;
}
}
#pragma mark - Notification Handlers
- ( void ) registerNotificationHandlers {
[ [ NSNotificationCenter defaultCenter ] addObserver : self selector : @ selector ( updatedDirectoryHandler : ) name : NOTIFICATION_DIRECTORY_UPDATE object : nil ] ;
@ -55,7 +62,7 @@ typedef BOOL (^ContactSearchBlock)(id, NSUInteger, BOOL*);
NSArray * currentUsers = [ self getSignalUsersFromContactsArray : latestContactsById . allValues ] ;
[ observableRedPhoneUsersController updateValue : currentUsers ] ;
}
}
#pragma mark - Address Book callbacks
@ -88,7 +95,9 @@ void onAddressBookChanged(ABAddressBookRef notifyAddressBook, CFDictionaryRef in
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions ( NULL , & creationError ) ;
checkOperationDescribe ( nil == creationError , [ ( ( __bridge NSError * ) creationError ) localizedDescription ] ) ;
ABAddressBookRequestAccessWithCompletion ( addressBookRef , ^( bool granted , CFErrorRef error ) {
/ / TO DO : DISPLAY ALERT
if ( !granted ) {
[ ContactsManager blockingContactDialog ] ;
}
} ) ;
[ observableContactsController updateValue : [ self getContactsFromAddressBook : addressBookRef ] ] ;
}
@ -103,6 +112,20 @@ void onAddressBookChanged(ABAddressBookRef notifyAddressBook, CFDictionaryRef in
}
}
+ ( void ) blockingContactDialog {
UIAlertController * controller = [ UIAlertController alertControllerWithTitle : NSLocalizedString ( @ "AB_PERMISSION_MISSING_TITLE ", nil )
message : NSLocalizedString ( @ "AB_PERMISSION_MISSING_BODY ", nil )
preferredStyle : UIAlertControllerStyleAlert ] ;
[ controller addAction : [ UIAlertAction actionWithTitle : NSLocalizedString ( @ "AB_PERMISSION_MISSING_ACTION ", nil )
style : UIAlertActionStyleDefault
handler : ^( UIAlertAction * action ) {
[ [ UIApplication sharedApplication ] openURL : [ NSURL URLWithString : UIApplicationOpenSettingsURLString ] ] ;
} ] ] ;
[ [ [ UIApplication sharedApplication ] keyWindow ] . rootViewController presentViewController : controller animated : YES completion : nil ] ;
}
- ( void ) setupLatestRedPhoneUsers : ( NSArray * ) users {
if ( users ) {
latestWhisperUsersById = [ ContactsManager keyContactsById : users ] ;
@ -126,6 +149,7 @@ void onAddressBookChanged(ABAddressBookRef notifyAddressBook, CFDictionaryRef in
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions ( NULL , & creationError ) ;
assert ( ( addressBookRef == nil ) == ( creationError != nil ) ) ;
if ( creationError != nil ) {
[ self blockingContactDialog ] ;
return [ TOCFuture futureWithFailure : ( __bridge_transfer id ) creationError ] ;
}
@ -138,6 +162,7 @@ void onAddressBookChanged(ABAddressBookRef notifyAddressBook, CFDictionaryRef in
[ futureAddressBookSource trySetResult : addressBook ] ;
} ) ;
} else {
[ self blockingContactDialog ] ;
[ futureAddressBookSource trySetFailure : ( __bridge id ) requestAccessError ] ;
}
} ) ;
@ -389,7 +414,7 @@ void onAddressBookChanged(ABAddressBookRef notifyAddressBook, CFDictionaryRef in
#pragma mark - Whisper User Management
- ( NSArray * ) getSignalUsersFromContactsArray : ( NSArray * ) contacts {
return [ [ contacts filter : ^int ( Contact * contact ) {
return [ [ contacts filter : ^int ( Contact * contact ) {
return [ self isContactRegisteredWithRedPhone : contact ] || contact . isTextSecureContact ;
} ] sortedArrayUsingComparator : [ [ self class ] contactComparator ] ] ;
}
@ -420,25 +445,25 @@ void onAddressBookChanged(ABAddressBookRef notifyAddressBook, CFDictionaryRef in
}
- ( NSArray * ) getNewItemsFrom : ( NSArray * ) newArray comparedTo : ( NSArray * ) oldArray {
NSMutableSet * newSet = [ NSMutableSet setWithArray : newArray ] ;
NSSet * oldSet = [ NSSet setWithArray : oldArray ] ;
NSMutableSet * newSet = [ NSMutableSet setWithArray : newArray ] ;
NSSet * oldSet = [ NSSet setWithArray : oldArray ] ;
[ newSet minusSet : oldSet ] ;
return newSet . allObjects ;
[ newSet minusSet : oldSet ] ;
return newSet . allObjects ;
}
- ( BOOL ) isContactRegisteredWithRedPhone : ( Contact * ) contact {
for ( PhoneNumber * phoneNumber in contact . parsedPhoneNumbers ) {
if ( [ self isPhoneNumberRegisteredWithRedPhone : phoneNumber ] ) {
return YES ;
}
}
return NO ;
for ( PhoneNumber * phoneNumber in contact . parsedPhoneNumbers ) {
if ( [ self isPhoneNumberRegisteredWithRedPhone : phoneNumber ] ) {
return YES ;
}
}
return NO ;
}
- ( BOOL ) isPhoneNumberRegisteredWithRedPhone : ( PhoneNumber * ) phoneNumber {
PhoneNumberDirectoryFilter * directory = Environment . getCurrent . phoneDirectoryManager . getCurrentFilter ;
return phoneNumber != nil && [ directory containsPhoneNumber : phoneNumber ] ;
PhoneNumberDirectoryFilter * directory = Environment . getCurrent . phoneDirectoryManager . getCurrentFilter ;
return phoneNumber != nil && [ directory containsPhoneNumber : phoneNumber ] ;
}
- ( NSString * ) nameStringForPhoneIdentifier : ( NSString * ) identifier {