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

Add bindable property to wrap a control's value #119

Conversation

dmcrodrigues
Copy link
Contributor

What's included in this PR?

  • Extended associatedProperty with a setUp closure to perform some configuration like setting up any signals that may affect the property once.
  • A bindable property, rex_valueChanged, to wrap a control's value. The property uses UIControlEvents.ValueChanged and UIControlEvents.EditingChanged events to detect changes and keep the value up-to-date. This prevents code duplication in each control to monitor changes.
  • Enables tests of UIControls that need to use -sendActionsForControlEvents: like UIDatePicker (test was disabled) and UISwitch.
  • Use the introduced setUp to install the signal that rex_dismissAnimated depends upon. This prevents repetition of events in cases where the property is accessed more than once (each access adds a new rac_signalForSelector for the dismiss of the view controller).

If this goes forward it will help to easily provide bindable properties for other controls like UISegmentedControl, UISlider, ...

return associatedObject(host, key: key) { host in
let property = MutableProperty(initial(host))

setUp?(property)
Copy link
Member

Choose a reason for hiding this comment

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

setUp should be as well @NoEscape

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It cannot be @noescape unless we change it to non-optional but it that case we need to provide a default implementation. What approach should I follow?

Copy link
Member

@RuiAAPeres RuiAAPeres Apr 30, 2016

Choose a reason for hiding this comment

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

I would prefer with it and and default { _ in }. This would be easier for the consumer, since he wouldn't have to reference self in the closure. There is also some minor optimization with it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dmcrodrigues dmcrodrigues force-pushed the dr/add-uicontrol-value-changed-property branch from 3dea651 to d8d9dff Compare April 30, 2016 22:20
/// events to detect changes and keep the value up-to-date.
//
@warn_unused_result(message="Did you forget to use the property?")
class func rex_valueChanged<Host: UIControl, T>(host: Host, getter: Host -> T, setter: (Host, T) -> ()) -> MutableProperty<T> {
Copy link
Member

Choose a reason for hiding this comment

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

I think the name rex_valueChanged is misleading.

Especially considering rex_controlEvents above, I first assumed that this was like a pre-filtered version of rex_controlEvents, but then I saw thats not really what it does.

I'm not sure what would be a good name, rex_controlEventUpdatedProperty seems a bit clumsy.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree that the name is not entirely correct mainly because this is a property with a state so the Changed part does not make much sense. I would suggest rex_value which I think is more accurate since is a bindable property for a value of a UIControl.

The name will clash with UISlider's value (in terms of naming only) but this property is not intended to be public, it's only a convenient method to be (re-)used internally. But I'm totally open for other suggestions.

Copy link
Member

Choose a reason for hiding this comment

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

Since its internal, I think rex_value should be fine

@dmcrodrigues dmcrodrigues force-pushed the dr/add-uicontrol-value-changed-property branch from d8d9dff to e9fe238 Compare May 5, 2016 20:52
@RuiAAPeres
Copy link
Member

@dmcrodrigues quick one, how is this related to #115?

@dmcrodrigues dmcrodrigues force-pushed the dr/add-uicontrol-value-changed-property branch from e9fe238 to 5123869 Compare July 6, 2016 08:21
@dmcrodrigues
Copy link
Contributor Author

@RuiAAPeres sorry I haven't seen your comment. #115 introduces a bindable property for CocoaAction, this one is about the value of a UIControl only. In resume: I refactored the code around bindable properties to avoid repeating the exact same code for each control.

The introduced `setUp` is extremely helpful to setup any signals that may
affect the property once.
This property is common to every `UIControl` and can be used by
providing a getter and a setter for the value wrapped. This property
uses `UIControlEvents.ValueChanged` and `UIControlEvents.EditingChanged`
events to detect changes and keep the value up-to-date. This kind of
logic can be reused instead of being defined and used for each control.
This prevents repetition of events in cases where the property is
accessed more than once (each access adds a new `rac_signalForSelector`
for the dismiss of the view controller).
@dmcrodrigues dmcrodrigues force-pushed the dr/add-uicontrol-value-changed-property branch from 5123869 to 98d4932 Compare July 6, 2016 11:30
@RuiAAPeres RuiAAPeres merged commit 4a3bd79 into RACCommunity:master Jul 6, 2016
@dmcrodrigues dmcrodrigues deleted the dr/add-uicontrol-value-changed-property branch July 7, 2016 15:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants