Skip to content

Commit

Permalink
Update library with provider in modules
Browse files Browse the repository at this point in the history
  • Loading branch information
keeshux committed Nov 17, 2024
1 parent 20813ac commit 134dab8
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"kind" : "remoteSourceControl",
"location" : "git@github.com:passepartoutvpn/passepartoutkit-source",
"state" : {
"revision" : "39cd828d3ee7cb502c4c0e36e3dc42e45bfae10b"
"revision" : "f1e33b960354abb8128d75914cb12e1d7b46430f"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Passepartout/Library/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ let package = Package(
],
dependencies: [
// .package(url: "git@github.com:passepartoutvpn/passepartoutkit-source", from: "0.11.0"),
.package(url: "git@github.com:passepartoutvpn/passepartoutkit-source", revision: "39cd828d3ee7cb502c4c0e36e3dc42e45bfae10b"),
.package(url: "git@github.com:passepartoutvpn/passepartoutkit-source", revision: "f1e33b960354abb8128d75914cb12e1d7b46430f"),
// .package(path: "../../../passepartoutkit-source"),
.package(url: "git@github.com:passepartoutvpn/passepartoutkit-source-openvpn-openssl", from: "0.9.1"),
// .package(url: "git@github.com:passepartoutvpn/passepartoutkit-source-openvpn-openssl", revision: "031863a1cd683962a7dfe68e20b91fa820a1ecce"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ import CommonUtils
import PassepartoutKit
import SwiftUI

extension ProviderEntityViewProviding where Self: ProviderCompatibleModule, EntityType.Configuration: ProviderConfigurationIdentifiable & Codable {
extension ProviderEntityViewProviding where Self: ProviderTransformable, EntityType.Configuration: ProviderConfigurationIdentifiable & Codable {
func vpnProviderEntityView(
with provider: ModuleMetadata.Provider,
with provider: SerializedProvider,
errorHandler: ErrorHandler,
onSelect: @escaping (any ProviderEntity & Encodable) async throws -> Void
) -> some View {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protocol ProviderEntityViewProviding {

@MainActor
func providerEntityView(
with provider: ModuleMetadata.Provider,
with provider: SerializedProvider,
errorHandler: ErrorHandler,
onSelect: @escaping (any ProviderEntity & Encodable) async throws -> Void
) -> EntityContent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ extension AppCoordinator {
enum ModalRoute: Identifiable {
case editProfile

case editProviderEntity(Profile, Module, ModuleMetadata.Provider)
case editProviderEntity(Profile, Module, SerializedProvider)

case migrateProfiles

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private extension InstalledProfileView {
}
}

func providerSelectorLabel(with provider: ModuleMetadata.Provider) -> some View {
func providerSelectorLabel(with provider: SerializedProvider) -> some View {
ProviderCountryFlag(provider: provider)
}
}
Expand Down Expand Up @@ -215,7 +215,7 @@ private struct ToggleButton: View {
}

private struct ProviderCountryFlag: View {
let provider: ModuleMetadata.Provider
let provider: SerializedProvider

var body: some View {
ThemeCountryFlag(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct ProviderEntitySelector: View {

let module: Module

let provider: ModuleMetadata.Provider
let provider: SerializedProvider

let errorHandler: ErrorHandler

Expand All @@ -61,10 +61,18 @@ private extension ProviderEntitySelector {
func onSelect(_ entity: any ProviderEntity & Encodable) async throws {
pp_log(.app, .info, "Select new provider entity: \(entity)")

var builder = profile.builder()
do {
try builder.setProviderEntity(entity, forModuleWithId: module.id)
guard var moduleBuilder = module.asProviderModuleBuilder else {
assertionFailure("Module is not a ProviderModuleBuilder?")
return
}
try moduleBuilder.setProviderEntity(entity)
let newModule = try moduleBuilder.tryBuild()

var builder = profile.builder()
builder.saveModule(newModule)
let newProfile = try builder.tryBuild()

try await profileManager.save(newProfile)

// will reconnect via AppContext observation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ extension OpenVPNModule.Builder: ModuleViewProviding {

extension OpenVPNModule: ProviderEntityViewProviding {
func providerEntityView(
with provider: ModuleMetadata.Provider,
with provider: SerializedProvider,
errorHandler: ErrorHandler,
onSelect: @escaping (any ProviderEntity & Encodable) async throws -> Void
) -> some View {
Expand Down
3 changes: 1 addition & 2 deletions Passepartout/Library/Sources/LegacyV2/Domain/MapperV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ struct MapperV2 {

if let provider = v2.provider {
if let module = try toProviderModule(provider) {
let providerId = ProviderID(rawValue: provider.name)
modules.append(module)
builder.setProviderId(providerId, forModuleWithId: module.id)
}
} else if let ovpn = v2.host?.ovpnSettings {
modules.append(try toOpenVPNModule(ovpn))
Expand Down Expand Up @@ -113,6 +111,7 @@ extension MapperV2 {
let settings = entry.value

var builder = OpenVPNModule.Builder()
builder.provider = SerializedProvider(id: ProviderID(rawValue: v2.name))
builder.credentials = settings.account.map(toOpenVPNCredentials)
return try builder.tryBuild()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ extension ProfileEditor {
} ?? removedModules[moduleId]
}

public func providerModule(withId moduleId: UUID) -> (any ProviderModuleBuilder)? {
module(withId: moduleId) as? any ProviderModuleBuilder
}

public func isActiveModule(withId moduleId: UUID) -> Bool {
editableProfile.isActiveModule(withId: moduleId)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,25 @@ extension Profile {

public var modulesBuilders: [any ModuleBuilder] {
modules.compactMap {
guard let buildableModule = $0 as? any BuildableType else {
return nil
}
let builder = buildableModule.builder() as any BuilderType
return builder as? any ModuleBuilder
$0.asModuleBuilder
}
}
}

extension Module {
public var asModuleBuilder: (any ModuleBuilder)? {
guard let buildableModule = self as? any BuildableType else {
return nil
}
let builder = buildableModule.builder() as any BuilderType
return builder as? any ModuleBuilder
}

public var asProviderModuleBuilder: (any ProviderModuleBuilder)? {
asModuleBuilder as? any ProviderModuleBuilder
}
}

private extension EditableProfile {
var activeConnectionModule: (any ModuleBuilder)? {
modules.first {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,25 @@ extension ProfileEditor {

public func binding(forProviderOf moduleId: UUID) -> Binding<ProviderID?> {
Binding { [weak self] in
self?.profile.providerId(forModuleWithId: moduleId)
self?.providerModule(withId: moduleId)?.providerId
} set: { [weak self] in
self?.profile.setProviderId($0, forModuleWithId: moduleId)
guard var builder = self?.providerModule(withId: moduleId) else {
return
}
builder.providerId = $0
self?.saveModule(builder, activating: false)
}
}

public func binding<E>(forProviderEntityOf moduleId: UUID) -> Binding<E?> where E: ProviderEntity & Codable {
Binding { [weak self] in
try? self?.profile.providerEntity(E.self, forModuleWithId: moduleId)
try? self?.providerModule(withId: moduleId)?.providerEntity(E.self)
} set: { [weak self] in
try? self?.profile.setProviderEntity($0, forModuleWithId: moduleId)
guard var builder = self?.providerModule(withId: moduleId) else {
return
}
try? builder.setProviderEntity($0)
self?.saveModule(builder, activating: false)
}
}

Expand Down

0 comments on commit 134dab8

Please sign in to comment.