@@ -17,11 +17,12 @@ struct UploaderSettingsView: View {
17
17
18
18
@State private var isAddSheetPresented = false
19
19
@State private var isImportSheetPresented = false
20
-
20
+ @State private var editingUploader : CustomUploader ?
21
+
21
22
var body : some View {
22
23
VStack {
23
24
if let uploaders = savedCustomUploaders {
24
- if ( uploaders. isEmpty) {
25
+ if uploaders. isEmpty {
25
26
HStack ( alignment: . center) {
26
27
VStack {
27
28
Text ( " You have no saved uploaders " )
@@ -30,69 +31,71 @@ struct UploaderSettingsView: View {
30
31
. foregroundColor ( . secondary)
31
32
}
32
33
}
33
- }
34
-
35
- ForEach ( uploaders. sorted ( by: { $0. name < $1. name } ) , id: \. self) { uploader in
36
- HStack {
37
- Text ( uploader. name)
38
- . font ( . subheadline)
39
-
40
- Spacer ( )
41
-
42
- Button ( action: {
43
- deleteCustomUploader ( uploader)
44
- } ) {
45
- Image ( systemName: " trash " )
46
- . resizable ( )
47
- . aspectRatio ( contentMode: /*@START_MENU_TOKEN@*/. fill/*@END_MENU_TOKEN@*/)
48
- . frame ( width: 12 , height: 12 )
49
- }
50
- . buttonStyle ( BorderlessButtonStyle ( ) )
51
- . help ( " Delete Uploader " )
52
-
53
- Button ( action: {
54
- testCustomUploader ( uploader)
55
- } ) {
56
- Image ( systemName: " icloud.and.arrow.up " )
57
- . resizable ( )
58
- . aspectRatio ( contentMode: /*@START_MENU_TOKEN@*/. fill/*@END_MENU_TOKEN@*/)
59
- . frame ( width: 12 , height: 12 )
60
- }
61
- . buttonStyle ( BorderlessButtonStyle ( ) )
62
- . help ( " Test Uploader " )
63
-
64
- Button ( action: {
65
- editCustomUploader ( uploader) // Edit button added
66
- } ) {
67
- Image ( systemName: " pencil " )
68
- . resizable ( )
69
- . aspectRatio ( contentMode: /*@START_MENU_TOKEN@*/. fill/*@END_MENU_TOKEN@*/)
70
- . frame ( width: 12 , height: 12 )
71
- }
72
- . buttonStyle ( BorderlessButtonStyle ( ) )
73
- . help ( " Edit Uploader " )
74
-
75
- Button ( action: {
76
- exportUploader ( uploader) // Export button added
77
- } ) {
78
- Image ( systemName: " square.and.arrow.up " )
79
- . resizable ( )
80
- . aspectRatio ( contentMode: /*@START_MENU_TOKEN@*/. fill/*@END_MENU_TOKEN@*/)
81
- . frame ( width: 12 , height: 12 )
34
+ } else {
35
+ ForEach ( uploaders. sorted ( by: { $0. name < $1. name } ) , id: \. self) { uploader in
36
+ HStack {
37
+ Text ( uploader. name)
38
+ . font ( . subheadline)
39
+
40
+ Spacer ( )
41
+
42
+ Button ( action: {
43
+ deleteCustomUploader ( uploader)
44
+ } ) {
45
+ Image ( systemName: " trash " )
46
+ . resizable ( )
47
+ . aspectRatio ( contentMode: . fill)
48
+ . frame ( width: 12 , height: 12 )
49
+ }
50
+ . buttonStyle ( BorderlessButtonStyle ( ) )
51
+ . help ( " Delete Uploader " )
52
+
53
+ Button ( action: {
54
+ testCustomUploader ( uploader)
55
+ } ) {
56
+ Image ( systemName: " icloud.and.arrow.up " )
57
+ . resizable ( )
58
+ . aspectRatio ( contentMode: . fill)
59
+ . frame ( width: 12 , height: 12 )
60
+ }
61
+ . buttonStyle ( BorderlessButtonStyle ( ) )
62
+ . help ( " Test Uploader " )
63
+
64
+ Button ( action: {
65
+ editingUploader = uploader
66
+ isAddSheetPresented. toggle ( )
67
+ } ) {
68
+ Image ( systemName: " pencil " )
69
+ . resizable ( )
70
+ . aspectRatio ( contentMode: . fill)
71
+ . frame ( width: 12 , height: 12 )
72
+ }
73
+ . buttonStyle ( BorderlessButtonStyle ( ) )
74
+ . help ( " Edit Uploader " )
75
+
76
+ Button ( action: {
77
+ exportUploader ( uploader)
78
+ } ) {
79
+ Image ( systemName: " square.and.arrow.up " )
80
+ . resizable ( )
81
+ . aspectRatio ( contentMode: . fill)
82
+ . frame ( width: 12 , height: 12 )
83
+ }
84
+ . buttonStyle ( BorderlessButtonStyle ( ) )
85
+ . help ( " Export Uploader " )
82
86
}
83
- . buttonStyle ( BorderlessButtonStyle ( ) )
84
- . help ( " Export Uploader " )
85
- }
86
- . padding ( . horizontal)
87
- . padding ( . vertical, 4 )
88
- } . padding ( . top)
87
+ . padding ( . horizontal)
88
+ . padding ( . vertical, 4 )
89
+ } . padding ( . top)
90
+ }
89
91
}
90
92
91
93
Divider ( ) . padding ( . horizontal)
92
94
Spacer ( )
93
95
94
96
HStack {
95
97
Button ( action: {
98
+ editingUploader = nil
96
99
isAddSheetPresented. toggle ( )
97
100
} ) {
98
101
Text ( " Create " )
@@ -118,7 +121,7 @@ struct UploaderSettingsView: View {
118
121
}
119
122
. rotationEffect ( aussieMode ? . degrees( 180 ) : . zero)
120
123
. sheet ( isPresented: $isAddSheetPresented) {
121
- AddCustomUploaderView ( )
124
+ AddCustomUploaderView ( uploader : $editingUploader )
122
125
. frame ( minWidth: 450 )
123
126
}
124
127
. sheet ( isPresented: $isImportSheetPresented) {
@@ -201,10 +204,6 @@ struct UploaderSettingsView: View {
201
204
uploadType = . IMGUR
202
205
}
203
206
204
- private func editCustomUploader( _ uploader: CustomUploader ) {
205
- isAddSheetPresented. toggle ( )
206
- }
207
-
208
207
private func exportUploader( _ uploader: CustomUploader ) {
209
208
let data = try ! JSONEncoder ( ) . encode ( uploader)
210
209
let savePanel = NSSavePanel ( )
@@ -226,18 +225,19 @@ struct UploaderSettingsView: View {
226
225
struct AddCustomUploaderView : View {
227
226
@Environment ( \. presentationMode) var presentationMode
228
227
@Default ( . savedCustomUploaders) var savedCustomUploaders
229
-
230
- @State private var uploaderName = " "
231
- @State private var requestURL = " "
232
- @State private var responseURL = " "
233
- @State private var deletionURL = " "
234
- @State private var fileFormName = " "
228
+ @Binding var uploader : CustomUploader ?
229
+
230
+ @State private var uploaderName : String = " "
231
+ @State private var requestURL : String = " "
232
+ @State private var responseURL : String = " "
233
+ @State private var deletionURL : String = " "
234
+ @State private var fileFormName : String = " "
235
235
@State private var header : [ CustomEntryModel ] = [ ]
236
236
@State private var formData : [ CustomEntryModel ] = [ ]
237
-
237
+
238
238
var body : some View {
239
239
ScrollView {
240
- Text ( " Create Custom Uploader " )
240
+ Text ( uploader == nil ? " Create Custom Uploader " : " Edit Custom Uploader" )
241
241
. font ( . title)
242
242
. padding ( )
243
243
Divider ( ) . padding ( . horizontal)
@@ -274,6 +274,17 @@ struct AddCustomUploaderView: View {
274
274
}
275
275
. padding ( )
276
276
}
277
+ . onAppear {
278
+ if let uploader = uploader {
279
+ uploaderName = uploader. name
280
+ requestURL = uploader. requestURL
281
+ responseURL = uploader. responseURL
282
+ deletionURL = uploader. deletionURL ?? " "
283
+ fileFormName = uploader. fileFormName ?? " "
284
+ header = uploader. headers? . map { CustomEntryModel ( key: $0. key, value: $0. value) } ?? [ ]
285
+ formData = uploader. formData? . map { CustomEntryModel ( key: $0. key, value: $0. value) } ?? [ ]
286
+ }
287
+ }
277
288
}
278
289
279
290
private struct InputField : View {
@@ -288,7 +299,6 @@ struct AddCustomUploaderView: View {
288
299
}
289
300
}
290
301
291
-
292
302
private func HeaderView( ) -> some View {
293
303
EntryListView ( title: " Headers " , entries: $header)
294
304
}
@@ -313,13 +323,13 @@ struct AddCustomUploaderView: View {
313
323
}
314
324
} ) {
315
325
ForEach ( entries) { entry in
316
- if ( entry == entries. last) {
326
+ if entry == entries. last {
317
327
CustomEntryView ( entry: $entries [ entries. firstIndex ( of: entry) !] )
318
328
. padding ( . horizontal)
319
329
}
320
330
}
321
331
322
- if ( !entries. isEmpty) {
332
+ if !entries. isEmpty {
323
333
Divider ( )
324
334
HStack {
325
335
VStack ( alignment: . leading) {
@@ -332,7 +342,7 @@ struct AddCustomUploaderView: View {
332
342
Button ( action: { } ) {
333
343
Image ( systemName: " minus.circle " )
334
344
} . opacity ( 0 )
335
- . disabled ( /*@START_MENU_TOKEN@*/ true /*@END_MENU_TOKEN@*/ )
345
+ . disabled ( true )
336
346
}
337
347
. frame ( maxWidth: . infinity)
338
348
Divider ( )
@@ -382,23 +392,22 @@ struct AddCustomUploaderView: View {
382
392
}
383
393
}
384
394
385
- let uploader = CustomUploader (
395
+ let newUploader = CustomUploader (
386
396
name: uploaderName,
387
397
requestURL: requestURL,
388
- headers: header. count == 0 ? nil : headerData,
389
- formData: formData. count == 0 ? nil : formDataModel,
398
+ headers: header. isEmpty ? nil : headerData,
399
+ formData: formData. isEmpty ? nil : formDataModel,
390
400
fileFormName: fileFormName. isEmpty ? nil : fileFormName,
391
401
responseURL: responseURL,
392
402
deletionURL: deletionURL. isEmpty ? nil : deletionURL
393
-
394
403
)
395
404
396
405
if var uploaders = savedCustomUploaders {
397
- uploaders. remove ( uploader )
398
- uploaders. insert ( uploader )
406
+ uploaders. remove ( newUploader )
407
+ uploaders. insert ( newUploader )
399
408
savedCustomUploaders = uploaders
400
409
} else {
401
- savedCustomUploaders = Set ( [ uploader ] )
410
+ savedCustomUploaders = Set ( [ newUploader ] )
402
411
}
403
412
404
413
presentationMode. wrappedValue. dismiss ( )
0 commit comments