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/>.
|
|
|
|
//
|
|
|
|
|
2024-11-02 09:11:59 +00:00
|
|
|
import CommonAPI
|
2024-11-03 12:16:13 +00:00
|
|
|
import CommonLibrary
|
2024-10-15 19:34:02 +00:00
|
|
|
import PassepartoutKit
|
|
|
|
import SwiftUI
|
2024-11-26 00:04:58 +00:00
|
|
|
import UILibrary
|
2024-10-15 19:34:02 +00:00
|
|
|
|
2024-12-03 15:18:05 +00:00
|
|
|
struct ProviderContentModifier<Entity, ProviderRows>: ViewModifier where Entity: ProviderEntityType, ProviderRows: View {
|
2024-10-15 19:34:02 +00:00
|
|
|
|
2024-10-28 18:55:42 +00:00
|
|
|
@EnvironmentObject
|
2024-11-01 08:47:50 +00:00
|
|
|
private var providerManager: ProviderManager
|
2024-10-28 18:55:42 +00:00
|
|
|
|
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 {
|
2024-11-18 16:43:01 +00:00
|
|
|
providerPicker
|
|
|
|
.themeSection()
|
|
|
|
|
2024-11-26 00:04:58 +00:00
|
|
|
if let providerId {
|
2024-11-18 16:43:01 +00:00
|
|
|
Group {
|
2024-10-15 19:34:02 +00:00
|
|
|
providerRows
|
2024-11-26 00:04:58 +00:00
|
|
|
RefreshInfrastructureButton(apis: apis, providerId: providerId)
|
2024-10-15 19:34:02 +00:00
|
|
|
}
|
2024-11-18 16:43:01 +00:00
|
|
|
.themeSection(footer: lastUpdatedString)
|
2024-10-15 19:34:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#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
|
|
|
}
|
2024-11-26 00:04:58 +00:00
|
|
|
if let providerId {
|
2024-11-18 16:43:01 +00:00
|
|
|
Section {
|
2024-10-15 19:34:02 +00:00
|
|
|
providerRows
|
|
|
|
HStack {
|
|
|
|
lastUpdatedString.map {
|
|
|
|
Text($0)
|
2024-12-01 22:23:51 +00:00
|
|
|
.themeSubtitle()
|
2024-10-15 19:34:02 +00:00
|
|
|
}
|
|
|
|
Spacer()
|
2024-11-26 00:04:58 +00:00
|
|
|
RefreshInfrastructureButton(apis: apis, providerId: providerId)
|
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-11-18 16:43:01 +00:00
|
|
|
isLoading: providerManager.isLoading,
|
|
|
|
paywallReason: $paywallReason
|
2024-10-15 19:34:02 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-12-04 08:26:10 +00:00
|
|
|
var supportedProviders: [Provider] {
|
2024-10-28 16:14:49 +00:00
|
|
|
providerManager
|
|
|
|
.providers
|
|
|
|
.filter {
|
2024-12-03 15:18:05 +00:00
|
|
|
$0.supports(Entity.ConfigurationType.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-11-23 19:31:22 +00:00
|
|
|
return providerManager.isLoading ? Strings.Views.Providers.LastUpdated.loading : nil
|
2024-10-15 19:34:02 +00:00
|
|
|
}
|
2024-11-23 19:31:22 +00:00
|
|
|
return Strings.Views.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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
}
|