Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for autocorrect change not sent to UITextField.rText, .rAttributedText #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Sources/UIControl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import ReactiveKit
control.addTarget(self, action: #selector(RKUIControlHelper.eventHandlerEditingChanged), forControlEvents: UIControlEvents.EditingChanged)
control.addTarget(self, action: #selector(RKUIControlHelper.eventHandlerEditingDidEnd), forControlEvents: UIControlEvents.EditingDidEnd)
control.addTarget(self, action: #selector(RKUIControlHelper.eventHandlerEditingDidEndOnExit), forControlEvents: UIControlEvents.EditingDidEndOnExit)
control.addTarget(self, action: #selector(RKUIControlHelper.eventHandlerAllEditingEvents), forControlEvents: UIControlEvents.AllEditingEvents)
}

func eventHandlerTouchDown() {
Expand Down Expand Up @@ -104,7 +105,11 @@ import ReactiveKit
func eventHandlerEditingDidEndOnExit() {
pushStream.next(.EditingDidEndOnExit)
}


func eventHandlerAllEditingEvents() {
pushStream.next(.AllEditingEvents)
}

deinit {
control?.removeTarget(self, action: nil, forControlEvents: UIControlEvents.AllEvents)
pushStream.completed()
Expand Down
22 changes: 14 additions & 8 deletions Sources/UITextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,15 @@ extension UITextField {
}.disposeIn(rBag)

self.rControlEvent
.filter { $0 == UIControlEvents.EditingChanged }
.filter { $0 == UIControlEvents.AllEditingEvents }
.observeNext { [weak self] event in
guard let unwrappedSelf = self else { return }
updatingFromSelf = true
unwrappedSelf.rText.value = unwrappedSelf.text
updatingFromSelf = false
// only send to rText if value changed, as .AllEditingEvents reports more than just changes
if unwrappedSelf.rText.value != unwrappedSelf.text {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way to avoid this? I don't like the idea of performing string comparison O(n) on each keystroke...

updatingFromSelf = true
unwrappedSelf.rText.value = unwrappedSelf.text
updatingFromSelf = false
}
}.disposeIn(rBag)

return rText
Expand All @@ -76,12 +79,15 @@ extension UITextField {
}.disposeIn(rBag)

self.rControlEvent
.filter { $0 == UIControlEvents.EditingChanged }
.filter { $0 == UIControlEvents.AllEditingEvents }
.observeNext { [weak self] event in
guard let unwrappedSelf = self else { return }
updatingFromSelf = true
unwrappedSelf.rAttributedText.value = unwrappedSelf.attributedText
updatingFromSelf = false
// only sent to rAttributedText if value changed, as .AllEditingEvents reports more than just changes
if unwrappedSelf.rAttributedText.value != unwrappedSelf.attributedText {
updatingFromSelf = true
unwrappedSelf.rAttributedText.value = unwrappedSelf.attributedText
updatingFromSelf = false
}
}.disposeIn(rBag)

return rAttributedText
Expand Down