@@ -50,13 +50,16 @@ private struct FileCell: View {
5050 }
5151
5252 func onRename( ) {
53+ focusedField = nil
54+
5355 Task {
5456 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)
5659 } catch {
5760 App . notificationManager. showErrorMessage ( error. localizedDescription)
61+ newname = item. name. removingPercentEncoding!
5862 }
59- focusedField = nil
6063 }
6164 }
6265
@@ -92,14 +95,30 @@ private struct FileCell: View {
9295
9396 if isRenaming {
9497 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+ }
103122 Spacer ( )
104123 Image ( systemName: " multiply.circle.fill " )
105124 . foregroundColor ( . gray)
@@ -138,22 +157,7 @@ private struct FileCell: View {
138157 onCopyFile: { showsDirectoryPicker. toggle ( ) }
139158 )
140159 }
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+
157161 }
158162 }
159163}
@@ -178,13 +182,16 @@ private struct FolderCell: View {
178182 }
179183
180184 func onRename( ) {
185+ focusedField = nil
186+
181187 Task {
182188 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)
184191 } catch {
185192 App . notificationManager. showErrorMessage ( error. localizedDescription)
193+ newname = item. name. removingPercentEncoding!
186194 }
187- focusedField = nil
188195 }
189196 }
190197
@@ -198,23 +205,29 @@ private struct FolderCell: View {
198205
199206 if isRenaming {
200207 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+ }
216230 }
217- }
218231 Spacer ( )
219232 Image ( systemName: " multiply.circle.fill " )
220233 . foregroundColor ( . gray)
@@ -270,12 +283,6 @@ private struct FolderCell: View {
270283 showsDirectoryPicker. toggle ( )
271284 } )
272285 }
273- . onReceive (
274- NotificationCenter . default. publisher ( for: UIResponder . keyboardWillHideNotification)
275- ) { _ in
276- isRenaming = false
277- newname = item. name. removingPercentEncoding!
278- }
279286 }
280287}
281288
0 commit comments