aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRoopesh Chander <roop@roopc.net>2019-04-08 13:22:06 +0530
committerRoopesh Chander <roop@roopc.net>2019-04-21 17:51:42 +0530
commit89a564ce628770cd58a7ecb64f0b6ca0b2ac6530 (patch)
tree4d0079075be6251ec27da582e491d4327aa06d1f
parentmacOS: Detect when updating from the App Store (diff)
downloadwireguard-apple-89a564ce628770cd58a7ecb64f0b6ca0b2ac6530.tar.xz
wireguard-apple-89a564ce628770cd58a7ecb64f0b6ca0b2ac6530.zip
Swift 5 migration: Make use of Result type
Signed-off-by: Roopesh Chander <roop@roopc.net>
-rw-r--r--WireGuard/WireGuard/Tunnel/TunnelsManager.swift16
-rw-r--r--WireGuard/WireGuard/UI/TunnelImporter.swift6
-rw-r--r--WireGuard/WireGuard/UI/iOS/ViewController/MainViewController.swift19
-rw-r--r--WireGuard/WireGuard/UI/iOS/ViewController/TunnelEditTableViewController.swift6
-rw-r--r--WireGuard/WireGuard/UI/iOS/ViewController/TunnelsListTableViewController.swift5
-rw-r--r--WireGuard/WireGuard/UI/macOS/AppDelegate.swift31
-rw-r--r--WireGuard/WireGuard/UI/macOS/ViewController/TunnelEditViewController.swift10
-rw-r--r--WireGuard/WireGuard/ZipArchive/ZipArchive.swift3
-rw-r--r--WireGuard/WireGuard/ZipArchive/ZipImporter.swift14
9 files changed, 55 insertions, 55 deletions
diff --git a/WireGuard/WireGuard/Tunnel/TunnelsManager.swift b/WireGuard/WireGuard/Tunnel/TunnelsManager.swift
index ce8008c..edf7690 100644
--- a/WireGuard/WireGuard/Tunnel/TunnelsManager.swift
+++ b/WireGuard/WireGuard/Tunnel/TunnelsManager.swift
@@ -33,7 +33,7 @@ class TunnelsManager {
startObservingTunnelConfigurations()
}
- static func create(completionHandler: @escaping (WireGuardResult<TunnelsManager>) -> Void) {
+ static func create(completionHandler: @escaping (Result<TunnelsManager, TunnelsManagerError>) -> Void) {
#if targetEnvironment(simulator)
completionHandler(.success(TunnelsManager(tunnelProviders: MockTunnels.createMockTunnels())))
#else
@@ -104,7 +104,7 @@ class TunnelsManager {
}
}
- func add(tunnelConfiguration: TunnelConfiguration, onDemandOption: ActivateOnDemandOption = .off, completionHandler: @escaping (WireGuardResult<TunnelContainer>) -> Void) {
+ func add(tunnelConfiguration: TunnelConfiguration, onDemandOption: ActivateOnDemandOption = .off, completionHandler: @escaping (Result<TunnelContainer, TunnelsManagerError>) -> Void) {
let tunnelName = tunnelConfiguration.name ?? ""
if tunnelName.isEmpty {
completionHandler(.failure(TunnelsManagerError.tunnelNameEmpty))
@@ -167,9 +167,15 @@ class TunnelsManager {
let tail = tunnelConfigurations.dropFirst()
add(tunnelConfiguration: head) { [weak self, tail] result in
DispatchQueue.main.async {
- let numberSuccessful = numberSuccessful + (result.isSuccess ? 1 : 0)
- let lastError = lastError ?? (result.error as? TunnelsManagerError)
- self?.addMultiple(tunnelConfigurations: tail, numberSuccessful: numberSuccessful, lastError: lastError, completionHandler: completionHandler)
+ var numberSuccessfulCount = numberSuccessful
+ var lastError: TunnelsManagerError?
+ switch result {
+ case .failure(let error):
+ lastError = error
+ case .success:
+ numberSuccessfulCount = numberSuccessful + 1
+ }
+ self?.addMultiple(tunnelConfigurations: tail, numberSuccessful: numberSuccessfulCount, lastError: lastError, completionHandler: completionHandler)
}
}
}
diff --git a/WireGuard/WireGuard/UI/TunnelImporter.swift b/WireGuard/WireGuard/UI/TunnelImporter.swift
index 70e536f..cf05ff9 100644
--- a/WireGuard/WireGuard/UI/TunnelImporter.swift
+++ b/WireGuard/WireGuard/UI/TunnelImporter.swift
@@ -16,10 +16,10 @@ class TunnelImporter {
if url.pathExtension.lowercased() == "zip" {
dispatchGroup.enter()
ZipImporter.importConfigFiles(from: url) { result in
- if let error = result.error {
+ switch result {
+ case .failure(let error):
lastFileImportErrorText = error.alertText
- }
- if let configsInZip = result.value {
+ case .success(let configsInZip):
configs.append(contentsOf: configsInZip)
}
dispatchGroup.leave()
diff --git a/WireGuard/WireGuard/UI/iOS/ViewController/MainViewController.swift b/WireGuard/WireGuard/UI/iOS/ViewController/MainViewController.swift
index 48cb7b0..380299c 100644
--- a/WireGuard/WireGuard/UI/iOS/ViewController/MainViewController.swift
+++ b/WireGuard/WireGuard/UI/iOS/ViewController/MainViewController.swift
@@ -42,19 +42,18 @@ class MainViewController: UISplitViewController {
TunnelsManager.create { [weak self] result in
guard let self = self else { return }
- if let error = result.error {
+ switch result {
+ case .failure(let error):
ErrorPresenter.showErrorAlert(error: error, from: self)
- return
- }
- let tunnelsManager: TunnelsManager = result.value!
-
- self.tunnelsManager = tunnelsManager
- self.tunnelsListVC?.setTunnelsManager(tunnelsManager: tunnelsManager)
+ case .success(let tunnelsManager):
+ self.tunnelsManager = tunnelsManager
+ self.tunnelsListVC?.setTunnelsManager(tunnelsManager: tunnelsManager)
- tunnelsManager.activationDelegate = self
+ tunnelsManager.activationDelegate = self
- self.onTunnelsManagerReady?(tunnelsManager)
- self.onTunnelsManagerReady = nil
+ self.onTunnelsManagerReady?(tunnelsManager)
+ self.onTunnelsManagerReady = nil
+ }
}
}
diff --git a/WireGuard/WireGuard/UI/iOS/ViewController/TunnelEditTableViewController.swift b/WireGuard/WireGuard/UI/iOS/ViewController/TunnelEditTableViewController.swift
index 6452bfb..e9c0995 100644
--- a/WireGuard/WireGuard/UI/iOS/ViewController/TunnelEditTableViewController.swift
+++ b/WireGuard/WireGuard/UI/iOS/ViewController/TunnelEditTableViewController.swift
@@ -127,10 +127,10 @@ class TunnelEditTableViewController: UITableViewController {
} else {
// We're adding a new tunnel
tunnelsManager.add(tunnelConfiguration: tunnelConfiguration, onDemandOption: onDemandOption) { [weak self] result in
- if let error = result.error {
+ switch result {
+ case .failure(let error):
ErrorPresenter.showErrorAlert(error: error, from: self)
- } else {
- let tunnel: TunnelContainer = result.value!
+ case .success(let tunnel):
self?.dismiss(animated: true, completion: nil)
self?.delegate?.tunnelSaved(tunnel: tunnel)
}
diff --git a/WireGuard/WireGuard/UI/iOS/ViewController/TunnelsListTableViewController.swift b/WireGuard/WireGuard/UI/iOS/ViewController/TunnelsListTableViewController.swift
index d54d7a1..eda1ffa 100644
--- a/WireGuard/WireGuard/UI/iOS/ViewController/TunnelsListTableViewController.swift
+++ b/WireGuard/WireGuard/UI/iOS/ViewController/TunnelsListTableViewController.swift
@@ -264,9 +264,10 @@ extension TunnelsListTableViewController: QRScanViewControllerDelegate {
func addScannedQRCode(tunnelConfiguration: TunnelConfiguration, qrScanViewController: QRScanViewController,
completionHandler: (() -> Void)?) {
tunnelsManager?.add(tunnelConfiguration: tunnelConfiguration) { result in
- if let error = result.error {
+ switch result {
+ case .failure(let error):
ErrorPresenter.showErrorAlert(error: error, from: qrScanViewController, onDismissal: completionHandler)
- } else {
+ case .success:
completionHandler?()
}
}
diff --git a/WireGuard/WireGuard/UI/macOS/AppDelegate.swift b/WireGuard/WireGuard/UI/macOS/AppDelegate.swift
index 3e98c20..a5f8c34 100644
--- a/WireGuard/WireGuard/UI/macOS/AppDelegate.swift
+++ b/WireGuard/WireGuard/UI/macOS/AppDelegate.swift
@@ -20,26 +20,25 @@ class AppDelegate: NSObject, NSApplicationDelegate {
TunnelsManager.create { [weak self] result in
guard let self = self else { return }
- if let error = result.error {
- ErrorPresenter.showErrorAlert(error: error, from: nil)
- return
- }
-
- let tunnelsManager: TunnelsManager = result.value!
- let statusMenu = StatusMenu(tunnelsManager: tunnelsManager)
- statusMenu.windowDelegate = self
+ switch result {
+ case .failure(let error):
+ ErrorPresenter.showErrorAlert(error: error, from: nil)
+ case .success(let tunnelsManager):
+ let statusMenu = StatusMenu(tunnelsManager: tunnelsManager)
+ statusMenu.windowDelegate = self
- let statusItemController = StatusItemController()
- statusItemController.statusItem.menu = statusMenu
+ let statusItemController = StatusItemController()
+ statusItemController.statusItem.menu = statusMenu
- let tunnelsTracker = TunnelsTracker(tunnelsManager: tunnelsManager)
- tunnelsTracker.statusMenu = statusMenu
- tunnelsTracker.statusItemController = statusItemController
+ let tunnelsTracker = TunnelsTracker(tunnelsManager: tunnelsManager)
+ tunnelsTracker.statusMenu = statusMenu
+ tunnelsTracker.statusItemController = statusItemController
- self.tunnelsManager = tunnelsManager
- self.tunnelsTracker = tunnelsTracker
- self.statusItemController = statusItemController
+ self.tunnelsManager = tunnelsManager
+ self.tunnelsTracker = tunnelsTracker
+ self.statusItemController = statusItemController
+ }
}
}
diff --git a/WireGuard/WireGuard/UI/macOS/ViewController/TunnelEditViewController.swift b/WireGuard/WireGuard/UI/macOS/ViewController/TunnelEditViewController.swift
index e643ffc..471c4fe 100644
--- a/WireGuard/WireGuard/UI/macOS/ViewController/TunnelEditViewController.swift
+++ b/WireGuard/WireGuard/UI/macOS/ViewController/TunnelEditViewController.swift
@@ -247,13 +247,13 @@ class TunnelEditViewController: NSViewController {
// We're creating a new tunnel
self.tunnelsManager.add(tunnelConfiguration: tunnelConfiguration, onDemandOption: onDemandOption) { [weak self] result in
self?.setUserInteractionEnabled(true)
- if let error = result.error {
+ switch result {
+ case .failure(let error):
ErrorPresenter.showErrorAlert(error: error, from: self)
- return
+ case .success(let tunnel):
+ self?.dismiss(self)
+ self?.delegate?.tunnelSaved(tunnel: tunnel)
}
- let tunnel: TunnelContainer = result.value!
- self?.dismiss(self)
- self?.delegate?.tunnelSaved(tunnel: tunnel)
}
}
}
diff --git a/WireGuard/WireGuard/ZipArchive/ZipArchive.swift b/WireGuard/WireGuard/ZipArchive/ZipArchive.swift
index 85623da..c946e24 100644
--- a/WireGuard/WireGuard/ZipArchive/ZipArchive.swift
+++ b/WireGuard/WireGuard/ZipArchive/ZipArchive.swift
@@ -7,6 +7,7 @@ enum ZipArchiveError: WireGuardAppError {
case cantOpenInputZipFile
case cantOpenOutputZipFileForWriting
case badArchive
+ case noTunnelsInZipArchive
var alertText: AlertText {
switch self {
@@ -16,6 +17,8 @@ enum ZipArchiveError: WireGuardAppError {
return (tr("alertCantOpenOutputZipFileForWritingTitle"), tr("alertCantOpenOutputZipFileForWritingMessage"))
case .badArchive:
return (tr("alertBadArchiveTitle"), tr("alertBadArchiveMessage"))
+ case .noTunnelsInZipArchive:
+ return (tr("alertNoTunnelsInImportedZipArchiveTitle"), tr("alertNoTunnelsInImportedZipArchiveMessage"))
}
}
}
diff --git a/WireGuard/WireGuard/ZipArchive/ZipImporter.swift b/WireGuard/WireGuard/ZipArchive/ZipImporter.swift
index ade30a6..499181a 100644
--- a/WireGuard/WireGuard/ZipArchive/ZipImporter.swift
+++ b/WireGuard/WireGuard/ZipArchive/ZipImporter.swift
@@ -3,16 +3,8 @@
import Foundation
-enum ZipImporterError: WireGuardAppError {
- case noTunnelsInZipArchive
-
- var alertText: AlertText {
- return (tr("alertNoTunnelsInImportedZipArchiveTitle"), tr("alertNoTunnelsInImportedZipArchiveMessage"))
- }
-}
-
class ZipImporter {
- static func importConfigFiles(from url: URL, completion: @escaping (WireGuardResult<[TunnelConfiguration?]>) -> Void) {
+ static func importConfigFiles(from url: URL, completion: @escaping (Result<[TunnelConfiguration?], ZipArchiveError>) -> Void) {
DispatchQueue.global(qos: .userInitiated).async {
var unarchivedFiles: [(fileBaseName: String, contents: Data)]
do {
@@ -28,9 +20,9 @@ class ZipImporter {
}
if unarchivedFiles.isEmpty {
- throw ZipImporterError.noTunnelsInZipArchive
+ throw ZipArchiveError.noTunnelsInZipArchive
}
- } catch let error as WireGuardAppError {
+ } catch let error as ZipArchiveError {
DispatchQueue.main.async { completion(.failure(error)) }
return
} catch {