2024-09-23 13:02:26 +00:00
|
|
|
//
|
|
|
|
// InstalledProfileView.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 9/3/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-03 12:16:13 +00:00
|
|
|
import CommonLibrary
|
2024-11-02 09:11:59 +00:00
|
|
|
import CommonUtils
|
2024-09-23 13:02:26 +00:00
|
|
|
import PassepartoutKit
|
|
|
|
import SwiftUI
|
2024-11-28 00:30:26 +00:00
|
|
|
import UITesting
|
2024-09-23 13:02:26 +00:00
|
|
|
|
|
|
|
struct InstalledProfileView: View, Routable {
|
|
|
|
|
|
|
|
@EnvironmentObject
|
2024-11-01 08:47:50 +00:00
|
|
|
private var theme: Theme
|
2024-09-23 13:02:26 +00:00
|
|
|
|
|
|
|
let layout: ProfilesLayout
|
|
|
|
|
|
|
|
let profileManager: ProfileManager
|
|
|
|
|
|
|
|
let profile: Profile?
|
|
|
|
|
2024-11-03 07:38:53 +00:00
|
|
|
let tunnel: ExtendedTunnel
|
2024-09-23 13:02:26 +00:00
|
|
|
|
|
|
|
let interactiveManager: InteractiveManager
|
|
|
|
|
|
|
|
let errorHandler: ErrorHandler
|
|
|
|
|
|
|
|
@Binding
|
|
|
|
var nextProfileId: Profile.ID?
|
|
|
|
|
2024-11-19 10:36:36 +00:00
|
|
|
var flow: ProfileFlow?
|
2024-09-23 13:02:26 +00:00
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
debugChanges()
|
|
|
|
return HStack(alignment: .center) {
|
|
|
|
cardView
|
2024-11-28 00:30:26 +00:00
|
|
|
.uiAccessibility(.App.installedProfile)
|
2024-09-23 13:02:26 +00:00
|
|
|
Spacer()
|
|
|
|
toggleButton
|
|
|
|
}
|
|
|
|
.modifier(HeaderModifier(layout: layout))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension InstalledProfileView {
|
|
|
|
var cardView: some View {
|
2024-11-27 13:49:23 +00:00
|
|
|
VStack(alignment: .leading, spacing: 20) {
|
2024-09-23 13:02:26 +00:00
|
|
|
HStack(alignment: .center) {
|
|
|
|
if profile != nil {
|
|
|
|
actionableNameView
|
2024-11-27 13:49:23 +00:00
|
|
|
Spacer(minLength: 10)
|
2024-09-23 13:02:26 +00:00
|
|
|
} else {
|
2024-11-26 08:42:47 +00:00
|
|
|
nameView()
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
2024-11-27 13:49:23 +00:00
|
|
|
Group {
|
|
|
|
if profile != nil {
|
|
|
|
statusView
|
|
|
|
} else {
|
|
|
|
Text(Strings.Views.App.InstalledProfile.None.status)
|
|
|
|
.foregroundStyle(.secondary)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.font(.body)
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
.modifier(CardModifier(layout: layout))
|
|
|
|
}
|
|
|
|
|
|
|
|
var actionableNameView: some View {
|
2024-11-26 08:42:47 +00:00
|
|
|
ThemeDisclosableMenu(content: menuContent, label: nameView)
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
|
2024-11-26 08:42:47 +00:00
|
|
|
func nameView() -> some View {
|
2024-11-27 13:49:23 +00:00
|
|
|
Text(profile?.name ?? Strings.Views.App.InstalledProfile.None.name)
|
2024-09-23 13:02:26 +00:00
|
|
|
.font(.title2)
|
|
|
|
.fontWeight(theme.relevantWeight)
|
|
|
|
.themeTruncating(.tail)
|
|
|
|
}
|
|
|
|
|
|
|
|
var statusView: some View {
|
2024-10-23 15:17:20 +00:00
|
|
|
HStack {
|
|
|
|
providerSelectorButton
|
2024-11-27 13:49:23 +00:00
|
|
|
StatusText(theme: theme, tunnel: tunnel)
|
2024-10-23 15:17:20 +00:00
|
|
|
}
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var toggleButton: some View {
|
2024-11-03 07:38:53 +00:00
|
|
|
ToggleButton(
|
|
|
|
theme: theme,
|
2024-09-23 13:02:26 +00:00
|
|
|
tunnel: tunnel,
|
|
|
|
profile: profile,
|
|
|
|
nextProfileId: $nextProfileId,
|
|
|
|
interactiveManager: interactiveManager,
|
2024-10-23 15:17:20 +00:00
|
|
|
errorHandler: errorHandler,
|
2024-11-03 07:38:53 +00:00
|
|
|
flow: flow
|
2024-10-23 15:17:20 +00:00
|
|
|
)
|
2024-11-27 13:49:23 +00:00
|
|
|
.opaque(profile != nil)
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
|
2024-11-26 08:42:47 +00:00
|
|
|
func menuContent() -> some View {
|
2024-09-23 13:02:26 +00:00
|
|
|
ProfileContextMenu(
|
2024-11-26 19:10:03 +00:00
|
|
|
style: .installedProfile,
|
2024-09-23 13:02:26 +00:00
|
|
|
profileManager: profileManager,
|
|
|
|
tunnel: tunnel,
|
2024-11-22 11:52:51 +00:00
|
|
|
preview: .init(profile ?? .mock),
|
2024-09-23 13:02:26 +00:00
|
|
|
interactiveManager: interactiveManager,
|
|
|
|
errorHandler: errorHandler,
|
2024-10-23 15:17:20 +00:00
|
|
|
flow: flow
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
var providerSelectorButton: some View {
|
|
|
|
profile?
|
2024-11-22 09:05:46 +00:00
|
|
|
.selectedProvider
|
|
|
|
.map { _, selection in
|
2024-10-23 15:17:20 +00:00
|
|
|
Button {
|
2024-11-24 00:01:04 +00:00
|
|
|
flow?.onProviderEntityRequired(profile!)
|
2024-10-23 15:17:20 +00:00
|
|
|
} label: {
|
2024-11-22 09:05:46 +00:00
|
|
|
providerSelectorLabel(with: selection)
|
2024-10-23 15:17:20 +00:00
|
|
|
}
|
|
|
|
.buttonStyle(.plain)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-17 21:27:02 +00:00
|
|
|
func providerSelectorLabel(with provider: SerializedProvider) -> some View {
|
2024-10-23 15:17:20 +00:00
|
|
|
ProviderCountryFlag(provider: provider)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-03 07:38:53 +00:00
|
|
|
// MARK: - Subviews (observing)
|
|
|
|
|
|
|
|
private struct StatusText: View {
|
|
|
|
|
|
|
|
@ObservedObject
|
|
|
|
var theme: Theme
|
|
|
|
|
|
|
|
@ObservedObject
|
|
|
|
var tunnel: ExtendedTunnel
|
|
|
|
|
|
|
|
var body: some View {
|
2024-11-10 18:39:43 +00:00
|
|
|
debugChanges()
|
|
|
|
return ConnectionStatusText(tunnel: tunnel)
|
2024-11-03 07:38:53 +00:00
|
|
|
.foregroundStyle(tunnel.statusColor(theme))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private struct ToggleButton: View {
|
|
|
|
|
|
|
|
@ObservedObject
|
|
|
|
var theme: Theme
|
|
|
|
|
|
|
|
@ObservedObject
|
|
|
|
var tunnel: ExtendedTunnel
|
|
|
|
|
|
|
|
let profile: Profile?
|
|
|
|
|
|
|
|
@Binding
|
|
|
|
var nextProfileId: Profile.ID?
|
|
|
|
|
|
|
|
@ObservedObject
|
|
|
|
var interactiveManager: InteractiveManager
|
|
|
|
|
|
|
|
@ObservedObject
|
|
|
|
var errorHandler: ErrorHandler
|
|
|
|
|
|
|
|
let flow: ProfileFlow?
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
TunnelToggleButton(
|
|
|
|
tunnel: tunnel,
|
|
|
|
profile: profile,
|
|
|
|
nextProfileId: $nextProfileId,
|
|
|
|
interactiveManager: interactiveManager,
|
|
|
|
errorHandler: errorHandler,
|
2024-11-18 16:43:01 +00:00
|
|
|
onProviderEntityRequired: {
|
2024-11-24 00:01:04 +00:00
|
|
|
flow?.onProviderEntityRequired($0)
|
2024-11-18 16:43:01 +00:00
|
|
|
},
|
|
|
|
onPurchaseRequired: {
|
|
|
|
flow?.onPurchaseRequired($0)
|
|
|
|
},
|
2024-11-03 07:38:53 +00:00
|
|
|
label: { _ in
|
|
|
|
ThemeImage(.tunnelToggle)
|
|
|
|
.scaleEffect(1.5, anchor: .trailing)
|
|
|
|
}
|
|
|
|
)
|
2024-11-26 23:58:33 +00:00
|
|
|
// XXX: #584, necessary to avoid cell selection
|
2024-11-03 07:38:53 +00:00
|
|
|
.buttonStyle(.plain)
|
|
|
|
.foregroundStyle(tunnel.statusColor(theme))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-23 15:17:20 +00:00
|
|
|
private struct ProviderCountryFlag: View {
|
2024-11-17 21:27:02 +00:00
|
|
|
let provider: SerializedProvider
|
2024-10-23 15:17:20 +00:00
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
ThemeCountryFlag(
|
2024-10-31 00:15:07 +00:00
|
|
|
provider.entity?.header.countryCode,
|
2024-10-28 19:30:22 +00:00
|
|
|
placeholderTip: Strings.Errors.App.Passepartout.missingProviderEntity,
|
2024-10-23 15:17:20 +00:00
|
|
|
countryTip: {
|
|
|
|
$0.localizedAsRegionCode
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private struct HeaderModifier: ViewModifier {
|
|
|
|
|
|
|
|
@EnvironmentObject
|
|
|
|
private var theme: Theme
|
|
|
|
|
|
|
|
let layout: ProfilesLayout
|
|
|
|
|
|
|
|
func body(content: Content) -> some View {
|
|
|
|
switch layout {
|
|
|
|
case .list:
|
|
|
|
content
|
|
|
|
.listRowInsets(.init())
|
|
|
|
#if os(iOS)
|
|
|
|
.padding(.horizontal)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
case .grid:
|
|
|
|
content
|
|
|
|
.themeGridCell(isSelected: false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private struct CardModifier: ViewModifier {
|
|
|
|
let layout: ProfilesLayout
|
|
|
|
|
|
|
|
func body(content: Content) -> some View {
|
|
|
|
switch layout {
|
|
|
|
case .list:
|
|
|
|
#if os(iOS)
|
|
|
|
content
|
|
|
|
.padding(.vertical)
|
2024-11-23 18:26:33 +00:00
|
|
|
#else
|
2024-09-23 13:02:26 +00:00
|
|
|
content
|
|
|
|
#endif
|
|
|
|
|
|
|
|
case .grid:
|
|
|
|
content
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Previews
|
|
|
|
|
|
|
|
#Preview("List") {
|
|
|
|
Form {
|
|
|
|
HeaderView(layout: .list)
|
|
|
|
Section {
|
|
|
|
ContentView()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.themeForm()
|
2024-10-02 14:05:40 +00:00
|
|
|
.withMockEnvironment()
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#Preview("Grid") {
|
|
|
|
ScrollView {
|
|
|
|
VStack {
|
|
|
|
HeaderView(layout: .grid)
|
|
|
|
.padding(.bottom)
|
|
|
|
ContentView()
|
|
|
|
.themeGridCell(isSelected: false)
|
|
|
|
}
|
|
|
|
.padding()
|
|
|
|
}
|
2024-10-02 14:05:40 +00:00
|
|
|
.withMockEnvironment()
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private struct HeaderView: View {
|
|
|
|
let layout: ProfilesLayout
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
InstalledProfileView(
|
|
|
|
layout: layout,
|
|
|
|
profileManager: .mock,
|
|
|
|
profile: .mock,
|
|
|
|
tunnel: .mock,
|
|
|
|
interactiveManager: InteractiveManager(),
|
|
|
|
errorHandler: .default(),
|
|
|
|
nextProfileId: .constant(nil)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private struct ContentView: View {
|
|
|
|
var body: some View {
|
|
|
|
ForEach(0..<3) { _ in
|
|
|
|
ProfileRowView(
|
|
|
|
style: .full,
|
|
|
|
profileManager: .mock,
|
|
|
|
tunnel: .mock,
|
2024-11-22 11:52:51 +00:00
|
|
|
preview: .init(.mock),
|
2024-09-23 13:02:26 +00:00
|
|
|
interactiveManager: InteractiveManager(),
|
|
|
|
errorHandler: .default(),
|
|
|
|
nextProfileId: .constant(nil),
|
|
|
|
withMarker: true
|
2024-10-23 15:17:20 +00:00
|
|
|
)
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|