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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
// 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 Foundation
// MARK: - JSON Export/Import Structures
struct UserPreferencesJSON: Codable {
// MARK: - Map Preferences
var showMapBackground: Bool?
var useCustomFont: Bool?
var customFontName: String?
var defaultExportFormat: String?
var useSmartLabelPositioning: Bool?
// MARK: - Editor Preferences
var useSmartEditor: Bool?
var useCustomEditorFont: Bool?
var customEditorFontName: String?
var editorFontSize: Double?
var softWrapLines: Bool?
var highlightMissingComponents: Bool?
// MARK: - Templates (as actual objects)
var mapTemplates: [Template]?
// MARK: - Stages (as actual objects)
var customStages: [CustomStage]?
// MARK: - UI Preferences
var viewStyle: String?
var zoom: Double?
}
struct UserPreferences: Codable {
// MARK: - Map Preferences
var showMapBackground: Bool
var useCustomFont: Bool
var customFontName: String
var defaultExportFormat: String
var useSmartLabelPositioning: Bool
// MARK: - Editor Preferences
var useSmartEditor: Bool
var useCustomEditorFont: Bool
var customEditorFontName: String
var editorFontSize: Double
var softWrapLines: Bool
var highlightMissingComponents: Bool
// MARK: - Templates
var mapTemplates: Data
// MARK: - Stages
var customStages: Data
// MARK: - UI Preferences
var viewStyle: String
var zoom: Double
// MARK: - Default Values
struct Defaults {
static let showMapBackground = true
static let useCustomFont = false
static let customFontName = "Helvetica"
static let defaultExportFormat = "png"
static let useSmartLabelPositioning = true
static let useSmartEditor = true
static let useCustomEditorFont = false
static let customEditorFontName = "Menlo"
static let editorFontSize = 14.0
static let softWrapLines = false
static let highlightMissingComponents = true
static let viewStyle = "horizontal"
static let zoom = 1.0
// Computed properties for complex defaults
static var mapTemplates: Data {
let defaultTemplate = Template(
name: String(localized: "map_template.default.title"),
content: String(localized: "map_template.default.content"),
isDefault: true
)
let emptyTemplate = Template(
name: String(localized: "map_template.empty.title"),
content: "",
isDefault: false
)
let templates = [defaultTemplate, emptyTemplate]
return (try? JSONEncoder().encode(templates)) ?? Data()
}
static var customStages: Data {
let defaultStage = CustomStage.cynefinDefault()
let stages = [defaultStage]
return (try? JSONEncoder().encode(stages)) ?? Data()
}
}
// MARK: - Initialization
init() {
self.showMapBackground = Defaults.showMapBackground
self.useCustomFont = Defaults.useCustomFont
self.customFontName = Defaults.customFontName
self.defaultExportFormat = Defaults.defaultExportFormat
self.useSmartLabelPositioning = Defaults.useSmartLabelPositioning
self.useSmartEditor = Defaults.useSmartEditor
self.useCustomEditorFont = Defaults.useCustomEditorFont
self.customEditorFontName = Defaults.customEditorFontName
self.editorFontSize = Defaults.editorFontSize
self.softWrapLines = Defaults.softWrapLines
self.highlightMissingComponents = Defaults.highlightMissingComponents
self.mapTemplates = Defaults.mapTemplates
self.customStages = Defaults.customStages
self.viewStyle = Defaults.viewStyle
self.zoom = Defaults.zoom
}
// MARK: - JSON Methods
func toJSON() -> String? {
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
do {
// Convert to JSON-friendly format
let jsonPrefs = UserPreferencesJSON(
showMapBackground: showMapBackground,
useCustomFont: useCustomFont,
customFontName: customFontName,
defaultExportFormat: defaultExportFormat,
useSmartLabelPositioning: useSmartLabelPositioning,
useSmartEditor: useSmartEditor,
useCustomEditorFont: useCustomEditorFont,
customEditorFontName: customEditorFontName,
editorFontSize: editorFontSize,
softWrapLines: softWrapLines,
highlightMissingComponents: highlightMissingComponents,
mapTemplates: templatesFromData(mapTemplates),
customStages: stagesFromData(customStages),
viewStyle: viewStyle,
zoom: zoom
)
let data = try encoder.encode(jsonPrefs)
return String(data: data, encoding: .utf8)
} catch {
return nil
}
}
mutating func updateFromJSON(_ json: String) -> Bool {
guard let data = json.data(using: .utf8) else { return false }
do {
let decoder = JSONDecoder()
let jsonPrefs = try decoder.decode(UserPreferencesJSON.self, from: data)
// Convert from JSON format back to UserPreferences
// Only update values that are present in the JSON (partial updates)
if let value = jsonPrefs.showMapBackground {
self.showMapBackground = value
}
if let value = jsonPrefs.useCustomFont {
self.useCustomFont = value
}
if let value = jsonPrefs.customFontName {
self.customFontName = value
}
if let value = jsonPrefs.defaultExportFormat {
self.defaultExportFormat = value
}
if let value = jsonPrefs.useSmartLabelPositioning {
self.useSmartLabelPositioning = value
}
if let value = jsonPrefs.useSmartEditor {
self.useSmartEditor = value
}
if let value = jsonPrefs.useCustomEditorFont {
self.useCustomEditorFont = value
}
if let value = jsonPrefs.customEditorFontName {
self.customEditorFontName = value
}
if let value = jsonPrefs.editorFontSize {
self.editorFontSize = value
}
if let value = jsonPrefs.softWrapLines {
self.softWrapLines = value
}
if let value = jsonPrefs.highlightMissingComponents {
self.highlightMissingComponents = value
}
if let value = jsonPrefs.mapTemplates {
self.mapTemplates = dataFromTemplates(value)
}
if let value = jsonPrefs.customStages {
self.customStages = dataFromStages(value)
}
if let value = jsonPrefs.viewStyle {
self.viewStyle = value
}
if let value = jsonPrefs.zoom {
self.zoom = value
}
apply()
return true
} catch {
print("JSON import error: \(error)")
return false
}
}
// MARK: - Helper Methods for Data/Object Conversion
private func templatesFromData(_ data: Data) -> [Template] {
guard let templates = try? JSONDecoder().decode([Template].self, from: data) else {
return []
}
return templates
}
private func dataFromTemplates(_ templates: [Template]) -> Data {
guard let data = try? JSONEncoder().encode(templates) else {
return Data()
}
return data
}
private func stagesFromData(_ data: Data) -> [CustomStage] {
guard let stages = try? JSONDecoder().decode([CustomStage].self, from: data) else {
return []
}
return stages
}
private func dataFromStages(_ stages: [CustomStage]) -> Data {
guard let data = try? JSONEncoder().encode(stages) else {
return Data()
}
return data
}
// MARK: - Reset to Defaults
mutating func resetToDefaults() {
self = UserPreferences()
apply()
}
// MARK: - Apply to UserDefaults
private func apply() {
let defaults = UserDefaults.standard
// Map Preferences
defaults.set(showMapBackground, forKey: "showMapBackground")
defaults.set(useCustomFont, forKey: "useCustomFont")
defaults.set(customFontName, forKey: "customFontName")
defaults.set(defaultExportFormat, forKey: "defaultExportFormat")
defaults.set(useSmartLabelPositioning, forKey: "useSmartLabelPositioning")
// Editor Preferences
defaults.set(useSmartEditor, forKey: "useSmartEditor")
defaults.set(useCustomEditorFont, forKey: "useCustomEditorFont")
defaults.set(customEditorFontName, forKey: "customEditorFontName")
defaults.set(editorFontSize, forKey: "editorFontSize")
defaults.set(softWrapLines, forKey: "softWrapLines")
defaults.set(highlightMissingComponents, forKey: "highlightMissingComponents")
// Templates
defaults.set(mapTemplates, forKey: "mapTemplates")
// Stages
defaults.set(customStages, forKey: "customStages")
// UI Preferences
defaults.set(viewStyle, forKey: "viewStyle")
defaults.set(zoom, forKey: "zoom")
}
// MARK: - Load from UserDefaults
static func loadFromUserDefaults() -> UserPreferences {
let defaults = UserDefaults.standard
var preferences = UserPreferences()
// Map Preferences
preferences.showMapBackground =
defaults.object(forKey: "showMapBackground") as? Bool ?? Defaults.showMapBackground
preferences.useCustomFont =
defaults.object(forKey: "useCustomFont") as? Bool ?? Defaults.useCustomFont
preferences.customFontName =
defaults.object(forKey: "customFontName") as? String ?? Defaults.customFontName
preferences.defaultExportFormat =
defaults.object(forKey: "defaultExportFormat") as? String ?? Defaults.defaultExportFormat
preferences.useSmartLabelPositioning =
defaults.object(forKey: "useSmartLabelPositioning") as? Bool
?? Defaults.useSmartLabelPositioning
// Editor Preferences
preferences.useSmartEditor =
defaults.object(forKey: "useSmartEditor") as? Bool ?? Defaults.useSmartEditor
preferences.useCustomEditorFont =
defaults.object(forKey: "useCustomEditorFont") as? Bool ?? Defaults.useCustomEditorFont
preferences.customEditorFontName =
defaults.object(forKey: "customEditorFontName") as? String ?? Defaults.customEditorFontName
preferences.editorFontSize =
defaults.object(forKey: "editorFontSize") as? Double ?? Defaults.editorFontSize
preferences.softWrapLines =
defaults.object(forKey: "softWrapLines") as? Bool ?? Defaults.softWrapLines
preferences.highlightMissingComponents =
defaults.object(forKey: "highlightMissingComponents") as? Bool
?? Defaults.highlightMissingComponents
// Templates
preferences.mapTemplates =
defaults.object(forKey: "mapTemplates") as? Data ?? Defaults.mapTemplates
// Stages
preferences.customStages =
defaults.object(forKey: "customStages") as? Data ?? Defaults.customStages
// UI Preferences
preferences.viewStyle = defaults.object(forKey: "viewStyle") as? String ?? Defaults.viewStyle
preferences.zoom = defaults.object(forKey: "zoom") as? Double ?? Defaults.zoom
return preferences
}
}
|