-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathProfileEditView+iOS.swift
198 lines (171 loc) · 5.63 KB
/
ProfileEditView+iOS.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//
// ProfileEditView+iOS.swift
// Passepartout
//
// Created by Davide De Rosa on 6/22/24.
// Copyright (c) 2025 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)
import CommonLibrary
import CommonUtils
import PassepartoutKit
import SwiftUI
struct ProfileEditView: View, Routable {
let profileManager: ProfileManager
@ObservedObject
var profileEditor: ProfileEditor
let initialModuleId: UUID?
let moduleViewFactory: any ModuleViewFactory
@Binding
var path: NavigationPath
@Binding
var paywallReason: PaywallReason?
var flow: ProfileCoordinator.Flow?
@State
private var errorModuleIds: Set<UUID> = []
var body: some View {
debugChanges()
return List {
ProfileNameSection(
name: $profileEditor.profile.name
)
modulesSection
ProfileStorageSection(
profileEditor: profileEditor,
paywallReason: $paywallReason
)
ProfileBehaviorSection(profileEditor: profileEditor)
ProfileActionsSection(
profileManager: profileManager,
profileEditor: profileEditor
)
}
.toolbar(content: toolbarContent)
.navigationTitle(Strings.Global.Nouns.profile)
.navigationBarBackButtonHidden(true)
.navigationDestination(for: NavigationRoute.self, destination: pushDestination)
.onLoad {
if let initialModuleId {
push(.moduleDetail(moduleId: initialModuleId))
}
}
}
}
// MARK: -
private extension ProfileEditView {
@ToolbarContentBuilder
func toolbarContent() -> some ToolbarContent {
ToolbarItem(placement: .confirmationAction) {
ProfileSaveButton(
title: Strings.Global.Actions.save,
errorModuleIds: $errorModuleIds
) {
try await flow?.onCommitEditing()
}
}
ToolbarItem(placement: .cancellationAction) {
Button(Strings.Global.Actions.cancel, role: .cancel) {
flow?.onCancelEditing()
}
.uiAccessibility(.Profile.cancel)
}
}
var modulesSection: some View {
Group {
ForEach(profileEditor.modules, id: \.id, content: moduleRow)
.onMove(perform: moveModules)
.onDelete(perform: removeModules)
addModuleMenu
}
.themeSection(
header: Strings.Global.Nouns.modules,
footer: Strings.Views.Profile.ModuleList.Section.footer
)
.themeTip(.Profile.buildYourProfile)
}
func moduleRow(for module: any ModuleBuilder) -> some View {
EditorModuleToggle(profileEditor: profileEditor, module: module) {
HStack {
NavigatingButton(module.description(inEditor: profileEditor)) {
push(.moduleDetail(moduleId: module.id))
}
.uiAccessibility(.Profile.moduleLink)
if errorModuleIds.contains(module.id) {
ThemeImage(.warning)
} else if profileEditor.isActiveModule(withId: module.id) {
PurchaseRequiredView(
for: module as? AppFeatureRequiring,
reason: $paywallReason
)
}
}
}
}
var addModuleMenu: some View {
AddModuleMenu(moduleTypes: profileEditor.availableModuleTypes) {
flow?.onNewModule($0)
} label: {
Text(Strings.Views.Profile.Rows.addModule)
}
}
}
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
)
.environment(\.navigationPath, $path)
}
}
func push(_ item: NavigationRoute) {
path.append(item)
}
}
#Preview {
NavigationStack {
ProfileEditView(
profileManager: .forPreviews,
profileEditor: ProfileEditor(profile: .newMockProfile()),
initialModuleId: nil,
moduleViewFactory: DefaultModuleViewFactory(registry: Registry()),
path: .constant(NavigationPath()),
paywallReason: .constant(nil)
)
}
.withMockEnvironment()
}
#endif