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-11-23 18:26:33 +00:00
|
|
|
import UILibrary
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-11-28 14:51:03 +00:00
|
|
|
public struct AppCoordinator: View, AppCoordinatorConforming, SizeClassProviding {
|
|
|
|
|
2024-12-01 21:34:41 +00:00
|
|
|
@EnvironmentObject
|
|
|
|
public var iapManager: IAPManager
|
|
|
|
|
2024-12-08 15:05:23 +00:00
|
|
|
@EnvironmentObject
|
|
|
|
public var preferencesManager: PreferencesManager
|
|
|
|
|
2024-11-28 14:51:03 +00:00
|
|
|
@Environment(\.isUITesting)
|
|
|
|
private var isUITesting
|
|
|
|
|
|
|
|
@Environment(\.horizontalSizeClass)
|
|
|
|
public var hsClass
|
|
|
|
|
|
|
|
@Environment(\.verticalSizeClass)
|
|
|
|
public var vsClass
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-11-26 20:21:49 +00:00
|
|
|
@AppStorage(UIPreference.profilesLayout.key)
|
2024-09-23 13:02:26 +00:00
|
|
|
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-12-01 21:34:41 +00:00
|
|
|
public 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
|
|
|
|
2024-11-24 19:01:30 +00:00
|
|
|
@State
|
|
|
|
private var isImporting = false
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-10-31 13:30:22 +00:00
|
|
|
@State
|
2024-11-24 19:01:30 +00:00
|
|
|
private var paywallReason: PaywallReason?
|
2024-10-31 13:30:22 +00:00
|
|
|
|
|
|
|
@State
|
2024-11-24 19:01:30 +00:00
|
|
|
private var modalRoute: ModalRoute?
|
2024-10-31 13:30:22 +00:00
|
|
|
|
|
|
|
@State
|
|
|
|
private var profilePath = NavigationPath()
|
|
|
|
|
2024-11-14 10:02:26 +00:00
|
|
|
@State
|
|
|
|
private var migrationPath = NavigationPath()
|
|
|
|
|
2024-11-24 19:01:30 +00:00
|
|
|
@StateObject
|
|
|
|
private var profileEditor = ProfileEditor()
|
2024-11-18 16:43:01 +00:00
|
|
|
|
2024-12-01 21:34:41 +00:00
|
|
|
@StateObject
|
|
|
|
private var interactiveManager = InteractiveManager()
|
|
|
|
|
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)
|
|
|
|
}
|
2024-11-25 20:55:31 +00:00
|
|
|
.modifier(OnboardingModifier(modalRoute: $modalRoute))
|
2024-11-18 16:43:01 +00:00
|
|
|
.modifier(PaywallModifier(reason: $paywallReason))
|
2024-10-31 13:30:22 +00:00
|
|
|
.themeModal(
|
|
|
|
item: $modalRoute,
|
2024-12-01 21:34:41 +00:00
|
|
|
options: modalRoute?.options(),
|
2024-10-31 13:30:22 +00:00
|
|
|
content: modalDestination
|
|
|
|
)
|
2024-12-06 09:26:27 +00:00
|
|
|
.withErrorHandler(errorHandler)
|
2024-12-01 21:34:41 +00:00
|
|
|
.onChange(of: interactiveManager.isPresented) {
|
|
|
|
modalRoute = $0 ? .interactiveLogin : nil
|
|
|
|
}
|
2024-10-31 13:30:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-01 21:34:41 +00:00
|
|
|
// MARK: -
|
2024-10-31 13:30:22 +00:00
|
|
|
|
|
|
|
extension AppCoordinator {
|
|
|
|
var contentView: some View {
|
|
|
|
ProfileContainerView(
|
2024-11-28 14:51:03 +00:00
|
|
|
layout: overriddenLayout,
|
2024-10-31 13:30:22 +00:00
|
|
|
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(
|
2024-12-08 15:05:23 +00:00
|
|
|
onEditProfile: onEditProfile,
|
2024-11-24 00:01:04 +00:00
|
|
|
onMigrateProfiles: {
|
|
|
|
modalRoute = .migrateProfiles
|
|
|
|
},
|
2024-12-01 21:34:41 +00:00
|
|
|
connectionFlow: .init(
|
|
|
|
onConnect: {
|
|
|
|
await onConnect($0, force: false)
|
|
|
|
},
|
|
|
|
onProviderEntityRequired: {
|
|
|
|
onProviderEntityRequired($0, force: false)
|
2024-11-26 23:58:33 +00:00
|
|
|
}
|
2024-12-01 21:34:41 +00:00
|
|
|
)
|
2024-10-31 13:30:22 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-12-01 21:34:41 +00:00
|
|
|
var overriddenLayout: ProfilesLayout {
|
|
|
|
if isUITesting {
|
|
|
|
return isBigDevice ? .grid : .list
|
|
|
|
}
|
|
|
|
return layout
|
|
|
|
}
|
|
|
|
|
|
|
|
var migrateViewStyle: MigrateView.Style {
|
|
|
|
#if os(iOS)
|
|
|
|
.list
|
|
|
|
#else
|
|
|
|
.table
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2024-10-31 13:30:22 +00:00
|
|
|
func toolbarContent() -> some ToolbarContent {
|
|
|
|
AppToolbar(
|
|
|
|
profileManager: profileManager,
|
2024-11-26 18:14:56 +00:00
|
|
|
registry: registry,
|
2024-10-31 13:30:22 +00:00
|
|
|
layout: $layout,
|
|
|
|
isImporting: $isImporting,
|
2024-11-23 18:26:33 +00:00
|
|
|
onPreferences: {
|
|
|
|
present(.preferences)
|
2024-10-31 13:30:22 +00:00
|
|
|
},
|
|
|
|
onAbout: {
|
|
|
|
present(.about)
|
|
|
|
},
|
2024-11-14 10:02:26 +00:00
|
|
|
onMigrateProfiles: {
|
|
|
|
present(.migrateProfiles)
|
|
|
|
},
|
2024-12-08 15:05:23 +00:00
|
|
|
onNewProfile: onNewProfile
|
2024-10-31 13:30:22 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
@ViewBuilder
|
|
|
|
func modalDestination(for item: ModalRoute?) -> some View {
|
|
|
|
switch item {
|
2024-11-18 16:43:01 +00:00
|
|
|
case .about:
|
2024-11-23 18:26:33 +00:00
|
|
|
AboutCoordinator(
|
2024-11-18 16:43:01 +00:00
|
|
|
profileManager: profileManager,
|
|
|
|
tunnel: tunnel
|
|
|
|
)
|
|
|
|
|
2024-11-26 18:14:56 +00:00
|
|
|
case .editProfile(let initialModuleId):
|
2024-10-31 13:30:22 +00:00
|
|
|
ProfileCoordinator(
|
2024-09-23 13:02:26 +00:00
|
|
|
profileManager: profileManager,
|
|
|
|
profileEditor: profileEditor,
|
2024-11-26 18:14:56 +00:00
|
|
|
initialModuleId: initialModuleId,
|
2024-11-08 11:37:09 +00:00
|
|
|
registry: registry,
|
|
|
|
moduleViewFactory: DefaultModuleViewFactory(registry: registry),
|
2024-10-31 13:30:22 +00:00
|
|
|
path: $profilePath,
|
2024-11-18 16:43:01 +00:00
|
|
|
onDismiss: onDismiss
|
2024-09-23 13:02:26 +00:00
|
|
|
)
|
2024-10-31 13:30:22 +00:00
|
|
|
|
2024-12-03 15:18:05 +00:00
|
|
|
case .editProviderEntity(let profile, let force, let module):
|
2024-10-31 13:30:22 +00:00
|
|
|
ProviderEntitySelector(
|
|
|
|
module: module,
|
2024-12-01 21:34:41 +00:00
|
|
|
errorHandler: errorHandler,
|
|
|
|
onSelect: {
|
2024-12-03 15:18:05 +00:00
|
|
|
try await onSelectProviderEntity(with: $0, in: profile, force: force)
|
2024-12-01 21:34:41 +00:00
|
|
|
}
|
2024-10-31 13:30:22 +00:00
|
|
|
)
|
|
|
|
|
2024-12-01 21:34:41 +00:00
|
|
|
case .interactiveLogin:
|
|
|
|
InteractiveCoordinator(style: .modal, manager: interactiveManager) {
|
|
|
|
errorHandler.handle(
|
|
|
|
$0,
|
|
|
|
title: interactiveManager.editor.profile.name,
|
|
|
|
message: Strings.Errors.App.tunnel
|
|
|
|
)
|
|
|
|
}
|
|
|
|
.presentationDetents([.medium])
|
|
|
|
|
2024-11-14 10:02:26 +00:00
|
|
|
case .migrateProfiles:
|
2024-11-14 14:11:25 +00:00
|
|
|
MigrateView(
|
|
|
|
style: migrateViewStyle,
|
|
|
|
profileManager: profileManager
|
|
|
|
)
|
|
|
|
.themeNavigationStack(closable: true, path: $migrationPath)
|
2024-11-14 10:02:26 +00:00
|
|
|
|
2024-11-23 18:26:33 +00:00
|
|
|
case .preferences:
|
|
|
|
PreferencesView(profileManager: profileManager)
|
2024-10-31 13:30:22 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
EmptyView()
|
|
|
|
}
|
|
|
|
}
|
2024-12-01 21:34:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Handlers
|
|
|
|
|
|
|
|
extension AppCoordinator {
|
|
|
|
public func onInteractiveLogin(_ profile: Profile, _ onComplete: @escaping InteractiveManager.CompletionBlock) {
|
|
|
|
pp_log(.app, .info, "Present interactive login")
|
2024-12-08 15:05:23 +00:00
|
|
|
interactiveManager.present(
|
|
|
|
with: profile,
|
|
|
|
preferencesManager: preferencesManager,
|
|
|
|
onComplete: onComplete
|
|
|
|
)
|
2024-12-01 21:34:41 +00:00
|
|
|
}
|
2024-10-31 13:30:22 +00:00
|
|
|
|
2024-12-01 21:34:41 +00:00
|
|
|
public func onProviderEntityRequired(_ profile: Profile, force: Bool) {
|
2024-12-03 15:18:05 +00:00
|
|
|
guard let module = profile.selectedProvider?.module else {
|
|
|
|
assertionFailure("Editing provider entity, but profile has no selected provider module")
|
2024-12-01 21:34:41 +00:00
|
|
|
return
|
2024-11-28 14:51:03 +00:00
|
|
|
}
|
2024-12-01 21:34:41 +00:00
|
|
|
pp_log(.app, .info, "Present provider entity selector")
|
2024-12-03 15:18:05 +00:00
|
|
|
present(.editProviderEntity(profile, force, module))
|
2024-11-28 14:51:03 +00:00
|
|
|
}
|
|
|
|
|
2024-12-01 21:34:41 +00:00
|
|
|
public func onPurchaseRequired(_ features: Set<AppFeature>) {
|
|
|
|
pp_log(.app, .info, "Present paywall for features: \(features)")
|
|
|
|
setLater(.init(features, needsConfirmation: true)) {
|
|
|
|
paywallReason = $0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public func onError(_ error: Error, profile: Profile) {
|
|
|
|
errorHandler.handle(
|
|
|
|
error,
|
|
|
|
title: profile.name,
|
|
|
|
message: Strings.Errors.App.tunnel
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension AppCoordinator {
|
2024-12-03 15:18:05 +00:00
|
|
|
func onSelectProviderEntity(with newModule: Module, in profile: Profile, force: Bool) async throws {
|
2024-12-01 21:34:41 +00:00
|
|
|
|
|
|
|
// XXX: select entity after dismissing
|
|
|
|
try await Task.sleep(for: .milliseconds(500))
|
|
|
|
|
2024-12-03 15:18:05 +00:00
|
|
|
pp_log(.app, .info, "Select new provider entity: (profile=\(profile.id), module=\(newModule.id))")
|
|
|
|
|
2024-12-01 21:34:41 +00:00
|
|
|
do {
|
2024-12-03 15:18:05 +00:00
|
|
|
var builder = profile.builder()
|
|
|
|
builder.saveModule(newModule)
|
|
|
|
let newProfile = try builder.tryBuild()
|
2024-12-01 21:34:41 +00:00
|
|
|
|
|
|
|
let wasConnected = newProfile.id == tunnel.currentProfile?.id && tunnel.status == .active
|
|
|
|
try await profileManager.save(newProfile, isLocal: true)
|
|
|
|
if !wasConnected {
|
|
|
|
pp_log(.app, .info, "Profile \(newProfile.id) was not connected, will connect to new provider entity")
|
|
|
|
await onConnect(newProfile, force: force)
|
|
|
|
} else {
|
|
|
|
pp_log(.app, .info, "Profile \(newProfile.id) was connected, will reconnect to new provider entity via AppContext observation")
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
pp_log(.app, .error, "Unable to save new provider entity: \(error)")
|
|
|
|
throw error
|
|
|
|
}
|
2024-11-14 10:02:26 +00:00
|
|
|
}
|
|
|
|
|
2024-12-08 15:05:23 +00:00
|
|
|
func onNewProfile(_ profile: EditableProfile, initialModuleId: UUID?) {
|
|
|
|
editProfile(profile, initialModuleId: initialModuleId)
|
|
|
|
}
|
|
|
|
|
|
|
|
func onEditProfile(_ preview: ProfilePreview) {
|
|
|
|
guard let profile = profileManager.profile(withId: preview.id) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
editProfile(profile.editable(), initialModuleId: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func editProfile(_ profile: EditableProfile, initialModuleId: UUID?) {
|
2024-10-31 13:30:22 +00:00
|
|
|
profilePath = NavigationPath()
|
2024-11-03 22:35:45 +00:00
|
|
|
let isShared = profileManager.isRemotelyShared(profileWithId: profile.id)
|
2024-12-08 15:05:23 +00:00
|
|
|
profileEditor.load(profile, isShared: isShared, preferencesManager: preferencesManager)
|
2024-11-26 18:14:56 +00:00
|
|
|
present(.editProfile(initialModuleId))
|
2024-10-31 13:30:22 +00:00
|
|
|
}
|
2024-12-01 21:34:41 +00:00
|
|
|
}
|
2024-10-31 13:30:22 +00:00
|
|
|
|
2024-12-01 21:34:41 +00:00
|
|
|
private extension AppCoordinator {
|
2024-10-31 13:30:22 +00:00
|
|
|
func present(_ route: ModalRoute?) {
|
2024-11-18 16:43:01 +00:00
|
|
|
setLater(route) {
|
|
|
|
modalRoute = $0
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
2024-12-01 21:34:41 +00:00
|
|
|
|
|
|
|
func onDismiss() {
|
|
|
|
present(nil)
|
|
|
|
}
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
2024-10-31 13:30:22 +00:00
|
|
|
|
2024-12-01 21:34:41 +00:00
|
|
|
// MARK: - Previews
|
|
|
|
|
2024-10-31 13:30:22 +00:00
|
|
|
#Preview {
|
|
|
|
AppCoordinator(
|
2024-11-28 16:31:17 +00:00
|
|
|
profileManager: .forPreviews,
|
|
|
|
tunnel: .forPreviews,
|
2024-10-31 13:30:22 +00:00
|
|
|
registry: Registry()
|
|
|
|
)
|
|
|
|
.withMockEnvironment()
|
|
|
|
}
|