diff --git a/Passepartout-iOS/Scenes/Organizer/OrganizerViewController.swift b/Passepartout-iOS/Scenes/Organizer/OrganizerViewController.swift index 04f42965..9b7bb949 100644 --- a/Passepartout-iOS/Scenes/Organizer/OrganizerViewController.swift +++ b/Passepartout-iOS/Scenes/Organizer/OrganizerViewController.swift @@ -409,7 +409,7 @@ extension OrganizerViewController { private func profile(at indexPath: IndexPath) -> ConnectionProfile { let id = sectionProfiles(at: indexPath)[indexPath.row] let section = model.section(for: indexPath.section) - let context: ConnectionService.ProfileKey.Context + let context: Context switch section { case .providers: context = .provider diff --git a/Passepartout/Sources/Model/ConnectionProfile.swift b/Passepartout/Sources/Model/ConnectionProfile.swift index 13e7ee80..c4978ea8 100644 --- a/Passepartout/Sources/Model/ConnectionProfile.swift +++ b/Passepartout/Sources/Model/ConnectionProfile.swift @@ -27,7 +27,15 @@ import Foundation import TunnelKit import NetworkExtension +enum Context: String, Codable { + case provider + + case host +} + protocol ConnectionProfile: class, EndpointDataSource { + var context: Context { get } + var id: String { get } var username: String? { get set } @@ -42,7 +50,7 @@ extension ConnectionProfile { guard let username = username else { return nil } - return "\(Bundle.main.bundleIdentifier!).\(id).\(username)" + return "\(Bundle.main.bundleIdentifier!).\(context.rawValue).\(id).\(username)" } func password(in keychain: Keychain) -> String? { diff --git a/Passepartout/Sources/Model/ConnectionService.swift b/Passepartout/Sources/Model/ConnectionService.swift index f06e75f3..03502b2a 100644 --- a/Passepartout/Sources/Model/ConnectionService.swift +++ b/Passepartout/Sources/Model/ConnectionService.swift @@ -50,12 +50,6 @@ class ConnectionService: Codable { } struct ProfileKey: RawRepresentable, Hashable, Codable { - enum Context: String { - case provider - - case host - } - let context: Context let id: String @@ -66,15 +60,7 @@ class ConnectionService: Codable { } init(_ profile: ConnectionProfile) { - if let _ = profile as? ProviderConnectionProfile { - context = .provider - } else if let _ = profile as? HostConnectionProfile { - context = .host - } else if let placeholder = profile as? PlaceholderConnectionProfile { - context = placeholder.context - } else { - fatalError("Unexpected profile type: \(type(of: profile))") - } + context = profile.context id = profile.id } @@ -288,7 +274,7 @@ class ConnectionService: Codable { } } - func profile(withContext context: ProfileKey.Context, id: String) -> ConnectionProfile? { + func profile(withContext context: Context, id: String) -> ConnectionProfile? { let key = ProfileKey(context, id) var profile = cache[key] if let _ = profile as? PlaceholderConnectionProfile { @@ -311,7 +297,7 @@ class ConnectionService: Codable { return profile } - func ids(forContext context: ProfileKey.Context) -> [String] { + func ids(forContext context: Context) -> [String] { return cache.keys.filter { $0.context == context }.map { $0.id } } @@ -485,6 +471,8 @@ class ConnectionService: Codable { } private class PlaceholderConnectionProfile: ConnectionProfile { + let context: Context + let id: String var username: String? @@ -507,8 +495,6 @@ private class PlaceholderConnectionProfile: ConnectionProfile { var customProtocol: TunnelKitProvider.EndpointProtocol? - let context: ConnectionService.ProfileKey.Context - init(_ key: ConnectionService.ProfileKey) { self.context = key.context self.id = key.id diff --git a/Passepartout/Sources/Model/Profiles/HostConnectionProfile.swift b/Passepartout/Sources/Model/Profiles/HostConnectionProfile.swift index 4d68c4ce..591cdebe 100644 --- a/Passepartout/Sources/Model/Profiles/HostConnectionProfile.swift +++ b/Passepartout/Sources/Model/Profiles/HostConnectionProfile.swift @@ -42,6 +42,8 @@ class HostConnectionProfile: ConnectionProfile, Codable, Equatable { // MARK: ConnectionProfile + let context: Context = .host + var id: String { return title } diff --git a/Passepartout/Sources/Model/Profiles/ProviderConnectionProfile.swift b/Passepartout/Sources/Model/Profiles/ProviderConnectionProfile.swift index 394ac632..11bde8ff 100644 --- a/Passepartout/Sources/Model/Profiles/ProviderConnectionProfile.swift +++ b/Passepartout/Sources/Model/Profiles/ProviderConnectionProfile.swift @@ -92,6 +92,8 @@ class ProviderConnectionProfile: ConnectionProfile, Codable, Equatable { // MARK: ConnectionProfile + let context: Context = .provider + var id: String { return name.rawValue }