Swift 5 migration: Make use of Result type
This commit is contained in:
parent
c46e4976f3
commit
8e1bcd3c04
|
@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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()
|
||||||
|
|
|
@ -42,12 +42,10 @@ 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):
|
||||||
}
|
|
||||||
let tunnelsManager: TunnelsManager = result.value!
|
|
||||||
|
|
||||||
self.tunnelsManager = tunnelsManager
|
self.tunnelsManager = tunnelsManager
|
||||||
self.tunnelsListVC?.setTunnelsManager(tunnelsManager: tunnelsManager)
|
self.tunnelsListVC?.setTunnelsManager(tunnelsManager: tunnelsManager)
|
||||||
|
|
||||||
|
@ -57,6 +55,7 @@ class MainViewController: UISplitViewController {
|
||||||
self.onTunnelsManagerReady = nil
|
self.onTunnelsManagerReady = nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -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?()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,13 +20,11 @@ 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 tunnelsManager: TunnelsManager = result.value!
|
|
||||||
|
|
||||||
let statusMenu = StatusMenu(tunnelsManager: tunnelsManager)
|
let statusMenu = StatusMenu(tunnelsManager: tunnelsManager)
|
||||||
statusMenu.windowDelegate = self
|
statusMenu.windowDelegate = self
|
||||||
|
|
||||||
|
@ -42,6 +40,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
|
||||||
self.statusItemController = statusItemController
|
self.statusItemController = statusItemController
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@objc func quit() {
|
@objc func quit() {
|
||||||
if let manageWindow = manageTunnelsWindowObject, manageWindow.attachedSheet != nil {
|
if let manageWindow = manageTunnelsWindowObject, manageWindow.attachedSheet != nil {
|
||||||
|
|
|
@ -247,16 +247,16 @@ 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):
|
||||||
}
|
|
||||||
let tunnel: TunnelContainer = result.value!
|
|
||||||
self?.dismiss(self)
|
self?.dismiss(self)
|
||||||
self?.delegate?.tunnelSaved(tunnel: tunnel)
|
self?.delegate?.tunnelSaved(tunnel: tunnel)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@objc func handleDiscardAction() {
|
@objc func handleDiscardAction() {
|
||||||
delegate?.tunnelEditingCancelled()
|
delegate?.tunnelEditingCancelled()
|
||||||
|
|
|
@ -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"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
Loading…
Reference in New Issue