RxViewBinder

Swift Platform Version Carthage compatible License

RxViewBinder 是一个简单的单向架构。
简单且易于实现。☀️

它被实现为一个响应式扩展。

流程

用法 (ViewBindable)

重要!!
需要在 state 结构体的构造函数中绑定 action 和 state。

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")
    }
  }

用法 (BindView)

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()

或者

  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, devmagi82@gmail.com

许可

RxViewBinder 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。