mirror of
https://github.com/passepartoutvpn/passepartout-apple.git
synced 2024-12-24 18:32:36 +00:00
cb530d8a65
Fixes #913
259 lines
7.1 KiB
Swift
259 lines
7.1 KiB
Swift
//
|
|
// ActiveProfileView.swift
|
|
// Passepartout
|
|
//
|
|
// Created by Davide De Rosa on 11/1/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 CommonLibrary
|
|
import CommonUtils
|
|
import PassepartoutKit
|
|
import SwiftUI
|
|
|
|
struct ActiveProfileView: View {
|
|
|
|
@EnvironmentObject
|
|
private var theme: Theme
|
|
|
|
@EnvironmentObject
|
|
private var providerManager: ProviderManager
|
|
|
|
let profile: Profile?
|
|
|
|
@ObservedObject
|
|
var tunnel: ExtendedTunnel
|
|
|
|
@Binding
|
|
var isSwitching: Bool
|
|
|
|
@FocusState.Binding
|
|
var focusedField: ProfileView.Field?
|
|
|
|
@ObservedObject
|
|
var interactiveManager: InteractiveManager
|
|
|
|
@ObservedObject
|
|
var errorHandler: ErrorHandler
|
|
|
|
var flow: AppFlow?
|
|
|
|
var body: some View {
|
|
VStack(spacing: .zero) {
|
|
VStack {
|
|
VStack {
|
|
currentProfileView
|
|
statusView
|
|
}
|
|
.padding(.bottom)
|
|
|
|
profile.map {
|
|
detailView(for: $0)
|
|
}
|
|
.padding(.bottom)
|
|
|
|
Group {
|
|
toggleConnectionButton
|
|
switchProfileButton
|
|
}
|
|
.clipShape(RoundedRectangle(cornerRadius: 50))
|
|
}
|
|
.padding(.horizontal, 100)
|
|
.padding(.top, 50)
|
|
|
|
Spacer()
|
|
}
|
|
}
|
|
}
|
|
|
|
private extension ActiveProfileView {
|
|
var currentProfileView: some View {
|
|
Text(profile?.name ?? Strings.Views.App.Rows.notInstalled)
|
|
.font(.title)
|
|
.fontWeight(.bold)
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
}
|
|
|
|
var statusView: some View {
|
|
ConnectionStatusText(tunnel: tunnel)
|
|
.font(.title2)
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
.foregroundStyle(tunnel.statusColor(theme))
|
|
.brightness(0.2)
|
|
}
|
|
|
|
func detailView(for profile: Profile) -> some View {
|
|
VStack(spacing: 10) {
|
|
if let connectionType = profile.localizedDescription(optionalStyle: .connectionType) {
|
|
ListRowView(title: Strings.Global.Nouns.protocol) {
|
|
Text(connectionType)
|
|
}
|
|
}
|
|
if let pair = profile.selectedProvider {
|
|
if let metadata = providerManager.provider(withId: pair.selection.id) {
|
|
ListRowView(title: Strings.Global.Nouns.provider) {
|
|
Text(metadata.description)
|
|
}
|
|
}
|
|
if let entity = pair.selection.entity {
|
|
ListRowView(title: Strings.Global.Nouns.country) {
|
|
ThemeCountryText(entity.header.countryCode)
|
|
}
|
|
}
|
|
}
|
|
if let otherList = profile.localizedDescription(optionalStyle: .nonConnectionTypes) {
|
|
ListRowView(title: otherList) {
|
|
EmptyView()
|
|
}
|
|
}
|
|
}
|
|
.font(.title3)
|
|
}
|
|
|
|
var toggleConnectionButton: some View {
|
|
TunnelToggleButton(
|
|
tunnel: tunnel,
|
|
profile: profile,
|
|
nextProfileId: .constant(nil),
|
|
interactiveManager: interactiveManager,
|
|
errorHandler: errorHandler,
|
|
onProviderEntityRequired: onProviderEntityRequired,
|
|
onPurchaseRequired: onPurchaseRequired,
|
|
label: {
|
|
Text($0 ? Strings.Global.Actions.connect : Strings.Global.Actions.disconnect)
|
|
.frame(maxWidth: .infinity)
|
|
.padding(.vertical, 10)
|
|
}
|
|
)
|
|
.background(toggleConnectionColor)
|
|
.fontWeight(.bold)
|
|
.focused($focusedField, equals: .connect)
|
|
}
|
|
|
|
var toggleConnectionColor: Color {
|
|
switch tunnel.status {
|
|
case .inactive:
|
|
return theme.activeColor
|
|
|
|
default:
|
|
return theme.errorColor
|
|
}
|
|
}
|
|
|
|
var switchProfileButton: some View {
|
|
Button {
|
|
isSwitching.toggle()
|
|
} label: {
|
|
Text(Strings.Global.Actions.select)
|
|
.frame(maxWidth: .infinity)
|
|
.padding(.vertical, 10)
|
|
}
|
|
.focused($focusedField, equals: .switchProfile)
|
|
}
|
|
}
|
|
|
|
private extension ActiveProfileView {
|
|
func onProviderEntityRequired(_ profile: Profile) {
|
|
flow?.onProviderEntityRequired(profile)
|
|
}
|
|
|
|
func onPurchaseRequired(_ features: Set<AppFeature>) {
|
|
flow?.onPurchaseRequired(features)
|
|
}
|
|
}
|
|
|
|
// MARK: - Previews
|
|
|
|
#Preview("Host") {
|
|
let profile: Profile = {
|
|
do {
|
|
let moduleBuilder = OpenVPNModule.Builder()
|
|
let module = try moduleBuilder.tryBuild()
|
|
|
|
let builder = Profile.Builder(
|
|
name: "Host",
|
|
modules: [module],
|
|
activatingModules: true
|
|
)
|
|
return try builder.tryBuild()
|
|
} catch {
|
|
fatalError(error.localizedDescription)
|
|
}
|
|
}()
|
|
|
|
HStack {
|
|
ContentPreview(profile: profile)
|
|
.frame(maxWidth: .infinity)
|
|
VStack {}
|
|
.frame(maxWidth: .infinity)
|
|
}
|
|
}
|
|
|
|
#Preview("Provider") {
|
|
let profile: Profile = {
|
|
do {
|
|
var moduleBuilder = OpenVPNModule.Builder()
|
|
moduleBuilder.providerId = .mullvad
|
|
let module = try moduleBuilder.tryBuild()
|
|
|
|
let builder = Profile.Builder(
|
|
name: "Provider",
|
|
modules: [module],
|
|
activatingModules: true
|
|
)
|
|
return try builder.tryBuild()
|
|
} catch {
|
|
fatalError(error.localizedDescription)
|
|
}
|
|
}()
|
|
|
|
HStack {
|
|
ContentPreview(profile: profile)
|
|
.frame(maxWidth: .infinity)
|
|
VStack {}
|
|
.frame(maxWidth: .infinity)
|
|
}
|
|
.task {
|
|
try? await ProviderManager.mock.fetchIndex(from: [API.bundled])
|
|
}
|
|
}
|
|
|
|
private struct ContentPreview: View {
|
|
let profile: Profile
|
|
|
|
@State
|
|
private var isSwitching = false
|
|
|
|
@FocusState
|
|
private var focusedField: ProfileView.Field?
|
|
|
|
var body: some View {
|
|
ActiveProfileView(
|
|
profile: profile,
|
|
tunnel: .mock,
|
|
isSwitching: $isSwitching,
|
|
focusedField: $focusedField,
|
|
interactiveManager: InteractiveManager(),
|
|
errorHandler: .default()
|
|
)
|
|
.withMockEnvironment()
|
|
}
|
|
}
|