Merge branch 'refactor-profile-encapsulation'

This commit is contained in:
Davide De Rosa 2021-01-08 19:11:38 +01:00
commit 0f44e9dc71
6 changed files with 59 additions and 10 deletions

View File

@ -430,7 +430,7 @@ class StatusMenu: NSObject {
return return
} }
assert(!group.pools.isEmpty) assert(!group.pools.isEmpty)
service.setPoolId(group.pools.randomElement()!.id, forProviderProfile: profile) profile.poolId = group.pools.randomElement()!.id
vpn.reconnect(completionHandler: nil) vpn.reconnect(completionHandler: nil)
// update menu // update menu
@ -447,7 +447,7 @@ class StatusMenu: NSObject {
guard let profile = service.activeProfile as? ProviderConnectionProfile else { guard let profile = service.activeProfile as? ProviderConnectionProfile else {
return return
} }
service.setPoolId(pool.id, forProviderProfile: profile) profile.poolId = pool.id
vpn.reconnect(completionHandler: nil) vpn.reconnect(completionHandler: nil)
// update menu // update menu

View File

@ -338,7 +338,7 @@ extension ServiceViewController: ProviderServiceViewDelegate {
uncheckedProviderProfile.presetId = fallback uncheckedProviderProfile.presetId = fallback
} }
service.setPoolId(pool.id, forProviderProfile: uncheckedProviderProfile) uncheckedProviderProfile.poolId = pool.id
// vpn.reinstallIfEnabled() // vpn.reinstallIfEnabled()
} }

View File

@ -48,6 +48,8 @@ public protocol ConnectionProfile: class, EndpointDataSource, CustomStringConver
var manualNetworkSettings: ProfileNetworkSettings? { get set } var manualNetworkSettings: ProfileNetworkSettings? { get set }
var serviceDelegate: ConnectionServiceDelegate? { get set }
func generate(from configuration: OpenVPNTunnelProvider.Configuration, preferences: Preferences) throws -> OpenVPNTunnelProvider.Configuration func generate(from configuration: OpenVPNTunnelProvider.Configuration, preferences: Preferences) throws -> OpenVPNTunnelProvider.Configuration
} }

View File

@ -27,6 +27,28 @@ import Foundation
import TunnelKit import TunnelKit
public class HostConnectionProfile: ConnectionProfile, Codable, Equatable { public class HostConnectionProfile: ConnectionProfile, Codable, Equatable {
// XXX: drop after @transient serviceDelegate
public enum CodingKeys: CodingKey {
case hostname
case parameters
case customAddress
case customProtocol
case id
case username
case trustedNetworks
case networkChoices
case manualNetworkSettings
}
public let hostname: String public let hostname: String
public var parameters: OpenVPNTunnelProvider.Configuration public var parameters: OpenVPNTunnelProvider.Configuration
@ -64,6 +86,8 @@ public class HostConnectionProfile: ConnectionProfile, Codable, Equatable {
public var manualNetworkSettings: ProfileNetworkSettings? public var manualNetworkSettings: ProfileNetworkSettings?
public weak var serviceDelegate: ConnectionServiceDelegate?
public func generate(from configuration: OpenVPNTunnelProvider.Configuration, preferences: Preferences) throws -> OpenVPNTunnelProvider.Configuration { public func generate(from configuration: OpenVPNTunnelProvider.Configuration, preferences: Preferences) throws -> OpenVPNTunnelProvider.Configuration {
guard let endpointProtocols = parameters.sessionConfiguration.endpointProtocols, !endpointProtocols.isEmpty else { guard let endpointProtocols = parameters.sessionConfiguration.endpointProtocols, !endpointProtocols.isEmpty else {
preconditionFailure("No endpointProtocols") preconditionFailure("No endpointProtocols")

View File

@ -48,6 +48,8 @@ public class PlaceholderConnectionProfile: ConnectionProfile {
public var manualNetworkSettings: ProfileNetworkSettings? public var manualNetworkSettings: ProfileNetworkSettings?
public weak var serviceDelegate: ConnectionServiceDelegate?
public func generate(from configuration: OpenVPNTunnelProvider.Configuration, preferences: Preferences) throws -> OpenVPNTunnelProvider.Configuration { public func generate(from configuration: OpenVPNTunnelProvider.Configuration, preferences: Preferences) throws -> OpenVPNTunnelProvider.Configuration {
fatalError("Generating configuration from a PlaceholderConnectionProfile") fatalError("Generating configuration from a PlaceholderConnectionProfile")
} }

View File

@ -27,6 +27,30 @@ import Foundation
import TunnelKit import TunnelKit
public class ProviderConnectionProfile: ConnectionProfile, Codable, Equatable { public class ProviderConnectionProfile: ConnectionProfile, Codable, Equatable {
// XXX: drop after @transient serviceDelegate
public enum CodingKeys: CodingKey {
case name
case poolId
case presetId
case customAddress
case customProtocol
case favoriteGroupIds
case username
case trustedNetworks
case networkChoices
case manualNetworkSettings
}
public let name: Infrastructure.Name public let name: Infrastructure.Name
public var infrastructure: Infrastructure { public var infrastructure: Infrastructure {
@ -39,6 +63,7 @@ public class ProviderConnectionProfile: ConnectionProfile, Codable, Equatable {
public var poolId: String { public var poolId: String {
didSet { didSet {
validateEndpoint() validateEndpoint()
serviceDelegate?.connectionService(didUpdate: self)
} }
} }
@ -49,6 +74,7 @@ public class ProviderConnectionProfile: ConnectionProfile, Codable, Equatable {
public var presetId: String { public var presetId: String {
didSet { didSet {
validateEndpoint() validateEndpoint()
serviceDelegate?.connectionService(didUpdate: self)
} }
} }
@ -122,6 +148,8 @@ public class ProviderConnectionProfile: ConnectionProfile, Codable, Equatable {
public var manualNetworkSettings: ProfileNetworkSettings? public var manualNetworkSettings: ProfileNetworkSettings?
public weak var serviceDelegate: ConnectionServiceDelegate?
public func generate(from configuration: OpenVPNTunnelProvider.Configuration, preferences: Preferences) throws -> OpenVPNTunnelProvider.Configuration { public func generate(from configuration: OpenVPNTunnelProvider.Configuration, preferences: Preferences) throws -> OpenVPNTunnelProvider.Configuration {
guard let pool = pool else { guard let pool = pool else {
preconditionFailure("Nil pool?") preconditionFailure("Nil pool?")
@ -214,10 +242,3 @@ public extension ProviderConnectionProfile {
return true return true
} }
} }
public extension ConnectionService {
func setPoolId(_ poolId: String, forProviderProfile profile: ProviderConnectionProfile) {
profile.poolId = poolId
delegate?.connectionService(didUpdate: profile)
}
}