|
|
@ -1136,7 +1136,6 @@ NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
|
|
|
|
|
|
|
+ (void)createRandomContacts:(NSUInteger)count
|
|
|
|
+ (void)createRandomContacts:(NSUInteger)count
|
|
|
|
{
|
|
|
|
{
|
|
|
|
OWSAssert(count > 0);
|
|
|
|
|
|
|
|
[self createRandomContacts:count contactHandler:nil];
|
|
|
|
[self createRandomContacts:count contactHandler:nil];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -1144,6 +1143,33 @@ NS_ASSUME_NONNULL_BEGIN
|
|
|
|
contactHandler:
|
|
|
|
contactHandler:
|
|
|
|
(nullable void (^)(CNContact *_Nonnull contact, NSUInteger idx, BOOL *_Nonnull stop))contactHandler
|
|
|
|
(nullable void (^)(CNContact *_Nonnull contact, NSUInteger idx, BOOL *_Nonnull stop))contactHandler
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
OWSAssert(count > 0);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
NSUInteger remainder = count;
|
|
|
|
|
|
|
|
const NSUInteger kMaxBatchSize = 20;
|
|
|
|
|
|
|
|
NSUInteger batch = MIN(kMaxBatchSize, remainder);
|
|
|
|
|
|
|
|
remainder -= batch;
|
|
|
|
|
|
|
|
[self createRandomContactsBatch:batch
|
|
|
|
|
|
|
|
contactHandler:contactHandler
|
|
|
|
|
|
|
|
batchCompletionHandler:^{
|
|
|
|
|
|
|
|
if (remainder > 0) {
|
|
|
|
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
|
|
|
|
[self createRandomContacts:remainder contactHandler:contactHandler];
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+ (void)createRandomContactsBatch:(NSUInteger)count
|
|
|
|
|
|
|
|
contactHandler:(nullable void (^)(
|
|
|
|
|
|
|
|
CNContact *_Nonnull contact, NSUInteger idx, BOOL *_Nonnull stop))contactHandler
|
|
|
|
|
|
|
|
batchCompletionHandler:(nullable void (^)())batchCompletionHandler
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
OWSAssert(count > 0);
|
|
|
|
|
|
|
|
OWSAssert(batchCompletionHandler);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DDLogDebug(@"createRandomContactsBatch: %zd", count);
|
|
|
|
|
|
|
|
|
|
|
|
CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
|
|
|
|
CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
|
|
|
|
if (status == CNAuthorizationStatusDenied || status == CNAuthorizationStatusRestricted) {
|
|
|
|
if (status == CNAuthorizationStatusDenied || status == CNAuthorizationStatusRestricted) {
|
|
|
|
[OWSAlerts showAlertWithTitle:@"Error" message:@"No contacts access."];
|
|
|
|
[OWSAlerts showAlertWithTitle:@"Error" message:@"No contacts access."];
|
|
|
@ -1164,6 +1190,7 @@ NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
|
|
|
|
|
|
|
CNSaveRequest *request = [[CNSaveRequest alloc] init];
|
|
|
|
CNSaveRequest *request = [[CNSaveRequest alloc] init];
|
|
|
|
for (NSUInteger i = 0; i < count; i++) {
|
|
|
|
for (NSUInteger i = 0; i < count; i++) {
|
|
|
|
|
|
|
|
@autoreleasepool {
|
|
|
|
CNMutableContact *contact = [[CNMutableContact alloc] init];
|
|
|
|
CNMutableContact *contact = [[CNMutableContact alloc] init];
|
|
|
|
contact.familyName = [@"Rando-" stringByAppendingString:[self randomLastName]];
|
|
|
|
contact.familyName = [@"Rando-" stringByAppendingString:[self randomLastName]];
|
|
|
|
contact.givenName = [self randomFirstName];
|
|
|
|
contact.givenName = [self randomFirstName];
|
|
|
@ -1183,9 +1210,9 @@ NS_ASSUME_NONNULL_BEGIN
|
|
|
|
NSUInteger avatarDiameter
|
|
|
|
NSUInteger avatarDiameter
|
|
|
|
= arc4random_uniform(kMaximumAvatarDiameter - kMinimumAvatarDiameter)
|
|
|
|
= arc4random_uniform(kMaximumAvatarDiameter - kMinimumAvatarDiameter)
|
|
|
|
+ kMinimumAvatarDiameter;
|
|
|
|
+ kMinimumAvatarDiameter;
|
|
|
|
// Note this doesn't work on iOS9, since iOS9 doesn't generate the imageThumbnailData from
|
|
|
|
// Note this doesn't work on iOS9, since iOS9 doesn't generate the imageThumbnailData
|
|
|
|
// programmatically assigned imageData. We could make our own thumbnail in Contact.m, but
|
|
|
|
// from programmatically assigned imageData. We could make our own thumbnail in
|
|
|
|
// it's not worth it for the sake of debug UI.
|
|
|
|
// Contact.m, but it's not worth it for the sake of debug UI.
|
|
|
|
contact.imageData = UIImageJPEGRepresentation(
|
|
|
|
contact.imageData = UIImageJPEGRepresentation(
|
|
|
|
[OWSAvatarBuilder buildRandomAvatarWithDiameter:avatarDiameter], (CGFloat)0.9);
|
|
|
|
[OWSAvatarBuilder buildRandomAvatarWithDiameter:avatarDiameter], (CGFloat)0.9);
|
|
|
|
DDLogDebug(@"avatar size: %lu bytes", (unsigned long)contact.imageData.length);
|
|
|
|
DDLogDebug(@"avatar size: %lu bytes", (unsigned long)contact.imageData.length);
|
|
|
@ -1194,15 +1221,22 @@ NS_ASSUME_NONNULL_BEGIN
|
|
|
|
[contacts addObject:contact];
|
|
|
|
[contacts addObject:contact];
|
|
|
|
[request addContact:contact toContainerWithIdentifier:nil];
|
|
|
|
[request addContact:contact toContainerWithIdentifier:nil];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DDLogError(@"Saving fake contacts: %zd", contacts.count);
|
|
|
|
|
|
|
|
|
|
|
|
NSError *saveError = nil;
|
|
|
|
NSError *saveError = nil;
|
|
|
|
if (![store executeSaveRequest:request error:&saveError]) {
|
|
|
|
if (![store executeSaveRequest:request error:&saveError]) {
|
|
|
|
NSLog(@"error = %@", saveError);
|
|
|
|
DDLogError(@"Error saving fake contacts: %@", saveError);
|
|
|
|
[OWSAlerts showAlertWithTitle:@"Error" message:saveError.localizedDescription];
|
|
|
|
[OWSAlerts showAlertWithTitle:@"Error" message:saveError.localizedDescription];
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
if (contactHandler) {
|
|
|
|
if (contactHandler) {
|
|
|
|
[contacts enumerateObjectsUsingBlock:contactHandler];
|
|
|
|
[contacts enumerateObjectsUsingBlock:contactHandler];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (batchCompletionHandler) {
|
|
|
|
|
|
|
|
batchCompletionHandler();
|
|
|
|
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -1243,10 +1277,10 @@ NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
|
|
|
|
|
|
|
NSError *saveError = nil;
|
|
|
|
NSError *saveError = nil;
|
|
|
|
if (!result || fetchError) {
|
|
|
|
if (!result || fetchError) {
|
|
|
|
NSLog(@"error = %@", fetchError);
|
|
|
|
DDLogError(@"error = %@", fetchError);
|
|
|
|
[OWSAlerts showAlertWithTitle:@"Error" message:fetchError.localizedDescription];
|
|
|
|
[OWSAlerts showAlertWithTitle:@"Error" message:fetchError.localizedDescription];
|
|
|
|
} else if (![store executeSaveRequest:request error:&saveError]) {
|
|
|
|
} else if (![store executeSaveRequest:request error:&saveError]) {
|
|
|
|
NSLog(@"error = %@", saveError);
|
|
|
|
DDLogError(@"error = %@", saveError);
|
|
|
|
[OWSAlerts showAlertWithTitle:@"Error" message:saveError.localizedDescription];
|
|
|
|
[OWSAlerts showAlertWithTitle:@"Error" message:saveError.localizedDescription];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
}];
|
|
|
|