Remove `fromViewController` state from ContactShareViewHelper

// FREEBIE
This commit is contained in:
Michael Kirk 2018-05-08 18:07:25 -04:00
parent 77bbbad70c
commit 42109593af
4 changed files with 43 additions and 103 deletions

View File

@ -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()
}
}

View File

@ -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()
}
}

View File

@ -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<UIViewControllerAnimatedTransiti
{
DDLogInfo(@"%@ in %s", self.logTag, __PRETTY_FUNCTION__);
// Do nothing.
[self.navigationController popToViewController:self animated:true];
}
@end

View File

@ -40,7 +40,7 @@ class MessageDetailViewController: OWSViewController, MediaGalleryDataSourceDele
var attachmentStream: TSAttachmentStream?
var messageBody: String?
private var contactShareViewHelper: ContactShareViewHelper?
private var contactShareViewHelper: ContactShareViewHelper
// MARK: Initializers
@ -55,8 +55,10 @@ class MessageDetailViewController: OWSViewController, MediaGalleryDataSourceDele
self.message = message
self.mode = mode
self.uiDatabaseConnection = OWSPrimaryStorage.shared().newDatabaseConnection()
self.contactShareViewHelper = ContactShareViewHelper(contactsManager: contactsManager)
super.init(nibName: nil, bundle: nil)
contactShareViewHelper.delegate = self
}
// MARK: View Lifecycle
@ -624,25 +626,15 @@ class MessageDetailViewController: OWSViewController, MediaGalleryDataSourceDele
}
func didTapSendMessage(toContactShare contactShare: ContactShareViewModel) {
contactShareViewHelper = ContactShareViewHelper(contactsManager: contactsManager,
fromViewController: self,
delegate: self)
contactShareViewHelper?.sendMessage(contactShare: contactShare)
contactShareViewHelper.sendMessage(contactShare: contactShare, fromViewController: self)
}
func didTapSendInvite(toContactShare contactShare: ContactShareViewModel) {
contactShareViewHelper = ContactShareViewHelper(contactsManager: contactsManager,
fromViewController: self,
delegate: self)
contactShareViewHelper?.inviteContact(contactShare: contactShare)
contactShareViewHelper.inviteContact(contactShare: contactShare, fromViewController: self)
}
func didTapShowAddToContactUI(forContactShare contactShare: ContactShareViewModel) {
contactShareViewHelper = ContactShareViewHelper(contactsManager: contactsManager,
fromViewController: self,
delegate: self)
contactShareViewHelper?.addToContacts(contactShare: contactShare)
contactShareViewHelper.addToContacts(contactShare: contactShare, fromViewController: self)
}
var audioAttachmentPlayer: OWSAudioPlayer?
@ -728,6 +720,7 @@ class MessageDetailViewController: OWSViewController, MediaGalleryDataSourceDele
// MARK: - ContactShareViewHelperDelegate
public func didCreateOrEditContact() {
navigationController?.popToViewController(self, animated: true)
updateContent()
}
}