mirror of https://github.com/oxen-io/session-ios
				
				
				
			
			You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			321 lines
		
	
	
		
			16 KiB
		
	
	
	
		
			Matlab
		
	
		
		
			
		
	
	
			321 lines
		
	
	
		
			16 KiB
		
	
	
	
		
			Matlab
		
	
| 
											9 years ago
										 | // | ||
|  | //  Copyright (c) 2017 Open Whisper Systems. All rights reserved. | ||
|  | // | ||
|  | 
 | ||
|  | #import "BlockListUIUtils.h" | ||
| 
											9 years ago
										 | #import "OWSContactsManager.h" | ||
| 
											9 years ago
										 | #import "PhoneNumber.h" | ||
| 
											8 years ago
										 | #import <SignalMessaging/SignalMessaging-Swift.h> | ||
| 
											9 years ago
										 | #import <SignalServiceKit/Contact.h> | ||
|  | #import <SignalServiceKit/OWSBlockingManager.h> | ||
| 
											9 years ago
										 | #import <SignalServiceKit/SignalAccount.h> | ||
| 
											9 years ago
										 | #import <SignalServiceKit/TSAccountManager.h> | ||
| 
											9 years ago
										 | 
 | ||
|  | NS_ASSUME_NONNULL_BEGIN | ||
|  | 
 | ||
| 
											8 years ago
										 | typedef void (^BlockAlertCompletionBlock)(UIAlertAction *action); | ||
| 
											9 years ago
										 | 
 | ||
| 
											9 years ago
										 | @implementation BlockListUIUtils | ||
|  | 
 | ||
| 
											9 years ago
										 | #pragma mark - Block | ||
|  | 
 | ||
| 
											9 years ago
										 | + (void)showBlockPhoneNumberActionSheet:(NSString *)phoneNumber | ||
|  |                      fromViewController:(UIViewController *)fromViewController | ||
|  |                         blockingManager:(OWSBlockingManager *)blockingManager | ||
| 
											9 years ago
										 |                         contactsManager:(OWSContactsManager *)contactsManager | ||
| 
											9 years ago
										 |                         completionBlock:(nullable BlockActionCompletionBlock)completionBlock | ||
| 
											9 years ago
										 | { | ||
| 
											9 years ago
										 |     NSString *displayName = [contactsManager displayNameForPhoneIdentifier:phoneNumber]; | ||
| 
											9 years ago
										 |     [self showBlockPhoneNumbersActionSheet:@[ phoneNumber ] | ||
|  |                                displayName:displayName | ||
|  |                         fromViewController:fromViewController | ||
|  |                            blockingManager:blockingManager | ||
|  |                            completionBlock:completionBlock]; | ||
|  | } | ||
|  | 
 | ||
| 
											9 years ago
										 | + (void)showBlockSignalAccountActionSheet:(SignalAccount *)signalAccount | ||
|  |                        fromViewController:(UIViewController *)fromViewController | ||
|  |                           blockingManager:(OWSBlockingManager *)blockingManager | ||
|  |                           contactsManager:(OWSContactsManager *)contactsManager | ||
|  |                           completionBlock:(nullable BlockActionCompletionBlock)completionBlock | ||
| 
											9 years ago
										 | { | ||
| 
											9 years ago
										 |     NSString *displayName = [contactsManager displayNameForSignalAccount:signalAccount]; | ||
|  |     [self showBlockPhoneNumbersActionSheet:@[ signalAccount.recipientId ] | ||
| 
											9 years ago
										 |                                displayName:displayName | ||
|  |                         fromViewController:fromViewController | ||
|  |                            blockingManager:blockingManager | ||
|  |                            completionBlock:completionBlock]; | ||
|  | } | ||
|  | 
 | ||
| 
											9 years ago
										 | + (void)showBlockPhoneNumbersActionSheet:(NSArray<NSString *> *)phoneNumbers | ||
|  |                              displayName:(NSString *)displayName | ||
|  |                       fromViewController:(UIViewController *)fromViewController | ||
|  |                          blockingManager:(OWSBlockingManager *)blockingManager | ||
| 
											9 years ago
										 |                          completionBlock:(nullable BlockActionCompletionBlock)completionBlock | ||
