From fc163fc9ff8e6408cfe262d758c402aada920cc1 Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Tue, 19 Mar 2019 01:24:06 +0530 Subject: iOS: Consolidate all showConfirmationAlert()s into one place Signed-off-by: Roopesh Chander --- .../UI/iOS/ConfirmationAlertPresenter.swift | 25 ++++++++++++++++++++++ .../TunnelDetailTableViewController.swift | 19 +++------------- .../TunnelEditTableViewController.swift | 19 +++------------- .../TunnelsListTableViewController.swift | 17 ++------------- 4 files changed, 33 insertions(+), 47 deletions(-) create mode 100644 WireGuard/WireGuard/UI/iOS/ConfirmationAlertPresenter.swift (limited to 'WireGuard/WireGuard/UI') diff --git a/WireGuard/WireGuard/UI/iOS/ConfirmationAlertPresenter.swift b/WireGuard/WireGuard/UI/iOS/ConfirmationAlertPresenter.swift new file mode 100644 index 0000000..8064fff --- /dev/null +++ b/WireGuard/WireGuard/UI/iOS/ConfirmationAlertPresenter.swift @@ -0,0 +1,25 @@ +// SPDX-License-Identifier: MIT +// Copyright © 2018-2019 WireGuard LLC. All Rights Reserved. + +import UIKit + +class ConfirmationAlertPresenter { + static func showConfirmationAlert(message: String, buttonTitle: String, from sourceObject: AnyObject, presentingVC: UIViewController, onConfirmed: @escaping (() -> Void)) { + let destroyAction = UIAlertAction(title: buttonTitle, style: .destructive) { _ in + onConfirmed() + } + let cancelAction = UIAlertAction(title: tr("actionCancel"), style: .cancel) + let alert = UIAlertController(title: "", message: message, preferredStyle: .actionSheet) + alert.addAction(destroyAction) + alert.addAction(cancelAction) + + if let sourceView = sourceObject as? UIView { + alert.popoverPresentationController?.sourceView = sourceView + alert.popoverPresentationController?.sourceRect = sourceView.bounds + } else if let sourceBarButtonItem = sourceObject as? UIBarButtonItem { + alert.popoverPresentationController?.barButtonItem = sourceBarButtonItem + } + + presentingVC.present(alert, animated: true, completion: nil) + } +} diff --git a/WireGuard/WireGuard/UI/iOS/ViewController/TunnelDetailTableViewController.swift b/WireGuard/WireGuard/UI/iOS/ViewController/TunnelDetailTableViewController.swift index f198e06..b2dca83 100644 --- a/WireGuard/WireGuard/UI/iOS/ViewController/TunnelDetailTableViewController.swift +++ b/WireGuard/WireGuard/UI/iOS/ViewController/TunnelDetailTableViewController.swift @@ -125,21 +125,6 @@ class TunnelDetailTableViewController: UITableViewController { } } - func showConfirmationAlert(message: String, buttonTitle: String, from sourceView: UIView, onConfirmed: @escaping (() -> Void)) { - let destroyAction = UIAlertAction(title: buttonTitle, style: .destructive) { _ in - onConfirmed() - } - let cancelAction = UIAlertAction(title: tr("actionCancel"), style: .cancel) - let alert = UIAlertController(title: "", message: message, preferredStyle: .actionSheet) - alert.addAction(destroyAction) - alert.addAction(cancelAction) - - alert.popoverPresentationController?.sourceView = sourceView - alert.popoverPresentationController?.sourceRect = sourceView.bounds - - present(alert, animated: true, completion: nil) - } - func startUpdatingRuntimeConfiguration() { reloadRuntimeConfiguration() reloadRuntimeConfigurationTimer?.invalidate() @@ -435,7 +420,9 @@ extension TunnelDetailTableViewController { cell.hasDestructiveAction = true cell.onTapped = { [weak self] in guard let self = self else { return } - self.showConfirmationAlert(message: tr("deleteTunnelConfirmationAlertMessage"), buttonTitle: tr("deleteTunnelConfirmationAlertButtonTitle"), from: cell) { [weak self] in + ConfirmationAlertPresenter.showConfirmationAlert(message: tr("deleteTunnelConfirmationAlertMessage"), + buttonTitle: tr("deleteTunnelConfirmationAlertButtonTitle"), + from: cell, presentingVC: self) { [weak self] in guard let self = self else { return } self.tunnelsManager.remove(tunnel: self.tunnel) { error in if error != nil { diff --git a/WireGuard/WireGuard/UI/iOS/ViewController/TunnelEditTableViewController.swift b/WireGuard/WireGuard/UI/iOS/ViewController/TunnelEditTableViewController.swift index 7d360d1..6452bfb 100644 --- a/WireGuard/WireGuard/UI/iOS/ViewController/TunnelEditTableViewController.swift +++ b/WireGuard/WireGuard/UI/iOS/ViewController/TunnelEditTableViewController.swift @@ -311,7 +311,9 @@ extension TunnelEditTableViewController { cell.hasDestructiveAction = true cell.onTapped = { [weak self, weak peerData] in guard let self = self, let peerData = peerData else { return } - self.showConfirmationAlert(message: tr("deletePeerConfirmationAlertMessage"), buttonTitle: tr("deletePeerConfirmationAlertButtonTitle"), from: cell) { [weak self] in + ConfirmationAlertPresenter.showConfirmationAlert(message: tr("deletePeerConfirmationAlertMessage"), + buttonTitle: tr("deletePeerConfirmationAlertButtonTitle"), + from: cell, presentingVC: self) { [weak self] in guard let self = self else { return } let removedSectionIndices = self.deletePeer(peer: peerData) let shouldShowExcludePrivateIPs = (self.tunnelViewModel.peersData.count == 1 && self.tunnelViewModel.peersData[0].shouldAllowExcludePrivateIPsControl) @@ -461,21 +463,6 @@ extension TunnelEditTableViewController { loadSections() return IndexSet(integer: interfaceFieldsBySection.count + peer.index) } - - func showConfirmationAlert(message: String, buttonTitle: String, from sourceView: UIView, onConfirmed: @escaping (() -> Void)) { - let destroyAction = UIAlertAction(title: buttonTitle, style: .destructive) { _ in - onConfirmed() - } - let cancelAction = UIAlertAction(title: tr("actionCancel"), style: .cancel) - let alert = UIAlertController(title: "", message: message, preferredStyle: .actionSheet) - alert.addAction(destroyAction) - alert.addAction(cancelAction) - - alert.popoverPresentationController?.sourceView = sourceView - alert.popoverPresentationController?.sourceRect = sourceView.bounds - - present(alert, animated: true, completion: nil) - } } extension TunnelEditTableViewController { diff --git a/WireGuard/WireGuard/UI/iOS/ViewController/TunnelsListTableViewController.swift b/WireGuard/WireGuard/UI/iOS/ViewController/TunnelsListTableViewController.swift index c09bb34..d54d7a1 100644 --- a/WireGuard/WireGuard/UI/iOS/ViewController/TunnelsListTableViewController.swift +++ b/WireGuard/WireGuard/UI/iOS/ViewController/TunnelsListTableViewController.swift @@ -238,7 +238,8 @@ class TunnelsListTableViewController: UIViewController { tr(format: "deleteTunnelConfirmationAlertButtonMessage (%d)", selectedTunnels.count) : tr(format: "deleteTunnelsConfirmationAlertButtonMessage (%d)", selectedTunnels.count) let title = tr("deleteTunnelsConfirmationAlertButtonTitle") - self.showConfirmationAlert(message: message, buttonTitle: title, from: sender) { [weak self] in + ConfirmationAlertPresenter.showConfirmationAlert(message: message, buttonTitle: title, + from: sender, presentingVC: self) { [weak self] in self?.tunnelsManager?.removeMultiple(tunnels: selectedTunnels) { [weak self] error in guard let self = self else { return } if let error = error { @@ -250,20 +251,6 @@ class TunnelsListTableViewController: UIViewController { } } } - - func showConfirmationAlert(message: String, buttonTitle: String, from barButtonItem: UIBarButtonItem, onConfirmed: @escaping (() -> Void)) { - let destroyAction = UIAlertAction(title: buttonTitle, style: .destructive) { _ in - onConfirmed() - } - let cancelAction = UIAlertAction(title: tr("actionCancel"), style: .cancel) - let alert = UIAlertController(title: "", message: message, preferredStyle: .actionSheet) - alert.addAction(destroyAction) - alert.addAction(cancelAction) - - alert.popoverPresentationController?.barButtonItem = barButtonItem - - present(alert, animated: true, completion: nil) - } } extension TunnelsListTableViewController: UIDocumentPickerDelegate { -- cgit v1.2.3-59-g8ed1b