2024-09-23 13:02:26 +00:00
|
|
|
//
|
|
|
|
// AppCoordinator.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 8/13/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
|
2024-11-03 10:12:19 +00:00
|
|
|
import CommonUtils
|
2024-09-23 13:02:26 +00:00
|
|
|
import PassepartoutKit
|
|
|
|
import SwiftUI
|
|
|
|
|
2024-10-31 13:30:22 +00:00
|
|
|
public struct AppCoordinator: View, AppCoordinatorConforming {
|
2024-09-23 13:02:26 +00:00
|
|
|
|
|
|
|
@AppStorage(AppPreference.profilesLayout.key)
|
|
|
|
private var layout: ProfilesLayout = .list
|
|
|
|
|
2024-10-29 13:30:41 +00:00
|
|
|
private let profileManager: ProfileManager
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-11-01 08:47:50 +00:00
|
|
|
private let tunnel: ExtendedTunnel
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-10-29 13:30:41 +00:00
|
|
|
private let registry: Registry
|
2024-09-23 13:02:26 +00:00
|
|
|
|
|
|
|
@StateObject
|
|
|
|
private var profileEditor = ProfileEditor()
|
|
|
|
|
2024-10-31 13:30:22 +00:00
|
|
|
@State
|
|
|
|
private var modalRoute: ModalRoute?
|
|
|
|
|
|
|
|
@State
|
|
|
|
private var isImporting = false
|
|
|
|
|
|
|
|
@State
|
|
|
|
private var profilePath = NavigationPath()
|
|
|
|
|
2024-11-03 10:12:19 +00:00
|
|
|
@StateObject
|
|
|
|
private var errorHandler: ErrorHandler = .default()
|
|
|
|
|
2024-09-23 13:02:26 +00:00
|
|
|
public init(
|
|
|
|
profileManager: ProfileManager,
|
2024-11-01 08:47:50 +00:00
|
|
|
tunnel: ExtendedTunnel,
|
2024-09-23 13:02:26 +00:00
|
|
|
registry: Registry
|
|
|
|
) {
|
|
|
|
self.profileManager = profileManager
|
|
|
|
self.tunnel = tunnel
|
|
|
|
self.registry = registry
|
|
|
|
}
|
|
|
|
|
|
|
|
public var body: some View {
|
2024-10-31 13:30:22 +00:00
|
|
|
NavigationStack {
|
|
|
|
contentView
|
|
|
|
.toolbar(content: toolbarContent)
|
|
|
|
}
|
|
|
|
.themeModal(
|
|
|
|
item: $modalRoute,
|
|
|
|
isRoot: true,
|
|
|
|
isInteractive: modalRoute?.isInteractive ?? true,
|
|
|
|
content: modalDestination
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Destinations
|
|
|
|
|
|
|
|
extension AppCoordinator {
|
|
|
|
enum ModalRoute: Identifiable {
|
|
|
|
case editProfile
|
|
|
|
|
|
|
|
case editProviderEntity(Profile, Module, ModuleMetadata.Provider)
|
|
|
|
|
|
|
|
case settings
|
|
|
|
|
|
|
|
case about
|
|
|
|
|
|
|
|
var id: Int {
|
|
|
|
switch self {
|
|
|
|
case .editProfile: return 1
|
|
|
|
case .editProviderEntity: return 2
|
|
|
|
case .settings: return 3
|
|
|
|
case .about: return 4
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var isInteractive: Bool {
|
|
|
|
switch self {
|
|
|
|
case .editProfile:
|
|
|
|
return false
|
|
|
|
|
|
|
|
default:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var contentView: some View {
|
|
|
|
ProfileContainerView(
|
|
|
|
layout: layout,
|
|
|
|
profileManager: profileManager,
|
|
|
|
tunnel: tunnel,
|
|
|
|
registry: registry,
|
|
|
|
isImporting: $isImporting,
|
2024-11-03 10:12:19 +00:00
|
|
|
errorHandler: errorHandler,
|
2024-10-31 13:30:22 +00:00
|
|
|
flow: .init(
|
|
|
|
onEditProfile: {
|
|
|
|
guard let profile = profileManager.profile(withId: $0.id) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
enterDetail(of: profile)
|
|
|
|
},
|
|
|
|
onEditProviderEntity: {
|
|
|
|
guard let pair = $0.firstProviderModuleWithMetadata else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
present(.editProviderEntity($0, pair.0, pair.1))
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func toolbarContent() -> some ToolbarContent {
|
|
|
|
AppToolbar(
|
|
|
|
profileManager: profileManager,
|
|
|
|
layout: $layout,
|
|
|
|
isImporting: $isImporting,
|
|
|
|
onSettings: {
|
|
|
|
present(.settings)
|
|
|
|
},
|
|
|
|
onAbout: {
|
|
|
|
present(.about)
|
|
|
|
},
|
|
|
|
onNewProfile: enterDetail
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
@ViewBuilder
|
|
|
|
func modalDestination(for item: ModalRoute?) -> some View {
|
|
|
|
switch item {
|
|
|
|
case .editProfile:
|
|
|
|
ProfileCoordinator(
|
2024-09-23 13:02:26 +00:00
|
|
|
profileManager: profileManager,
|
|
|
|
profileEditor: profileEditor,
|
2024-11-08 11:37:09 +00:00
|
|
|
registry: registry,
|
|
|
|
moduleViewFactory: DefaultModuleViewFactory(registry: registry),
|
2024-10-31 13:30:22 +00:00
|
|
|
modally: true,
|
|
|
|
path: $profilePath,
|
|
|
|
onDismiss: {
|
|
|
|
present(nil)
|
|
|
|
}
|
2024-09-23 13:02:26 +00:00
|
|
|
)
|
2024-10-31 13:30:22 +00:00
|
|
|
|
|
|
|
case .editProviderEntity(let profile, let module, let provider):
|
|
|
|
ProviderEntitySelector(
|
2024-09-23 13:02:26 +00:00
|
|
|
profileManager: profileManager,
|
|
|
|
tunnel: tunnel,
|
2024-10-31 13:30:22 +00:00
|
|
|
profile: profile,
|
|
|
|
module: module,
|
2024-11-03 10:12:19 +00:00
|
|
|
provider: provider,
|
|
|
|
errorHandler: errorHandler
|
2024-10-31 13:30:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
case .settings:
|
|
|
|
SettingsView(profileManager: profileManager)
|
|
|
|
|
|
|
|
case .about:
|
|
|
|
AboutRouterView(
|
|
|
|
profileManager: profileManager,
|
|
|
|
tunnel: tunnel
|
2024-09-23 13:02:26 +00:00
|
|
|
)
|
2024-10-31 13:30:22 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
EmptyView()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func enterDetail(of profile: Profile) {
|
|
|
|
profilePath = NavigationPath()
|
2024-11-03 22:35:45 +00:00
|
|
|
let isShared = profileManager.isRemotelyShared(profileWithId: profile.id)
|
|
|
|
profileEditor.editProfile(profile, isShared: isShared)
|
2024-10-31 13:30:22 +00:00
|
|
|
present(.editProfile)
|
|
|
|
}
|
|
|
|
|
|
|
|
func present(_ route: ModalRoute?) {
|
|
|
|
// XXX: this is a workaround for #791 on iOS 16
|
|
|
|
Task {
|
|
|
|
try await Task.sleep(for: .milliseconds(50))
|
|
|
|
modalRoute = route
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-10-31 13:30:22 +00:00
|
|
|
|
|
|
|
#Preview {
|
|
|
|
AppCoordinator(
|
|
|
|
profileManager: .mock,
|
|
|
|
tunnel: .mock,
|
|
|
|
registry: Registry()
|
|
|
|
)
|
|
|
|
.withMockEnvironment()
|
|
|
|
}
|