| 
											9 years ago
										 | { | ||
|  |     OWSAssert(phoneNumbers.count > 0); | ||
|  |     OWSAssert(displayName.length > 0); | ||
|  |     OWSAssert(fromViewController); | ||
|  |     OWSAssert(blockingManager); | ||
|  | 
 | ||
| 
											9 years ago
										 |     NSString *localContactId = [TSAccountManager localNumber]; | ||
|  |     OWSAssert(localContactId.length > 0); | ||
|  |     for (NSString *phoneNumber in phoneNumbers) { | ||
|  |         OWSAssert(phoneNumber.length > 0); | ||
|  | 
 | ||
|  |         if ([localContactId isEqualToString:phoneNumber]) { | ||
|  |             [self showOkAlertWithTitle:NSLocalizedString(@"BLOCK_LIST_VIEW_CANT_BLOCK_SELF_ALERT_TITLE", | ||
|  |                                            @"The title of the 'You can't block yourself' alert.") | ||
|  |                                message:NSLocalizedString(@"BLOCK_LIST_VIEW_CANT_BLOCK_SELF_ALERT_MESSAGE", | ||
| 
											9 years ago
										 |                                            @"The message of the 'You can't block yourself' alert.") | ||
| 
											9 years ago
										 |                     fromViewController:fromViewController | ||
| 
											8 years ago
										 |                        completionBlock:^(UIAlertAction *action) { | ||
| 
											9 years ago
										 |                            if (completionBlock) { | ||
|  |                                completionBlock(NO); | ||
|  |                            } | ||
|  |                        }]; | ||
|  |             return; | ||
|  |         } | ||
|  |     } | ||
|  | 
 | ||
| 
											9 years ago
										 |     NSString *title = [NSString stringWithFormat:NSLocalizedString(@"BLOCK_LIST_BLOCK_TITLE_FORMAT", | ||
| 
											9 years ago
										 |                                                      @"A format for the 'block user' action sheet title. Embeds {{the " | ||
|  |                                                      @"blocked user's name or phone number}}."), | ||
| 
											9 years ago
										 |                                 [self formatDisplayNameForAlertTitle:displayName]]; | ||
| 
											9 years ago
										 | 
 | ||
|  |     UIAlertController *actionSheetController = | ||
| 
											9 years ago
										 |         [UIAlertController alertControllerWithTitle:title | ||
| 
											9 years ago
										 |                                             message:NSLocalizedString(@"BLOCK_BEHAVIOR_EXPLANATION", | ||
| 
											8 years ago
										 |                                                         @"An explanation of the consequences of blocking another user.") | ||
| 
											9 years ago
										 |                                      preferredStyle:UIAlertControllerStyleActionSheet]; | ||
| 
											9 years ago
										 | 
 | ||
|  |     UIAlertAction *unblockAction = [UIAlertAction | ||
|  |         actionWithTitle:NSLocalizedString(@"BLOCK_LIST_BLOCK_BUTTON", @"Button label for the 'block' button") | ||
| 
											9 years ago
										 |                   style:UIAlertActionStyleDestructive | ||
| 
											9 years ago
										 |                 handler:^(UIAlertAction *_Nonnull action) { | ||
|  |                     [self blockPhoneNumbers:phoneNumbers | ||
|  |                                 displayName:displayName | ||
|  |                          fromViewController:fromViewController | ||
| 
											9 years ago
										 |                             blockingManager:blockingManager | ||
| 
											8 years ago
										 |                             completionBlock:^(UIAlertAction *ignore) { | ||
| 
											9 years ago
										 |                                 if (completionBlock) { | ||
|  |                                     completionBlock(YES); | ||
|  |                                 } | ||
|  |                             }]; | ||
| 
											9 years ago
										 |                 }]; | ||
|  |     [actionSheetController addAction:unblockAction]; | ||
|  | 
 | ||
| 
											8 years ago
										 |     UIAlertAction *dismissAction = [UIAlertAction actionWithTitle:CommonStrings.cancelButton | ||
| 
											9 years ago
										 |                                                             style:UIAlertActionStyleCancel | ||
|  |                                                           handler:^(UIAlertAction *_Nonnull action) { | ||
|  |                                                               if (completionBlock) { | ||
|  |                                                                   completionBlock(NO); | ||
|  |                                                               } | ||
|  |                                                           }]; | ||
|  |     [actionSheetController addAction:dismissAction]; | ||
|  | 
 | ||
|  |     [fromViewController presentViewController:actionSheetController animated:YES completion:nil]; | ||
|  | } | ||
|  | 
 | ||
