Make Observable for UIButton isHighlighted property - ios

I'm trying to create isHighlighted Observable for my UIButton, to send sequence every time isHiglighted for UIButton has changed. And I've write something like this
extension Reactive where Base: UIButton {
var isHighlighted: Observable<Bool> {
let property = self.base.rx.controlProperty(editingEvents: .allTouchEvents,
getter: { _ in self.base.isHighlighted },
setter: { (_, _) in })
return property
.distinctUntilChanged()
.asObservable()
}
}
The problem is, that it doesn't work for .touchUpInside. If I drag finger outside UIButton and then come back, it works fine, but not for tap action. I think immediately after .touchUpInside it's still highlighted for very short time.

Thanks #iWheelBuy I've created code that works, without RxOptional, I based it partially on your answer so Thank you! Here is working code:
extension Reactive where Base: UIButton {
var isHighlighted: Observable<Bool> {
let anyObservable = self.base.rx.methodInvoked(#selector(setter: self.base.isHighlighted))
let boolObservable = anyObservable
.flatMap { Observable.from(optional: $0.first as? Bool) }
.startWith(self.base.isHighlighted)
.distinctUntilChanged()
.share()
return boolObservable
}
}

I think I have a solution. It can be simplified, but I just copy paste the full solution I have.
public extension Reactive where Base: UIButton {
public func isHighlighted() -> Observable<Bool> {
let selector = #selector(setter: UIButton.isHighlighted)
let value: ([Any]) -> Bool? = { $0.first(where: { $0 is Bool }) as? Bool }
return base
.observable(selector: selector, value: value)
.filterNil()
.startWith(base.isHighlighted)
.distinctUntilChanged()
.share(replay: 1, scope: .whileConnected)
}
}
Also, to make it work. You need RxOptional and some extra code:
public enum InvocationTime: Int {
case afterMessageIsInvoked
case beforeMessageIsInvoked
}
and
public extension NSObject {
public func observable<T>(selector: Selector, value: #escaping ([Any]) -> T, when: InvocationTime = .afterMessageIsInvoked) -> Observable<T> {
let observable: Observable<[Any]> = {
switch when {
case .afterMessageIsInvoked:
return rx.methodInvoked(selector)
case .beforeMessageIsInvoked:
return rx.sentMessage(selector)
}
}()
return observable
.map({ value($0) })
.share(replay: 1, scope: .whileConnected)
}
}
Hope it helps (^

Related

RxSwift UITableView binding when also using a UISearchController

In my view controller I have a UISearchController associated with the UITableView. So all my normal table view datasource methods do the old
if isSearching {
// use filteredTableData array
} else {
// use SharedModel.shared.participants
}
I'm not clear on how I'd implement that using RxCocoa as I'm brand new to Rx.
Create a Variable like below
var tableViewOptions = Variable<[String]>([]) // replace String with your own object
bind tableViewOptions to tableview after your view loads.
tableViewOptions
.asObservable()
.bind(to: self.tableView
.rx
.items(cellIdentifier: "cellIdentifier",
cellType: CustomCell.self)) { _, values, cell in
// do your stuff
}
Then when ever you search change the values of tableViewOptions like below.
if isSearching {
tableViewOptions.value = filteredTableArray
} else {
tableViewOptions.value = SharedModel.shared.participants
}
I solve this by declaring a decorator for an Observable and an extension for a UISearchBar (you could also declare it for the UISearchController):
//FIXME: Item can't be type constrained. Have to use optional casting.
class FilterableByTitleCollection<Item>: ObservableType {
private let origin: Observable<Array<Item>>
private let filteringStrategySource: Observable<TitlableModelFilteringStrategy> //FIXME: This is a strategy source
init<Origin: ObservableType>(
origin: Origin,
filteringStrategySource: Observable<TitlableModelFilteringStrategy>) where Origin.E == Array<Item> {
self.origin = origin.asObservable()
self.filteringStrategySource = filteringStrategySource
}
typealias E = Array<Item>
func subscribe<O:ObserverType>(_ observer: O) -> Disposable where O.E == Array<Item> {
return Observable.combineLatest(
origin,
filteringStrategySource
)
.observeOn(ConcurrentDispatchQueueScheduler(qos: .userInitiated))
.map{ origin, strategy in
guard origin is Array<Titlable> else { assert(false); return origin }
return origin.filter{ strategy.shouldInclude(item: $0 as! Titlable) }
}
.observeOn(MainScheduler.instance)
.subscribe(observer)
}
}
...
extension UISearchBar {
var titlableFilteringStrategy: Observable<TitlableModelFilteringStrategy> {
return Observable<String?>.merge(
[
self.rx.text.asObservable(),
self.rx.textDidEndEditing
.map{ [weak self] in
assert(self != nil)
return self?.text
},
self.rx.cancelButtonClicked.map{ Optional<String>.some("") }
]
).distinctUntilChanged{ (old: String?, new: String?) -> Bool in
old == new
}.map{ TitlableModelFilteringStrategy(filteringPredicate: $0) }
}
}
...
struct TitlableModelFilteringStrategy {
private let filteringPredicate: String
init(filteringPredicate: String?) {
self.filteringPredicate = filteringPredicate ?? ""
}
func shouldInclude(item: Titlable) -> Bool {
return filteringPredicate.isEmpty ? true : item.title.localizedCaseInsensitiveContains(filteringPredicate)
}
func equals(to another: TitlableModelFilteringStrategy) -> Bool {
return filteringPredicate == another.filteringPredicate
}
}
...
protocol Titlable {
var title: String { get }
}

RxSwift convert Observable<Bool> to Observable<Void>

I am not so convinced with RxSwift yet, and it's really hard to cleat understanding. After reviewing different materials, I cant' still work and manipulate sequences.
On the whole I have problem with type converting:
Cannot convert return expression of type 'Observable<Bool>' to return type 'Observable<Void>' (aka 'Observable<()>')
I have CocoaAction processing, and should return Observable<Void>
func stopProject() -> CocoaAction {
return CocoaAction { _ in
let stopProject = stop(project: self.projectId)
return stopProject.asObservable() //wrong converting
}
}
The stop function return Observable<Bool>
Finish view:
return stop(project: self.projectId).flatMap { _ in
Observable<Void>.empty()
}
let voidObservable = boolObservable.map { Void() }
let voidObservable = boolObservable.map { _ in Void() }
And in the case that you only want to emit a value if the boolean value is true:
let voidObservable = boolObservable.filter { $0 }.map { _ in Void() }
Adding this extension:
public extension ObservableType {
func mapToVoid() -> Observable<Void> {
return map { _ in }
}
}
so you could do
myObservable.mapToVoid()

Custom UISlider with RxSwift support

I have created a custom control thad works as UISlider. Now I want to use it with RxSwift and for this I need rx_value like property for my control.
I have found RxSwift code for UISlider on GitHub:
extension UISlider {
/**
Reactive wrapper for `value` property.
*/
public var rx_value: ControlProperty<Float> {
return UIControl.rx_value(
self,
getter: { slider in
slider.value
}, setter: { slider, value in
slider.value = value
}
)
}
}
It does look simple enough so I decided just to repeat it in my control but apparently rx_value property of UIControl is private. What could be a workaround for this?
UPDATE:
That is how I'm going to use this property:
var latestRank: Observable<Int> {
return oneSlider
.rx_distance.asObservable()
.throttle(0.5, scheduler: MainScheduler.instance)
.distinctUntilChanged()
and in viewModel:
distanceRank
.observeOn(MainScheduler.instance).subscribeNext { rank in
print(rank)
self.discoverNodes(true)
}
In viewModel distanceRank is defined as this:
let distanceRank: Observable<Int>
And this is extension for rx_distance:
#if os(iOS) || os(tvOS)
import Foundation
#if !RX_NO_MODULE
import RxSwift
import RxCocoa
#endif
import UIKit
extension OneSlider {
public var rx_distance: ControlProperty<Int> {
return UIControl.valuePublic(
self,
getter: { slider in
slider.distanceRank
}, setter: { slider, value in
slider.distanceRank = value
}
)
}
}
extension UIControl {
static func valuePublic<T, ControlType: UIControl>(control: ControlType, getter: ControlType -> T, setter: (ControlType, T) -> ()) -> ControlProperty<T> {
let values: Observable<T> = Observable.deferred { [weak control] in
guard let existingSelf = control else {
return Observable.empty()
}
return existingSelf.rx_controlEvent([.AllEditingEvents, .ValueChanged])
.flatMap { _ in
return control.map { Observable.just(getter($0)) } ?? Observable.empty()
}
.startWith(getter(existingSelf))
}
return ControlProperty(values: values, valueSink: UIBindingObserver(UIElement: control) { control, value in
setter(control, value)
})
}
}
#endif

Dynamically defined Function-type vars

I am trying to extend a UIKit class (which I can't edit normally) by creating a new function and a new Function-type variable, which is going to use ObjC Runtime to make it look&feel like a stored property.
extension UITextField {
private struct DynamicallyDefinedVars {
static var oneVar = "oneVar"
}
var oneVar: ((String?)->Bool)? {
get{
return objc_getAssociatedObject(self, &DynamicallyDefinedVars.oneVar) as? (String?)->Bool
}
set{
if let newValue: AnyObject = newValue {
objc_setAssociatedObject(self, &DynamicallyDefinedVars.oneVar, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
}
func callTheVarFunc() -> Bool {
if let oneVar = oneVar {
return oneVar("Foo")
}
return true
}
}
What I hope to achieve:
var foo: UITextField
foo.oneVar = { (bar: String?) -> Bool in
return true
}
if foo.callTheVarFunc {
doSomething
}
But I am getting the following error:
Cannot convert value of type '((String?) -> Bool)?' to specified type 'AnyObject?'
It would work fine if oneVar was typed something like String or an array of sorts, but I see the Function Types are not included in AnyObject, thus giving me issues when trying to objc_setAssociatedObject. Any thoughts on how I can get the desired behaviour (through extensions, without subclassing)? Each instance has to have a different oneVar value to be used with the callTheVarFunc function.
I've just seen this problem, in Swift closures cannot be casted to AnyObject so you can workaround this annoying thing creating a custom class:
extension UITextField {
class CustomClosure {
var closure: ((String?)->Bool)?
init(_ closure: ((String?)->Bool)?) {
self.closure = closure
}
}
private struct DynamicallyDefinedVars {
static var oneVar = "oneVar"
}
var oneVar: ((String?)->Bool)? {
get{
if let cl = objc_getAssociatedObject(self, &DynamicallyDefinedVars.oneVar) as? CustomClosure {
return cl.closure
}
return nil
}
set{
objc_setAssociatedObject(self, &DynamicallyDefinedVars.oneVar,CustomClosure(newValue), .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
func callTheVarFunc() -> Bool {
if let oneVar = oneVar {
return oneVar("Foo")
}
return true
}
}

How to implement a basic UITextField input + UIButton action scenario using ReactiveCocoa 3?

I'm a Swift and ReactiveCocoa noob at the same time. Using MVVM and Reactive Cocoa v3.0-beta.4 framework, I'd like to implement this setup, to learn the basics of the new RAC 3 framework.
I have a text field and I want the text input to contain more than 3 letters, for validation. If the text passes the validation, the button underneath should be enabled. When the button receives the touch down event, I want to trigger an action using the view model's property.
Since there are very few resources about RAC 3.0 beta at the moment, I implemented the following by reading the QAs on the framework's Github repo. Here's what I could come up with so far:
ViewModel.swift
class ViewModel {
var text = MutableProperty<String>("")
let action: Action<String, Bool, NoError>
let validatedTextProducer: SignalProducer<AnyObject?, NoError>
init() {
let validation: Signal<String, NoError> -> Signal<AnyObject?, NoError> = map ({
string in
return (count(string) > 3) as AnyObject?
})
validatedTextProducer = text.producer.lift(validation)
//Dummy action for now. Will make a network request using the text property in the real app.
action = Action { _ in
return SignalProducer { sink, disposable in
sendNext(sink, true)
sendCompleted(sink)
}
}
}
}
ViewController.swift
class ViewController: UIViewController {
private lazy var txtField: UITextField = {
return createTextFieldAsSubviewOfView(self.view)
}()
private lazy var button: UIButton = {
return createButtonAsSubviewOfView(self.view)
}()
private lazy var buttonEnabled: DynamicProperty = {
return DynamicProperty(object: self.button, keyPath: "enabled")
}()
private let viewModel = ViewModel()
private var cocoaAction: CocoaAction?
override func viewDidLoad() {
super.viewDidLoad()
view.setNeedsUpdateConstraints()
bindSignals()
}
func bindSignals() {
viewModel.text <~ textSignal(txtField)
buttonEnabled <~ viewModel.validatedTextProducer
cocoaAction = CocoaAction(viewModel.action, input:"Actually I don't need any input.")
button.addTarget(cocoaAction, action: CocoaAction.selector, forControlEvents: UIControlEvents.TouchDown)
viewModel.action.values.observe(next: {value in
println("view model action result \(value)")
})
}
override func updateViewConstraints() {
super.updateViewConstraints()
//Some autolayout code here
}
}
RACUtilities.swift
func textSignal(textField: UITextField) -> SignalProducer<String, NoError> {
return textField.rac_textSignal().toSignalProducer()
|> map { $0! as! String }
|> catch {_ in SignalProducer(value: "") }
}
With this setup, the button gets enabled when the view model's text is longer than 3 characters. When the user taps on the button, the view model's action runs and I can get the return value as true. So far so good.
My question is: Inside the view model's action, I want to use its stored text property and update the code to make a network request using it. So, I don't need an input from the view controller's side. How can I not require an input for my Action property?
From the ReactiveCocoa/CHANGELOG.md:
An action must indicate the type of input it accepts, the type of output it produces, and what kinds of errors can occur (if any).
So currently there is no way to define an Action without an input.
I suppose you could declare that you don't care about input by making it AnyObject? and creating CocoaAction with convenience initialiser:
cocoaAction = CocoaAction(viewModel.action)
Additional remarks
I dont't like using AnyObject? instead of Bool for validatedTextProducer. I suppose you preferred it because binding to the buttonEnabled property requires AnyObject?. I would rather cast it there though, instead of sacrificing type clarity of my view model (see example below).
You might want to restrict execution of the Action on the view model level as well as UI, e.g.:
class ViewModel {
var text = MutableProperty<String>("")
let action: Action<AnyObject?, Bool, NoError>
// if you want to provide outside access to the property
var textValid: PropertyOf<Bool> {
return PropertyOf(_textValid)
}
private let _textValid = MutableProperty(false)
init() {
let validation: Signal<String, NoError> -> Signal<Bool, NoError> = map { string in
return count(string) > 3
}
_textValid <~ text.producer |> validation
action = Action(enabledIf:_textValid) { _ in
//...
}
}
}
And binding to buttonEnabled:
func bindSignals() {
buttonEnabled <~ viewModel.action.enabled.producer |> map { $0 as AnyObject }
//...
}
If you take a look at Colin Eberhardt blog post on ReactiveCocoa 3 there's a very nice approach to this problem.
Basically because it's still in beta there's no extension on UIView that makes those properties easy to use with RAC3 but you can add them easily. I would recommend adding a UIKit+RAC3.swift extension and adding them as you need:
import UIKit
import ReactiveCocoa
struct AssociationKey {
static var hidden: UInt8 = 1
static var alpha: UInt8 = 2
static var text: UInt8 = 3
static var enabled: UInt8 = 4
}
func lazyAssociatedProperty<T: AnyObject>(host: AnyObject,
key: UnsafePointer<Void>, factory: ()->T) -> T {
var associatedProperty = objc_getAssociatedObject(host, key) as? T
if associatedProperty == nil {
associatedProperty = factory()
objc_setAssociatedObject(host, key, associatedProperty,
UInt(OBJC_ASSOCIATION_RETAIN))
}
return associatedProperty!
}
func lazyMutableProperty<T>(host: AnyObject, key: UnsafePointer<Void>,
setter: T -> (), getter: () -> T) -> MutableProperty<T> {
return lazyAssociatedProperty(host, key) {
var property = MutableProperty<T>(getter())
property.producer
.start(next: {
newValue in
setter(newValue)
})
return property
}
}
extension UIView {
public var rac_alpha: MutableProperty<CGFloat> {
return lazyMutableProperty(self, &AssociationKey.alpha, { self.alpha = $0 }, { self.alpha })
}
public var rac_hidden: MutableProperty<Bool> {
return lazyMutableProperty(self, &AssociationKey.hidden, { self.hidden = $0 }, { self.hidden })
}
}
extension UIBarItem {
public var rac_enabled: MutableProperty<Bool> {
return lazyMutableProperty(self, &AssociationKey.enabled, { self.enabled = $0 }, { self.enabled })
}
}
That way you simply replace the RAC = RACObserve logic by (for example):
var date = MutableProperty<NSDate?>(nil)
var time = MutableProperty<Int?>(nil)
let doneItem = UIBarButtonItem()
doneItem.rac_enabled <~ date.producer
|> combineLatestWith(time.producer)
|> map { return $0.0 != nil && $0.1 != nil }
Again this is all taken from his blog post which far more descriptive than this answer. I highly recommend anyone interested in using RAC 3 reads his amazing posts and tutorials:
A first look at RAC 3
Signal Producers and API Clarity
MVVM and RAC 3

Resources