DeclarativeHub / Bond

A Swift binding framework
MIT License
4.23k stars 359 forks source link

Feature request: Create bindable text property for UISearchBar text #507

Closed harryblam closed 6 years ago

harryblam commented 6 years ago

Much like we use textField.reactive.text to bind to a string value, it seems the UISearchBar does not have this ability.

Not sure if it is there by design, or if we could extend Bond to include this.

dantheli commented 6 years ago

This is an extension for UISearchBar's text property:

public var delegate: ProtocolProxy {
        return protocolProxy(for: UISearchBarDelegate.self, keyPath: \.delegate)
    }

    public var text: DynamicSubject<String?> {
        let selector = #selector(UISearchBarDelegate.searchBar(_:textDidChange:))
        let signal = delegate.signal(for: selector) { (subject: SafePublishSubject<Void>, _: UISearchBar, _: NSString) in
            subject.next()
        }

        return dynamicSubject(signal: signal, get: { $0.text }, set: { $0.text = $1 })
    }
}

It'd be nice to include this in Bond, perhaps one of us can make a PR at some point.

harryblam commented 6 years ago

@dantheli you sir, are both a gentleman and a squire. Will do now.

srdanrasic commented 6 years ago

Great work guys, thanks!