@@ -106,8 +106,6 @@ public struct ResponsiveTextField {
106
106
/// behaviour will be called.
107
107
var standardEditActionHandler : StandardEditActionHandling < UITextField > ?
108
108
109
- fileprivate var shouldUpdateView : Bool = true
110
-
111
109
public init (
112
110
placeholder: String ,
113
111
text: Binding < String > ,
@@ -133,12 +131,6 @@ public struct ResponsiveTextField {
133
131
self . supportedStandardEditActions = supportedStandardEditActions
134
132
self . standardEditActionHandler = standardEditActionHandler
135
133
}
136
-
137
- fileprivate mutating func firstResponderDemandFulfilled( ) {
138
- shouldUpdateView = false
139
- firstResponderDemand? . wrappedValue = nil
140
- shouldUpdateView = false
141
- }
142
134
}
143
135
144
136
// MARK: - Managing the first responder state
@@ -323,7 +315,10 @@ extension ResponsiveTextField: UIViewRepresentable {
323
315
/// first responder.
324
316
///
325
317
public func updateUIView( _ uiView: UITextField , context: Context ) {
326
- guard shouldUpdateView else { return }
318
+ guard context. coordinator. shouldUpdateView else {
319
+ context. coordinator. shouldUpdateView = true
320
+ return
321
+ }
327
322
328
323
uiView. isEnabled = isEnabled
329
324
uiView. isSecureTextEntry = isSecure
@@ -336,13 +331,19 @@ extension ResponsiveTextField: UIViewRepresentable {
336
331
uiView. resignFirstResponder ( )
337
332
case ( false , . shouldBecomeFirstResponder) :
338
333
uiView. becomeFirstResponder ( )
334
+ case ( _, nil ) :
335
+ // If there is no demand then there's nothing to do.
336
+ break
339
337
default :
340
- context. coordinator. resetFirstResponderDemand ( )
338
+ // If the current responder state matches the demand then
339
+ // the demand is already fulfilled so we can just reset it.
340
+ context. coordinator. firstResponderDemandFulfilled ( )
341
341
}
342
342
}
343
343
344
344
public class Coordinator : NSObject , UITextFieldDelegate {
345
345
var parent : ResponsiveTextField
346
+ var shouldUpdateView : Bool = true
346
347
347
348
@Binding
348
349
var text : String
@@ -352,15 +353,16 @@ extension ResponsiveTextField: UIViewRepresentable {
352
353
self . _text = textField. text
353
354
}
354
355
355
- public func resetFirstResponderDemand( ) {
356
- parent. firstResponderDemandFulfilled ( )
356
+ fileprivate func firstResponderDemandFulfilled( ) {
357
+ shouldUpdateView = false
358
+ parent. firstResponderDemand? . wrappedValue = nil
357
359
}
358
360
359
361
public func textFieldShouldBeginEditing( _ textField: UITextField ) -> Bool {
360
362
if let canBecomeFirstResponder = parent. onFirstResponderStateChanged? . canBecomeFirstResponder {
361
363
let shouldBeginEditing = canBecomeFirstResponder ( )
362
364
if !shouldBeginEditing {
363
- parent . firstResponderDemandFulfilled ( )
365
+ firstResponderDemandFulfilled ( )
364
366
}
365
367
return shouldBeginEditing
366
368
}
@@ -369,14 +371,14 @@ extension ResponsiveTextField: UIViewRepresentable {
369
371
370
372
public func textFieldDidBeginEditing( _ textField: UITextField ) {
371
373
parent. onFirstResponderStateChanged ? ( true )
372
- parent . firstResponderDemandFulfilled ( )
374
+ firstResponderDemandFulfilled ( )
373
375
}
374
376
375
377
public func textFieldShouldEndEditing( _ textField: UITextField ) -> Bool {
376
378
if let canResignFirstResponder = parent. onFirstResponderStateChanged? . canResignFirstResponder {
377
379
let shouldEndEditing = canResignFirstResponder ( )
378
380
if !shouldEndEditing {
379
- parent . firstResponderDemandFulfilled ( )
381
+ firstResponderDemandFulfilled ( )
380
382
}
381
383
return shouldEndEditing
382
384
}
@@ -385,7 +387,7 @@ extension ResponsiveTextField: UIViewRepresentable {
385
387
386
388
public func textFieldDidEndEditing( _ textField: UITextField ) {
387
389
parent. onFirstResponderStateChanged ? ( false )
388
- parent . firstResponderDemandFulfilled ( )
390
+ firstResponderDemandFulfilled ( )
389
391
}
390
392
391
393
public func textFieldShouldReturn( _ textField: UITextField ) -> Bool {
0 commit comments