@@ -50,13 +50,16 @@ private struct FileCell: View {
50
50
}
51
51
52
52
func onRename( ) {
53
+ focusedField = nil
54
+
53
55
Task {
54
56
do {
55
- try await App . renameFile ( url: URL ( string: item. url) !, name: newname)
57
+ try await App . renameFile (
58
+ url: URL ( string: item. url) !, name: newname)
56
59
} catch {
57
60
App . notificationManager. showErrorMessage ( error. localizedDescription)
61
+ newname = item. name. removingPercentEncoding!
58
62
}
59
- focusedField = nil
60
63
}
61
64
}
62
65
@@ -92,14 +95,30 @@ private struct FileCell: View {
92
95
93
96
if isRenaming {
94
97
HStack {
95
- TextField (
96
- item. name. removingPercentEncoding!, text: $newname,
97
- onCommit: onRename
98
- )
99
- . focused ( $focusedField, equals: . rename)
100
- . font ( . subheadline)
101
- . disableAutocorrection ( true )
102
- . autocapitalization ( . none)
98
+ TextField ( item. name. removingPercentEncoding!, text: $newname)
99
+ . font ( . subheadline)
100
+ . disableAutocorrection ( true )
101
+ . autocapitalization ( . none)
102
+ . focused ( $focusedField, equals: . rename)
103
+ . onSubmit ( onRename)
104
+ . onReceive (
105
+ NotificationCenter . default. publisher (
106
+ for: UITextField . textDidBeginEditingNotification)
107
+ ) { obj in
108
+ if let textField = obj. object as? UITextField {
109
+ textField. selectedTextRange = textField. textRange (
110
+ from: textField. beginningOfDocument,
111
+ to: textField. endOfDocument
112
+ )
113
+ }
114
+ }
115
+ . onChange ( of: focusedField) { field in
116
+ if field == nil {
117
+ DispatchQueue . main. asyncAfter ( deadline: . now( ) + 0.2 ) {
118
+ isRenaming = false
119
+ }
120
+ }
121
+ }
103
122
Spacer ( )
104
123
Image ( systemName: " multiply.circle.fill " )
105
124
. foregroundColor ( . gray)
@@ -138,22 +157,7 @@ private struct FileCell: View {
138
157
onCopyFile: { showsDirectoryPicker. toggle ( ) }
139
158
)
140
159
}
141
- . onReceive (
142
- NotificationCenter . default. publisher ( for: UIResponder . keyboardWillHideNotification)
143
- ) { _ in
144
- isRenaming = false
145
- newname = item. name. removingPercentEncoding!
146
- }
147
- . onReceive (
148
- NotificationCenter . default. publisher (
149
- for: UITextField . textDidBeginEditingNotification)
150
- ) { obj in
151
- if let textField = obj. object as? UITextField {
152
- textField. selectedTextRange = textField. textRange (
153
- from: textField. beginningOfDocument, to: textField. endOfDocument
154
- )
155
- }
156
- }
160
+
157
161
}
158
162
}
159
163
}
@@ -178,13 +182,16 @@ private struct FolderCell: View {
178
182
}
179
183
180
184
func onRename( ) {
185
+ focusedField = nil
186
+
181
187
Task {
182
188
do {
183
- try await App . renameFile ( url: URL ( string: item. url) !, name: newname)
189
+ try await App . renameFile (
190
+ url: URL ( string: item. url) !, name: newname)
184
191
} catch {
185
192
App . notificationManager. showErrorMessage ( error. localizedDescription)
193
+ newname = item. name. removingPercentEncoding!
186
194
}
187
- focusedField = nil
188
195
}
189
196
}
190
197
@@ -198,23 +205,29 @@ private struct FolderCell: View {
198
205
199
206
if isRenaming {
200
207
HStack {
201
- TextField (
202
- item. name. removingPercentEncoding!, text: $newname,
203
- onCommit: onRename
204
- )
205
- . focused ( $focusedField, equals: . rename)
206
- . font ( . subheadline)
207
- . disableAutocorrection ( true )
208
- . autocapitalization ( . none)
209
- . onReceive (
210
- NotificationCenter . default. publisher (
211
- for: UITextField . textDidBeginEditingNotification)
212
- ) { obj in
213
- if let textField = obj. object as? UITextField {
214
- textField. selectedTextRange = textField. textRange (
215
- from: textField. beginningOfDocument, to: textField. endOfDocument)
208
+ TextField ( item. name. removingPercentEncoding!, text: $newname)
209
+ . font ( . subheadline)
210
+ . disableAutocorrection ( true )
211
+ . autocapitalization ( . none)
212
+ . focused ( $focusedField, equals: . rename)
213
+ . onSubmit ( onRename)
214
+ . onReceive (
215
+ NotificationCenter . default. publisher (
216
+ for: UITextField . textDidBeginEditingNotification)
217
+ ) { obj in
218
+ if let textField = obj. object as? UITextField {
219
+ textField. selectedTextRange = textField. textRange (
220
+ from: textField. beginningOfDocument, to: textField. endOfDocument
221
+ )
222
+ }
223
+ }
224
+ . onChange ( of: focusedField) { field in
225
+ if field == nil {
226
+ DispatchQueue . main. asyncAfter ( deadline: . now( ) + 0.2 ) {
227
+ focusedField = . rename
228
+ }
229
+ }
216
230
}
217
- }
218
231
Spacer ( )
219
232
Image ( systemName: " multiply.circle.fill " )
220
233
. foregroundColor ( . gray)
@@ -270,12 +283,6 @@ private struct FolderCell: View {
270
283
showsDirectoryPicker. toggle ( )
271
284
} )
272
285
}
273
- . onReceive (
274
- NotificationCenter . default. publisher ( for: UIResponder . keyboardWillHideNotification)
275
- ) { _ in
276
- isRenaming = false
277
- newname = item. name. removingPercentEncoding!
278
- }
279
286
}
280
287
}
281
288
0 commit comments