diff options
| author | Jon Petersson <jon.petersson@kvadrat.se> | 2024-01-02 16:56:58 +0100 |
|---|---|---|
| committer | Bug Magnet <marco.nikic@mullvad.net> | 2024-01-25 16:56:03 +0100 |
| commit | f838ac813eb5370ee2ee372aa85ea24fb5fd5644 (patch) | |
| tree | c235d9d40ec260f0d4f7808f0f549154f6a80a9e /ios/MullvadSettings/AccessMethodRepository.swift | |
| parent | ebf5f4cf288a3f9fd5959e8ef9194ae563d88d9a (diff) | |
| download | mullvadvpn-f838ac813eb5370ee2ee372aa85ea24fb5fd5644.tar.xz mullvadvpn-f838ac813eb5370ee2ee372aa85ea24fb5fd5644.zip | |
Update API access methods functionality UI to conform with designs
Diffstat (limited to 'ios/MullvadSettings/AccessMethodRepository.swift')
| -rw-r--r-- | ios/MullvadSettings/AccessMethodRepository.swift | 55 |
1 files changed, 30 insertions, 25 deletions
diff --git a/ios/MullvadSettings/AccessMethodRepository.swift b/ios/MullvadSettings/AccessMethodRepository.swift index 6f2b253b2f..0b03f03817 100644 --- a/ios/MullvadSettings/AccessMethodRepository.swift +++ b/ios/MullvadSettings/AccessMethodRepository.swift @@ -8,22 +8,27 @@ import Combine import Foundation +import MullvadLogging public class AccessMethodRepository: AccessMethodRepositoryProtocol { - let passthroughSubject: CurrentValueSubject<[PersistentAccessMethod], Never> = CurrentValueSubject([]) + private let logger = Logger(label: "AccessMethodRepository") + private let direct = PersistentAccessMethod( id: UUID(uuidString: "C9DB7457-2A55-42C3-A926-C07F82131994")!, - name: "", + name: "Direct", isEnabled: true, proxyConfiguration: .direct ) + private let bridge = PersistentAccessMethod( id: UUID(uuidString: "8586E75A-CA7B-4432-B70D-EE65F3F95084")!, - name: "", + name: "Mullvad bridges", isEnabled: true, proxyConfiguration: .bridges ) + let passthroughSubject: CurrentValueSubject<[PersistentAccessMethod], Never> = CurrentValueSubject([]) + public var publisher: AnyPublisher<[PersistentAccessMethod], Never> { passthroughSubject.eraseToAnyPublisher() } @@ -36,35 +41,19 @@ public class AccessMethodRepository: AccessMethodRepositoryProtocol { add([direct, bridge]) } - public func add(_ method: PersistentAccessMethod) { - add([method]) - } - - public func add(_ methods: [PersistentAccessMethod]) { + public func save(_ method: PersistentAccessMethod) { var storedMethods = fetchAll() - methods.forEach { method in - guard !storedMethods.contains(where: { $0.id == method.id }) else { return } + if let index = storedMethods.firstIndex(where: { $0.id == method.id }) { + storedMethods[index] = method + } else { storedMethods.append(method) } do { try writeApiAccessMethods(storedMethods) } catch { - print("Could not add access method(s): \(methods) \nError: \(error)") - } - } - - public func update(_ method: PersistentAccessMethod) { - var methods = fetchAll() - - guard let index = methods.firstIndex(where: { $0.id == method.id }) else { return } - methods[index] = method - - do { - try writeApiAccessMethods(methods) - } catch { - print("Could not update access method: \(method) \nError: \(error)") + logger.error("Could not update access methods: \(storedMethods) \nError: \(error)") } } @@ -81,7 +70,7 @@ public class AccessMethodRepository: AccessMethodRepositoryProtocol { do { try writeApiAccessMethods(methods) } catch { - print("Could not delete access method with id: \(id) \nError: \(error)") + logger.error("Could not delete access method with id: \(id) \nError: \(error)") } } @@ -97,6 +86,22 @@ public class AccessMethodRepository: AccessMethodRepositoryProtocol { add([direct, bridge]) } + private func add(_ methods: [PersistentAccessMethod]) { + var storedMethods = fetchAll() + + methods.forEach { method in + if !storedMethods.contains(where: { $0.id == method.id }) { + storedMethods.append(method) + } + } + + do { + try writeApiAccessMethods(storedMethods) + } catch { + logger.error("Could not update access methods: \(storedMethods) \nError: \(error)") + } + } + private func readApiAccessMethods() throws -> [PersistentAccessMethod] { let parser = makeParser() let data = try SettingsManager.store.read(key: .apiAccessMethods) |
