@@ -14,6 +14,12 @@ protocol FileListDelegate: AnyObject {
14
14
func deleteFiles( fileIDs: [ Int ] , onSuccess: ( ( ) -> Void ) ? ) async -> Bool
15
15
@MainActor
16
16
func renameFile( fileID: Int , newName: String , onSuccess: ( ( ) -> Void ) ? ) async -> Bool
17
+ @MainActor
18
+ func setFilePassword( fileID: Int , password: String , onSuccess: ( ( ) -> Void ) ? ) async -> Bool
19
+ @MainActor
20
+ func setFilePrivate( fileID: Int , isPrivate: Bool , onSuccess: ( ( ) -> Void ) ? ) async -> Bool
21
+ @MainActor
22
+ func setFileExpiration( fileID: Int , expr: String , onSuccess: ( ( ) -> Void ) ? ) async -> Bool
17
23
}
18
24
19
25
@MainActor
@@ -89,6 +95,69 @@ class FileListManager: ObservableObject, FileListDelegate {
89
95
}
90
96
return status
91
97
}
98
+
99
+ func setFilePassword( fileID: Int , password: String , onSuccess: ( ( ) -> Void ) ? ) async -> Bool {
100
+ guard let serverInstance = server. wrappedValue,
101
+ let url = URL ( string: serverInstance. url) else {
102
+ return false
103
+ }
104
+
105
+ let api = DFAPI ( url: url, token: serverInstance. token)
106
+ let status = await api. editFiles ( fileIDs: [ fileID] , changes: [ " password " : password] , selectedServer: serverInstance)
107
+ if status {
108
+ withAnimation {
109
+ if let index = files. firstIndex ( where: { $0. id == fileID } ) {
110
+ var updatedFiles = files
111
+ updatedFiles [ index] . password = password
112
+ files = updatedFiles
113
+ }
114
+ onSuccess ? ( )
115
+ }
116
+ }
117
+ return status
118
+ }
119
+
120
+ func setFilePrivate( fileID: Int , isPrivate: Bool , onSuccess: ( ( ) -> Void ) ? ) async -> Bool {
121
+ guard let serverInstance = server. wrappedValue,
122
+ let url = URL ( string: serverInstance. url) else {
123
+ return false
124
+ }
125
+
126
+ let api = DFAPI ( url: url, token: serverInstance. token)
127
+ let status = await api. editFiles ( fileIDs: [ fileID] , changes: [ " private " : isPrivate] , selectedServer: serverInstance)
128
+ if status {
129
+ withAnimation {
130
+ if let index = files. firstIndex ( where: { $0. id == fileID } ) {
131
+ var updatedFiles = files
132
+ updatedFiles [ index] . private = isPrivate
133
+ files = updatedFiles
134
+ }
135
+ onSuccess ? ( )
136
+ }
137
+ }
138
+ return status
139
+ }
140
+
141
+ func setFileExpiration( fileID: Int , expr: String , onSuccess: ( ( ) -> Void ) ? ) async -> Bool {
142
+ guard let serverInstance = server. wrappedValue,
143
+ let url = URL ( string: serverInstance. url) else {
144
+ return false
145
+ }
146
+
147
+ let api = DFAPI ( url: url, token: serverInstance. token)
148
+ let status = await api. editFiles ( fileIDs: [ fileID] , changes: [ " expr " : expr] , selectedServer: serverInstance)
149
+ if status {
150
+ withAnimation {
151
+ if let index = files. firstIndex ( where: { $0. id == fileID } ) {
152
+ var updatedFiles = files
153
+ updatedFiles [ index] . expr = expr
154
+ files = updatedFiles
155
+ }
156
+ onSuccess ? ( )
157
+ }
158
+ }
159
+ return status
160
+ }
92
161
}
93
162
94
163
struct CustomLabel : LabelStyle {
@@ -104,9 +173,9 @@ struct CustomLabel: LabelStyle {
104
173
105
174
struct FileRowView : View {
106
175
@Binding var file : DFFile
107
- @ State var isPrivate : Bool
108
- @ State var hasPassword : Bool
109
- @ State var hasExpiration : Bool
176
+ var isPrivate : Bool { file . private }
177
+ var hasPassword : Bool { file . password != " " }
178
+ var hasExpiration : Bool { file . expr != " " }
110
179
let serverURL : URL
111
180
112
181
private func getIcon( ) -> String {
@@ -303,9 +372,6 @@ struct FileListView: View {
303
372
if files [ index] . mime. starts ( with: " image/ " ) {
304
373
FileRowView (
305
374
file: $fileListManager. files [ index] ,
306
- isPrivate: files [ index] . private,
307
- hasPassword: ( files [ index] . password != " " ) ,
308
- hasExpiration: ( files [ index] . expr != " " ) ,
309
375
serverURL: URL ( string: server. wrappedValue!. url) !
310
376
)
311
377
. contextMenu {
@@ -324,9 +390,6 @@ struct FileListView: View {
324
390
} else {
325
391
FileRowView (
326
392
file: $fileListManager. files [ index] ,
327
- isPrivate: files [ index] . private,
328
- hasPassword: ( files [ index] . password != " " ) ,
329
- hasExpiration: ( files [ index] . expr != " " ) ,
330
393
serverURL: URL ( string: server. wrappedValue!. url) !
331
394
)
332
395
. contextMenu {
@@ -453,7 +516,7 @@ struct FileListView: View {
453
516
if let file = fileToExpire {
454
517
let expirationValue = expirationText
455
518
Task {
456
- await setFileExpr ( file: file, expr: expirationValue)
519
+ await setFileExpiration ( file: file, expr: expirationValue)
457
520
await MainActor . run {
458
521
expirationText = " "
459
522
fileToExpire = nil
@@ -748,37 +811,17 @@ struct FileListView: View {
748
811
749
812
@MainActor
750
813
private func toggleFilePrivacy( file: DFFile ) async {
751
- guard let serverInstance = server. wrappedValue,
752
- let url = URL ( string: serverInstance. url) else {
753
- return
754
- }
755
- let api = DFAPI ( url: url, token: serverInstance. token)
756
- // Toggle the private status (if currently private, make it public and vice versa)
757
- let _ = await api. editFiles ( fileIDs: [ file. id] , changes: [ " private " : !file. private] , selectedServer: serverInstance)
758
- await refreshFiles ( ) // TODO: update local data instead of refresh
814
+ let _ = await fileListManager. setFilePrivate ( fileID: file. id, isPrivate: !file. private, onSuccess: nil )
759
815
}
760
816
761
817
@MainActor
762
- private func setFileExpr( file: DFFile , expr: String ? ) async {
763
- guard let serverInstance = server. wrappedValue,
764
- let url = URL ( string: serverInstance. url) else {
765
- return
766
- }
767
-
768
- let api = DFAPI ( url: url, token: serverInstance. token)
769
- let _ = await api. editFiles ( fileIDs: [ file. id] , changes: [ " expr " : expr ?? " " ] , selectedServer: serverInstance)
770
- await refreshFiles ( ) // TODO: update local data instead of refresh
818
+ private func setFileExpiration( file: DFFile , expr: String ) async {
819
+ let _ = await fileListManager. setFileExpiration ( fileID: file. id, expr: expr, onSuccess: nil )
771
820
}
772
821
773
822
@MainActor
774
- private func setFilePassword( file: DFFile , password: String ? ) async {
775
- guard let serverInstance = server. wrappedValue,
776
- let url = URL ( string: serverInstance. url) else {
777
- return
778
- }
779
- let api = DFAPI ( url: url, token: serverInstance. token)
780
- let _ = await api. editFiles ( fileIDs: [ file. id] , changes: [ " password " : password ?? " " ] , selectedServer: serverInstance)
781
- await refreshFiles ( ) // TODO: update local data instead of refresh
823
+ private func setFilePassword( file: DFFile , password: String ) async {
824
+ let _ = await fileListManager. setFilePassword ( fileID: file. id, password: password, onSuccess: nil )
782
825
}
783
826
784
827
@MainActor
0 commit comments