2024-11-02 07:41:32 +00:00
|
|
|
//
|
|
|
|
// 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/>.
|
|
|
|
//
|
|
|
|
|
2024-11-03 12:16:13 +00:00
|
|
|
import CommonLibrary
|
2024-11-02 09:11:59 +00:00
|
|
|
import CommonUtils
|
2024-11-02 07:41:32 +00:00
|
|
|
import PassepartoutKit
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct ActiveProfileView: View {
|
2024-11-22 01:20:41 +00:00
|
|
|
|
|
|
|
@EnvironmentObject
|
|
|
|
private var theme: Theme
|
|
|
|
|
|
|
|
@EnvironmentObject
|
|
|
|
private var providerManager: ProviderManager
|
|
|
|
|
2024-11-02 07:41:32 +00:00
|
|
|
let profile: Profile?
|
|
|
|
|
|
|
|
@ObservedObject
|
|
|
|
var tunnel: ExtendedTunnel
|
|
|
|
|
|
|
|
@Binding
|
|
|
|
var isSwitching: Bool
|
|
|
|
|
|
|
|
@FocusState.Binding
|
|
|
|
var focusedField: ProfileView.Field?
|
|
|
|
|
|
|
|
@ObservedObject
|
|
|
|
var errorHandler: ErrorHandler
|
|
|
|
|
2024-12-01 21:34:41 +00:00
|
|
|
var flow: ConnectionFlow?
|
2024-11-24 00:01:04 +00:00
|
|
|
|
2024-11-02 07:41:32 +00:00
|
|
|
var body: some View {
|
2024-11-22 09:05:46 +00:00
|
|
|
VStack(spacing: .zero) {
|
2024-11-02 07:41:32 +00:00
|
|
|
VStack {
|
2024-11-22 09:05:46 +00:00
|
|
|
VStack {
|
|
|
|
currentProfileView
|
|
|
|
statusView
|
|
|
|
}
|
|
|
|
.padding(.bottom)
|
2024-11-22 01:20:41 +00:00
|
|
|
|
2024-11-22 09:05:46 +00:00
|
|
|
profile.map {
|
|
|
|
detailView(for: $0)
|
|
|
|
}
|
|
|
|
.padding(.bottom)
|
2024-11-02 07:41:32 +00:00
|
|
|
|
2024-11-22 09:05:46 +00:00
|
|
|
Group {
|
|
|
|
toggleConnectionButton
|
|
|
|
switchProfileButton
|
|
|
|
}
|
|
|
|
.clipShape(RoundedRectangle(cornerRadius: 50))
|
2024-11-22 01:20:41 +00:00
|
|
|
}
|
2024-11-22 09:05:46 +00:00
|
|
|
.padding(.horizontal, 100)
|
2024-11-22 09:56:37 +00:00
|
|
|
.padding(.top, 50)
|
2024-11-22 01:20:41 +00:00
|
|
|
|
2024-11-22 09:05:46 +00:00
|
|
|
Spacer()
|
|
|
|
}
|
2024-11-02 07:41:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension ActiveProfileView {
|
|
|
|
var currentProfileView: some View {
|
2024-11-27 13:49:23 +00:00
|
|
|
Text(profile?.name ?? Strings.Views.App.InstalledProfile.None.name)
|
2024-11-02 07:41:32 +00:00
|
|
|
.font(.title)
|
2024-11-27 13:49:23 +00:00
|
|
|
.fontWeight(theme.relevantWeight)
|
2024-11-22 01:20:41 +00:00
|
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
2024-11-02 07:41:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var statusView: some View {
|
|
|
|
ConnectionStatusText(tunnel: tunnel)
|
|
|
|
.font(.title2)
|
2024-11-22 01:20:41 +00:00
|
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
|
|
.foregroundStyle(tunnel.statusColor(theme))
|
|
|
|
.brightness(0.2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func detailView(for profile: Profile) -> some View {
|
|
|
|
VStack(spacing: 10) {
|
2024-11-22 11:52:51 +00:00
|
|
|
if let connectionType = profile.localizedDescription(optionalStyle: .connectionType) {
|
2024-11-23 22:01:11 +00:00
|
|
|
ListRowView(title: Strings.Global.Nouns.protocol) {
|
2024-11-22 11:52:51 +00:00
|
|
|
Text(connectionType)
|
2024-11-22 01:20:41 +00:00
|
|
|
}
|
|
|
|
}
|
2024-11-22 09:05:46 +00:00
|
|
|
if let pair = profile.selectedProvider {
|
2024-12-04 08:26:10 +00:00
|
|
|
if let provider = providerManager.provider(withId: pair.selection.id) {
|
2024-11-23 22:01:11 +00:00
|
|
|
ListRowView(title: Strings.Global.Nouns.provider) {
|
2024-12-04 08:26:10 +00:00
|
|
|
Text(provider.description)
|
2024-11-22 01:20:41 +00:00
|
|
|
}
|
|
|
|
}
|
2024-11-22 09:05:46 +00:00
|
|
|
if let entity = pair.selection.entity {
|
2024-11-23 22:01:11 +00:00
|
|
|
ListRowView(title: Strings.Global.Nouns.country) {
|
2024-11-22 01:20:41 +00:00
|
|
|
ThemeCountryText(entity.header.countryCode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-11-22 11:52:51 +00:00
|
|
|
if let otherList = profile.localizedDescription(optionalStyle: .nonConnectionTypes) {
|
2024-11-23 22:01:11 +00:00
|
|
|
ListRowView(title: otherList) {
|
2024-11-22 09:56:37 +00:00
|
|
|
EmptyView()
|
|
|
|
}
|
|
|
|
}
|
2024-11-22 01:20:41 +00:00
|
|
|
}
|
|
|
|
.font(.title3)
|
2024-11-02 07:41:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var toggleConnectionButton: some View {
|
|
|
|
TunnelToggleButton(
|
|
|
|
tunnel: tunnel,
|
|
|
|
profile: profile,
|
|
|
|
nextProfileId: .constant(nil),
|
|
|
|
errorHandler: errorHandler,
|
2024-12-01 21:34:41 +00:00
|
|
|
flow: flow,
|
2024-11-02 07:41:32 +00:00
|
|
|
label: {
|
2024-11-23 19:31:22 +00:00
|
|
|
Text($0 ? Strings.Global.Actions.connect : Strings.Global.Actions.disconnect)
|
2024-11-02 07:41:32 +00:00
|
|
|
.frame(maxWidth: .infinity)
|
2024-11-22 01:20:41 +00:00
|
|
|
.padding(.vertical, 10)
|
2024-11-02 07:41:32 +00:00
|
|
|
}
|
|
|
|
)
|
2024-11-22 01:20:41 +00:00
|
|
|
.background(toggleConnectionColor)
|
2024-11-27 13:49:23 +00:00
|
|
|
.fontWeight(theme.relevantWeight)
|
2024-11-02 07:41:32 +00:00
|
|
|
.focused($focusedField, equals: .connect)
|
|
|
|
}
|
|
|
|
|
2024-11-22 01:20:41 +00:00
|
|
|
var toggleConnectionColor: Color {
|
|
|
|
switch tunnel.status {
|
|
|
|
case .inactive:
|
|
|
|
return theme.activeColor
|
|
|
|
|
|
|
|
default:
|
|
|
|
return theme.errorColor
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-02 07:41:32 +00:00
|
|
|
var switchProfileButton: some View {
|
|
|
|
Button {
|
|
|
|
isSwitching.toggle()
|
|
|
|
} label: {
|
2024-11-23 19:31:22 +00:00
|
|
|
Text(Strings.Global.Actions.select)
|
2024-11-02 07:41:32 +00:00
|
|
|
.frame(maxWidth: .infinity)
|
2024-11-22 01:20:41 +00:00
|
|
|
.padding(.vertical, 10)
|
2024-11-02 07:41:32 +00:00
|
|
|
}
|
|
|
|
.focused($focusedField, equals: .switchProfile)
|
|
|
|
}
|
|
|
|
}
|
2024-11-22 01:20:41 +00:00
|
|
|
|
2024-11-22 09:05:46 +00:00
|
|
|
// MARK: - Previews
|
|
|
|
|
2024-11-22 01:20:41 +00:00
|
|
|
#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 {
|
2024-11-28 16:31:17 +00:00
|
|
|
try? await ProviderManager.forPreviews.fetchIndex(from: [API.bundled])
|
2024-11-22 01:20:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2024-11-28 16:31:17 +00:00
|
|
|
tunnel: .forPreviews,
|
2024-11-22 01:20:41 +00:00
|
|
|
isSwitching: $isSwitching,
|
|
|
|
focusedField: $focusedField,
|
|
|
|
errorHandler: .default()
|
|
|
|
)
|
|
|
|
.withMockEnvironment()
|
|
|
|
}
|
|
|
|
}
|