Skip to content

Commit 6bfa103

Browse files
committed
OrderDetailsViewController: Splits Contact Customer Alert
1 parent f62dd1a commit 6bfa103

File tree

1 file changed

+70
-48
lines changed

1 file changed

+70
-48
lines changed

WooCommerce/Classes/ViewRelated/Orders/OrderDetails/OrderDetailsViewController.swift

Lines changed: 70 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,8 @@ private extension OrderDetailsViewController {
332332

333333
cell.textLabel?.text = phoneNumber
334334
cell.accessoryImageView.image = Gridicon.iconOfType(.ellipsis)
335-
cell.onTouchUp = { [weak self] in
336-
self?.phoneButtonAction()
335+
cell.onTouchUp = { [weak self] sender in
336+
self?.displayContactCustomerAlert(from: sender)
337337
}
338338

339339
cell.isAccessibilityElement = true
@@ -486,42 +486,7 @@ private extension OrderDetailsViewController {
486486

487487
// MARK: - Actions
488488
//
489-
extension OrderDetailsViewController {
490-
@objc func phoneButtonAction() {
491-
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
492-
actionSheet.view.tintColor = StyleManager.wooCommerceBrandColor
493-
let dismissAction = UIAlertAction(title: NSLocalizedString("Dismiss", comment: "Dismiss the action sheet"), style: .cancel)
494-
actionSheet.addAction(dismissAction)
495-
496-
let callAction = UIAlertAction(title: NSLocalizedString("Call", comment: "Call phone number button title"), style: .default) { [weak self] action in
497-
WooAnalytics.shared.track(.orderDetailCustomerPhoneOptionTapped)
498-
guard let phone = self?.viewModel.order.billingAddress?.cleanedPhoneNumber else {
499-
return
500-
}
501-
if let url = URL(string: "telprompt://" + phone),
502-
UIApplication.shared.canOpenURL(url) {
503-
UIApplication.shared.open(url, options: [:], completionHandler: nil)
504-
WooAnalytics.shared.track(.orderContactAction, withProperties: ["id": self?.viewModel.order.orderID ?? 0,
505-
"status": self?.viewModel.order.status.rawValue ?? String(),
506-
"type": "call"])
507-
}
508-
}
509-
actionSheet.addAction(callAction)
510-
511-
let messageAction = UIAlertAction(title: NSLocalizedString("Message", comment: "Message phone number button title"), style: .default) { [weak self] action in
512-
WooAnalytics.shared.track(.orderDetailCustomerSMSOptionTapped)
513-
self?.sendTextMessageIfPossible()
514-
}
515-
516-
actionSheet.addAction(messageAction)
517-
WooAnalytics.shared.track(.orderDetailCustomerPhoneMenuTapped)
518-
present(actionSheet, animated: true)
519-
}
520-
521-
@objc func emailButtonAction() {
522-
WooAnalytics.shared.track(.orderDetailCustomerEmailTapped)
523-
sendEmailIfPossible()
524-
}
489+
private extension OrderDetailsViewController {
525490

526491
func toggleBillingFooter() {
527492
displaysBillingDetails = !displaysBillingDetails
@@ -643,23 +608,73 @@ extension OrderDetailsViewController: UITableViewDelegate {
643608
}
644609

645610

646-
// MARK: - MFMessageComposeViewControllerDelegate Conformance
611+
// MARK: - Contact Alert
647612
//
648-
extension OrderDetailsViewController: MFMessageComposeViewControllerDelegate {
649-
func sendTextMessageIfPossible() {
650-
guard let phoneNumber = viewModel.order.billingAddress?.cleanedPhoneNumber else {
613+
private extension OrderDetailsViewController {
614+
615+
/// Displays an alert that offers several contact methods to reach the customer: [Phone / Message]
616+
///
617+
func displayContactCustomerAlert(from sourceView: UIView) {
618+
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
619+
actionSheet.view.tintColor = StyleManager.wooCommerceBrandColor
620+
621+
actionSheet.addCancelActionWithTitle(ContactAction.dismiss)
622+
actionSheet.addDefaultActionWithTitle(ContactAction.call) { [weak self] _ in
623+
guard let phoneURL = self?.viewModel.order.billingAddress?.cleanedPhoneNumberAsActionableURL else {
624+
return
625+
}
626+
627+
WooAnalytics.shared.track(.orderDetailCustomerPhoneOptionTapped)
628+
self?.callCustomerIfPossible(at: phoneURL)
629+
}
630+
631+
actionSheet.addDefaultActionWithTitle(ContactAction.message) { [weak self] _ in
632+
WooAnalytics.shared.track(.orderDetailCustomerSMSOptionTapped)
633+
self?.displayMessageComposerIfPossible()
634+
}
635+
636+
let popoverController = actionSheet.popoverPresentationController
637+
popoverController?.sourceView = sourceView
638+
popoverController?.sourceRect = sourceView.bounds
639+
640+
present(actionSheet, animated: true)
641+
642+
WooAnalytics.shared.track(.orderDetailCustomerPhoneMenuTapped)
643+
}
644+
645+
/// Attempts to perform a phone call at the specified URL
646+
///
647+
func callCustomerIfPossible(at phoneURL: URL) {
648+
guard UIApplication.shared.canOpenURL(phoneURL) else {
651649
return
652650
}
653651

654-
if MFMessageComposeViewController.canSendText() {
655-
sendTextMessage(to: phoneNumber)
656-
WooAnalytics.shared.track(.orderContactAction, withProperties: ["id": viewModel.order.orderID,
657-
"status": viewModel.order.status.rawValue,
658-
"type": "sms"])
652+
UIApplication.shared.open(phoneURL, options: [:], completionHandler: nil)
653+
WooAnalytics.shared.track(.orderContactAction, withProperties: ["id": self.viewModel.order.orderID,
654+
"status": self.viewModel.order.status.rawValue,
655+
"type": "call"])
656+
657+
}
658+
}
659+
660+
661+
// MARK: - MFMessageComposeViewControllerDelegate Conformance
662+
//
663+
extension OrderDetailsViewController: MFMessageComposeViewControllerDelegate {
664+
func displayMessageComposerIfPossible() {
665+
guard let phoneNumber = viewModel.order.billingAddress?.cleanedPhoneNumber,
666+
MFMessageComposeViewController.canSendText()
667+
else {
668+
return
659669
}
670+
671+
displayMessageComposer(for: phoneNumber)
672+
WooAnalytics.shared.track(.orderContactAction, withProperties: ["id": viewModel.order.orderID,
673+
"status": viewModel.order.status.rawValue,
674+
"type": "sms"])
660675
}
661676

662-
private func sendTextMessage(to phoneNumber: String) {
677+
private func displayMessageComposer(for phoneNumber: String) {
663678
let controller = MFMessageComposeViewController()
664679
controller.recipients = [phoneNumber]
665680
controller.messageComposeDelegate = self
@@ -710,6 +725,13 @@ extension OrderDetailsViewController: MFMailComposeViewControllerDelegate {
710725
// MARK: - Constants
711726
//
712727
private extension OrderDetailsViewController {
728+
729+
enum ContactAction {
730+
static let dismiss = NSLocalizedString("Dismiss", comment: "Dismiss the action sheet")
731+
static let call = NSLocalizedString("Call", comment: "Call phone number button title")
732+
static let message = NSLocalizedString("Message", comment: "Message phone number button title")
733+
}
734+
713735
enum Constants {
714736
static let rowHeight = CGFloat(38)
715737
static let sectionHeight = CGFloat(44)

0 commit comments

Comments
 (0)