[DEBUG-UI] create fake contact threads

// FREEBIE
This commit is contained in:
Michael Kirk 2017-07-31 17:40:34 -04:00
parent bdd31fc772
commit 092578045e
3 changed files with 52 additions and 2 deletions

View file

@ -7,9 +7,14 @@
NS_ASSUME_NONNULL_BEGIN
@class OWSTableSection;
@class CNContact;
@interface DebugUIContacts : DebugUIPage
+ (void)createRandomContacts:(NSUInteger)count
contactHandler:
(nullable void (^)(CNContact *_Nonnull contact, NSUInteger idx, BOOL *_Nonnull stop))contactHandler;
@end
NS_ASSUME_NONNULL_END

View file

@ -1137,13 +1137,20 @@ NS_ASSUME_NONNULL_BEGIN
+ (void)createRandomContacts:(int)count
{
OWSAssert(count > 0);
[self createRandomContacts:count contactHandler:nil];
}
+ (void)createRandomContacts:(NSUInteger)count
contactHandler:
(nullable void (^)(CNContact *_Nonnull contact, NSUInteger idx, BOOL *_Nonnull stop))contactHandler
{
CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
if (status == CNAuthorizationStatusDenied || status == CNAuthorizationStatusRestricted) {
[OWSAlerts showAlertWithTitle:@"Error" message:@"No contacts access."];
return;
}
NSMutableArray<CNContact *> *contacts = [NSMutableArray new];
CNContactStore *store = [[CNContactStore alloc] init];
[store
requestAccessForEntityType:CNEntityTypeContacts
@ -1166,12 +1173,17 @@ NS_ASSUME_NONNULL_BEGIN
value:[CNPhoneNumber phoneNumberWithStringValue:[self randomPhoneNumber]]];
contact.phoneNumbers = @[ homePhone ];
[contacts addObject:contact];
[request addContact:contact toContainerWithIdentifier:nil];
}
NSError *saveError = nil;
if (![store executeSaveRequest:request error:&saveError]) {
NSLog(@"error = %@", saveError);
[OWSAlerts showAlertWithTitle:@"Error" message:saveError.localizedDescription];
} else {
if (contactHandler) {
[contacts enumerateObjectsUsingBlock:contactHandler];
}
}
}];
}

View file

@ -3,6 +3,7 @@
//
#import "DebugUIMessages.h"
#import "DebugUIContacts.h"
#import "Environment.h"
#import "OWSTableViewController.h"
#import "Signal-Swift.h"
@ -82,6 +83,22 @@ NS_ASSUME_NONNULL_BEGIN
actionBlock:^{
[DebugUIMessages sendFakeMessages:10 thread:thread];
}],
[OWSTableItem itemWithTitle:@"Create 100 fake threads with 10 messages"
actionBlock:^{
[DebugUIMessages createFakeThreads:100 withFakeMessages:10];
}],
[OWSTableItem itemWithTitle:@"Create 10 fake threads with 100 messages"
actionBlock:^{
[DebugUIMessages createFakeThreads:10 withFakeMessages:100];
}],
[OWSTableItem itemWithTitle:@"Create 10 fake threads with 10 messages"
actionBlock:^{
[DebugUIMessages createFakeThreads:10 withFakeMessages:10];
}],
[OWSTableItem itemWithTitle:@"Create 100 fake threads with 100 messages"
actionBlock:^{
[DebugUIMessages createFakeThreads:100 withFakeMessages:100];
}],
[OWSTableItem itemWithTitle:@"Create 1k fake messages"
actionBlock:^{
[DebugUIMessages sendFakeMessages:1000 thread:thread];
@ -836,11 +853,27 @@ NS_ASSUME_NONNULL_BEGIN
}];
}
+ (void)sendFakeMessages:(int)counter thread:(TSThread *)thread
+ (void)createFakeThreads:(NSUInteger)threadCount withFakeMessages:(NSUInteger)messageCount
{
[DebugUIContacts
createRandomContacts:threadCount
contactHandler:^(CNContact *_Nonnull contact, NSUInteger idx, BOOL *_Nonnull stop) {
NSString *phoneNumberText = contact.phoneNumbers.firstObject.value.stringValue;
OWSAssert(phoneNumberText);
PhoneNumber *phoneNumber = [PhoneNumber tryParsePhoneNumberFromUserSpecifiedText:phoneNumberText];
OWSAssert(phoneNumber);
OWSAssert(phoneNumber.toE164);
TSContactThread *contactThread = [TSContactThread getOrCreateThreadWithContactId:phoneNumber.toE164];
[self sendFakeMessages:messageCount thread:contactThread];
}];
}
+ (void)sendFakeMessages:(NSUInteger)counter thread:(TSThread *)thread
{
[TSStorageManager.sharedManager.dbReadWriteConnection readWriteWithBlock:^(
YapDatabaseReadWriteTransaction *transaction) {
for (int i = 0; i < counter; i++) {
for (NSUInteger i = 0; i < counter; i++) {
NSString *randomText = [self randomText];
switch (arc4random_uniform(4)) {
case 0: {