Skip to content

Commit

Permalink
Embrace simplifications in PassepartoutProviders (#975)
Browse files Browse the repository at this point in the history
Update library with the new domain reorganization.
  • Loading branch information
keeshux authored Dec 3, 2024
1 parent 7611f6f commit 2d93fa6
Show file tree
Hide file tree
Showing 26 changed files with 127 additions and 221 deletions.
2 changes: 1 addition & 1 deletion Library/Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"kind" : "remoteSourceControl",
"location" : "[email protected]:passepartoutvpn/passepartoutkit-source",
"state" : {
"revision" : "cdc7a637ddebdda3dda5c4c4ccd58055eb2ee5d8"
"revision" : "ff9a47ee0aad9a2a2947f5ce23346ced93d3d3d2"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Library/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ let package = Package(
],
dependencies: [
// .package(url: "[email protected]:passepartoutvpn/passepartoutkit-source", from: "0.12.0"),
.package(url: "[email protected]:passepartoutvpn/passepartoutkit-source", revision: "cdc7a637ddebdda3dda5c4c4ccd58055eb2ee5d8"),
.package(url: "[email protected]:passepartoutvpn/passepartoutkit-source", revision: "ff9a47ee0aad9a2a2947f5ce23346ced93d3d3d2"),
// .package(path: "../../passepartoutkit-source"),
.package(url: "[email protected]:passepartoutvpn/passepartoutkit-source-openvpn-openssl", from: "0.9.1"),
// .package(url: "[email protected]:passepartoutvpn/passepartoutkit-source-openvpn-openssl", revision: "031863a1cd683962a7dfe68e20b91fa820a1ecce"),
Expand Down

This file was deleted.

7 changes: 4 additions & 3 deletions Library/Sources/AppUIMain/Views/App/AddProfileMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,11 @@ private struct ProvidersSubmenu: View {
Button(provider.description) {
var editable = EditableProfile()
editable.name = provider.description
if var newModule = moduleType.newModule(with: registry) as? any ProviderModuleBuilder {
newModule.providerId = provider.id
editable.modules.append(newModule)
let newModule = moduleType.newModule(with: registry, providerId: provider.id)
if let providerBuilder = newModule as? any ProviderBuilder {
assert(providerBuilder.providerId == provider.id)
}
editable.modules.append(newModule)
editable.modules.append(OnDemandModule.Builder())
editable.activeModulesIds = Set(editable.modules.map(\.id))
onSelect(editable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ extension AppCoordinator {

case editProfile(UUID?)

case editProviderEntity(Profile, Bool, Module, SerializedProvider)
case editProviderEntity(Profile, Bool, Module)

case interactiveLogin

Expand Down
29 changes: 11 additions & 18 deletions Library/Sources/AppUIMain/Views/App/AppCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -191,18 +191,12 @@ extension AppCoordinator {
onDismiss: onDismiss
)

case .editProviderEntity(let profile, let force, let module, let provider):
case .editProviderEntity(let profile, let force, let module):
ProviderEntitySelector(
module: module,
provider: provider,
errorHandler: errorHandler,
onSelect: {
try await onSelectProviderEntity(
$0,
force: force,
module: module,
profile: profile
)
try await onSelectProviderEntity(with: $0, in: profile, force: force)
}
)

Expand Down Expand Up @@ -241,11 +235,12 @@ extension AppCoordinator {
}

public func onProviderEntityRequired(_ profile: Profile, force: Bool) {
guard let pair = profile.selectedProvider else {
guard let module = profile.selectedProvider?.module else {
assertionFailure("Editing provider entity, but profile has no selected provider module")
return
}
pp_log(.app, .info, "Present provider entity selector")
present(.editProviderEntity(profile, force, pair.module, pair.selection))
present(.editProviderEntity(profile, force, module))
}

public func onPurchaseRequired(_ features: Set<AppFeature>) {
Expand All @@ -265,19 +260,17 @@ extension AppCoordinator {
}

private extension AppCoordinator {
func onSelectProviderEntity(
_ entity: any ProviderEntity & Encodable,
force: Bool,
module: Module,
profile: Profile
) async throws {
func onSelectProviderEntity(with newModule: Module, in profile: Profile, force: Bool) async throws {

// XXX: select entity after dismissing
try await Task.sleep(for: .milliseconds(500))

pp_log(.app, .info, "Select new provider entity: \(entity)")
pp_log(.app, .info, "Select new provider entity: (profile=\(profile.id), module=\(newModule.id))")

do {
let newProfile = try profile.withEntity(entity, in: module)
var builder = profile.builder()
builder.saveModule(newModule)
let newProfile = try builder.tryBuild()

let wasConnected = newProfile.id == tunnel.currentProfile?.id && tunnel.status == .active
try await profileManager.save(newProfile, isLocal: true)
Expand Down
12 changes: 6 additions & 6 deletions Library/Sources/AppUIMain/Views/App/InstalledProfileView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,16 @@ private extension InstalledProfileView {
.selectedProvider
.map { _, selection in
Button {
flow?.connectionFlow?.onProviderEntityRequired(profile!)
flow?.connectionFlow?.onProviderEntityRequired(profile!) // never nil due to .map
} label: {
providerSelectorLabel(with: selection)
providerSelectorLabel(with: selection.entity?.header)
}
.buttonStyle(.plain)
}
}

func providerSelectorLabel(with provider: SerializedProvider) -> some View {
ProviderCountryFlag(provider: provider)
func providerSelectorLabel(with entity: ProviderEntityHeader?) -> some View {
ProviderCountryFlag(entity: entity)
}
}

Expand Down Expand Up @@ -198,11 +198,11 @@ private struct ToggleButton: View {
}

private struct ProviderCountryFlag: View {
let provider: SerializedProvider
let entity: ProviderEntityHeader?

var body: some View {
ThemeCountryFlag(
provider.entity?.header.countryCode,
entity?.countryCode,
placeholderTip: Strings.Errors.App.Passepartout.missingProviderEntity,
countryTip: {
$0.localizedAsRegionCode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,22 @@ extension OpenVPNModule.Builder: ModuleViewProviding {

extension OpenVPNModule: ProviderEntityViewProviding {
public func providerEntityView(
with provider: SerializedProvider,
errorHandler: ErrorHandler,
onSelect: @escaping (any ProviderEntity & Encodable) async throws -> Void
onSelect: @escaping (Module) async throws -> Void
) -> some View {
vpnProviderEntityView(with: provider, errorHandler: errorHandler, onSelect: onSelect)
providerSelection.map {
VPNProviderServerCoordinator(
moduleId: id,
providerId: $0.id,
selectedEntity: $0.entity,
onSelect: {
var newBuilder = builder()
newBuilder.providerEntity = $0
let newModule = try newBuilder.tryBuild()
try await onSelect(newModule)
},
errorHandler: errorHandler
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,22 @@ extension WireGuardModule.Builder: ModuleViewProviding {

extension WireGuardModule: ProviderEntityViewProviding {
public func providerEntityView(
with provider: SerializedProvider,
errorHandler: ErrorHandler,
onSelect: @escaping (any ProviderEntity & Encodable) async throws -> Void
onSelect: @escaping (Module) async throws -> Void
) -> some View {
vpnProviderEntityView(with: provider, errorHandler: errorHandler, onSelect: onSelect)
providerSelection.map {
VPNProviderServerCoordinator(
moduleId: id,
providerId: $0.id,
selectedEntity: $0.entity,
onSelect: {
var newBuilder = builder()
newBuilder.providerEntity = $0
let newModule = try newBuilder.tryBuild()
try await onSelect(newModule)
},
errorHandler: errorHandler
)
}
}
}
26 changes: 9 additions & 17 deletions Library/Sources/AppUIMain/Views/Modules/OpenVPNView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ struct OpenVPNView: View, ModuleDraftEditing {
)
.modifier(PaywallModifier(reason: $paywallReason))
.navigationDestination(for: Subroute.self, destination: destination)
.themeAnimation(on: providerId.wrappedValue, category: .modules)
.themeAnimation(on: draft.wrappedValue.providerId, category: .modules)
.withErrorHandler(errorHandler)
}
}
Expand All @@ -106,14 +106,14 @@ private extension OpenVPNView {
credentialsRoute: Subroute.credentials
)
} else {
importView
importButton
.modifier(providerModifier)
}
}

@ViewBuilder
var importView: some View {
if providerId.wrappedValue == nil {
var importButton: some View {
if draft.wrappedValue.providerId == nil {
Button(Strings.Modules.General.Rows.importFromFile.withTrailingDots) {
isImporting = true
}
Expand Down Expand Up @@ -152,22 +152,14 @@ private extension OpenVPNView {
)
}

var providerId: Binding<ProviderID?> {
editor.binding(forProviderOf: module.id)
}

var providerEntity: Binding<VPNEntity<OpenVPN.Configuration>?> {
editor.binding(forProviderEntityOf: module.id)
}

var providerAccountRows: [ModuleRow]? {
[.push(caption: Strings.Modules.Openvpn.credentials, route: HashableRoute(Subroute.credentials))]
}
}

private extension OpenVPNView {
func onSelectServer(server: VPNServer, preset: VPNPreset<OpenVPN.Configuration>) {
providerEntity.wrappedValue = VPNEntity(server: server, preset: preset)
draft.wrappedValue.providerEntity = VPNEntity(server: server, preset: preset)
path.wrappedValue.removeLast()
}

Expand Down Expand Up @@ -222,12 +214,12 @@ private extension OpenVPNView {
func destination(for route: Subroute) -> some View {
switch route {
case .providerServer:
providerId.wrappedValue.map {
draft.wrappedValue.providerSelection.map {
VPNProviderServerView(
moduleId: module.id,
providerId: $0,
providerId: $0.id,
configurationType: OpenVPN.Configuration.self,
selectedEntity: providerEntity.wrappedValue,
selectedEntity: $0.entity,
filtersWithSelection: true,
onSelect: onSelectServer
)
Expand All @@ -236,7 +228,7 @@ private extension OpenVPNView {
case .credentials:
Form {
OpenVPNCredentialsView(
providerId: draft.providerId.wrappedValue,
providerId: draft.wrappedValue.providerId,
isInteractive: draft.isInteractive,
credentials: draft.credentials
)
Expand Down
18 changes: 5 additions & 13 deletions Library/Sources/AppUIMain/Views/Modules/WireGuardView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ struct WireGuardView: View, ModuleDraftEditing {
.moduleView(editor: editor, draft: draft.wrappedValue)
.modifier(PaywallModifier(reason: $paywallReason))
.navigationDestination(for: Subroute.self, destination: destination)
.themeAnimation(on: providerId.wrappedValue, category: .modules)
.themeAnimation(on: draft.wrappedValue.providerId, category: .modules)
.withErrorHandler(errorHandler)
}
}
Expand Down Expand Up @@ -82,22 +82,14 @@ private extension WireGuardView {
)
}

var providerId: Binding<ProviderID?> {
editor.binding(forProviderOf: module.id)
}

var providerEntity: Binding<VPNEntity<WireGuard.Configuration>?> {
editor.binding(forProviderEntityOf: module.id)
}

var providerKeyRows: [ModuleRow]? {
[.push(caption: Strings.Modules.Wireguard.providerKey, route: HashableRoute(Subroute.providerKey))]
}
}

private extension WireGuardView {
func onSelectServer(server: VPNServer, preset: VPNPreset<WireGuard.Configuration>) {
providerEntity.wrappedValue = VPNEntity(server: server, preset: preset)
draft.wrappedValue.providerEntity = VPNEntity(server: server, preset: preset)
path.wrappedValue.removeLast()
}

Expand All @@ -119,12 +111,12 @@ private extension WireGuardView {
func destination(for route: Subroute) -> some View {
switch route {
case .providerServer:
providerId.wrappedValue.map {
draft.wrappedValue.providerSelection.map {
VPNProviderServerView(
moduleId: module.id,
providerId: $0,
providerId: $0.id,
configurationType: WireGuard.Configuration.self,
selectedEntity: providerEntity.wrappedValue,
selectedEntity: $0.entity,
filtersWithSelection: true,
onSelect: onSelectServer
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import PassepartoutKit
import SwiftUI
import UILibrary

struct ProviderContentModifier<Entity, ProviderRows>: ViewModifier where Entity: ProviderEntity, Entity.Configuration: ProviderConfigurationIdentifiable & Codable, ProviderRows: View {
struct ProviderContentModifier<Entity, ProviderRows>: ViewModifier where Entity: ProviderEntityType, ProviderRows: View {

@EnvironmentObject
private var providerManager: ProviderManager
Expand Down Expand Up @@ -122,7 +122,7 @@ private extension ProviderContentModifier {
providerManager
.providers
.filter {
$0.supports(Entity.Configuration.self)
$0.supports(Entity.ConfigurationType.self)
}
}

Expand Down
Loading

0 comments on commit 2d93fa6

Please sign in to comment.