|  | + (void)blockPhoneNumbers:(NSArray<NSString *> *)phoneNumbers | ||
|  |               displayName:(NSString *)displayName | ||
|  |        fromViewController:(UIViewController *)fromViewController | ||
|  |           blockingManager:(OWSBlockingManager *)blockingManager | ||
| 
											9 years ago
										 |           completionBlock:(BlockAlertCompletionBlock)completionBlock | ||
| 
											9 years ago
										 | { | ||
|  |     OWSAssert(phoneNumbers.count > 0); | ||
|  |     OWSAssert(displayName.length > 0); | ||
|  |     OWSAssert(fromViewController); | ||
|  |     OWSAssert(blockingManager); | ||
|  | 
 | ||
|  |     for (NSString *phoneNumber in phoneNumbers) { | ||
|  |         OWSAssert(phoneNumber.length > 0); | ||
|  |         [blockingManager addBlockedPhoneNumber:phoneNumber]; | ||
|  |     } | ||
|  | 
 | ||
|  |     [self showOkAlertWithTitle:NSLocalizedString( | ||
|  |                                    @"BLOCK_LIST_VIEW_BLOCKED_ALERT_TITLE", @"The title of the 'user blocked' alert.") | ||
|  |                        message:[NSString | ||
|  |                                    stringWithFormat:NSLocalizedString(@"BLOCK_LIST_VIEW_BLOCKED_ALERT_MESSAGE_FORMAT", | ||
|  |                                                         @"The message format of the 'user blocked' " | ||
|  |                                                         @"alert. Embeds {{the blocked user's name or phone number}}."), | ||
| 
											9 years ago
										 |                                    [self formatDisplayNameForAlertMessage:displayName]] | ||
| 
											9 years ago
										 |             fromViewController:fromViewController | ||
|  |                completionBlock:completionBlock]; | ||
| 
											9 years ago
										 | } | ||
|  | 
 | ||
| 
											9 years ago
										 | #pragma mark - Unblock | ||
|  | 
 | ||
| 
											9 years ago
										 | + (void)showUnblockPhoneNumberActionSheet:(NSString *)phoneNumber | ||
|  |                        fromViewController:(UIViewController *)fromViewController | ||
|  |                           blockingManager:(OWSBlockingManager *)blockingManager | ||
| 
											9 years ago
										 |                           contactsManager:(OWSContactsManager *)contactsManager | ||
| 
											9 years ago
										 |                           completionBlock:(nullable BlockActionCompletionBlock)completionBlock | ||
| 
											9 years ago
										 | { | ||
| 
											9 years ago
										 |     NSString *displayName = [contactsManager displayNameForPhoneIdentifier:phoneNumber]; | ||
|  |     [self showUnblockPhoneNumbersActionSheet:@[ phoneNumber ] | ||
|  |                                  displayName:displayName | ||
|  |                           fromViewController:fromViewController | ||
|  |                              blockingManager:blockingManager | ||
|  |                              completionBlock:completionBlock]; | ||
| 
											9 years ago
										 | } | ||
|  | 
 | ||
| 
											9 years ago
										 | + (void)showUnblockSignalAccountActionSheet:(SignalAccount *)signalAccount | ||
|  |                          fromViewController:(UIViewController *)fromViewController | ||
|  |                             blockingManager:(OWSBlockingManager *)blockingManager | ||
|  |                             contactsManager:(OWSContactsManager *)contactsManager | ||
|  |                             completionBlock:(nullable BlockActionCompletionBlock)completionBlock | ||
| 
											9 years ago
										 | { | ||
| 
											9 years ago
										 |     NSString *displayName = [contactsManager displayNameForSignalAccount:signalAccount]; | ||
|  |     [self showUnblockPhoneNumbersActionSheet:@[ signalAccount.recipientId ] | ||
| 
											9 years ago
										 |                                  displayName:displayName | ||
|  |                           fromViewController:fromViewController | ||
|  |                              blockingManager:blockingManager | ||
|  |                              completionBlock:completionBlock]; | ||
| 
											9 years ago
										 | } | ||
|  | 
 | ||
