2024-09-23 13:02:26 +00:00
|
|
|
//
|
|
|
|
// ProfileCoordinator.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 9/3/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-09-23 13:02:26 +00:00
|
|
|
import PassepartoutKit
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct ProfileCoordinator: View {
|
|
|
|
struct Flow {
|
|
|
|
let onNewModule: (ModuleType) -> Void
|
|
|
|
|
|
|
|
let onCommitEditing: () async throws -> Void
|
|
|
|
|
|
|
|
let onCancelEditing: () -> Void
|
|
|
|
}
|
|
|
|
|
|
|
|
@EnvironmentObject
|
|
|
|
private var theme: Theme
|
|
|
|
|
|
|
|
@EnvironmentObject
|
|
|
|
private var iapManager: IAPManager
|
|
|
|
|
|
|
|
let profileManager: ProfileManager
|
|
|
|
|
|
|
|
let profileEditor: ProfileEditor
|
|
|
|
|
2024-11-26 18:14:56 +00:00
|
|
|
let initialModuleId: UUID?
|
|
|
|
|
2024-11-08 11:37:09 +00:00
|
|
|
let registry: Registry
|
|
|
|
|
2024-09-23 13:02:26 +00:00
|
|
|
let moduleViewFactory: any ModuleViewFactory
|
|
|
|
|
|
|
|
@Binding
|
|
|
|
var path: NavigationPath
|
|
|
|
|
|
|
|
let onDismiss: () -> Void
|
|
|
|
|
|
|
|
@State
|
|
|
|
private var paywallReason: PaywallReason?
|
|
|
|
|
|
|
|
@StateObject
|
|
|
|
private var errorHandler: ErrorHandler = .default()
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
contentView
|
2024-11-24 19:01:30 +00:00
|
|
|
.modifier(PaywallModifier(
|
|
|
|
reason: $paywallReason,
|
2024-11-24 00:01:04 +00:00
|
|
|
okTitle: Strings.Views.Profile.Alerts.Purchase.Buttons.ok,
|
|
|
|
okAction: onDismiss
|
|
|
|
))
|
2024-09-23 13:02:26 +00:00
|
|
|
.withErrorHandler(errorHandler)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Destinations
|
|
|
|
|
|
|
|
private extension ProfileCoordinator {
|
|
|
|
var contentView: some View {
|
|
|
|
#if os(iOS)
|
|
|
|
ProfileEditView(
|
|
|
|
profileEditor: profileEditor,
|
2024-11-26 18:14:56 +00:00
|
|
|
initialModuleId: initialModuleId,
|
2024-09-23 13:02:26 +00:00
|
|
|
moduleViewFactory: moduleViewFactory,
|
|
|
|
path: $path,
|
2024-11-18 16:43:01 +00:00
|
|
|
paywallReason: $paywallReason,
|
2024-09-23 13:02:26 +00:00
|
|
|
flow: .init(
|
|
|
|
onNewModule: onNewModule,
|
|
|
|
onCommitEditing: onCommitEditing,
|
|
|
|
onCancelEditing: onCancelEditing
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.themeNavigationDetail()
|
2024-11-28 14:51:03 +00:00
|
|
|
.themeNavigationStack(path: $path)
|
2024-11-23 18:26:33 +00:00
|
|
|
#else
|
2024-09-23 13:02:26 +00:00
|
|
|
ProfileSplitView(
|
|
|
|
profileEditor: profileEditor,
|
2024-11-26 18:14:56 +00:00
|
|
|
initialModuleId: initialModuleId,
|
2024-09-23 13:02:26 +00:00
|
|
|
moduleViewFactory: moduleViewFactory,
|
2024-11-18 16:43:01 +00:00
|
|
|
paywallReason: $paywallReason,
|
2024-09-23 13:02:26 +00:00
|
|
|
flow: .init(
|
|
|
|
onNewModule: onNewModule,
|
|
|
|
onCommitEditing: onCommitEditing,
|
|
|
|
onCancelEditing: onCancelEditing
|
|
|
|
)
|
|
|
|
)
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension ProfileCoordinator {
|
|
|
|
func onNewModule(_ moduleType: ModuleType) {
|
2024-11-08 11:37:09 +00:00
|
|
|
let module = moduleType.newModule(with: registry)
|
2024-09-23 13:02:26 +00:00
|
|
|
withAnimation(theme.animation(for: .modules)) {
|
|
|
|
profileEditor.saveModule(module, activating: true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func onCommitEditing() async throws {
|
|
|
|
do {
|
2024-11-18 16:43:01 +00:00
|
|
|
if !iapManager.isRestricted {
|
|
|
|
try await onCommitEditingStandard()
|
|
|
|
} else {
|
|
|
|
try await onCommitEditingRestricted()
|
|
|
|
}
|
2024-09-23 13:02:26 +00:00
|
|
|
} catch {
|
2024-11-23 19:31:22 +00:00
|
|
|
errorHandler.handle(error, title: Strings.Global.Actions.save)
|
2024-09-23 13:02:26 +00:00
|
|
|
throw error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-18 16:43:01 +00:00
|
|
|
// standard: always save, warn if purchase required
|
|
|
|
func onCommitEditingStandard() async throws {
|
2024-12-09 07:44:13 +00:00
|
|
|
let savedProfile = try await profileEditor.save(to: profileManager)
|
2024-11-18 16:43:01 +00:00
|
|
|
do {
|
2024-11-19 07:55:41 +00:00
|
|
|
try iapManager.verify(savedProfile)
|
2024-11-18 16:43:01 +00:00
|
|
|
} catch AppError.ineligibleProfile(let requiredFeatures) {
|
2024-11-24 19:01:30 +00:00
|
|
|
paywallReason = .init(requiredFeatures, needsConfirmation: true)
|
2024-11-18 16:43:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
onDismiss()
|
|
|
|
}
|
|
|
|
|
|
|
|
// restricted: verify before saving
|
|
|
|
func onCommitEditingRestricted() async throws {
|
|
|
|
do {
|
|
|
|
try iapManager.verify(profileEditor.activeModules)
|
|
|
|
} catch AppError.ineligibleProfile(let requiredFeatures) {
|
2024-11-24 19:01:30 +00:00
|
|
|
paywallReason = .init(requiredFeatures)
|
2024-11-18 16:43:01 +00:00
|
|
|
return
|
|
|
|
}
|
2024-12-09 07:44:13 +00:00
|
|
|
try await profileEditor.save(to: profileManager)
|
2024-11-18 16:43:01 +00:00
|
|
|
onDismiss()
|
|
|
|
}
|
|
|
|
|
2024-09-23 13:02:26 +00:00
|
|
|
func onCancelEditing() {
|
2024-12-08 15:05:23 +00:00
|
|
|
profileEditor.discard()
|
2024-09-23 13:02:26 +00:00
|
|
|
onDismiss()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Previews
|
|
|
|
|
|
|
|
#Preview {
|
|
|
|
ProfileCoordinator(
|
2024-11-28 16:31:17 +00:00
|
|
|
profileManager: .forPreviews,
|
2024-10-29 13:30:41 +00:00
|
|
|
profileEditor: ProfileEditor(profile: .newMockProfile()),
|
2024-11-26 18:14:56 +00:00
|
|
|
initialModuleId: nil,
|
2024-11-08 11:37:09 +00:00
|
|
|
registry: Registry(),
|
|
|
|
moduleViewFactory: DefaultModuleViewFactory(registry: Registry()),
|
2024-09-23 13:02:26 +00:00
|
|
|
path: .constant(NavigationPath()),
|
|
|
|
onDismiss: {}
|
|
|
|
)
|
2024-10-02 14:05:40 +00:00
|
|
|
.withMockEnvironment()
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|