2024-11-02 07:41:32 +00:00
|
|
|
//
|
|
|
|
// ProfileView.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 10/31/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
|
2024-11-02 09:11:59 +00:00
|
|
|
import UILibrary
|
2024-11-02 07:41:32 +00:00
|
|
|
|
|
|
|
struct ProfileView: View, TunnelInstallationProviding {
|
|
|
|
enum Field: Hashable {
|
|
|
|
case connect
|
|
|
|
|
|
|
|
case switchProfile
|
|
|
|
|
|
|
|
case profile(Profile.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
@EnvironmentObject
|
|
|
|
private var theme: Theme
|
|
|
|
|
|
|
|
@ObservedObject
|
|
|
|
var profileManager: ProfileManager
|
|
|
|
|
|
|
|
@ObservedObject
|
|
|
|
var tunnel: ExtendedTunnel
|
|
|
|
|
|
|
|
@State
|
2024-11-05 15:13:03 +00:00
|
|
|
private var showsSidePanel = false
|
2024-11-02 07:41:32 +00:00
|
|
|
|
|
|
|
@FocusState
|
|
|
|
private var focusedField: Field?
|
|
|
|
|
|
|
|
@StateObject
|
|
|
|
private var interactiveManager = InteractiveManager()
|
|
|
|
|
|
|
|
@StateObject
|
|
|
|
private var errorHandler: ErrorHandler = .default()
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
GeometryReader { geo in
|
|
|
|
HStack(spacing: .zero) {
|
|
|
|
VStack {
|
|
|
|
activeView
|
|
|
|
.padding(.horizontal)
|
2024-11-22 01:20:41 +00:00
|
|
|
.frame(width: geo.size.width * 0.6)
|
2024-11-02 07:41:32 +00:00
|
|
|
.focusSection()
|
|
|
|
}
|
|
|
|
.frame(maxWidth: .infinity)
|
2024-11-02 14:24:41 +00:00
|
|
|
.disabled(interactiveManager.isPresented)
|
2024-11-02 07:41:32 +00:00
|
|
|
|
2024-11-05 15:13:03 +00:00
|
|
|
if showsSidePanel {
|
2024-11-22 01:20:41 +00:00
|
|
|
sidePanelView
|
|
|
|
.focusSection()
|
2024-11-02 07:41:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.ignoresSafeArea(edges: .horizontal)
|
2024-11-22 01:20:41 +00:00
|
|
|
.background(theme.primaryColor.opacity(0.6).gradient)
|
2024-11-05 15:13:03 +00:00
|
|
|
.themeAnimation(on: showsSidePanel, category: .profiles)
|
2024-11-02 07:41:32 +00:00
|
|
|
.withErrorHandler(errorHandler)
|
2024-11-02 14:24:41 +00:00
|
|
|
.defaultFocus($focusedField, .switchProfile)
|
2024-11-22 01:20:41 +00:00
|
|
|
.onChange(of: tunnel.status, onTunnelStatus)
|
|
|
|
.onChange(of: tunnel.currentProfile, onTunnelCurrentProfile)
|
|
|
|
.onChange(of: interactiveManager.isPresented, onInteractivePresented)
|
|
|
|
.onChange(of: focusedField, onFocus)
|
2024-11-02 07:41:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension ProfileView {
|
|
|
|
var currentProfile: Profile? {
|
|
|
|
guard let id = tunnel.currentProfile?.id else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return profileManager.profile(withId: id)
|
|
|
|
}
|
|
|
|
|
|
|
|
var activeView: some View {
|
|
|
|
ActiveProfileView(
|
|
|
|
profile: currentProfile,
|
|
|
|
tunnel: tunnel,
|
2024-11-05 15:13:03 +00:00
|
|
|
isSwitching: $showsSidePanel,
|
2024-11-02 07:41:32 +00:00
|
|
|
focusedField: $focusedField,
|
|
|
|
interactiveManager: interactiveManager,
|
|
|
|
errorHandler: errorHandler
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-11-22 01:20:41 +00:00
|
|
|
var sidePanelView: some View {
|
|
|
|
ZStack {
|
|
|
|
listView
|
|
|
|
.padding(.horizontal)
|
|
|
|
.opaque(!interactiveManager.isPresented)
|
|
|
|
|
|
|
|
if interactiveManager.isPresented {
|
|
|
|
interactiveView
|
|
|
|
.padding(.horizontal, 100)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// .frame(width: geo.size.width * 0.5) // seems redundant
|
|
|
|
}
|
|
|
|
|
2024-11-02 14:24:41 +00:00
|
|
|
var interactiveView: some View {
|
|
|
|
InteractiveCoordinator(style: .inline(withCancel: false), manager: interactiveManager) {
|
|
|
|
errorHandler.handle(
|
|
|
|
$0,
|
|
|
|
title: Strings.Global.connection,
|
|
|
|
message: Strings.Views.Profiles.Errors.tunnel
|
|
|
|
)
|
|
|
|
}
|
|
|
|
.font(.body)
|
|
|
|
.onExitCommand {
|
|
|
|
let formerProfileId = interactiveManager.editor.profile.id
|
|
|
|
focusedField = .profile(formerProfileId)
|
|
|
|
interactiveManager.isPresented = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-02 07:41:32 +00:00
|
|
|
var listView: some View {
|
|
|
|
ProfileListView(
|
|
|
|
profileManager: profileManager,
|
|
|
|
tunnel: tunnel,
|
|
|
|
focusedField: $focusedField,
|
|
|
|
interactiveManager: interactiveManager,
|
|
|
|
errorHandler: errorHandler
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-22 01:20:41 +00:00
|
|
|
private extension ProfileView {
|
|
|
|
func onTunnelStatus(old: TunnelStatus, new: TunnelStatus) {
|
|
|
|
if new == .activating {
|
|
|
|
showsSidePanel = false
|
|
|
|
focusedField = .connect
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func onTunnelCurrentProfile(old: TunnelCurrentProfile?, new: TunnelCurrentProfile?) {
|
|
|
|
if focusedField == .connect && new == nil {
|
|
|
|
focusedField = .switchProfile
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func onInteractivePresented(old: Bool, new: Bool) {
|
|
|
|
if new {
|
|
|
|
showsSidePanel = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func onFocus(old: Field?, new: Field?) {
|
|
|
|
switch new {
|
|
|
|
case .connect:
|
|
|
|
showsSidePanel = false
|
|
|
|
|
|
|
|
case .switchProfile:
|
|
|
|
showsSidePanel = true
|
|
|
|
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: -
|
|
|
|
|
2024-11-02 07:41:32 +00:00
|
|
|
#Preview {
|
|
|
|
ProfileView(
|
|
|
|
profileManager: .mock,
|
|
|
|
tunnel: .mock
|
|
|
|
)
|
2024-11-22 01:20:41 +00:00
|
|
|
.withMockEnvironment()
|
2024-11-02 07:41:32 +00:00
|
|
|
}
|