|  | + (void)showUnblockPhoneNumbersActionSheet:(NSArray<NSString *> *)phoneNumbers | ||
|  |                                displayName:(NSString *)displayName | ||
|  |                         fromViewController:(UIViewController *)fromViewController | ||
|  |                            blockingManager:(OWSBlockingManager *)blockingManager | ||
|  |                            completionBlock:(nullable BlockActionCompletionBlock)completionBlock | ||
|  | { | ||
|  |     OWSAssert(phoneNumbers.count > 0); | ||
|  |     OWSAssert(displayName.length > 0); | ||
| 
											9 years ago
										 |     OWSAssert(fromViewController); | ||
|  |     OWSAssert(blockingManager); | ||
| 
											9 years ago
										 | 
 | ||
| 
											9 years ago
										 |     NSString *title = [NSString stringWithFormat:NSLocalizedString(@"BLOCK_LIST_UNBLOCK_TITLE_FORMAT", | ||
| 
											9 years ago
										 |                                                      @"A format for the 'unblock user' action sheet title. Embeds " | ||
|  |                                                      @"{{the blocked user's name or phone number}}."), | ||
| 
											9 years ago
										 |                                 [self formatDisplayNameForAlertTitle:displayName]]; | ||
| 
											9 years ago
										 | 
 | ||
|  |     UIAlertController *actionSheetController = | ||
|  |         [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleActionSheet]; | ||
|  | 
 | ||
|  |     UIAlertAction *unblockAction = [UIAlertAction | ||
|  |         actionWithTitle:NSLocalizedString(@"BLOCK_LIST_UNBLOCK_BUTTON", @"Button label for the 'unblock' button") | ||
| 
											9 years ago
										 |                   style:UIAlertActionStyleDestructive | ||
| 
											9 years ago
										 |                 handler:^(UIAlertAction *_Nonnull action) { | ||
| 
											9 years ago
										 |                     [BlockListUIUtils unblockPhoneNumbers:phoneNumbers | ||
|  |                                               displayName:displayName | ||
|  |                                        fromViewController:fromViewController | ||
|  |                                           blockingManager:blockingManager | ||
| 
											8 years ago
										 |                                           completionBlock:^(UIAlertAction *ignore) { | ||
| 
											9 years ago
										 |                                               if (completionBlock) { | ||
|  |                                                   completionBlock(NO); | ||
|  |                                               } | ||
|  |                                           }]; | ||
| 
											9 years ago
										 |                 }]; | ||
|  |     [actionSheetController addAction:unblockAction]; | ||
|  | 
 | ||
| 
											8 years ago
										 |     UIAlertAction *dismissAction = [UIAlertAction actionWithTitle:CommonStrings.cancelButton | ||
| 
											9 years ago
										 |                                                             style:UIAlertActionStyleCancel | ||
|  |                                                           handler:^(UIAlertAction *_Nonnull action) { | ||
|  |                                                               if (completionBlock) { | ||
|  |                                                                   completionBlock(YES); | ||
|  |                                                               } | ||
|  |                                                           }]; | ||
|  |     [actionSheetController addAction:dismissAction]; | ||
|  | 
 | ||
|  |     [fromViewController presentViewController:actionSheetController animated:YES completion:nil]; | ||
|  | } | ||
|  | 
 | ||
| 
											9 years ago
										 | + (void)unblockPhoneNumbers:(NSArray<NSString *> *)phoneNumbers | ||
|  |                 displayName:(NSString *)displayName | ||
|  |          fromViewController:(UIViewController *)fromViewController | ||
|  |             blockingManager:(OWSBlockingManager *)blockingManager | ||
|  |             completionBlock:(BlockAlertCompletionBlock)completionBlock | ||
| 
											9 years ago
										 | { | ||
| 
											9 years ago
										 |     OWSAssert(phoneNumbers.count > 0); | ||
| 
											9 years ago
										 |     OWSAssert(displayName.length > 0); | ||
|  |     OWSAssert(fromViewController); | ||
|  |     OWSAssert(blockingManager); | ||
|  | 
 | ||
| 
											9 years ago
										 |     for (NSString *phoneNumber in phoneNumbers) { | ||
|  |         OWSAssert(phoneNumber.length > 0); | ||
|  |         [blockingManager removeBlockedPhoneNumber:phoneNumber]; | ||
|  |     } | ||
| 
											9 years ago
										 | 
 | ||
|  |     [self showOkAlertWithTitle:NSLocalizedString(@"BLOCK_LIST_VIEW_UNBLOCKED_ALERT_TITLE", | ||
|  |                                    @"The title of the 'user unblocked' alert.") | ||
|  |                        message:[NSString | ||
|  |                                    stringWithFormat:NSLocalizedString(@"BLOCK_LIST_VIEW_UNBLOCKED_ALERT_MESSAGE_FORMAT", | ||
|  |                                                         @"The message format of the 'user unblocked' " | ||
| 
											9 years ago
										 |                                                         @"alert. Embeds {{the blocked user's name or phone number}}."), | ||
| 
											9 years ago
										 |                                    [self formatDisplayNameForAlertMessage:displayName]] | ||
| 
											9 years ago
										 |             fromViewController:fromViewController | ||
|  |                completionBlock:completionBlock]; | ||
| 
											9 years ago
										 | } | ||
