Skip to content

Commit 838593f

Browse files
committed
Fix first responder demand reset. (fixes #4)
This got broken in a previous refactor. I've moved this logic down into the coordinator itself now.
1 parent 681ad57 commit 838593f

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

Sources/ResponsiveTextField/ResponsiveTextField.swift

+18-16
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,6 @@ public struct ResponsiveTextField {
106106
/// behaviour will be called.
107107
var standardEditActionHandler: StandardEditActionHandling<UITextField>?
108108

109-
fileprivate var shouldUpdateView: Bool = true
110-
111109
public init(
112110
placeholder: String,
113111
text: Binding<String>,
@@ -133,12 +131,6 @@ public struct ResponsiveTextField {
133131
self.supportedStandardEditActions = supportedStandardEditActions
134132
self.standardEditActionHandler = standardEditActionHandler
135133
}
136-
137-
fileprivate mutating func firstResponderDemandFulfilled() {
138-
shouldUpdateView = false
139-
firstResponderDemand?.wrappedValue = nil
140-
shouldUpdateView = false
141-
}
142134
}
143135

144136
// MARK: - Managing the first responder state
@@ -323,7 +315,10 @@ extension ResponsiveTextField: UIViewRepresentable {
323315
/// first responder.
324316
///
325317
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+
}
327322

328323
uiView.isEnabled = isEnabled
329324
uiView.isSecureTextEntry = isSecure
@@ -336,13 +331,19 @@ extension ResponsiveTextField: UIViewRepresentable {
336331
uiView.resignFirstResponder()
337332
case (false, .shouldBecomeFirstResponder):
338333
uiView.becomeFirstResponder()
334+
case (_, nil):
335+
// If there is no demand then there's nothing to do.
336+
break
339337
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()
341341
}
342342
}
343343

344344
public class Coordinator: NSObject, UITextFieldDelegate {
345345
var parent: ResponsiveTextField
346+
var shouldUpdateView: Bool = true
346347

347348
@Binding
348349
var text: String
@@ -352,15 +353,16 @@ extension ResponsiveTextField: UIViewRepresentable {
352353
self._text = textField.text
353354
}
354355

355-
public func resetFirstResponderDemand() {
356-
parent.firstResponderDemandFulfilled()
356+
fileprivate func firstResponderDemandFulfilled() {
357+
shouldUpdateView = false
358+
parent.firstResponderDemand?.wrappedValue = nil
357359
}
358360

359361
public func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
360362
if let canBecomeFirstResponder = parent.onFirstResponderStateChanged?.canBecomeFirstResponder {
361363
let shouldBeginEditing = canBecomeFirstResponder()
362364
if !shouldBeginEditing {
363-
parent.firstResponderDemandFulfilled()
365+
firstResponderDemandFulfilled()
364366
}
365367
return shouldBeginEditing
366368
}
@@ -369,14 +371,14 @@ extension ResponsiveTextField: UIViewRepresentable {
369371

370372
public func textFieldDidBeginEditing(_ textField: UITextField) {
371373
parent.onFirstResponderStateChanged?(true)
372-
parent.firstResponderDemandFulfilled()
374+
firstResponderDemandFulfilled()
373375
}
374376

375377
public func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
376378
if let canResignFirstResponder = parent.onFirstResponderStateChanged?.canResignFirstResponder {
377379
let shouldEndEditing = canResignFirstResponder()
378380
if !shouldEndEditing {
379-
parent.firstResponderDemandFulfilled()
381+
firstResponderDemandFulfilled()
380382
}
381383
return shouldEndEditing
382384
}
@@ -385,7 +387,7 @@ extension ResponsiveTextField: UIViewRepresentable {
385387

386388
public func textFieldDidEndEditing(_ textField: UITextField) {
387389
parent.onFirstResponderStateChanged?(false)
388-
parent.firstResponderDemandFulfilled()
390+
firstResponderDemandFulfilled()
389391
}
390392

391393
public func textFieldShouldReturn(_ textField: UITextField) -> Bool {

0 commit comments

Comments
 (0)