From 42109593af610a76be7cb4235105c234868d6f33 Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Tue, 8 May 2018 18:07:25 -0400 Subject: [PATCH] Remove `fromViewController` state from ContactShareViewHelper // FREEBIE --- .../ContactShareViewHelper.swift | 94 ++++--------------- .../ContactViewController.swift | 16 ++-- .../ConversationViewController.m | 13 ++- .../MessageDetailViewController.swift | 23 ++--- 4 files changed, 43 insertions(+), 103 deletions(-) diff --git a/Signal/src/ViewControllers/ContactShareViewHelper.swift b/Signal/src/ViewControllers/ContactShareViewHelper.swift index 3c1402378..a87c56189 100644 --- a/Signal/src/ViewControllers/ContactShareViewHelper.swift +++ b/Signal/src/ViewControllers/ContactShareViewHelper.swift @@ -18,14 +18,11 @@ public class ContactShareViewHelper: NSObject, CNContactViewControllerDelegate { weak var delegate: ContactShareViewHelperDelegate? let contactsManager: OWSContactsManager - weak var fromViewController: UIViewController? - public required init(contactsManager: OWSContactsManager, fromViewController: UIViewController, delegate: ContactShareViewHelperDelegate) { + public required init(contactsManager: OWSContactsManager) { SwiftAssertIsOnMainThread(#function) self.contactsManager = contactsManager - self.fromViewController = fromViewController - self.delegate = delegate super.init() } @@ -33,27 +30,27 @@ public class ContactShareViewHelper: NSObject, CNContactViewControllerDelegate { // MARK: Actions @objc - public func sendMessage(contactShare: ContactShareViewModel) { + public func sendMessage(contactShare: ContactShareViewModel, fromViewController: UIViewController) { Logger.info("\(logTag) \(#function)") - presentThreadAndPeform(action: .compose, contactShare: contactShare) + presentThreadAndPeform(action: .compose, contactShare: contactShare, fromViewController: fromViewController) } @objc - public func audioCall(contactShare: ContactShareViewModel) { + public func audioCall(contactShare: ContactShareViewModel, fromViewController: UIViewController) { Logger.info("\(logTag) \(#function)") - presentThreadAndPeform(action: .audioCall, contactShare: contactShare) + presentThreadAndPeform(action: .audioCall, contactShare: contactShare, fromViewController: fromViewController) } @objc - public func videoCall(contactShare: ContactShareViewModel) { + public func videoCall(contactShare: ContactShareViewModel, fromViewController: UIViewController) { Logger.info("\(logTag) \(#function)") - presentThreadAndPeform(action: .videoCall, contactShare: contactShare) + presentThreadAndPeform(action: .videoCall, contactShare: contactShare, fromViewController: fromViewController) } - private func presentThreadAndPeform(action: ConversationViewAction, contactShare: ContactShareViewModel) { + private func presentThreadAndPeform(action: ConversationViewAction, contactShare: ContactShareViewModel, fromViewController: UIViewController) { // TODO: We're taking the first Signal account id. We might // want to let the user select if there's more than one. let phoneNumbers = contactShare.systemContactsWithSignalAccountPhoneNumbers(contactsManager) @@ -67,20 +64,15 @@ public class ContactShareViewHelper: NSObject, CNContactViewControllerDelegate { return } - showPhoneNumberPicker(phoneNumbers: phoneNumbers, completion: { (recipientId) in + showPhoneNumberPicker(phoneNumbers: phoneNumbers, fromViewController: fromViewController, completion: { (recipientId) in SignalApp.shared().presentConversation(forRecipientId: recipientId, action: action) }) } @objc - public func inviteContact(contactShare: ContactShareViewModel) { + public func inviteContact(contactShare: ContactShareViewModel, fromViewController: UIViewController) { Logger.info("\(logTag) \(#function)") - guard let fromViewController = fromViewController else { - owsFail("\(logTag) missing fromViewController") - return - } - guard MFMessageComposeViewController.canSendText() else { Logger.info("\(logTag) Device cannot send text") OWSAlerts.showErrorAlert(message: NSLocalizedString("UNSUPPORTED_FEATURE_ERROR", comment: "")) @@ -97,37 +89,27 @@ public class ContactShareViewHelper: NSObject, CNContactViewControllerDelegate { inviteFlow.sendSMSTo(phoneNumbers: phoneNumbers) } - func addToContacts(contactShare: ContactShareViewModel) { + func addToContacts(contactShare: ContactShareViewModel, fromViewController: UIViewController) { Logger.info("\(logTag) \(#function)") - guard let fromViewController = fromViewController else { - owsFail("\(logTag) missing fromViewController") - return - } - let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) actionSheet.addAction(UIAlertAction(title: NSLocalizedString("CONVERSATION_SETTINGS_NEW_CONTACT", comment: "Label for 'new contact' button in conversation settings view."), style: .default) { _ in - self.didPressCreateNewContact(contactShare: contactShare) + self.didPressCreateNewContact(contactShare: contactShare, fromViewController: fromViewController) }) actionSheet.addAction(UIAlertAction(title: NSLocalizedString("CONVERSATION_SETTINGS_ADD_TO_EXISTING_CONTACT", comment: "Label for 'new contact' button in conversation settings view."), style: .default) { _ in - self.didPressAddToExistingContact(contactShare: contactShare) + self.didPressAddToExistingContact(contactShare: contactShare, fromViewController: fromViewController) }) actionSheet.addAction(OWSAlerts.cancelAction) fromViewController.present(actionSheet, animated: true) } - private func showPhoneNumberPicker(phoneNumbers: [String], completion :@escaping ((String) -> Void)) { - - guard let fromViewController = fromViewController else { - owsFail("\(logTag) missing fromViewController") - return - } + private func showPhoneNumberPicker(phoneNumbers: [String], fromViewController: UIViewController, completion :@escaping ((String) -> Void)) { let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) @@ -142,27 +124,21 @@ public class ContactShareViewHelper: NSObject, CNContactViewControllerDelegate { fromViewController.present(actionSheet, animated: true) } - func didPressCreateNewContact(contactShare: ContactShareViewModel) { + func didPressCreateNewContact(contactShare: ContactShareViewModel, fromViewController: UIViewController) { Logger.info("\(logTag) \(#function)") - presentNewContactView(contactShare: contactShare) + presentNewContactView(contactShare: contactShare, fromViewController: fromViewController) } - func didPressAddToExistingContact(contactShare: ContactShareViewModel) { + func didPressAddToExistingContact(contactShare: ContactShareViewModel, fromViewController: UIViewController) { Logger.info("\(logTag) \(#function)") - presentSelectAddToExistingContactView(contactShare: contactShare) + presentSelectAddToExistingContactView(contactShare: contactShare, fromViewController: fromViewController) } // MARK: - - private func presentNewContactView(contactShare: ContactShareViewModel) { - - guard let fromViewController = fromViewController else { - owsFail("\(logTag) missing fromViewController") - return - } - + private func presentNewContactView(contactShare: ContactShareViewModel, fromViewController: UIViewController) { guard contactsManager.supportsContactEditing else { owsFail("\(logTag) Contact editing not supported") return @@ -201,13 +177,7 @@ public class ContactShareViewHelper: NSObject, CNContactViewControllerDelegate { UIUtil.applyDefaultSystemAppearence() } - private func presentSelectAddToExistingContactView(contactShare: ContactShareViewModel) { - - guard let fromViewController = fromViewController else { - owsFail("\(logTag) missing fromViewController") - return - } - + private func presentSelectAddToExistingContactView(contactShare: ContactShareViewModel, fromViewController: UIViewController) { guard contactsManager.supportsContactEditing else { owsFail("\(logTag) Contact editing not supported") return @@ -242,46 +212,22 @@ public class ContactShareViewHelper: NSObject, CNContactViewControllerDelegate { @objc public func contactViewController(_ viewController: CNContactViewController, didCompleteWith contact: CNContact?) { Logger.info("\(logTag) \(#function)") - guard let fromViewController = fromViewController else { - owsFail("\(logTag) missing fromViewController") - return - } - - guard let navigationController = fromViewController.navigationController else { - owsFail("\(logTag) missing navigationController") - return - } - guard let delegate = delegate else { owsFail("\(logTag) missing delegate") return } - navigationController.popToViewController(fromViewController, animated: true) - delegate.didCreateOrEditContact() } @objc public func didFinishEditingContact() { Logger.info("\(logTag) \(#function)") - guard let fromViewController = fromViewController else { - owsFail("\(logTag) missing fromViewController") - return - } - - guard let navigationController = fromViewController.navigationController else { - owsFail("\(logTag) missing navigationController") - return - } - guard let delegate = delegate else { owsFail("\(logTag) missing delegate") return } - navigationController.popToViewController(fromViewController, animated: true) - delegate.didCreateOrEditContact() } } diff --git a/Signal/src/ViewControllers/ContactViewController.swift b/Signal/src/ViewControllers/ContactViewController.swift index b39648876..42b7001f6 100644 --- a/Signal/src/ViewControllers/ContactViewController.swift +++ b/Signal/src/ViewControllers/ContactViewController.swift @@ -37,7 +37,7 @@ class ContactViewController: OWSViewController, ContactShareViewHelperDelegate { private let contactShare: ContactShareViewModel - private var helper: ContactShareViewHelper! + private var contactShareViewHelper: ContactShareViewHelper // MARK: - Initializers @@ -49,10 +49,11 @@ class ContactViewController: OWSViewController, ContactShareViewHelperDelegate { required init(contactShare: ContactShareViewModel) { contactsManager = Environment.current().contactsManager self.contactShare = contactShare + self.contactShareViewHelper = ContactShareViewHelper(contactsManager: contactsManager) super.init(nibName: nil, bundle: nil) - self.helper = ContactShareViewHelper(contactsManager: contactsManager, fromViewController: self, delegate: self) + contactShareViewHelper.delegate = self updateMode() @@ -478,31 +479,31 @@ class ContactViewController: OWSViewController, ContactShareViewHelperDelegate { func didPressSendMessage() { Logger.info("\(logTag) \(#function)") - self.helper.sendMessage(contactShare: self.contactShare) + self.contactShareViewHelper.sendMessage(contactShare: self.contactShare, fromViewController: self) } func didPressAudioCall() { Logger.info("\(logTag) \(#function)") - self.helper.audioCall(contactShare: self.contactShare) + self.contactShareViewHelper.audioCall(contactShare: self.contactShare, fromViewController: self) } func didPressVideoCall() { Logger.info("\(logTag) \(#function)") - self.helper.videoCall(contactShare: self.contactShare) + self.contactShareViewHelper.videoCall(contactShare: self.contactShare, fromViewController: self) } func didPressInvite() { Logger.info("\(logTag) \(#function)") - self.helper.inviteContact(contactShare: self.contactShare) + self.contactShareViewHelper.inviteContact(contactShare: self.contactShare, fromViewController: self) } func didPressAddToContacts() { Logger.info("\(logTag) \(#function)") - self.helper.addToContacts(contactShare: self.contactShare) + self.contactShareViewHelper.addToContacts(contactShare: self.contactShare, fromViewController: self) } func didPressDismiss() { @@ -551,6 +552,7 @@ class ContactViewController: OWSViewController, ContactShareViewHelperDelegate { public func didCreateOrEditContact() { Logger.info("\(logTag) \(#function)") + navigationController?.popToViewController(self, animated: true) updateContent() } } diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m index bb7c91e54..f8fcac9f6 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m @@ -276,9 +276,8 @@ typedef enum : NSUInteger { _networkManager = [TSNetworkManager sharedManager]; _blockingManager = [OWSBlockingManager sharedManager]; _contactsViewHelper = [[ContactsViewHelper alloc] initWithDelegate:self]; - _contactShareViewHelper = [[ContactShareViewHelper alloc] initWithContactsManager:self.contactsManager - fromViewController:self - delegate:self]; + _contactShareViewHelper = [[ContactShareViewHelper alloc] initWithContactsManager:self.contactsManager]; + _contactShareViewHelper.delegate = self; NSString *audioActivityDescription = [NSString stringWithFormat:@"%@ voice note", self.logTag]; _voiceNoteAudioActivity = [[AudioActivity alloc] initWithAudioDescription:audioActivityDescription]; @@ -2117,7 +2116,7 @@ typedef enum : NSUInteger { OWSAssertIsOnMainThread(); OWSAssert(contactShare); - [self.contactShareViewHelper sendMessageWithContactShare:contactShare]; + [self.contactShareViewHelper sendMessageWithContactShare:contactShare fromViewController:self]; } - (void)didTapSendInviteToContactShare:(ContactShareViewModel *)contactShare @@ -2125,7 +2124,7 @@ typedef enum : NSUInteger { OWSAssertIsOnMainThread(); OWSAssert(contactShare); - [self.contactShareViewHelper inviteContactWithContactShare:contactShare]; + [self.contactShareViewHelper inviteContactWithContactShare:contactShare fromViewController:self]; } - (void)didTapShowAddToContactUIForContactShare:(ContactShareViewModel *)contactShare @@ -2133,7 +2132,7 @@ typedef enum : NSUInteger { OWSAssertIsOnMainThread(); OWSAssert(contactShare); - [self.contactShareViewHelper addToContactsWithContactShare:contactShare]; + [self.contactShareViewHelper addToContactsWithContactShare:contactShare fromViewController:self]; } - (void)didTapFailedIncomingAttachment:(ConversationViewItem *)viewItem @@ -5066,7 +5065,7 @@ interactionControllerForAnimationController:(id