2024-09-23 13:02:26 +00:00
|
|
|
//
|
|
|
|
// ProfileEditView+iOS.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 6/22/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/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
#if os(iOS)
|
|
|
|
|
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 ProfileEditView: View, Routable {
|
|
|
|
|
|
|
|
@ObservedObject
|
|
|
|
var profileEditor: ProfileEditor
|
|
|
|
|
2024-11-26 18:14:56 +00:00
|
|
|
let initialModuleId: UUID?
|
|
|
|
|
2024-09-23 13:02:26 +00:00
|
|
|
let moduleViewFactory: any ModuleViewFactory
|
|
|
|
|
|
|
|
@Binding
|
|
|
|
var path: NavigationPath
|
|
|
|
|
2024-11-18 16:43:01 +00:00
|
|
|
@Binding
|
|
|
|
var paywallReason: PaywallReason?
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-11-18 16:43:01 +00:00
|
|
|
var flow: ProfileCoordinator.Flow?
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-11-05 09:03:54 +00:00
|
|
|
@State
|
2024-11-18 16:43:01 +00:00
|
|
|
private var errorModuleIds: Set<UUID> = []
|
2024-11-05 09:03:54 +00:00
|
|
|
|
2024-09-23 13:02:26 +00:00
|
|
|
var body: some View {
|
|
|
|
debugChanges()
|
|
|
|
return List {
|
|
|
|
NameSection(
|
2024-10-10 22:24:06 +00:00
|
|
|
name: $profileEditor.profile.name,
|
2024-09-23 13:02:26 +00:00
|
|
|
placeholder: Strings.Placeholders.Profile.name
|
|
|
|
)
|
2024-11-05 09:03:54 +00:00
|
|
|
modulesSection
|
2024-09-23 13:02:26 +00:00
|
|
|
StorageSection(
|
2024-11-05 09:03:54 +00:00
|
|
|
profileEditor: profileEditor,
|
|
|
|
paywallReason: $paywallReason
|
2024-09-23 13:02:26 +00:00
|
|
|
)
|
2024-11-05 09:03:54 +00:00
|
|
|
UUIDSection(uuid: profileEditor.profile.id)
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
.toolbar(content: toolbarContent)
|
2024-11-23 19:31:22 +00:00
|
|
|
.navigationTitle(Strings.Global.Nouns.profile)
|
2024-09-23 13:02:26 +00:00
|
|
|
.navigationBarBackButtonHidden(true)
|
|
|
|
.navigationDestination(for: NavigationRoute.self, destination: pushDestination)
|
2024-11-26 18:14:56 +00:00
|
|
|
.onLoad {
|
|
|
|
if let initialModuleId {
|
|
|
|
push(.moduleDetail(moduleId: initialModuleId))
|
|
|
|
}
|
|
|
|
}
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: -
|
|
|
|
|
|
|
|
private extension ProfileEditView {
|
|
|
|
|
|
|
|
@ToolbarContentBuilder
|
|
|
|
func toolbarContent() -> some ToolbarContent {
|
2024-11-02 14:24:41 +00:00
|
|
|
ToolbarItem(placement: .confirmationAction) {
|
2024-09-23 13:02:26 +00:00
|
|
|
ProfileSaveButton(
|
2024-11-23 19:31:22 +00:00
|
|
|
title: Strings.Global.Actions.save,
|
2024-11-18 16:43:01 +00:00
|
|
|
errorModuleIds: $errorModuleIds
|
2024-09-23 13:02:26 +00:00
|
|
|
) {
|
|
|
|
try await flow?.onCommitEditing()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ToolbarItem(placement: .cancellationAction) {
|
2024-11-23 19:31:22 +00:00
|
|
|
Button(Strings.Global.Actions.cancel, role: .cancel) {
|
2024-09-23 13:02:26 +00:00
|
|
|
flow?.onCancelEditing()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-05 09:03:54 +00:00
|
|
|
var modulesSection: some View {
|
|
|
|
Group {
|
|
|
|
ForEach(profileEditor.modules, id: \.id, content: moduleRow)
|
|
|
|
.onMove(perform: moveModules)
|
|
|
|
.onDelete(perform: removeModules)
|
|
|
|
|
|
|
|
addModuleButton
|
|
|
|
}
|
|
|
|
.themeSection(
|
2024-11-23 19:31:22 +00:00
|
|
|
header: Strings.Global.Nouns.modules,
|
2024-11-05 09:03:54 +00:00
|
|
|
footer: Strings.Views.Profile.ModuleList.Section.footer
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-10-06 11:41:02 +00:00
|
|
|
func moduleRow(for module: any ModuleBuilder) -> some View {
|
2024-09-23 13:02:26 +00:00
|
|
|
EditorModuleToggle(profileEditor: profileEditor, module: module) {
|
|
|
|
Button {
|
|
|
|
push(.moduleDetail(moduleId: module.id))
|
|
|
|
} label: {
|
|
|
|
HStack {
|
|
|
|
Text(module.description(inEditor: profileEditor))
|
2024-11-18 16:43:01 +00:00
|
|
|
if errorModuleIds.contains(module.id) {
|
|
|
|
ThemeImage(.warning)
|
|
|
|
} else if profileEditor.isActiveModule(withId: module.id) {
|
|
|
|
PurchaseRequiredButton(
|
|
|
|
for: module as? AppFeatureRequiring,
|
|
|
|
paywallReason: $paywallReason
|
|
|
|
)
|
|
|
|
}
|
2024-09-23 13:02:26 +00:00
|
|
|
Spacer()
|
|
|
|
}
|
|
|
|
.contentShape(.rect)
|
|
|
|
}
|
2024-11-18 16:43:01 +00:00
|
|
|
.buttonStyle(.plain)
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var addModuleButton: some View {
|
|
|
|
let moduleTypes = profileEditor.availableModuleTypes.sorted {
|
2024-11-09 14:20:59 +00:00
|
|
|
$0.localizedDescription.lowercased() < $1.localizedDescription.lowercased()
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
return Menu {
|
|
|
|
ForEach(moduleTypes) { selectedType in
|
|
|
|
Button(selectedType.localizedDescription) {
|
|
|
|
flow?.onNewModule(selectedType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} label: {
|
|
|
|
Text(Strings.Views.Profile.Rows.addModule)
|
|
|
|
}
|
|
|
|
.disabled(moduleTypes.isEmpty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension ProfileEditView {
|
|
|
|
func moveModules(from offsets: IndexSet, to newOffset: Int) {
|
|
|
|
profileEditor.moveModules(from: offsets, to: newOffset)
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeModules(at offsets: IndexSet) {
|
|
|
|
profileEditor.removeModules(at: offsets)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Destinations
|
|
|
|
|
|
|
|
private extension ProfileEditView {
|
|
|
|
enum NavigationRoute: Hashable {
|
|
|
|
case moduleDetail(moduleId: UUID)
|
|
|
|
}
|
|
|
|
|
|
|
|
@ViewBuilder
|
|
|
|
func pushDestination(for item: NavigationRoute) -> some View {
|
|
|
|
switch item {
|
|
|
|
case .moduleDetail(let moduleId):
|
|
|
|
ModuleDetailView(
|
|
|
|
profileEditor: profileEditor,
|
|
|
|
moduleId: moduleId,
|
|
|
|
moduleViewFactory: moduleViewFactory
|
|
|
|
)
|
2024-10-23 13:46:00 +00:00
|
|
|
.environment(\.navigationPath, $path)
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func push(_ item: NavigationRoute) {
|
|
|
|
path.append(item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#Preview {
|
|
|
|
NavigationStack {
|
|
|
|
ProfileEditView(
|
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 14:51:52 +00:00
|
|
|
moduleViewFactory: DefaultModuleViewFactory(registry: Registry()),
|
2024-11-18 16:43:01 +00:00
|
|
|
path: .constant(NavigationPath()),
|
|
|
|
paywallReason: .constant(nil)
|
2024-09-23 13:02:26 +00:00
|
|
|
)
|
|
|
|
}
|
2024-10-02 14:05:40 +00:00
|
|
|
.withMockEnvironment()
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|