jobandtalent / AnimatedTextInput

Animated UITextField and UITextView replacement for iOS
MIT License
760 stars 128 forks source link

RxSwift and AnimatedTextInput #113

Open AlexandrPonomarev opened 5 years ago

AlexandrPonomarev commented 5 years ago

Hey. Maybe someone implemented the Rx extension for the AnimatedTextInput like the standard RxSwift extensions for UITextView and UITextField (UITextView + Rx / UITextField + Rx)? Share the code please.

siavashalipour commented 5 years ago

pretty simple just add this:

extension Reactive where Base: AnimatedTextInput {
  /// Reactive wrapper for `text` property.
  public var text: ControlProperty<String?> {
    return value
  }

  /// Reactive wrapper for `text` property.
  public var value: ControlProperty<String?> {
    return base.rx.controlPropertyWithDefaultEvents(
      getter: { textField in
        textField.text
    },
      setter: { textField, value in
        // This check is important because setting text value always clears control state
        // including marked text selection which is imporant for proper input
        // when IME input method is used.
        if textField.text != value {
          textField.text = value
        }
    }
    )
  }
  fileprivate func controlPropertyWithDefaultEvents<T>(
    editingEvents: UIControl.Event = [.allEditingEvents, .valueChanged],
    getter: @escaping (Base) -> T,
    setter: @escaping (Base, T) -> ()
    ) -> ControlProperty<T> {
    return controlProperty(
      editingEvents: editingEvents,
      getter: getter,
      setter: setter
    )
  }
}