From c0f5bda2be5e6e684df8ff055713b8a4cf6040f8 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Mon, 18 Sep 2017 15:35:14 -0400 Subject: [PATCH] Respond to CR. // FREEBIE --- .../AppSettingsViewController.m | 27 +++--- .../ConversationViewController.m | 86 +++++++++---------- ...ModalActivityIndicatorViewController.swift | 6 +- .../ViewControllers/NewGroupViewController.m | 48 ++++++----- .../SelectRecipientViewController.m | 67 +++++++-------- 5 files changed, 115 insertions(+), 119 deletions(-) diff --git a/Signal/src/ViewControllers/AppSettingsViewController.m b/Signal/src/ViewControllers/AppSettingsViewController.m index a7e1bdbd1..b6024d8dd 100644 --- a/Signal/src/ViewControllers/AppSettingsViewController.m +++ b/Signal/src/ViewControllers/AppSettingsViewController.m @@ -379,21 +379,18 @@ [ModalActivityIndicatorViewController presentFromViewController:self canCancel:NO - presentCompletion:^(ModalActivityIndicatorViewController *modalActivityIndicator) { - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - [TSAccountManager unregisterTextSecureWithSuccess:^{ - [Environment resetAppData]; - } - failure:^(NSError *error) { - dispatch_async(dispatch_get_main_queue(), ^{ - [modalActivityIndicator dismissWithCompletion:^{ - [OWSAlerts - showAlertWithTitle:NSLocalizedString(@"UNREGISTER_SIGNAL_FAIL", @"")]; - }]; - }); - }]; - }); - }]; + backgroundBlock:^(ModalActivityIndicatorViewController *modalActivityIndicator) { + [TSAccountManager unregisterTextSecureWithSuccess:^{ + [Environment resetAppData]; + } + failure:^(NSError *error) { + dispatch_async(dispatch_get_main_queue(), ^{ + [modalActivityIndicator dismissWithCompletion:^{ + [OWSAlerts showAlertWithTitle:NSLocalizedString(@"UNREGISTER_SIGNAL_FAIL", @"")]; + }]; + }); + }]; + }]; } #pragma mark - Socket Status Notifications diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m index c3c18607b..97d7ef9ed 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m @@ -3456,50 +3456,48 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) { [ModalActivityIndicatorViewController presentFromViewController:self canCancel:YES - presentCompletion:^(ModalActivityIndicatorViewController *modalActivityIndicator) { - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - AVAsset *video = [AVAsset assetWithURL:movieURL]; - AVAssetExportSession *exportSession = - [AVAssetExportSession exportSessionWithAsset:video - presetName:AVAssetExportPresetMediumQuality]; - exportSession.shouldOptimizeForNetworkUse = YES; - exportSession.outputFileType = AVFileTypeMPEG4; - NSURL *compressedVideoUrl = [[self videoTempFolder] - URLByAppendingPathComponent:[[[NSUUID UUID] UUIDString] - stringByAppendingPathExtension:@"mp4"]]; - exportSession.outputURL = compressedVideoUrl; - [exportSession exportAsynchronouslyWithCompletionHandler:^{ - dispatch_async(dispatch_get_main_queue(), ^{ - OWSAssert([NSThread isMainThread]); - - if (modalActivityIndicator.wasCancelled) { - return; - } - - [modalActivityIndicator dismissWithCompletion:^{ - DataSource *_Nullable dataSource = - [DataSourcePath dataSourceWithURL:compressedVideoUrl]; - [dataSource setSourceFilename:filename]; - // Remove temporary file when complete. - [dataSource setShouldDeleteOnDeallocation]; - SignalAttachment *attachment = - [SignalAttachment attachmentWithDataSource:dataSource - dataUTI:(NSString *)kUTTypeMPEG4]; - if (!attachment || [attachment hasError]) { - DDLogError(@"%@ %s Invalid attachment: %@.", - self.tag, - __PRETTY_FUNCTION__, - attachment ? [attachment errorName] : @"Missing data"); - [self showErrorAlertForAttachment:attachment]; - } else { - [self tryToSendAttachmentIfApproved:attachment - skipApprovalDialog:skipApprovalDialog]; - } - }]; - }); - }]; - }); - }]; + backgroundBlock:^(ModalActivityIndicatorViewController *modalActivityIndicator) { + AVAsset *video = [AVAsset assetWithURL:movieURL]; + AVAssetExportSession *exportSession = + [AVAssetExportSession exportSessionWithAsset:video + presetName:AVAssetExportPresetMediumQuality]; + exportSession.shouldOptimizeForNetworkUse = YES; + exportSession.outputFileType = AVFileTypeMPEG4; + NSURL *compressedVideoUrl = [[self videoTempFolder] + URLByAppendingPathComponent:[[[NSUUID UUID] UUIDString] + stringByAppendingPathExtension:@"mp4"]]; + exportSession.outputURL = compressedVideoUrl; + [exportSession exportAsynchronouslyWithCompletionHandler:^{ + dispatch_async(dispatch_get_main_queue(), ^{ + OWSAssert([NSThread isMainThread]); + + if (modalActivityIndicator.wasCancelled) { + return; + } + + [modalActivityIndicator dismissWithCompletion:^{ + DataSource *_Nullable dataSource = + [DataSourcePath dataSourceWithURL:compressedVideoUrl]; + [dataSource setSourceFilename:filename]; + // Remove temporary file when complete. + [dataSource setShouldDeleteOnDeallocation]; + SignalAttachment *attachment = + [SignalAttachment attachmentWithDataSource:dataSource + dataUTI:(NSString *)kUTTypeMPEG4]; + if (!attachment || [attachment hasError]) { + DDLogError(@"%@ %s Invalid attachment: %@.", + self.tag, + __PRETTY_FUNCTION__, + attachment ? [attachment errorName] : @"Missing data"); + [self showErrorAlertForAttachment:attachment]; + } else { + [self tryToSendAttachmentIfApproved:attachment + skipApprovalDialog:skipApprovalDialog]; + } + }]; + }); + }]; + }]; } diff --git a/Signal/src/ViewControllers/ModalActivityIndicatorViewController.swift b/Signal/src/ViewControllers/ModalActivityIndicatorViewController.swift index 7a7c5375b..e66df9a6d 100644 --- a/Signal/src/ViewControllers/ModalActivityIndicatorViewController.swift +++ b/Signal/src/ViewControllers/ModalActivityIndicatorViewController.swift @@ -36,7 +36,7 @@ class ModalActivityIndicatorViewController: OWSViewController { } public class func present(fromViewController: UIViewController, - canCancel: Bool, presentCompletion : @escaping (ModalActivityIndicatorViewController) -> Void) { + canCancel: Bool, backgroundBlock : @escaping (ModalActivityIndicatorViewController) -> Void) { AssertIsOnMainThread() let view = ModalActivityIndicatorViewController(canCancel:canCancel) @@ -44,7 +44,9 @@ class ModalActivityIndicatorViewController: OWSViewController { view.modalPresentationStyle = .overFullScreen fromViewController.present(view, animated: false) { - presentCompletion(view) + DispatchQueue.global().async { + backgroundBlock(view) + } } } diff --git a/Signal/src/ViewControllers/NewGroupViewController.m b/Signal/src/ViewControllers/NewGroupViewController.m index a33520e36..9374740e8 100644 --- a/Signal/src/ViewControllers/NewGroupViewController.m +++ b/Signal/src/ViewControllers/NewGroupViewController.m @@ -486,29 +486,31 @@ const NSUInteger kNewGroupViewControllerAvatarWidth = 68; [ModalActivityIndicatorViewController presentFromViewController:self canCancel:NO - presentCompletion:^(ModalActivityIndicatorViewController *modalActivityIndicator) { - TSOutgoingMessage *message = - [[TSOutgoingMessage alloc] initWithTimestamp:[NSDate ows_millisecondTimeStamp] - inThread:thread - groupMetaMessage:TSGroupMessageNew]; - - // This will save the message. - [message updateWithCustomMessage:NSLocalizedString(@"GROUP_CREATED", nil)]; - - if (model.groupImage) { - NSData *data = UIImagePNGRepresentation(model.groupImage); - DataSource *_Nullable dataSource = - [DataSourceValue dataSourceWithData:data fileExtension:@"png"]; - [self.messageSender sendAttachmentData:dataSource - contentType:OWSMimeTypeImagePng - sourceFilename:nil - inMessage:message - success:successHandler - failure:failureHandler]; - } else { - [self.messageSender sendMessage:message success:successHandler failure:failureHandler]; - } - }]; + backgroundBlock:^(ModalActivityIndicatorViewController *modalActivityIndicator) { + TSOutgoingMessage *message = + [[TSOutgoingMessage alloc] initWithTimestamp:[NSDate ows_millisecondTimeStamp] + inThread:thread + groupMetaMessage:TSGroupMessageNew]; + + // This will save the message. + [message updateWithCustomMessage:NSLocalizedString(@"GROUP_CREATED", nil)]; + + dispatch_async(dispatch_get_main_queue(), ^{ + if (model.groupImage) { + NSData *data = UIImagePNGRepresentation(model.groupImage); + DataSource *_Nullable dataSource = + [DataSourceValue dataSourceWithData:data fileExtension:@"png"]; + [self.messageSender sendAttachmentData:dataSource + contentType:OWSMimeTypeImagePng + sourceFilename:nil + inMessage:message + success:successHandler + failure:failureHandler]; + } else { + [self.messageSender sendMessage:message success:successHandler failure:failureHandler]; + } + }); + }]; } - (TSGroupModel *)makeGroup diff --git a/Signal/src/ViewControllers/SelectRecipientViewController.m b/Signal/src/ViewControllers/SelectRecipientViewController.m index acf5e33d5..a8b32b90b 100644 --- a/Signal/src/ViewControllers/SelectRecipientViewController.m +++ b/Signal/src/ViewControllers/SelectRecipientViewController.m @@ -310,41 +310,38 @@ NSString *const kSelectRecipientViewControllerCellIdentifier = @"kSelectRecipien [ModalActivityIndicatorViewController presentFromViewController:self canCancel:YES - presentCompletion:^(ModalActivityIndicatorViewController *modalActivityIndicator) { - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - [[ContactsUpdater sharedUpdater] lookupIdentifiers:possiblePhoneNumbers - success:^(NSArray *recipients) { - OWSAssert([NSThread isMainThread]); - OWSAssert(recipients.count > 0); - - if (modalActivityIndicator.wasCancelled) { - return; - } - - NSString *recipientId = recipients[0].uniqueId; - [modalActivityIndicator - dismissViewControllerAnimated:NO - completion:^{ - [weakSelf.delegate phoneNumberWasSelected:recipientId]; - }]; - } - failure:^(NSError *error) { - OWSAssert([NSThread isMainThread]); - if (modalActivityIndicator.wasCancelled) { - return; - } - [modalActivityIndicator - dismissViewControllerAnimated:NO - completion:^{ - [OWSAlerts - showAlertWithTitle: - NSLocalizedString(@"ALERT_ERROR_TITLE", - @"Title for a generic error alert.") - message:error.localizedDescription]; - }]; - }]; - }); - }]; + backgroundBlock:^(ModalActivityIndicatorViewController *modalActivityIndicator) { + [[ContactsUpdater sharedUpdater] lookupIdentifiers:possiblePhoneNumbers + success:^(NSArray *recipients) { + OWSAssert([NSThread isMainThread]); + OWSAssert(recipients.count > 0); + + if (modalActivityIndicator.wasCancelled) { + return; + } + + NSString *recipientId = recipients[0].uniqueId; + [modalActivityIndicator + dismissViewControllerAnimated:NO + completion:^{ + [weakSelf.delegate phoneNumberWasSelected:recipientId]; + }]; + } + failure:^(NSError *error) { + OWSAssert([NSThread isMainThread]); + if (modalActivityIndicator.wasCancelled) { + return; + } + [modalActivityIndicator + dismissViewControllerAnimated:NO + completion:^{ + [OWSAlerts showAlertWithTitle: + NSLocalizedString(@"ALERT_ERROR_TITLE", + @"Title for a generic error alert.") + message:error.localizedDescription]; + }]; + }]; + }]; } else { NSString *recipientId = possiblePhoneNumbers[0]; [self.delegate phoneNumberWasSelected:recipientId];