summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAndrej Mihajlov <and@mullvad.net>2022-11-24 16:49:48 +0100
committerAndrej Mihajlov <and@mullvad.net>2022-11-28 10:39:08 +0100
commit35452115580bbb484b865ef0956a80f4a4f96ca8 (patch)
treeb0f78b78358c42180c24a131c3c2828267138fe1
parent48fd1e95e2b4fa862b06b7b3eb4c0ff03436ac28 (diff)
downloadmullvadvpn-35452115580bbb484b865ef0956a80f4a4f96ca8.tar.xz
mullvadvpn-35452115580bbb484b865ef0956a80f4a4f96ca8.zip
Add OperationCompletion.get() helper
-rw-r--r--ios/MullvadVPN/SettingsManager/SettingsMigration/MigrationFromV1ToV2.swift45
-rw-r--r--ios/Operations/OperationCompletion.swift14
2 files changed, 25 insertions, 34 deletions
diff --git a/ios/MullvadVPN/SettingsManager/SettingsMigration/MigrationFromV1ToV2.swift b/ios/MullvadVPN/SettingsManager/SettingsMigration/MigrationFromV1ToV2.swift
index ffa5839943..34956cd1e0 100644
--- a/ios/MullvadVPN/SettingsManager/SettingsMigration/MigrationFromV1ToV2.swift
+++ b/ios/MullvadVPN/SettingsManager/SettingsMigration/MigrationFromV1ToV2.swift
@@ -19,8 +19,8 @@ class MigrationFromV1ToV2: Migration {
private var accountTask: Cancellable?
private var deviceTask: Cancellable?
- private var accountCompletion: OperationCompletion<REST.AccountData, REST.Error>?
- private var devicesCompletion: OperationCompletion<[REST.Device], REST.Error>?
+ private var accountCompletion: OperationCompletion<REST.AccountData, REST.Error> = .cancelled
+ private var devicesCompletion: OperationCompletion<[REST.Device], REST.Error> = .cancelled
private let legacySettings: LegacyTunnelSettings
@@ -79,27 +79,16 @@ class MigrationFromV1ToV2: Migration {
}
dispatchGroup.notify(queue: .main) {
- switch (self.accountCompletion, self.devicesCompletion) {
- case let (.success(accountData), .success(deviceData)):
- // Migrate settings if all data is available.
-
- let result = Result {
- try self.migrateSettings(
- store: store,
- parser: parser,
- settings: self.legacySettings,
- accountData: accountData,
- devices: deviceData
- )
- }
-
- completion(result.error)
-
- default:
- let errors = [self.accountCompletion?.error, self.devicesCompletion?.error]
- .compactMap { $0 }
- completion(MigrateLegacySettingsError(underlyingErrors: errors))
+ let result = Result {
+ try self.migrateSettings(
+ store: store,
+ parser: parser,
+ settings: self.legacySettings,
+ accountData: try self.accountCompletion.get(),
+ devices: try self.devicesCompletion.get()
+ )
}
+ completion(result.error)
}
}
@@ -175,15 +164,3 @@ class MigrationFromV1ToV2: Migration {
try store.write(deviceData, for: .deviceState)
}
}
-
-struct MigrateLegacySettingsError: WrappingError {
- let underlyingErrors: [REST.Error]
-
- var underlyingError: Error? {
- return underlyingErrors.first
- }
-
- var errorDescription: String? {
- return "Failed to migrate legacy settings to v2"
- }
-}
diff --git a/ios/Operations/OperationCompletion.swift b/ios/Operations/OperationCompletion.swift
index 0e004bfe50..49a34e0884 100644
--- a/ios/Operations/OperationCompletion.swift
+++ b/ios/Operations/OperationCompletion.swift
@@ -65,6 +65,14 @@ public enum OperationCompletion<Success, Failure: Error> {
}
}
+ public func get() throws -> Success {
+ if let result = result {
+ return try result.get()
+ } else {
+ throw OperationCancellationError()
+ }
+ }
+
public func map<NewSuccess>(_ block: (Success) -> NewSuccess)
-> OperationCompletion<NewSuccess, Failure>
{
@@ -143,3 +151,9 @@ public enum OperationCompletion<Success, Failure: Error> {
return mapError { $0 }
}
}
+
+public struct OperationCancellationError: LocalizedError {
+ public var errorDescription: String? {
+ return "Operation was cancelled."
+ }
+}