2022-04-12 13:09:14 +00:00
|
|
|
//
|
|
|
|
// AddProviderView.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 2/10/22.
|
|
|
|
// Copyright (c) 2022 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 SwiftUI
|
|
|
|
import PassepartoutCore
|
|
|
|
|
|
|
|
struct AddProviderView: View {
|
|
|
|
@ObservedObject private var providerManager: ProviderManager
|
|
|
|
|
|
|
|
@ObservedObject private var productManager: ProductManager
|
|
|
|
|
|
|
|
private let bindings: AddProfileView.Bindings
|
|
|
|
|
|
|
|
@StateObject private var viewModel = ViewModel()
|
|
|
|
|
|
|
|
init(bindings: AddProfileView.Bindings) {
|
|
|
|
providerManager = .shared
|
|
|
|
productManager = .shared
|
|
|
|
self.bindings = bindings
|
|
|
|
}
|
|
|
|
|
2022-04-21 13:08:40 +00:00
|
|
|
private var providers: [ProviderMetadata] {
|
|
|
|
providerManager.allProviders()
|
|
|
|
.filter {
|
|
|
|
$0.supportedVPNProtocols.contains(viewModel.selectedVPNProtocol)
|
|
|
|
}.sorted()
|
|
|
|
}
|
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
private var availableVPNProtocols: [VPNProtocolType] {
|
|
|
|
var protos: Set<VPNProtocolType> = []
|
2022-04-21 13:08:40 +00:00
|
|
|
providers.forEach {
|
2022-04-12 13:09:14 +00:00
|
|
|
$0.supportedVPNProtocols.forEach {
|
|
|
|
protos.insert($0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return protos.sorted()
|
|
|
|
}
|
|
|
|
|
|
|
|
private func isFetchingProvider(_ name: ProviderName) -> Bool {
|
|
|
|
if case .provider(name) = viewModel.pendingOperation {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
private var isUpdatingIndex: Bool {
|
|
|
|
if case .index = viewModel.pendingOperation {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
ZStack {
|
2022-04-22 17:02:54 +00:00
|
|
|
ForEach(providers, id: \.navigationId, content: hiddenProviderLink)
|
2022-04-12 13:09:14 +00:00
|
|
|
ScrollViewReader { scrollProxy in
|
|
|
|
List {
|
|
|
|
mainSection
|
2022-04-21 13:08:40 +00:00
|
|
|
if !providers.isEmpty {
|
2022-04-12 13:09:14 +00:00
|
|
|
providersSection
|
|
|
|
}
|
2022-04-26 20:15:39 +00:00
|
|
|
}.onAppear {
|
|
|
|
viewModel.updateIndex(providerManager)
|
2022-04-12 13:09:14 +00:00
|
|
|
}.onChange(of: viewModel.errorMessage) {
|
|
|
|
onErrorMessage($0, scrollProxy)
|
|
|
|
}.disabled(viewModel.pendingOperation != nil)
|
2022-04-23 10:08:24 +00:00
|
|
|
.themeAnimation(on: providers)
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
2022-04-21 13:56:26 +00:00
|
|
|
}.toolbar {
|
|
|
|
themeCloseItem(isPresented: bindings.$isPresented)
|
|
|
|
}.sheet(isPresented: $viewModel.isPaywallPresented) {
|
2022-04-12 13:09:14 +00:00
|
|
|
NavigationView {
|
2022-04-13 19:01:06 +00:00
|
|
|
PaywallView(isPresented: $viewModel.isPaywallPresented)
|
2022-04-12 13:09:14 +00:00
|
|
|
}.themeGlobal()
|
2022-04-21 13:56:26 +00:00
|
|
|
}.navigationTitle(L10n.AddProfile.Shared.title)
|
|
|
|
.themeSecondaryView()
|
2022-04-19 08:10:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
private var mainSection: some View {
|
|
|
|
Section(
|
|
|
|
footer: Text(L10n.AddProfile.Provider.Sections.Vpn.footer)
|
|
|
|
) {
|
|
|
|
let protos = availableVPNProtocols
|
|
|
|
if !protos.isEmpty {
|
|
|
|
themeTextPicker(
|
|
|
|
L10n.Global.Strings.protocol,
|
|
|
|
selection: $viewModel.selectedVPNProtocol,
|
|
|
|
values: protos,
|
|
|
|
description: \.description
|
|
|
|
)
|
|
|
|
}
|
|
|
|
updateListButton
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private var providersSection: some View {
|
|
|
|
Section(
|
|
|
|
footer: themeErrorMessage(viewModel.errorMessage)
|
|
|
|
) {
|
2022-04-21 13:08:40 +00:00
|
|
|
ForEach(providers, content: providerRow)
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func providerRow(_ metadata: ProviderMetadata) -> some View {
|
|
|
|
Button {
|
|
|
|
presentOrPurchaseProvider(metadata)
|
|
|
|
} label: {
|
|
|
|
Label(metadata.description, image: themeAssetsProviderImage(metadata.name))
|
|
|
|
}.withTrailingProgress(when: isFetchingProvider(metadata.name))
|
|
|
|
}
|
|
|
|
|
2022-04-22 17:02:54 +00:00
|
|
|
private func hiddenProviderLink(_ metadata: ProviderMetadata) -> some View {
|
2022-04-12 13:09:14 +00:00
|
|
|
NavigationLink("", tag: metadata, selection: $viewModel.selectedProvider) {
|
|
|
|
NameView(
|
|
|
|
profile: $viewModel.pendingProfile,
|
|
|
|
providerMetadata: metadata,
|
|
|
|
bindings: bindings
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private var updateListButton: some View {
|
|
|
|
Button(L10n.AddProfile.Provider.Items.updateList) {
|
|
|
|
viewModel.updateIndex(providerManager)
|
|
|
|
}.withTrailingProgress(when: isUpdatingIndex)
|
|
|
|
}
|
|
|
|
|
|
|
|
// eligibility: select or purchase provider
|
|
|
|
private func presentOrPurchaseProvider(_ metadata: ProviderMetadata) {
|
|
|
|
if productManager.isEligible(forProvider: metadata.name) {
|
|
|
|
viewModel.selectProvider(metadata, providerManager)
|
|
|
|
} else {
|
|
|
|
viewModel.presentPaywall()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func onErrorMessage(_ message: String?, _ scrollProxy: ScrollViewProxy) {
|
|
|
|
guard let _ = message else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
scrollToErrorMessage(scrollProxy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension AddProviderView {
|
|
|
|
private func scrollToErrorMessage(_ proxy: ScrollViewProxy) {
|
2022-04-21 13:08:40 +00:00
|
|
|
proxy.maybeScrollTo(providers.last?.id, animated: true)
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension ProviderMetadata {
|
|
|
|
var navigationId: String {
|
|
|
|
return "navigation.\(name)"
|
|
|
|
}
|
|
|
|
}
|