2024-10-10 22:24:06 +00:00
|
|
|
//
|
|
|
|
// ProviderPanelModifier.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 10/7/24.
|
|
|
|
// Copyright (c) 2024 Davide De Rosa. All rights reserved.
|
|
|
|
//
|
|
|
|
// https://github.com/passepartoutvpn
|
|
|
|
//
|
|
|
|
// This file is part of Passepartout.
|
|
|
|
//
|
|
|
|
// Passepartout is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Passepartout is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
import AppLibrary
|
|
|
|
import PassepartoutKit
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct ProviderPanelModifier<Entity, ProviderContent>: ViewModifier where Entity: ProviderEntity, Entity.Configuration: ProviderConfigurationIdentifiable & Codable, ProviderContent: View {
|
|
|
|
|
|
|
|
@EnvironmentObject
|
|
|
|
private var providerManager: ProviderManager
|
|
|
|
|
|
|
|
var apis: [APIMapper] = API.shared
|
|
|
|
|
2024-10-11 17:11:42 +00:00
|
|
|
let isRequired: Bool
|
|
|
|
|
2024-10-10 22:24:06 +00:00
|
|
|
@Binding
|
|
|
|
var providerId: ProviderID?
|
|
|
|
|
|
|
|
@Binding
|
|
|
|
var selectedEntity: Entity?
|
|
|
|
|
|
|
|
@ViewBuilder
|
|
|
|
let providerContent: (ProviderID, Entity?) -> ProviderContent
|
|
|
|
|
|
|
|
func body(content: Content) -> some View {
|
|
|
|
providerPicker
|
|
|
|
.task {
|
|
|
|
await refreshIndex()
|
|
|
|
}
|
|
|
|
|
|
|
|
if let providerId {
|
|
|
|
providerContent(providerId, selectedEntity)
|
2024-10-11 17:11:42 +00:00
|
|
|
} else if !isRequired {
|
2024-10-10 22:24:06 +00:00
|
|
|
content
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension ProviderPanelModifier {
|
|
|
|
var supportedProviders: [ProviderMetadata] {
|
|
|
|
providerManager.providers.filter {
|
|
|
|
$0.supports(Entity.Configuration.self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var providerPicker: some View {
|
|
|
|
let hasProviders = !supportedProviders.isEmpty
|
|
|
|
return Picker(Strings.Global.provider, selection: $providerId) {
|
|
|
|
if hasProviders {
|
2024-10-11 17:11:42 +00:00
|
|
|
Text(Strings.Global.none)
|
|
|
|
.tag(nil as ProviderID?)
|
|
|
|
ForEach(supportedProviders, id: \.id) {
|
2024-10-10 22:24:06 +00:00
|
|
|
Text($0.description)
|
2024-10-11 17:11:42 +00:00
|
|
|
.tag($0.id as ProviderID?)
|
2024-10-10 22:24:06 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Text(" ") // enforce constant picker height on iOS
|
|
|
|
.tag(providerId) // tag always exists
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.onChange(of: providerId) { _ in
|
|
|
|
selectedEntity = nil
|
|
|
|
}
|
|
|
|
.disabled(!hasProviders)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension ProviderPanelModifier {
|
|
|
|
|
|
|
|
// FIXME: #707, fetch bundled providers on launch
|
|
|
|
// FIXME: #704, rate-limit fetch
|
|
|
|
func refreshIndex() async {
|
|
|
|
do {
|
|
|
|
try await providerManager.fetchIndex(from: apis)
|
|
|
|
} catch {
|
|
|
|
pp_log(.app, .error, "Unable to fetch index: \(error)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension ProviderID {
|
|
|
|
var nilIfEmpty: ProviderID? {
|
|
|
|
!rawValue.isEmpty ? self : nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Preview
|
|
|
|
|
|
|
|
#Preview {
|
|
|
|
@State
|
|
|
|
var providerId: ProviderID? = .hideme
|
|
|
|
|
|
|
|
@State
|
|
|
|
var vpnEntity: VPNEntity<OpenVPN.Configuration>?
|
|
|
|
|
|
|
|
return List {
|
|
|
|
EmptyView()
|
|
|
|
.modifier(ProviderPanelModifier(
|
|
|
|
apis: [API.bundled],
|
2024-10-11 17:11:42 +00:00
|
|
|
isRequired: false,
|
2024-10-10 22:24:06 +00:00
|
|
|
providerId: $providerId,
|
|
|
|
selectedEntity: $vpnEntity,
|
2024-10-11 17:11:42 +00:00
|
|
|
providerContent: { _, entity in
|
2024-10-10 22:24:06 +00:00
|
|
|
HStack {
|
|
|
|
Text("Server")
|
|
|
|
Spacer()
|
|
|
|
Text(entity?.server.serverId ?? "None")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
))
|
|
|
|
}
|
|
|
|
.withMockEnvironment()
|
|
|
|
}
|