Upgrade to Pro — share decks privately, control downloads, hide ads and more …

try! ReactorKit

try! ReactorKit

ReactorKit Meetup Japan at Wantedly, Inc.
日本初 ReactorKit のコミュニティーイベント開催。作者来日で特別講演
https://wantedly.connpass.com/event/90261/

Satoshi Hachiya

June 28, 2018
Tweet

More Decks by Satoshi Hachiya

Other Decks in Programming

Transcript

  1. ReactorKit • I just started learning RxSwift and ReactorKit •

    The documents are easy to understand • The examples are abundant # Anytime you ready!
  2. !

  3. viewController.reactor = MyViewReactor() func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey:

    Any]?) -> Bool { let viewController = self.window?.rootViewController as! MyViewController viewController.reactor = MyViewReactor() return true }
  4. Runtime Error class MyViewController: UIViewController, StoryboardView { ... func bind(reactor:

    MyViewReactor) { collectionView.rx.contentOffset .map { _ in Reactor.Action.increase } .bind(to: reactor.action) .disposed(by: disposeBag) } ... }
  5. + UICollectionViewDelegate class PageMenuViewController: UIViewController, UICollectionViewDelegate, StoryboardView { ... func

    bind(reactor: MyViewReactor) { collectionView.rx.contentOffset .map { _ in Reactor.Action.increase } .bind(to: reactor.action) .disposed(by: disposeBag) } ... }
  6. View ➡ Action func bind(reactor: CounterViewReactor) { increaseButton.rx.tap .map {

    Reactor.Action.increase } .bind(to: reactor.action) .disposed(by: disposeBag) ... }
  7. Action ➡ Mutation func mutate(action: Action) -> Observable<Mutation> { switch

    action { case .increase: return Observable.concat([ ..., Observable.just(Mutation.increaseValue).delay(0.5, scheduler: MainScheduler.instance), ..., ]) case .decrease: ... } }
  8. Mutation State func reduce(state: State, mutation: Mutation) -> State {

    var state = state switch mutation { case .increaseValue: state.value += 1 ... return state }
  9. State ➡ View func bind(reactor: CounterViewReactor) { ... reactor.state.map {

    $0.value } .distinctUntilChanged() .map { "\($0)" } .bind(to: valueLabel.rx.text) .disposed(by: disposeBag) ... }
  10. Good readability of the code • ReactorKit makes it easier

    to... • separate the responsibílities • manage the state of the views • manage the data flow