ReactiveFeedback icon indicating copy to clipboard operation
ReactiveFeedback copied to clipboard

[PoC] UI binding improvements

Open andersio opened this issue 4 years ago • 1 comments

Allow specific key paths of the feedback loop to be bound directly with UI elements, by:

  1. Declare the bindable key paths as FeedbackLoopLense.
class MyViewModel {
    @FeedbackLoopLense
    var note: String

    private let feedbackLoop: FeedbackLoop<State, Event>
}
  1. Connect the lenses with the feedback loop.
    init() {
        // ...
        _note = feedbackLoop[\.note]
    }
  1. Handle the mutations in your reducer.
    static func reduce(_ state: State, _ event: FeedbackLoopEvent<State, Event>) {
        switch event {
            case let .feedback(event):
                // ... Process your conventional feedback events here.
            case let .mutation(mutation):
                var state = state

                // Try to open the mutation as `\State.note`.
                try? mutation.open(as: \.note) { note, keyPath in
                    // Accept only alphanumerics, whitespaces and newlines.
                    let chars = CharacterSet.alphanumerics.union(.whitespacesAndNewlines)
                    state.note = note.filter { $0.unicodeScalars.allSatisfy(chars.contains) }
                }

                return state
        }
    }
  1. Setup your bindings.
class MyViewController: UIViewController {
    override func viewDidLoad() {
        // ...

        viewModel.$note <~ textView.reactive.continuousTextValues
        textView.reactive.text <~ viewModel.$note
    }
}

andersio avatar Jan 16 '20 11:01 andersio

There should be no e at the end of FeedbackLoopLense

mluisbrown avatar Feb 25 '20 12:02 mluisbrown

Stale PR, older than 3 years old, automatically closed.

ryanb93 avatar Mar 21 '24 11:03 ryanb93