RxViewBinder is a simple one-way architecture.
Simple and easy to implement. :sunny:
It is implemented as a reactive extension.
important!!
You need to bind the action and state in the constructor of the state structure.
final class SampleViewBinder: ViewBindable {
enum Command {
case fetch
}
struct Action {
let value: PublishRelay<String> = PublishRelay()
}
struct State {
let value: Driver<String>
init(action: Action) {
// Action and state binding
value = action.value.asDriver(onErrorJustReturn: "")
}
}
let action = Action()
lazy var state = State(action: self.action)
}
func binding(command: Command) {
switch command {
case .fetch:
Observable<String>.just("test")
.bind(to: action.value)
.disposed(by: self.disposeBag)
}
}
func binding(command: Command) {
switch command {
case .fetch:
action.value.accept("test")
}
}
final class ViewController: UIViewController, BindView {
typealias ViewBinder = SampleViewBinder
init(viewBinder: ViewBinder) {
defer { self.viewBinder = viewBinder }
super.init(nibName: nil, bundle: nil)
}
}
let vc = ViewController()
vc.viewBinder = SampleViewBinder()
or
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.viewBinder = ViewBinder()
}
func command(viewBinder: ViewBinder) {
self.rx.methodInvoked(#selector(UIViewController.viewDidLoad))
.map { _ in ViewBinder.Command.fetch }
.bind(to: viewBinder.command)
.disposed(by: self.disposeBag)
}
func state(viewBinder: ViewBinder) {
viewBinder.state
.value
.drive(onNext: { print($0) })
.disposed(by: self.disposeBag)
}
pod 'RxViewBinder', '~> 2.0.0'
github "magi82/RxViewBinder" ~> 2.0.0
magi82, [email protected]
RxViewBinder is available under the MIT license. See the LICENSE file for more info.
link |
Stars: 32 |
Last commit: 3 years ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics