From 89a564ce628770cd58a7ecb64f0b6ca0b2ac6530 Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Mon, 8 Apr 2019 13:22:06 +0530 Subject: [PATCH] Swift 5 migration: Make use of Result type Signed-off-by: Roopesh Chander --- .../WireGuard/Tunnel/TunnelsManager.swift | 16 ++++++--- WireGuard/WireGuard/UI/TunnelImporter.swift | 6 ++-- .../ViewController/MainViewController.swift | 21 ++++++----- .../TunnelEditTableViewController.swift | 6 ++-- .../TunnelsListTableViewController.swift | 5 +-- .../WireGuard/UI/macOS/AppDelegate.swift | 35 +++++++++---------- .../TunnelEditViewController.swift | 10 +++--- .../WireGuard/ZipArchive/ZipArchive.swift | 3 ++ .../WireGuard/ZipArchive/ZipImporter.swift | 14 ++------ 9 files changed, 58 insertions(+), 58 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) -> Void) { + static func create(completionHandler: @escaping (Result) -> 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) -> Void) { + func add(tunnelConfiguration: TunnelConfiguration, onDemandOption: ActivateOnDemandOption = .off, completionHandler: @escaping (Result) -> 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 + case .success(let tunnelsManager): + self.tunnelsManager = tunnelsManager + self.tunnelsListVC?.setTunnelsManager(tunnelsManager: tunnelsManager) + + tunnelsManager.activationDelegate = self + + self.onTunnelsManagerReady?(tunnelsManager) + self.onTunnelsManagerReady = nil } - let tunnelsManager: TunnelsManager = result.value! - - self.tunnelsManager = tunnelsManager - self.tunnelsListVC?.setTunnelsManager(tunnelsManager: tunnelsManager) - - tunnelsManager.activationDelegate = self - - 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 { + + switch result { + case .failure(let error): ErrorPresenter.showErrorAlert(error: error, from: nil) - return + case .success(let tunnelsManager): + let statusMenu = StatusMenu(tunnelsManager: tunnelsManager) + statusMenu.windowDelegate = self + + let statusItemController = StatusItemController() + statusItemController.statusItem.menu = statusMenu + + let tunnelsTracker = TunnelsTracker(tunnelsManager: tunnelsManager) + tunnelsTracker.statusMenu = statusMenu + tunnelsTracker.statusItemController = statusItemController + + self.tunnelsManager = tunnelsManager + self.tunnelsTracker = tunnelsTracker + self.statusItemController = statusItemController } - - let tunnelsManager: TunnelsManager = result.value! - - let statusMenu = StatusMenu(tunnelsManager: tunnelsManager) - statusMenu.windowDelegate = self - - let statusItemController = StatusItemController() - statusItemController.statusItem.menu = statusMenu - - let tunnelsTracker = TunnelsTracker(tunnelsManager: tunnelsManager) - tunnelsTracker.statusMenu = statusMenu - tunnelsTracker.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 {