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
|
// Copyright (C) 2024 Rubén Beltrán del Río
// This program 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.
// This program 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 this program. If not, see https://map.tranquil.systems.
import SwiftUI
struct StagesPreferencesView: View {
@AppStorage("customStages") private var customStagesData: Data = Data()
@State private var customStages: [CustomStage] = []
var body: some View {
VStack(alignment: .leading, spacing: Dimensions.Spacing.regular) {
Text("preferences.stages.title")
.font(.Theme.Title.emphasized)
.lineSpacing(Dimensions.LineHeight.title - Dimensions.FontSize.title)
Text("preferences.stages.explanation")
.font(.Theme.Body.regular)
.kerning(Dimensions.Kerning.body)
.lineSpacing(Dimensions.LineHeight.body - Dimensions.FontSize.body)
.padding(.top, Dimensions.Spacing.loose)
.padding(.bottom, Dimensions.Spacing.regular)
Table(customStages) {
TableColumn("Name") { customStage in
TextField(
"Stage Name",
text: Binding(
get: { customStage.name },
set: { newValue in
updateStageName(customStage.id, newValue)
}
)
)
.textFieldStyle(.plain)
}
.width(120)
TableColumn("Stage I") { customStage in
TextField(
"Stage I",
text: Binding(
get: { customStage.stage.i },
set: { newValue in
updateStageValue(customStage.id, \.i, newValue)
}
)
)
.textFieldStyle(.plain)
}
TableColumn("Stage II") { customStage in
TextField(
"Stage II",
text: Binding(
get: { customStage.stage.ii },
set: { newValue in
updateStageValue(customStage.id, \.ii, newValue)
}
)
)
.textFieldStyle(.plain)
}
TableColumn("Stage III") { customStage in
TextField(
"Stage III",
text: Binding(
get: { customStage.stage.iii },
set: { newValue in
updateStageValue(customStage.id, \.iii, newValue)
}
)
)
.textFieldStyle(.plain)
}
TableColumn("Stage IV") { customStage in
TextField(
"Stage IV",
text: Binding(
get: { customStage.stage.iv },
set: { newValue in
updateStageValue(customStage.id, \.iv, newValue)
}
)
)
.textFieldStyle(.plain)
}
TableColumn("") { customStage in
Button(action: { removeStage(customStage.id) }) {
Image(systemName: "minus")
}
.buttonStyle(.borderless)
}
.width(30)
}
.tableStyle(.bordered(alternatesRowBackgrounds: true))
HStack {
Spacer()
Button(action: addStage) {
Image(systemName: "plus")
}
.buttonStyle(.borderless)
}
}
.padding(.vertical, Dimensions.Spacing.loose)
.padding(.horizontal, Dimensions.Spacing.indulgent)
.onAppear {
loadStages()
}
}
private func loadStages() {
if let decoded = try? JSONDecoder().decode([CustomStage].self, from: customStagesData) {
customStages = decoded
} else {
// First use - create default Cynefin stage
customStages = [CustomStage.cynefinDefault()]
saveStages() // Save the default to UserDefaults
}
}
private func saveStages() {
if let encoded = try? JSONEncoder().encode(customStages) {
customStagesData = encoded
}
}
private func addStage() {
customStages.append(CustomStage.placeholderDefault())
saveStages()
}
private func removeStage(_ id: UUID) {
customStages.removeAll { $0.id == id }
saveStages()
}
private func updateStageName(_ id: UUID, _ newName: String) {
if let index = customStages.firstIndex(where: { $0.id == id }) {
customStages[index].name = newName
saveStages()
}
}
private func updateStageValue(
_ id: UUID, _ keyPath: WritableKeyPath<Stage, String>, _ value: String
) {
if let index = customStages.firstIndex(where: { $0.id == id }) {
customStages[index].stage[keyPath: keyPath] = value
saveStages()
}
}
}
#Preview {
StagesPreferencesView()
}
|