Swift 5 migration: Make use of Result type

Signed-off-by: Roopesh Chander <roop@roopc.net>
This commit is contained in:
Roopesh Chander 2019-04-08 13:22:06 +05:30
parent 178fe86d36
commit 89a564ce62
9 changed files with 58 additions and 58 deletions

View File

@ -33,7 +33,7 @@ class TunnelsManager {
startObservingTunnelConfigurations() startObservingTunnelConfigurations()
} }
static func create(completionHandler: @escaping (WireGuardResult<TunnelsManager>) -> Void) { static func create(completionHandler: @escaping (Result<TunnelsManager, TunnelsManagerError>) -> Void) {
#if targetEnvironment(simulator) #if targetEnvironment(simulator)
completionHandler(.success(TunnelsManager(tunnelProviders: MockTunnels.createMockTunnels()))) completionHandler(.success(TunnelsManager(tunnelProviders: MockTunnels.createMockTunnels())))
#else #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 ?? "" let tunnelName = tunnelConfiguration.name ?? ""
if tunnelName.isEmpty { if tunnelName.isEmpty {
completionHandler(.failure(TunnelsManagerError.tunnelNameEmpty)) completionHandler(.failure(TunnelsManagerError.tunnelNameEmpty))
@ -167,9 +167,15 @@ class TunnelsManager {
let tail = tunnelConfigurations.dropFirst() let tail = tunnelConfigurations.dropFirst()
add(tunnelConfiguration: head) { [weak self, tail] result in add(tunnelConfiguration: head) { [weak self, tail] result in
DispatchQueue.main.async { DispatchQueue.main.async {
let numberSuccessful = numberSuccessful + (result.isSuccess ? 1 : 0) var numberSuccessfulCount = numberSuccessful
let lastError = lastError ?? (result.error as? TunnelsManagerError) var lastError: TunnelsManagerError?
self?.addMultiple(tunnelConfigurations: tail, numberSuccessful: numberSuccessful, lastError: lastError, completionHandler: completionHandler) switch result {
case .failure(let error):
lastError = error
case .success:
numberSuccessfulCount = numberSuccessful + 1
}
self?.addMultiple(tunnelConfigurations: tail, numberSuccessful: numberSuccessfulCount, lastError: lastError, completionHandler: completionHandler)
} }
} }
} }

View File

@ -16,10 +16,10 @@ class TunnelImporter {
if url.pathExtension.lowercased() == "zip" { if url.pathExtension.lowercased() == "zip" {
dispatchGroup.enter() dispatchGroup.enter()
ZipImporter.importConfigFiles(from: url) { result in ZipImporter.importConfigFiles(from: url) { result in
if let error = result.error { switch result {
case .failure(let error):
lastFileImportErrorText = error.alertText lastFileImportErrorText = error.alertText
} case .success(let configsInZip):
if let configsInZip = result.value {
configs.append(contentsOf: configsInZip) configs.append(contentsOf: configsInZip)
} }
dispatchGroup.leave() dispatchGroup.leave()

View File

@ -42,19 +42,18 @@ class MainViewController: UISplitViewController {
TunnelsManager.create { [weak self] result in TunnelsManager.create { [weak self] result in
guard let self = self else { return } guard let self = self else { return }
if let error = result.error { switch result {
case .failure(let error):
ErrorPresenter.showErrorAlert(error: error, from: self) 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
} }
} }

View File

@ -127,10 +127,10 @@ class TunnelEditTableViewController: UITableViewController {
} else { } else {
// We're adding a new tunnel // We're adding a new tunnel
tunnelsManager.add(tunnelConfiguration: tunnelConfiguration, onDemandOption: onDemandOption) { [weak self] result in 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) ErrorPresenter.showErrorAlert(error: error, from: self)
} else { case .success(let tunnel):
let tunnel: TunnelContainer = result.value!
self?.dismiss(animated: true, completion: nil) self?.dismiss(animated: true, completion: nil)
self?.delegate?.tunnelSaved(tunnel: tunnel) self?.delegate?.tunnelSaved(tunnel: tunnel)
} }

View File

@ -264,9 +264,10 @@ extension TunnelsListTableViewController: QRScanViewControllerDelegate {
func addScannedQRCode(tunnelConfiguration: TunnelConfiguration, qrScanViewController: QRScanViewController, func addScannedQRCode(tunnelConfiguration: TunnelConfiguration, qrScanViewController: QRScanViewController,
completionHandler: (() -> Void)?) { completionHandler: (() -> Void)?) {
tunnelsManager?.add(tunnelConfiguration: tunnelConfiguration) { result in 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) ErrorPresenter.showErrorAlert(error: error, from: qrScanViewController, onDismissal: completionHandler)
} else { case .success:
completionHandler?() completionHandler?()
} }
} }

View File

@ -20,26 +20,25 @@ class AppDelegate: NSObject, NSApplicationDelegate {
TunnelsManager.create { [weak self] result in TunnelsManager.create { [weak self] result in
guard let self = self else { return } guard let self = self else { return }
if let error = result.error {
switch result {
case .failure(let error):
ErrorPresenter.showErrorAlert(error: error, from: nil) 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
} }
} }

View File

@ -247,13 +247,13 @@ class TunnelEditViewController: NSViewController {
// We're creating a new tunnel // We're creating a new tunnel
self.tunnelsManager.add(tunnelConfiguration: tunnelConfiguration, onDemandOption: onDemandOption) { [weak self] result in self.tunnelsManager.add(tunnelConfiguration: tunnelConfiguration, onDemandOption: onDemandOption) { [weak self] result in
self?.setUserInteractionEnabled(true) self?.setUserInteractionEnabled(true)
if let error = result.error { switch result {
case .failure(let error):
ErrorPresenter.showErrorAlert(error: error, from: self) 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)
} }
} }
} }

View File

@ -7,6 +7,7 @@ enum ZipArchiveError: WireGuardAppError {
case cantOpenInputZipFile case cantOpenInputZipFile
case cantOpenOutputZipFileForWriting case cantOpenOutputZipFileForWriting
case badArchive case badArchive
case noTunnelsInZipArchive
var alertText: AlertText { var alertText: AlertText {
switch self { switch self {
@ -16,6 +17,8 @@ enum ZipArchiveError: WireGuardAppError {
return (tr("alertCantOpenOutputZipFileForWritingTitle"), tr("alertCantOpenOutputZipFileForWritingMessage")) return (tr("alertCantOpenOutputZipFileForWritingTitle"), tr("alertCantOpenOutputZipFileForWritingMessage"))
case .badArchive: case .badArchive:
return (tr("alertBadArchiveTitle"), tr("alertBadArchiveMessage")) return (tr("alertBadArchiveTitle"), tr("alertBadArchiveMessage"))
case .noTunnelsInZipArchive:
return (tr("alertNoTunnelsInImportedZipArchiveTitle"), tr("alertNoTunnelsInImportedZipArchiveMessage"))
} }
} }
} }

View File

@ -3,16 +3,8 @@
import Foundation import Foundation
enum ZipImporterError: WireGuardAppError {
case noTunnelsInZipArchive
var alertText: AlertText {
return (tr("alertNoTunnelsInImportedZipArchiveTitle"), tr("alertNoTunnelsInImportedZipArchiveMessage"))
}
}
class ZipImporter { 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 { DispatchQueue.global(qos: .userInitiated).async {
var unarchivedFiles: [(fileBaseName: String, contents: Data)] var unarchivedFiles: [(fileBaseName: String, contents: Data)]
do { do {
@ -28,9 +20,9 @@ class ZipImporter {
} }
if unarchivedFiles.isEmpty { 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)) } DispatchQueue.main.async { completion(.failure(error)) }
return return
} catch { } catch {