From 02ff2f783ca6da5985713802125f4932b3026d5b Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Thu, 1 Nov 2018 11:45:09 +0530 Subject: [PATCH] VPN: An ErrorPresenter class to consolidate showing of error alerts --- WireGuard/WireGuard.xcodeproj/project.pbxproj | 6 ++- .../WireGuard/UI/iOS/ErrorPresenter.swift | 46 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 WireGuard/WireGuard/UI/iOS/ErrorPresenter.swift 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 = ""; }; 6F7774EE21722D97006A79B3 /* TunnelsManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TunnelsManager.swift; sourceTree = ""; }; 6F7774F221774263006A79B3 /* TunnelEditTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TunnelEditTableViewController.swift; sourceTree = ""; }; - 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 = ""; }; + 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 = ""; }; 6FDEF7F621863B6100D8FBF6 /* unzip.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = unzip.c; sourceTree = ""; }; 6FDEF7F721863B6100D8FBF6 /* zip.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = zip.c; sourceTree = ""; }; @@ -194,6 +196,7 @@ 6F7774F221774263006A79B3 /* TunnelEditTableViewController.swift */, 6F628C40217F47DB003482A3 /* TunnelDetailTableViewController.swift */, 6FDEF805218725D200D8FBF6 /* SettingsTableViewController.swift */, + 6F919EC2218A2AE90023B400 /* ErrorPresenter.swift */, ); path = iOS; sourceTree = ""; @@ -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) + } +}