2024-10-15 19:34:02 +00:00
|
|
|
//
|
|
|
|
// ProviderContentModifier.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 10/14/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 PassepartoutKit
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct ProviderContentModifier<Entity, ProviderRows>: ViewModifier where Entity: ProviderEntity, Entity.Configuration: ProviderConfigurationIdentifiable & Codable, ProviderRows: View {
|
|
|
|
|
|
|
|
@EnvironmentObject
|
|
|
|
private var providerManager: ProviderManager
|
|
|
|
|
2024-10-28 18:55:42 +00:00
|
|
|
@EnvironmentObject
|
|
|
|
private var iapManager: IAPManager
|
|
|
|
|
2024-10-18 16:12:28 +00:00
|
|
|
let apis: [APIMapper]
|
2024-10-15 19:34:02 +00:00
|
|
|
|
|
|
|
@Binding
|
|
|
|
var providerId: ProviderID?
|
|
|
|
|
|
|
|
let entityType: Entity.Type
|
|
|
|
|
2024-10-28 18:55:42 +00:00
|
|
|
@Binding
|
|
|
|
var paywallReason: PaywallReason?
|
|
|
|
|
2024-10-15 19:34:02 +00:00
|
|
|
@ViewBuilder
|
|
|
|
let providerRows: ProviderRows
|
|
|
|
|
|
|
|
let onSelectProvider: (ProviderManager, ProviderID?, _ isInitial: Bool) -> Void
|
|
|
|
|
|
|
|
func body(content: Content) -> some View {
|
|
|
|
providerView
|
|
|
|
.onLoad(perform: loadCurrentProvider)
|
|
|
|
.onChange(of: providerId) { newId in
|
|
|
|
Task {
|
|
|
|
if let newId {
|
|
|
|
await refreshInfrastructure(for: newId)
|
|
|
|
}
|
|
|
|
onSelectProvider(providerManager, newId, false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.disabled(providerManager.isLoading)
|
|
|
|
|
2024-10-28 19:07:19 +00:00
|
|
|
content
|
2024-10-15 19:34:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static func == (lhs: Self, rhs: Self) -> Bool {
|
|
|
|
lhs.providerId == rhs.providerId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension ProviderContentModifier {
|
|
|
|
|
|
|
|
#if os(iOS)
|
2024-10-28 18:55:42 +00:00
|
|
|
@ViewBuilder
|
2024-10-15 19:34:02 +00:00
|
|
|
var providerView: some View {
|
|
|
|
Group {
|
|
|
|
providerPicker
|
2024-10-28 18:55:42 +00:00
|
|
|
purchaseButton
|
|
|
|
}
|
|
|
|
.themeSection()
|
|
|
|
Group {
|
2024-10-15 19:34:02 +00:00
|
|
|
if providerId != nil {
|
|
|
|
providerRows
|
|
|
|
refreshButton {
|
|
|
|
HStack {
|
2024-10-25 09:38:27 +00:00
|
|
|
Text(Strings.Providers.refreshInfrastructure)
|
2024-10-15 19:34:02 +00:00
|
|
|
if providerManager.isLoading {
|
|
|
|
Spacer()
|
|
|
|
ProgressView()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.themeSection(footer: lastUpdatedString)
|
|
|
|
}
|
|
|
|
#else
|
2024-10-28 18:55:42 +00:00
|
|
|
@ViewBuilder
|
2024-10-15 19:34:02 +00:00
|
|
|
var providerView: some View {
|
2024-10-28 18:55:42 +00:00
|
|
|
Section {
|
2024-10-15 19:34:02 +00:00
|
|
|
providerPicker
|
2024-10-28 18:55:42 +00:00
|
|
|
purchaseButton
|
|
|
|
}
|
|
|
|
Section {
|
2024-10-15 19:34:02 +00:00
|
|
|
if providerId != nil {
|
|
|
|
providerRows
|
|
|
|
HStack {
|
|
|
|
lastUpdatedString.map {
|
|
|
|
Text($0)
|
|
|
|
.foregroundStyle(.secondary)
|
|
|
|
}
|
|
|
|
Spacer()
|
|
|
|
refreshButton {
|
2024-10-25 09:38:27 +00:00
|
|
|
Text(Strings.Providers.refreshInfrastructure)
|
2024-10-15 19:34:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
var providerPicker: some View {
|
|
|
|
ProviderPicker(
|
|
|
|
providers: supportedProviders,
|
|
|
|
providerId: $providerId,
|
2024-10-28 19:07:19 +00:00
|
|
|
isRequired: true,
|
2024-10-15 19:34:02 +00:00
|
|
|
isLoading: providerManager.isLoading
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-10-28 18:55:42 +00:00
|
|
|
@ViewBuilder
|
|
|
|
var purchaseButton: some View {
|
2024-10-28 20:02:45 +00:00
|
|
|
switch iapManager.paywallReason(forFeature: .providers) {
|
|
|
|
case .purchase(let appFeature):
|
2024-10-28 18:55:42 +00:00
|
|
|
Button(Strings.Providers.Picker.purchase) {
|
2024-10-28 20:02:45 +00:00
|
|
|
paywallReason = .purchase(appFeature)
|
2024-10-28 18:55:42 +00:00
|
|
|
}
|
2024-10-28 20:02:45 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
EmptyView()
|
2024-10-28 18:55:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-15 19:34:02 +00:00
|
|
|
func refreshButton<Label>(label: () -> Label) -> some View where Label: View {
|
|
|
|
Button(action: onRefreshInfrastructure, label: label)
|
|
|
|
}
|
|
|
|
|
|
|
|
var supportedProviders: [ProviderMetadata] {
|
2024-10-28 16:14:49 +00:00
|
|
|
providerManager
|
|
|
|
.providers
|
|
|
|
.filter {
|
2024-10-28 18:55:42 +00:00
|
|
|
iapManager.isEligible(forProvider: $0.id) && $0.supports(Entity.Configuration.self)
|
2024-10-28 16:14:49 +00:00
|
|
|
}
|
2024-10-15 19:34:02 +00:00
|
|
|
}
|
|
|
|
|
2024-10-28 16:14:49 +00:00
|
|
|
var lastUpdate: Date? {
|
2024-10-15 19:34:02 +00:00
|
|
|
guard let providerId else {
|
|
|
|
return nil
|
|
|
|
}
|
2024-10-28 16:14:49 +00:00
|
|
|
return providerManager.lastUpdate(for: providerId)
|
2024-10-15 19:34:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var lastUpdatedString: String? {
|
2024-10-28 16:14:49 +00:00
|
|
|
guard let lastUpdate else {
|
2024-10-25 09:38:27 +00:00
|
|
|
return providerManager.isLoading ? Strings.Providers.LastUpdated.loading : nil
|
2024-10-15 19:34:02 +00:00
|
|
|
}
|
2024-10-28 20:38:26 +00:00
|
|
|
return Strings.Providers.lastUpdated(lastUpdate.localizedDescription(style: .timestamp))
|
2024-10-15 19:34:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension ProviderContentModifier {
|
|
|
|
func loadCurrentProvider() {
|
|
|
|
Task {
|
2024-10-23 21:04:29 +00:00
|
|
|
await refreshIndex()
|
2024-10-15 19:34:02 +00:00
|
|
|
if let providerId {
|
|
|
|
onSelectProvider(providerManager, providerId, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@discardableResult
|
|
|
|
func refreshIndex() async -> Bool {
|
|
|
|
do {
|
|
|
|
try await providerManager.fetchIndex(from: apis)
|
|
|
|
return true
|
|
|
|
} catch {
|
|
|
|
pp_log(.app, .error, "Unable to fetch index: \(error)")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@discardableResult
|
|
|
|
func refreshInfrastructure(for providerId: ProviderID) async -> Bool {
|
|
|
|
do {
|
2024-10-25 08:54:28 +00:00
|
|
|
try await providerManager.fetchVPNInfrastructure(from: apis, for: providerId)
|
2024-10-15 19:34:02 +00:00
|
|
|
return true
|
|
|
|
} catch {
|
|
|
|
pp_log(.app, .error, "Unable to refresh infrastructure: \(error)")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func onRefreshInfrastructure() {
|
|
|
|
guard let providerId else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
Task {
|
|
|
|
await refreshInfrastructure(for: providerId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Preview
|
|
|
|
|
|
|
|
#Preview {
|
|
|
|
List {
|
|
|
|
EmptyView()
|
|
|
|
.modifier(ProviderContentModifier(
|
|
|
|
apis: [API.bundled],
|
|
|
|
providerId: .constant(.hideme),
|
|
|
|
entityType: VPNEntity<OpenVPN.Configuration>.self,
|
2024-10-28 18:55:42 +00:00
|
|
|
paywallReason: .constant(nil),
|
2024-10-15 19:34:02 +00:00
|
|
|
providerRows: {},
|
|
|
|
onSelectProvider: { _, _, _ in }
|
|
|
|
))
|
|
|
|
}
|
|
|
|
.withMockEnvironment()
|
|
|
|
}
|