|  | 
 | ||
|  | + (void)showBlockFailedAlert:(UIViewController *)fromViewController | ||
| 
											9 years ago
										 |              completionBlock:(BlockAlertCompletionBlock)completionBlock | ||
| 
											9 years ago
										 | { | ||
|  |     OWSAssert(fromViewController); | ||
|  | 
 | ||
|  |     [self showOkAlertWithTitle:NSLocalizedString(@"BLOCK_LIST_VIEW_BLOCK_FAILED_ALERT_TITLE", | ||
|  |                                    @"The title of the 'block user failed' alert.") | ||
|  |                        message:NSLocalizedString(@"BLOCK_LIST_VIEW_BLOCK_FAILED_ALERT_MESSAGE", | ||
|  |                                    @"The title of the 'block user failed' alert.") | ||
| 
											9 years ago
										 |             fromViewController:fromViewController | ||
|  |                completionBlock:completionBlock]; | ||
| 
											9 years ago
										 | } | ||
|  | 
 | ||
|  | + (void)showUnblockFailedAlert:(UIViewController *)fromViewController | ||
| 
											9 years ago
										 |                completionBlock:(BlockAlertCompletionBlock)completionBlock | ||
| 
											9 years ago
										 | { | ||
|  |     OWSAssert(fromViewController); | ||
|  | 
 | ||
|  |     [self showOkAlertWithTitle:NSLocalizedString(@"BLOCK_LIST_VIEW_UNBLOCK_FAILED_ALERT_TITLE", | ||
|  |                                    @"The title of the 'unblock user failed' alert.") | ||
|  |                        message:NSLocalizedString(@"BLOCK_LIST_VIEW_UNBLOCK_FAILED_ALERT_MESSAGE", | ||
|  |                                    @"The title of the 'unblock user failed' alert.") | ||
| 
											9 years ago
										 |             fromViewController:fromViewController | ||
|  |                completionBlock:completionBlock]; | ||
| 
											9 years ago
										 | } | ||
|  | 
 | ||
| 
											9 years ago
										 | #pragma mark - UI | ||
|  | 
 | ||
| 
											9 years ago
										 | + (void)showOkAlertWithTitle:(NSString *)title | ||
|  |                      message:(NSString *)message | ||
|  |           fromViewController:(UIViewController *)fromViewController | ||
| 
											9 years ago
										 |              completionBlock:(BlockAlertCompletionBlock)completionBlock | ||
| 
											9 years ago
										 | { | ||
|  |     OWSAssert(title.length > 0); | ||
|  |     OWSAssert(message.length > 0); | ||
|  |     OWSAssert(fromViewController); | ||
|  | 
 | ||
|  |     UIAlertController *controller = | ||
|  |         [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]; | ||
|  | 
 | ||
|  |     [controller addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) | ||
|  |                                                    style:UIAlertActionStyleDefault | ||
| 
											9 years ago
										 |                                                  handler:completionBlock]]; | ||
| 
											9 years ago
										 |     [fromViewController presentViewController:controller animated:YES completion:nil]; | ||
|  | } | ||
|  | 
 | ||
| 
											9 years ago
										 | + (NSString *)formatDisplayNameForAlertTitle:(NSString *)displayName | ||
|  | { | ||
|  |     return [self formatDisplayName:displayName withMaxLength:20]; | ||
|  | } | ||
|  | 
 | ||
|  | + (NSString *)formatDisplayNameForAlertMessage:(NSString *)displayName | ||
|  | { | ||
|  |     return [self formatDisplayName:displayName withMaxLength:127]; | ||
|  | } | ||
|  | 
 | ||
|  | + (NSString *)formatDisplayName:(NSString *)displayName withMaxLength:(NSUInteger)maxLength | ||
|  | { | ||
|  |     OWSAssert(displayName.length > 0); | ||
|  | 
 | ||
|  |     if (displayName.length > maxLength) { | ||
|  |         return [[displayName substringToIndex:maxLength] stringByAppendingString:@"…"]; | ||
|  |     } | ||
|  | 
 | ||
|  |     return displayName; | ||
|  | } | ||
|  | 
 | ||
| 
											9 years ago
										 | @end | ||
|  | 
 | ||
|  | NS_ASSUME_NONNULL_END |