diff options
author | Roopesh Chander <roop@roopc.net> | 2018-11-01 11:45:09 +0530 |
---|---|---|
committer | Roopesh Chander <roop@roopc.net> | 2018-11-01 11:45:44 +0530 |
commit | bd1820e9f58c7460f9a71d499b84c3a62c8c66e0 (patch) | |
tree | ad0ee272b926b7b0e5c1a7688f64b3232119098a /WireGuard | |
parent | VPN: Introduce an error enum for adding/modifying/removing tunnels (diff) | |
download | wireguard-apple-bd1820e9f58c7460f9a71d499b84c3a62c8c66e0.tar.xz wireguard-apple-bd1820e9f58c7460f9a71d499b84c3a62c8c66e0.zip |
VPN: An ErrorPresenter class to consolidate showing of error alerts
Signed-off-by: Roopesh Chander <roop@roopc.net>
Diffstat (limited to 'WireGuard')
-rw-r--r-- | WireGuard/WireGuard.xcodeproj/project.pbxproj | 6 | ||||
-rw-r--r-- | WireGuard/WireGuard/UI/iOS/ErrorPresenter.swift | 46 |
2 files changed, 51 insertions, 1 deletions
diff --git a/WireGuard/WireGuard.xcodeproj/project.pbxproj b/WireGuard/WireGuard.xcodeproj/project.pbxproj index cdcdef6..966cf71 100644 --- a/WireGuard/WireGuard.xcodeproj/project.pbxproj +++ b/WireGuard/WireGuard.xcodeproj/project.pbxproj @@ -28,6 +28,7 @@ 6F7774EA217229DB006A79B3 /* IPAddressRange.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F7774E9217229DB006A79B3 /* IPAddressRange.swift */; }; 6F7774EF21722D97006A79B3 /* TunnelsManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F7774EE21722D97006A79B3 /* TunnelsManager.swift */; }; 6F7774F321774263006A79B3 /* TunnelEditTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F7774F221774263006A79B3 /* TunnelEditTableViewController.swift */; }; + 6F919EC3218A2AE90023B400 /* ErrorPresenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F919EC2218A2AE90023B400 /* ErrorPresenter.swift */; }; 6FDEF7E421846C1A00D8FBF6 /* libwg-go.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 6FDEF7E321846C1A00D8FBF6 /* libwg-go.a */; }; 6FDEF7E62185EFB200D8FBF6 /* QRScanViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FDEF7E52185EFAF00D8FBF6 /* QRScanViewController.swift */; }; 6FDEF7FB21863B6100D8FBF6 /* unzip.c in Sources */ = {isa = PBXBuildFile; fileRef = 6FDEF7F621863B6100D8FBF6 /* unzip.c */; }; @@ -97,7 +98,8 @@ 6F7774E9217229DB006A79B3 /* IPAddressRange.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IPAddressRange.swift; sourceTree = "<group>"; }; 6F7774EE21722D97006A79B3 /* TunnelsManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TunnelsManager.swift; sourceTree = "<group>"; }; 6F7774F221774263006A79B3 /* TunnelEditTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TunnelEditTableViewController.swift; sourceTree = "<group>"; }; - 6FDEF7E321846C1A00D8FBF6 /* libwg-go.a */ = {isa = PBXFileReference; explicitFileType = "archive.ar"; path = "libwg-go.a"; sourceTree = BUILT_PRODUCTS_DIR; }; + 6F919EC2218A2AE90023B400 /* ErrorPresenter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ErrorPresenter.swift; sourceTree = "<group>"; }; + 6FDEF7E321846C1A00D8FBF6 /* libwg-go.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; path = "libwg-go.a"; sourceTree = BUILT_PRODUCTS_DIR; }; 6FDEF7E52185EFAF00D8FBF6 /* QRScanViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = QRScanViewController.swift; sourceTree = "<group>"; }; 6FDEF7F621863B6100D8FBF6 /* unzip.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = unzip.c; sourceTree = "<group>"; }; 6FDEF7F721863B6100D8FBF6 /* zip.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = zip.c; sourceTree = "<group>"; }; @@ -194,6 +196,7 @@ 6F7774F221774263006A79B3 /* TunnelEditTableViewController.swift */, 6F628C40217F47DB003482A3 /* TunnelDetailTableViewController.swift */, 6FDEF805218725D200D8FBF6 /* SettingsTableViewController.swift */, + 6F919EC2218A2AE90023B400 /* ErrorPresenter.swift */, ); path = iOS; sourceTree = "<group>"; @@ -473,6 +476,7 @@ 6FDEF7FC21863B6100D8FBF6 /* zip.c in Sources */, 6F628C3F217F3413003482A3 /* DNSServer.swift in Sources */, 6F628C3D217F09E9003482A3 /* TunnelViewModel.swift in Sources */, + 6F919EC3218A2AE90023B400 /* ErrorPresenter.swift in Sources */, 6FDEF8082187442100D8FBF6 /* WgQuickConfigFileWriter.swift in Sources */, 6F7774EA217229DB006A79B3 /* IPAddressRange.swift in Sources */, 6F7774E82172020C006A79B3 /* Configuration.swift in Sources */, diff --git a/WireGuard/WireGuard/UI/iOS/ErrorPresenter.swift b/WireGuard/WireGuard/UI/iOS/ErrorPresenter.swift new file mode 100644 index 0000000..8cdb921 --- /dev/null +++ b/WireGuard/WireGuard/UI/iOS/ErrorPresenter.swift @@ -0,0 +1,46 @@ +// SPDX-License-Identifier: MIT +// Copyright © 2018 WireGuard LLC. All Rights Reserved. + +import UIKit +import os.log + +class ErrorPresenter { + static func errorMessage(for error: Error) -> (String, String)? { + switch (error) { + case TunnelManagementError.tunnelAlreadyExistsWithThatName: + return ("Name already in use", "A tunnel with that name already exists. Please pick a different name.") + case TunnelManagementError.vpnSystemErrorOnAddTunnel: + return ("Could not create tunnel", "Internal error") + case TunnelManagementError.vpnSystemErrorOnModifyTunnel: + return ("Could not modify tunnel", "Internal error") + case TunnelManagementError.vpnSystemErrorOnRemoveTunnel: + return ("Could not remove tunnel", "Internal error") + default: + os_log("ErrorPresenter: Error not presented: %{public}@", log: OSLog.default, type: .error, "\(error)") + return nil + } + } + + static func showErrorAlert(error: Error, from sourceVC: UIViewController?, onDismissal: (() -> Void)? = nil) { + guard let sourceVC = sourceVC else { return } + guard let (title, message) = ErrorPresenter.errorMessage(for: error) else { return } + let okAction = UIAlertAction(title: "Ok", style: .default) { (_) in + onDismissal?() + } + let alert = UIAlertController(title: title, message: message, preferredStyle: .alert) + alert.addAction(okAction) + + sourceVC.present(alert, animated: true) + } + + static func showErrorAlert(title: String, message: String, from sourceVC: UIViewController?, onDismissal: (() -> Void)? = nil) { + guard let sourceVC = sourceVC else { return } + let okAction = UIAlertAction(title: "Ok", style: .default) { (_) in + onDismissal?() + } + let alert = UIAlertController(title: title, message: message, preferredStyle: .alert) + alert.addAction(okAction) + + sourceVC.present(alert, animated: true) + } +} |