passepartout-apple/Passepartout/App/Views/AddProviderView.swift

171 lines
5.3 KiB
Swift
Raw Normal View History

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