From c500e7890f9ef5a359a0a0240da1be7db5fa4615 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Tue, 4 Apr 2017 15:54:11 -0400 Subject: [PATCH] Improve completion handling of block actions. // FREEBIE --- Signal/src/ViewControllers/BlockListUIUtils.m | 51 +++++++---- .../ViewControllers/MessagesViewController.m | 89 ++++++++----------- .../translations/en.lproj/Localizable.strings | 10 ++- 3 files changed, 79 insertions(+), 71 deletions(-) diff --git a/Signal/src/ViewControllers/BlockListUIUtils.m b/Signal/src/ViewControllers/BlockListUIUtils.m index 1af210bf0..ba329101e 100644 --- a/Signal/src/ViewControllers/BlockListUIUtils.m +++ b/Signal/src/ViewControllers/BlockListUIUtils.m @@ -10,6 +10,8 @@ NS_ASSUME_NONNULL_BEGIN +typedef void (^BlockAlertCompletionBlock)(); + @implementation BlockListUIUtils + (void)showBlockContactActionSheet:(Contact *)contact @@ -27,10 +29,12 @@ NS_ASSUME_NONNULL_BEGIN if (phoneNumbers.count < 1) { DDLogError(@"%@ Contact has no phone numbers", self.tag); OWSAssert(0); - [self showBlockFailedAlert:fromViewController]; - if (completionBlock) { - completionBlock(NO); - } + [self showBlockFailedAlert:fromViewController + completionBlock:^{ + if (completionBlock) { + completionBlock(NO); + } + }]; return; } NSString *displayName = [contactsManager displayNameForContact:contact]; @@ -81,10 +85,12 @@ NS_ASSUME_NONNULL_BEGIN [self blockPhoneNumbers:phoneNumbers displayName:displayName fromViewController:fromViewController - blockingManager:blockingManager]; - if (completionBlock) { - completionBlock(YES); - } + blockingManager:blockingManager + completionBlock:^{ + if (completionBlock) { + completionBlock(YES); + } + }]; }]; [actionSheetController addAction:unblockAction]; @@ -104,6 +110,7 @@ NS_ASSUME_NONNULL_BEGIN displayName:(NSString *)displayName fromViewController:(UIViewController *)fromViewController blockingManager:(OWSBlockingManager *)blockingManager + completionBlock:(BlockAlertCompletionBlock)completionBlock { OWSAssert(phoneNumbers.count > 0); OWSAssert(displayName.length > 0); @@ -122,7 +129,8 @@ NS_ASSUME_NONNULL_BEGIN @"The message format of the 'user blocked' " @"alert. Embeds {{the blocked user's name or phone number}}."), [self formatDisplayNameForAlertMessage:displayName]] - fromViewController:fromViewController]; + fromViewController:fromViewController + completionBlock:completionBlock]; } + (void)showUnblockPhoneNumberActionSheet:(NSString *)phoneNumber @@ -153,10 +161,12 @@ NS_ASSUME_NONNULL_BEGIN [BlockListUIUtils unblockPhoneNumber:phoneNumber displayName:displayName fromViewController:fromViewController - blockingManager:blockingManager]; - if (completionBlock) { - completionBlock(NO); - } + blockingManager:blockingManager + completionBlock:^{ + if (completionBlock) { + completionBlock(NO); + } + }]; }]; [actionSheetController addAction:unblockAction]; @@ -176,6 +186,7 @@ NS_ASSUME_NONNULL_BEGIN displayName:(NSString *)displayName fromViewController:(UIViewController *)fromViewController blockingManager:(OWSBlockingManager *)blockingManager + completionBlock:(BlockAlertCompletionBlock)completionBlock { OWSAssert(phoneNumber.length > 0); OWSAssert(displayName.length > 0); @@ -191,10 +202,12 @@ NS_ASSUME_NONNULL_BEGIN @"The message format of the 'user unblocked' " @"alert. Embeds {{the blocked user's name or phone number}}."), [self formatDisplayNameForAlertMessage:displayName]] - fromViewController:fromViewController]; + fromViewController:fromViewController + completionBlock:completionBlock]; } + (void)showBlockFailedAlert:(UIViewController *)fromViewController + completionBlock:(BlockAlertCompletionBlock)completionBlock { OWSAssert(fromViewController); @@ -202,10 +215,12 @@ NS_ASSUME_NONNULL_BEGIN @"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.") - fromViewController:fromViewController]; + fromViewController:fromViewController + completionBlock:completionBlock]; } + (void)showUnblockFailedAlert:(UIViewController *)fromViewController + completionBlock:(BlockAlertCompletionBlock)completionBlock { OWSAssert(fromViewController); @@ -213,12 +228,14 @@ NS_ASSUME_NONNULL_BEGIN @"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.") - fromViewController:fromViewController]; + fromViewController:fromViewController + completionBlock:completionBlock]; } + (void)showOkAlertWithTitle:(NSString *)title message:(NSString *)message fromViewController:(UIViewController *)fromViewController + completionBlock:(BlockAlertCompletionBlock)completionBlock { OWSAssert(title.length > 0); OWSAssert(message.length > 0); @@ -229,7 +246,7 @@ NS_ASSUME_NONNULL_BEGIN [controller addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleDefault - handler:nil]]; + handler:completionBlock]]; [fromViewController presentViewController:controller animated:YES completion:nil]; } diff --git a/Signal/src/ViewControllers/MessagesViewController.m b/Signal/src/ViewControllers/MessagesViewController.m index 1dec6edda..894c91adb 100644 --- a/Signal/src/ViewControllers/MessagesViewController.m +++ b/Signal/src/ViewControllers/MessagesViewController.m @@ -282,8 +282,6 @@ typedef enum : NSUInteger { - (void)addNotificationListeners { - // I have not added this to toggleObservers since I am - // not con [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(blockedPhoneNumbersDidChange:) name:kNSNotificationName_BlockedPhoneNumbersDidChange @@ -595,12 +593,7 @@ typedef enum : NSUInteger { if ([self isBlockedContactConversation]) { // If this a blocked 1:1 conversation, offer to unblock the user. - NSString *contactIdentifier = ((TSContactThread *)self.thread).contactIdentifier; - [BlockListUIUtils showUnblockPhoneNumberActionSheet:contactIdentifier - fromViewController:self - blockingManager:_blockingManager - contactsManager:_contactsManager - completionBlock:nil]; + [self showUnblockContactUI:nil]; } else { // If this a group conversation with at least one blocked member, // Show the block list view. @@ -612,6 +605,18 @@ typedef enum : NSUInteger { } } +- (void)showUnblockContactUI:(BlockActionCompletionBlock)completionBlock +{ + OWSAssert([self.thread isKindOfClass:[TSContactThread class]]); + + NSString *contactIdentifier = ((TSContactThread *)self.thread).contactIdentifier; + [BlockListUIUtils showUnblockPhoneNumberActionSheet:contactIdentifier + fromViewController:self + blockingManager:_blockingManager + contactsManager:_contactsManager + completionBlock:completionBlock]; +} + - (BOOL)isBlockedContactConversation { if (![self.thread isKindOfClass:[TSContactThread class]]) { @@ -979,16 +984,11 @@ typedef enum : NSUInteger { if ([self isBlockedContactConversation]) { __weak MessagesViewController *weakSelf = self; - NSString *contactIdentifier = ((TSContactThread *)self.thread).contactIdentifier; - [BlockListUIUtils showUnblockPhoneNumberActionSheet:contactIdentifier - fromViewController:self - blockingManager:_blockingManager - contactsManager:_contactsManager - completionBlock:^(BOOL isBlocked) { - if (!isBlocked) { - [weakSelf callAction:nil]; - } - }]; + [self showUnblockContactUI:^(BOOL isBlocked) { + if (!isBlocked) { + [weakSelf callAction:nil]; + } + }]; return; } @@ -1009,20 +1009,15 @@ typedef enum : NSUInteger { { if ([self isBlockedContactConversation]) { __weak MessagesViewController *weakSelf = self; - NSString *contactIdentifier = ((TSContactThread *)self.thread).contactIdentifier; - [BlockListUIUtils showUnblockPhoneNumberActionSheet:contactIdentifier - fromViewController:self - blockingManager:_blockingManager - contactsManager:_contactsManager - completionBlock:^(BOOL isBlocked) { - if (!isBlocked) { - [weakSelf didPressSendButton:button - withMessageText:text - senderId:senderId - senderDisplayName:senderDisplayName - date:date]; - } - }]; + [self showUnblockContactUI:^(BOOL isBlocked) { + if (!isBlocked) { + [weakSelf didPressSendButton:button + withMessageText:text + senderId:senderId + senderDisplayName:senderDisplayName + date:date]; + } + }]; return; } @@ -2491,16 +2486,11 @@ typedef enum : NSUInteger { if ([self isBlockedContactConversation]) { __weak MessagesViewController *weakSelf = self; - NSString *contactIdentifier = ((TSContactThread *)self.thread).contactIdentifier; - [BlockListUIUtils showUnblockPhoneNumberActionSheet:contactIdentifier - fromViewController:self - blockingManager:_blockingManager - contactsManager:_contactsManager - completionBlock:^(BOOL isBlocked) { - if (!isBlocked) { - [weakSelf didPressAccessoryButton:nil]; - } - }]; + [self showUnblockContactUI:^(BOOL isBlocked) { + if (!isBlocked) { + [weakSelf didPressAccessoryButton:nil]; + } + }]; return; } @@ -2739,16 +2729,11 @@ typedef enum : NSUInteger { if ([self isBlockedContactConversation]) { __weak MessagesViewController *weakSelf = self; - NSString *contactIdentifier = ((TSContactThread *)self.thread).contactIdentifier; - [BlockListUIUtils showUnblockPhoneNumberActionSheet:contactIdentifier - fromViewController:self - blockingManager:_blockingManager - contactsManager:_contactsManager - completionBlock:^(BOOL isBlocked) { - if (!isBlocked) { - [weakSelf didPasteAttachment:attachment]; - } - }]; + [self showUnblockContactUI:^(BOOL isBlocked) { + if (!isBlocked) { + [weakSelf didPasteAttachment:attachment]; + } + }]; return; } diff --git a/Signal/translations/en.lproj/Localizable.strings b/Signal/translations/en.lproj/Localizable.strings index 4fe8c68a8..16f165ac7 100644 --- a/Signal/translations/en.lproj/Localizable.strings +++ b/Signal/translations/en.lproj/Localizable.strings @@ -346,6 +346,9 @@ /* No comment provided by engineer. */ "ERROR_MESSAGE_WRONG_TRUSTED_IDENTITY_KEY" = "Safety number changed. Tap to verify."; +/* No comment provided by engineer. */ +"ERROR_WAS_DETECTED_TITLE" = "ERROR_WAS_DETECTED_TITLE"; + /* during registration */ "EXISTING_USER_REGISTRATION_ALERT_BODY" = "At this time Signal can only be active on one mobile device per phone number."; @@ -527,9 +530,9 @@ "MESSAGE_STATUS_UPLOADING" = "Uploading..."; /* Indicates that this 1:1 conversation has been blocked. */ -"MESSAGES_VIEW_CONTACT_BLOCKED" = "This user is blocked"; +"MESSAGES_VIEW_CONTACT_BLOCKED" = "This User is Blocked"; -/* Indicates that some members of this group has been blocked. Embeds {{the number of blocked user in this group}}. */ +/* Indicates that a single member of this group has been blocked. */ "MESSAGES_VIEW_GROUP_1_MEMBER_BLOCKED_FORMAT" = "1 Blocked Member of this Group"; /* Indicates that some members of this group has been blocked. Embeds {{the number of blocked user in this group}}. */ @@ -976,6 +979,9 @@ /* No comment provided by engineer. */ "SUCCESSFUL_VERIFICATION_TITLE" = "Safety Number Verified!"; +/* No comment provided by engineer. */ +"TIMEOUT_CONTACTS_DETAIL" = "TIMEOUT_CONTACTS_DETAIL"; + /* No comment provided by engineer. */ "TXT_CANCEL_TITLE" = "Cancel";