ReactiveFeedback
ReactiveFeedback copied to clipboard
[PoC] UI binding improvements
Allow specific key paths of the feedback loop to be bound directly with UI elements, by:
- Declare the bindable key paths as
FeedbackLoopLense
.
class MyViewModel {
@FeedbackLoopLense
var note: String
private let feedbackLoop: FeedbackLoop<State, Event>
}
- Connect the lenses with the feedback loop.
init() {
// ...
_note = feedbackLoop[\.note]
}
- 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
}
}
- Setup your bindings.
class MyViewController: UIViewController {
override func viewDidLoad() {
// ...
viewModel.$note <~ textView.reactive.continuousTextValues
textView.reactive.text <~ viewModel.$note
}
}
There should be no e
at the end of FeedbackLoopLense
Stale PR, older than 3 years old, automatically closed.