-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlistEditorView.swift
233 lines (222 loc) · 8.22 KB
/
PlistEditorView.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
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
//
// PlistEditorView.swift
//
// Created by Speedyfriend67 on 27.06.24
//
import SwiftUI
struct PlistEditorView: View {
@State private var plistContent: [String: Any] = [:]
@State private var isLoading: Bool = true
@State private var showError: Bool = false
let fileURL: URL
var body: some View {
VStack {
if isLoading {
ProgressView("Loading...")
.padding()
} else if showError {
Text("Failed to load file.")
.foregroundColor(.red)
} else {
List {
ForEach(plistContent.keys.sorted(), id: \.self) { key in
HStack {
Text(key)
.font(.headline)
Spacer()
valueView(for: key, value: plistContent[key]!)
}
}
}
.listStyle(InsetGroupedListStyle())
.navigationBarItems(trailing: Button(action: saveFile) {
Text("Save")
})
}
}
.onAppear(perform: loadFile)
.navigationTitle(fileURL.lastPathComponent)
.navigationBarTitleDisplayMode(.inline)
}
private func loadFile() {
print("Loading file at URL: \(fileURL)")
DispatchQueue.global(qos: .userInitiated).async {
do {
let data = try Data(contentsOf: fileURL)
if let plist = try PropertyListSerialization.propertyList(from: data, options: [], format: nil) as? [String: Any] {
DispatchQueue.main.async {
self.plistContent = plist
self.isLoading = false
}
} else {
DispatchQueue.main.async {
self.isLoading = false
self.showError = true
}
}
} catch {
DispatchQueue.main.async {
self.isLoading = false
self.showError = true
print("Failed to load file: \(error.localizedDescription)")
}
}
}
}
private func saveFile() {
DispatchQueue.global(qos: .userInitiated).async {
do {
let data = try PropertyListSerialization.data(fromPropertyList: self.plistContent, format: .xml, options: 0)
try data.write(to: self.fileURL)
DispatchQueue.main.async {
// Provide feedback to the user if needed
}
} catch {
DispatchQueue.main.async {
self.showError = true
}
}
}
}
@ViewBuilder
private func valueView(for key: String, value: Any) -> some View {
if let stringValue = value as? String {
TextField("", text: Binding(
get: { stringValue },
set: { plistContent[key] = $0 }
))
.textFieldStyle(RoundedBorderTextFieldStyle())
} else if let numberValue = value as? NSNumber {
TextField("", value: Binding(
get: { numberValue },
set: { plistContent[key] = $0 }
), formatter: NumberFormatter())
.textFieldStyle(RoundedBorderTextFieldStyle())
} else if let boolValue = value as? Bool {
Toggle("", isOn: Binding(
get: { boolValue },
set: { plistContent[key] = $0 }
))
} else if let arrayValue = value as? [Any] {
NavigationLink(destination: PlistArrayView(array: Binding(
get: { arrayValue },
set: { plistContent[key] = $0 }
))) {
Text("Array (\(arrayValue.count))")
}
} else if let dictValue = value as? [String: Any] {
NavigationLink(destination: NestedPlistEditorView(plistContent: Binding(
get: { dictValue },
set: { plistContent[key] = $0 }
))) {
Text("Dictionary (\(dictValue.count))")
}
} else {
EmptyView()
}
}
}
struct PlistArrayView: View {
@Binding var array: [Any]
var body: some View {
List {
ForEach(array.indices, id: \.self) { index in
if let stringValue = array[index] as? String {
TextField("", text: Binding(
get: { stringValue },
set: { array[index] = $0 }
))
.textFieldStyle(RoundedBorderTextFieldStyle())
} else if let numberValue = array[index] as? NSNumber {
TextField("", value: Binding(
get: { numberValue },
set: { array[index] = $0 }
), formatter: NumberFormatter())
.textFieldStyle(RoundedBorderTextFieldStyle())
} else if let boolValue = array[index] as? Bool {
Toggle("", isOn: Binding(
get: { boolValue },
set: { array[index] = $0 }
))
} else if let arrayValue = array[index] as? [Any] {
NavigationLink(destination: PlistArrayView(array: Binding(
get: { arrayValue },
set: { array[index] = $0 }
))) {
Text("Array (\(arrayValue.count))")
}
} else if let dictValue = array[index] as? [String: Any] {
NavigationLink(destination: NestedPlistEditorView(plistContent: Binding(
get: { dictValue },
set: { array[index] = $0 }
))) {
Text("Dictionary (\(dictValue.count))")
}
}
}
}
.listStyle(InsetGroupedListStyle())
.navigationTitle("Array")
.navigationBarTitleDisplayMode(.inline)
}
}
struct NestedPlistEditorView: View {
@Binding var plistContent: [String: Any]
var body: some View {
List {
ForEach(plistContent.keys.sorted(), id: \.self) { key in
HStack {
Text(key)
.font(.headline)
Spacer()
valueView(for: key, value: plistContent[key]!)
}
}
}
.listStyle(InsetGroupedListStyle())
.navigationTitle("Dictionary")
.navigationBarTitleDisplayMode(.inline)
}
@ViewBuilder
private func valueView(for key: String, value: Any) -> some View {
if let stringValue = value as? String {
TextField("", text: Binding(
get: { stringValue },
set: { plistContent[key] = $0 }
))
.textFieldStyle(RoundedBorderTextFieldStyle())
} else if let numberValue = value as? NSNumber {
TextField("", value: Binding(
get: { numberValue },
set: { plistContent[key] = $0 }
), formatter: NumberFormatter())
.textFieldStyle(RoundedBorderTextFieldStyle())
} else if let boolValue = value as? Bool {
Toggle("", isOn: Binding(
get: { boolValue },
set: { plistContent[key] = $0 }
))
} else if let arrayValue = value as? [Any] {
NavigationLink(destination: PlistArrayView(array: Binding(
get: { arrayValue },
set: { plistContent[key] = $0 }
))) {
Text("Array (\(arrayValue.count))")
}
} else if let dictValue = value as? [String: Any] {
NavigationLink(destination: NestedPlistEditorView(plistContent: Binding(
get: { dictValue },
set: { plistContent[key] = $0 }
))) {
Text("Dictionary (\(dictValue.count))")
}
} else {
EmptyView()
}
}
}
struct PlistEditorView_Previews: PreviewProvider {
static var previews: some View {
PlistEditorView(fileURL: URL(fileURLWithPath: "/var/tmp/sample.plist"))
}
}