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

MVVMの実装を縛るFrameworkを開発・導入し、チームでばらつきがあった実装を統一する

Ae276805027a01983503c3edafbdb6b2?s=47 Taiki Suzuki
September 17, 2019

 MVVMの実装を縛るFrameworkを開発・導入し、チームでばらつきがあった実装を統一する

iOSDC Japan 2019 Reject Conference days1[非公式]
https://iosdc-reject-conference.connpass.com/event/137280/

Unio
https://github.com/cats-oss/Unio

Ae276805027a01983503c3edafbdb6b2?s=128

Taiki Suzuki

September 17, 2019
Tweet

More Decks by Taiki Suzuki

Other Decks in Programming

Transcript

  1. MVVMͷ࣮૷ΛറΔFrameworkΛ։ൃɾಋೖ͠ νʔϜͰ͹Β͖͕࣮ͭ͋ͬͨ૷Λ౷Ұ͢Δ iOSDC Japan 2019 Reject Conference days1 @marty_suzuki

  2. marty_suzuki marty-suzuki Taiki Suzuki ΠϯλʔωοτςϨϏہ ʮAbemaTVʯΛ୲౰͢ΔiOSΤϯδ χΞɻ2014೥αΠόʔΤʔδΣϯτ ৽ଔೖࣾɻ ίϛϡχςΟαʔϏεͰαʔόʔα ΠυΛ୲౰ͨ͠ޙɺiOSΤϯδχΞʹ

    స޲͠ϑΝογϣϯ௨ൢαΠτ ʮVILECTʯͷ্ཱͪ͛ɾӡӦʹै ࣄɻͦͷޙ৽ײ֮SNSʮ755ʯͰͷ։ ൃΛܦͯɺ2017೥3݄ΑΓݱ৬ɻ Popular repositories Overview SAHistoryNavigationViewController Swift 1,559 SAHistoryNavigationViewController realizes iOS task manager like UI in UINavigationController. SABlurImageView Swift 523 You can use blur effect and it's animation easily to call only two methods. URLEmbeddedView Swift 549 URLEmbeddedView automatically caches the object that is confirmed the Open Graph Protocol. ReverseExtension Swift 1,446 A UITableView extension that enables cell insertion from the bottom of a table view.
  3. ͸͡Ίʹ MVVMͰ։ൃͨ͜͠ͱ͕͋Δ

  4. ͸͡Ίʹ ։ൃऀʹΑͬͯ ViewModelͷ࣮૷ํ๏ʹҧ͍͕͋Δ ͱײ͡·ͤΜ͔ʁ

  5. ྫ class SearchViewModel { let repositories: Observable<[Repository]> private let _repositories

    = BehaviorRelay<[Repository]>(value: []) private let _search = PublishRelay<Void>() private let _text = PublishRelay<String?>() private let disposeBag = DisposeBag() init() { let apiAction = SearchAPIAction() self.repositories = _repositories.asObservable() apiAction.response .bind(to: _repositories) .disposed(by: disposeBag) _search .withLatestFrom(_text) { $1 } .flatMap { $0.map(Observable.just) ?? .empty() } .subscribe(onNext: { apiAction.execute($0) }) .disposed(by: disposeBag) } func search() { _search.accept(()) } func setText(_ text: String?) { _text.accept(text) } }
  6. ྫ class SearchViewModel { let repositories: Observable<[Repository]> private let _repositories

    = BehaviorRelay<[Repository]>(value: []) private let _search = PublishRelay<Void>() private let _text = PublishRelay<String?>() private let disposeBag = DisposeBag() init() { let apiAction = SearchAPIAction() self.repositories = _repositories.asObservable() apiAction.response .bind(to: _repositories) .disposed(by: disposeBag) _search .withLatestFrom(_text) { $1 } .flatMap { $0.map(Observable.just) ?? .empty() } .subscribe(onNext: { apiAction.execute($0) }) .disposed(by: disposeBag) } func search() { _search.accept(()) } func setText(_ text: String?) { _text.accept(text) } } ϩδοΫͷ࣮૷
  7. ྫ class SearchViewModel { let repositories: Observable<[Repository]> private let _repositories

    = BehaviorRelay<[Repository]>(value: []) private let _search = PublishRelay<Void>() private let _text = PublishRelay<String?>() private let disposeBag = DisposeBag() init() { let apiAction = SearchAPIAction() self.repositories = _repositories.asObservable() apiAction.response .bind(to: _repositories) .disposed(by: disposeBag) _search .withLatestFrom(_text) { $1 } .flatMap { $0.map(Observable.just) ?? .empty() } .subscribe(onNext: { apiAction.execute($0) }) .disposed(by: disposeBag) } func search() { _search.accept(()) } func setText(_ text: String?) { _text.accept(text) } } ֎෦͔Βͷೖྗ
  8. ྫ class SearchViewModel { let repositories: Observable<[Repository]> private let _repositories

    = BehaviorRelay<[Repository]>(value: []) private let _search = PublishRelay<Void>() private let _text = PublishRelay<String?>() private let disposeBag = DisposeBag() init() { let apiAction = SearchAPIAction() self.repositories = _repositories.asObservable() apiAction.response .bind(to: _repositories) .disposed(by: disposeBag) _search .withLatestFrom(_text) { $1 } .flatMap { $0.map(Observable.just) ?? .empty() } .subscribe(onNext: { apiAction.execute($0) }) .disposed(by: disposeBag) } func search() { _search.accept(()) } func setText(_ text: String?) { _text.accept(text) } } ೖྗͷϦϨʔ
  9. ྫ class SearchViewModel { let repositories: Observable<[Repository]> private let _repositories

    = BehaviorRelay<[Repository]>(value: []) private let _search = PublishRelay<Void>() private let _text = PublishRelay<String?>() private let disposeBag = DisposeBag() init() { let apiAction = SearchAPIAction() self.repositories = _repositories.asObservable() apiAction.response .bind(to: _repositories) .disposed(by: disposeBag) _search .withLatestFrom(_text) { $1 } .flatMap { $0.map(Observable.just) ?? .empty() } .subscribe(onNext: { apiAction.execute($0) }) .disposed(by: disposeBag) } func search() { _search.accept(()) } func setText(_ text: String?) { _text.accept(text) } } ಺෦ঢ়ଶ
  10. ྫ class SearchViewModel { let repositories: Observable<[Repository]> private let _repositories

    = BehaviorRelay<[Repository]>(value: []) private let _search = PublishRelay<Void>() private let _text = PublishRelay<String?>() private let disposeBag = DisposeBag() init() { let apiAction = SearchAPIAction() self.repositories = _repositories.asObservable() apiAction.response .bind(to: _repositories) .disposed(by: disposeBag) _search .withLatestFrom(_text) { $1 } .flatMap { $0.map(Observable.just) ?? .empty() } .subscribe(onNext: { apiAction.execute($0) }) .disposed(by: disposeBag) } func search() { _search.accept(()) } func setText(_ text: String?) { _text.accept(text) } } ֎෦΁ͷग़ྗ
  11. ྫ - ViewͱViewModelͷ઀ଓ let viewModel: SearchViewModel let tableView: UITableView let

    searchButton: UIButton let textField: UITextField viewModel.repositories .bind(to: tableView.rx.items(cellIdentifier: "Cell", cellType: UITableViewCell.self) ) { row, repository, cell in cell.textLabel?.text = repository.name } .disposed(by: disposeBag) searchButton.rx.tap .subscribe(onNext: { viewModel.search() }) .disposed(by: disposeBag) textField.rx.text .subscribe(onNext: { viewModel.setText($0) }) .disposed(by: disposeBag)
  12. ྫ - ViewͱViewModelͷ઀ଓ let viewModel: SearchViewModel let tableView: UITableView let

    searchButton: UIButton let textField: UITextField viewModel.repositories .bind(to: tableView.rx.items(cellIdentifier: "Cell", cellType: UITableViewCell.self) ) { row, repository, cell in cell.textLabel?.text = repository.name } .disposed(by: disposeBag) searchButton.rx.tap .subscribe(onNext: { viewModel.search() }) .disposed(by: disposeBag) textField.rx.text .subscribe(onNext: { viewModel.setText($0) }) .disposed(by: disposeBag) ग़ྗͷ൓ө
  13. ྫ - ViewͱViewModelͷ઀ଓ let viewModel: SearchViewModel let tableView: UITableView let

    searchButton: UIButton let textField: UITextField viewModel.repositories .bind(to: tableView.rx.items(cellIdentifier: "Cell", cellType: UITableViewCell.self) ) { row, repository, cell in cell.textLabel?.text = repository.name } .disposed(by: disposeBag) searchButton.rx.tap .subscribe(onNext: { viewModel.search() }) .disposed(by: disposeBag) textField.rx.text .subscribe(onNext: { viewModel.setText($0) }) .disposed(by: disposeBag) ϢʔβʔΞΫγϣϯͷ఻ୡ
  14. ྫ - ࣮૷ʹ͕ࠩग़΍͍͢Օॴᶃ class SearchViewModel { let repositories: Observable<[Repository]> private

    let _repositories = BehaviorRelay<[Repository]>(value: []) private let _search = PublishRelay<Void>() private let _text = PublishRelay<String?>() private let disposeBag = DisposeBag() init() { let apiAction = SearchAPIAction() self.repositories = _repositories.asObservable() apiAction.response .bind(to: _repositories) .disposed(by: disposeBag) _search .withLatestFrom(_text) { $1 } .flatMap { $0.map(Observable.just) ?? .empty() } .subscribe(onNext: { apiAction.execute($0) }) .disposed(by: disposeBag) } func search() { _search.accept(()) } func setText(_ text: String?) { _text.accept(text) } }
  15. ྫ - ࣮૷ʹ͕ࠩग़΍͍͢Օॴᶃ class SearchViewModel { let repositories: Observable<[Repository]> private

    let _repositories = BehaviorRelay<[Repository]>(value: []) private let _search = PublishRelay<Void>() private let _text = PublishRelay<String?>() private let disposeBag = DisposeBag() init() { let apiAction = SearchAPIAction() self.repositories = _repositories.asObservable() apiAction.response .bind(to: _repositories) .disposed(by: disposeBag) _search .withLatestFrom(_text) { $1 } .flatMap { $0.map(Observable.just) ?? .empty() } .subscribe(onNext: { apiAction.execute($0) }) .disposed(by: disposeBag) } func search() { _search.accept(()) } func setText(_ text: String?) { _text.accept(text) } } JOUJBMJ[FSͷϩʔΧϧม਺ͱͯ͠ ఆٛ͢Δ͜ͱ΋Ͱ͖Δ
  16. ྫ - ࣮૷ʹ͕ࠩग़΍͍͢Օॴᶃ class SearchViewModel { let repositories: Observable<[Repository]> private

    let _search = PublishRelay<Void>() private let _text = PublishRelay<String?>() private let disposeBag = DisposeBag() init() { let apiAction = SearchAPIAction() let _repositories = BehaviorRelay<[Repository]>(value: []) self.repositories = _repositories.asObservable() apiAction.response .bind(to: _repositories) .disposed(by: disposeBag) _search .withLatestFrom(_text) { $1 } .flatMap { $0.map(Observable.just) ?? .empty() } .subscribe(onNext: { apiAction.execute($0) }) .disposed(by: disposeBag) } func search() { _search.accept(()) } func setText(_ text: String?) { _text.accept(text) } }
  17. ྫ - ࣮૷ʹ͕ࠩग़΍͍͢Օॴᶃ class SearchViewModel { let repositories: Observable<[Repository]> private

    let _search = PublishRelay<Void>() private let _text = PublishRelay<String?>() private let disposeBag = DisposeBag() init() { let apiAction = SearchAPIAction() let _repositories = BehaviorRelay<[Repository]>(value: []) self.repositories = _repositories.asObservable() apiAction.response .bind(to: _repositories) .disposed(by: disposeBag) _search .withLatestFrom(_text) { $1 } .flatMap { $0.map(Observable.just) ?? .empty() } .subscribe(onNext: { apiAction.execute($0) }) .disposed(by: disposeBag) } func search() { _search.accept(()) } func setText(_ text: String?) { _text.accept(text) } } JOUJBMJ[FSͷϩʔΧϧม਺ͱͯ͠ ఆٛ͢Δ͜ͱ΋Ͱ͖Δ
  18. ྫ - ࣮૷ʹ͕ࠩग़΍͍͢Օॴᶄ class SearchViewModel { let repositories: Observable<[Repository]> private

    let _repositories = BehaviorRelay<[Repository]>(value: []) private let _search = PublishRelay<Void>() private let _text = PublishRelay<String?>() private let disposeBag = DisposeBag() init() { let apiAction = SearchAPIAction() self.repositories = _repositories.asObservable() apiAction.response .bind(to: _repositories) .disposed(by: disposeBag) _search .withLatestFrom(_text) { $1 } .flatMap { $0.map(Observable.just) ?? .empty() } .subscribe(onNext: { apiAction.execute($0) }) .disposed(by: disposeBag) } func search() { _search.accept(()) } func setText(_ text: String?) { _text.accept(text) } }
  19. ྫ - ࣮૷ʹ͕ࠩग़΍͍͢Օॴᶄ class SearchViewModel { let repositories: Observable<[Repository]> private

    let _repositories = BehaviorRelay<[Repository]>(value: []) private let _search = PublishRelay<Void>() private let _text = PublishRelay<String?>() private let disposeBag = DisposeBag() init() { let apiAction = SearchAPIAction() self.repositories = _repositories.asObservable() apiAction.response .bind(to: _repositories) .disposed(by: disposeBag) _search .withLatestFrom(_text) { $1 } .flatMap { $0.map(Observable.just) ?? .empty() } .subscribe(onNext: { apiAction.execute($0) }) .disposed(by: disposeBag) } func search() { _search.accept(()) } func setText(_ text: String?) { _text.accept(text) } } "OZ0CTFSWFSͰఆٛ͢Δ͜ͱ΋Ͱ͖Δ
  20. ྫ - ࣮૷ʹ͕ࠩग़΍͍͢Օॴᶄ class SearchViewModel { let search: AnyObserver<Void> let

    setText: AnyObserver<String?> let repositories: Observable<[Repository]> private let _repositories = BehaviorRelay<[Repository]>(value: []) private let _search = PublishSubject<Void>() private let _setText = PublishSubject<String?>() private let disposeBag = DisposeBag() init() { let apiAction = SearchAPIAction() self.repositories = _repositories.asObservable() self.search = _search.asObserver() self.setText = _setText.asObserver() apiAction.response .bind(to: _repositories) .disposed(by: disposeBag) _search .withLatestFrom(_setText) { $1 } .flatMap { $0.map(Observable.just) ?? .empty() } .subscribe(onNext: { apiAction.execute($0) }) .disposed(by: disposeBag) } }
  21. ྫ - ࣮૷ʹ͕ࠩग़΍͍͢Օॴᶄ class SearchViewModel { let search: AnyObserver<Void> let

    setText: AnyObserver<String?> let repositories: Observable<[Repository]> private let _repositories = BehaviorRelay<[Repository]>(value: []) private let _search = PublishSubject<Void>() private let _setText = PublishSubject<String?>() private let disposeBag = DisposeBag() init() { let apiAction = SearchAPIAction() self.repositories = _repositories.asObservable() self.search = _search.asObserver() self.setText = _setText.asObserver() apiAction.response .bind(to: _repositories) .disposed(by: disposeBag) _search .withLatestFrom(_setText) { $1 } .flatMap { $0.map(Observable.just) ?? .empty() } .subscribe(onNext: { apiAction.execute($0) }) .disposed(by: disposeBag) } } "OZ0CTFSWFSͰఆٛ͢Δ͜ͱ΋Ͱ͖Δ
  22. ྫ - ࣮૷ʹ͕ࠩग़΍͍͢Օॴᶄ let viewModel: SearchViewModel let tableView: UITableView let

    searchButton: UIButton let textField: UITextField viewModel.repositories .bind(to: tableView.rx.items(cellIdentifier: "Cell", cellType: UITableViewCell.self) ) { row, repository, cell in cell.textLabel?.text = repository.name } .disposed(by: disposeBag) searchButton.rx.tap .bind(to: viewModel.search) .disposed(by: disposeBag) textField.rx.text .bind(to: viewModel.setText) .disposed(by: disposeBag)
  23. ྫ - ࣮૷ʹ͕ࠩग़΍͍͢Օॴᶄ let viewModel: SearchViewModel let tableView: UITableView let

    searchButton: UIButton let textField: UITextField viewModel.repositories .bind(to: tableView.rx.items(cellIdentifier: "Cell", cellType: UITableViewCell.self) ) { row, repository, cell in cell.textLabel?.text = repository.name } .disposed(by: disposeBag) searchButton.rx.tap .bind(to: viewModel.search) .disposed(by: disposeBag) textField.rx.text .bind(to: viewModel.setText) .disposed(by: disposeBag) 7JFX.PEFMଆ͕"OZ0CTFSWFSʹ ͳͬͨ͜ͱͰGVODCJOE UP ʹͳΔ
  24. ྫ - ࣮૷ʹ͕ࠩग़΍͍͢Օॴᶅ class SearchViewModel { let repositories: Observable<[Repository]> private

    let disposeBag = DisposeBag() init(search: Observable<Void>, setText: Observable<String?>) { let apiAction = SearchAPIAction() let _repositories = BehaviorRelay<[Repository]>(value: []) self.repositories = _repositories.asObservable() apiAction.response .bind(to: _repositories) .disposed(by: disposeBag) search .withLatestFrom(setText) { $1 } .flatMap { $0.map(Observable.just) ?? .empty() } .subscribe(onNext: { apiAction.execute($0) }) .disposed(by: disposeBag) } }
  25. ྫ - ࣮૷ʹ͕ࠩग़΍͍͢Օॴᶅ class SearchViewModel { let repositories: Observable<[Repository]> private

    let disposeBag = DisposeBag() init(search: Observable<Void>, setText: Observable<String?>) { let apiAction = SearchAPIAction() let _repositories = BehaviorRelay<[Repository]>(value: []) self.repositories = _repositories.asObservable() apiAction.response .bind(to: _repositories) .disposed(by: disposeBag) search .withLatestFrom(setText) { $1 } .flatMap { $0.map(Observable.just) ?? .empty() } .subscribe(onNext: { apiAction.execute($0) }) .disposed(by: disposeBag) } } *OJUJBMJ[FSͷҾ਺ͰೖྗΛड͚औΔ
  26. ྫ - ࣮૷ʹ͕ࠩग़΍͍͢Օॴᶅ let tableView: UITableView let searchButton: UIButton let

    textField: UITextField let viewModel = SearchViewModel( search: searchButton.rx.tap.asObservable(), setText: textField.rx.text.asObservable() ) viewModel.repositories .bind(to: tableView.rx.items(cellIdentifier: "Cell", cellType: UITableViewCell.self) ) { row, repository, cell in cell.textLabel?.text = repository.name } .disposed(by: disposeBag)
  27. ྫ - ࣮૷ʹ͕ࠩग़΍͍͢Օॴᶅ let tableView: UITableView let searchButton: UIButton let

    textField: UITextField let viewModel = SearchViewModel( search: searchButton.rx.tap.asObservable(), setText: textField.rx.text.asObservable() ) viewModel.repositories .bind(to: tableView.rx.items(cellIdentifier: "Cell", cellType: UITableViewCell.self) ) { row, repository, cell in cell.textLabel?.text = repository.name } .disposed(by: disposeBag) 7JFX.PEFMͷJOJUJBMJ[FSͷҾ਺ Ͱ౉͍ͯ͠ΔͷͰɺCJOEͷ࣮૷͕ 7JFXଆʹ͸ͳ͍
  28. ྫ - ࣮૷Ͱɺͦͷଞͷ΋Ͳ͔͍͠Օॴ class SearchViewModel { let repositories: Observable<[Repository]> private

    let _repositories = BehaviorRelay<[Repository]>(value: []) private let _search = PublishRelay<Void>() private let _text = PublishRelay<String?>() private let disposeBag = DisposeBag() init() { let apiAction = SearchAPIAction() self.repositories = _repositories.asObservable() apiAction.response .bind(to: _repositories) .disposed(by: disposeBag) _search .withLatestFrom(_text) { $1 } .flatMap { $0.map(Observable.just) ?? .empty() } .subscribe(onNext: { apiAction.execute($0) }) .disposed(by: disposeBag) } func search() { _search.accept(()) } func setText(_ text: String?) { _text.accept(text) } }
  29. ྫ - ࣮૷Ͱɺͦͷଞͷ΋Ͳ͔͍͠Օॴ class SearchViewModel { let repositories: Observable<[Repository]> private

    let _repositories = BehaviorRelay<[Repository]>(value: []) private let _search = PublishRelay<Void>() private let _text = PublishRelay<String?>() private let disposeBag = DisposeBag() init() { let apiAction = SearchAPIAction() self.repositories = _repositories.asObservable() apiAction.response .bind(to: _repositories) .disposed(by: disposeBag) _search .withLatestFrom(_text) { $1 } .flatMap { $0.map(Observable.just) ?? .empty() } .subscribe(onNext: { apiAction.execute($0) }) .disposed(by: disposeBag) } func search() { _search.accept(()) } func setText(_ text: String?) { _text.accept(text) } } ಺෦Ͱ͸ೖྗΛड͚෇͚Δ͚ͩͳ͸ͣͳͷʹ GVODBDDFQU @ ͕ݺ΂ͯ͠·͏
  30. ྫ - ࣮૷Ͱɺͦͷଞͷ΋Ͳ͔͍͠Օॴ class SearchViewModel { let repositories: Observable<[Repository]> private

    let _repositories = BehaviorRelay<[Repository]>(value: []) private let _search = PublishRelay<Void>() private let _text = PublishRelay<String?>() private let disposeBag = DisposeBag() init() { let apiAction = SearchAPIAction() self.repositories = _repositories.asObservable() apiAction.response .bind(to: _repositories) .disposed(by: disposeBag) _search .withLatestFrom(_text) { $1 } .flatMap { $0.map(Observable.just) ?? .empty() } .subscribe(onNext: { apiAction.execute($0) }) .disposed(by: disposeBag) } func search() { _search.accept(()) } func setText(_ text: String?) { _text.accept(text) } } ೖྗͷϦϨʔ༻ʹಉ͡Α͏ͳఆٛΛ ࠶౓͠ͳ͚Ε͹ͳΒͳ͍
  31. ྫ - ࣮૷Ͱɺͦͷଞͷ΋Ͳ͔͍͠Օॴ class SearchViewModel { let repositories: Observable<[Repository]> private

    let _repositories = BehaviorRelay<[Repository]>(value: []) private let _search = PublishRelay<Void>() private let _text = PublishRelay<String?>() private let disposeBag = DisposeBag() init() { let apiAction = SearchAPIAction() self.repositories = _repositories.asObservable() apiAction.response .bind(to: _repositories) .disposed(by: disposeBag) _search .withLatestFrom(_text) { $1 } .flatMap { $0.map(Observable.just) ?? .empty() } .subscribe(onNext: { apiAction.execute($0) }) .disposed(by: disposeBag) } func search() { _search.accept(()) } func setText(_ text: String?) { _text.accept(text) } } ಛͷఆٛ৔ॴ͕ܾ·͍ͬͯͳ͍ ͷͰΫϥε಺ͷͲ͜ʹͰ΋ఆٛ Ͱ͖ͯ͠·͏
  32. ViewModel͕େ͖͘ͳ͖ͬͯͨͱ͖ʹ 1. ࣮૷ํ๏ʹҧ͍͕͋Δ 2. ೖग़ྗ͕୯ํ޲ʹͳͬͯͳ͍ ݁ߏਏ͘ͳ͍Ͱ͔͢

  33. IUUQTHJUIVCDPNDBUTPTT6OJP

  34. • Input • Output • State • Extra • Logic

    • UnioStream • Input • Output • State • Extra • Logic • UnioStream Unioͷߏ੒ཁૉ
  35. • Input • Output • State • Extra • Logic

    • UnioStream Unioͷߏ੒ཁૉ
  36. Input - Unioͷߏ੒ཁૉ struct Input: InputType { let search =

    PublishRelay<String>() let selectIndex = PublishSubject<Int>() }
  37. Input - Unioͷߏ੒ཁૉ struct Input: InputType { let search =

    PublishRelay<String>() let selectIndex = PublishSubject<Int>() } 7JFX.PEFM΁ͷೖྗΛఆٛ͢Δ৔ॴ
  38. let input: InputWrapper<Input> input.accept("GitHub", for: \.search) Input - Unioͷߏ੒ཁૉ struct

    Input: InputType { let search = PublishRelay<String>() let selectIndex = PublishSubject<Int>() }
  39. let input: InputWrapper<Input> input.accept("GitHub", for: \.search) Input - Unioͷߏ੒ཁૉ struct

    Input: InputType { let search = PublishRelay<String>() let selectIndex = PublishSubject<Int>() } 6OJPͰ͸*OQVU8SBQQFSʹ ϥοϓ͞Εͯެ։͞ΕΔ
  40. let input: InputWrapper<Input> input.accept("GitHub", for: \.search) Input - Unioͷߏ੒ཁૉ struct

    Input: InputType { let search = PublishRelay<String>() let selectIndex = PublishSubject<Int>() } 1VCMJTI3FMBZͰఆٛ͞Ε͍ͯΔ͕ GVODBDDFQU @ ͔࣮͠ߦͰ͖ͳ͍ʂ
  41. InputWrapperͷ࢓૊Έ class InputWrapper<Input: InputType> { private let input: Input func

    accept<T>(_ value: T, for keyPath: KeyPath<Input, PublishRelay<T>>) { input[keyPath: keyPath].accept(value) } } struct Input: InputType { let search = PublishRelay<String>() let selectIndex = PublishSubject<Int>() } let input: InputWrapper<Input> input.accept("GitHub", for: \.search)
  42. InputWrapperͷ࢓૊Έ class InputWrapper<Input: InputType> { private let input: Input func

    accept<T>(_ value: T, for keyPath: KeyPath<Input, PublishRelay<T>>) { input[keyPath: keyPath].accept(value) } } QSJWBUFͰJOQVU͕಺෦Ͱอ࣋͞Ε͍ͯΔ ͭ·Γɺ*OQVU8SBQQFS֎͔ΒJOQVUࣗମ΍ ͦͷQSPQFSUZʹ௚઀ΞΫηεͰ͖ͳ͍ struct Input: InputType { let search = PublishRelay<String>() let selectIndex = PublishSubject<Int>() } let input: InputWrapper<Input> input.accept("GitHub", for: \.search)
  43. InputWrapperͷ࢓૊Έ class InputWrapper<Input: InputType> { private let input: Input func

    accept<T>(_ value: T, for keyPath: KeyPath<Input, PublishRelay<T>>) { input[keyPath: keyPath].accept(value) } } *OQVUͰ1VCMJTI3FMBZ5Ͱఆٛ͞Ε͍ͯΔ QSPQFSUZͷ,FZ1BUIΛड͚औΔ struct Input: InputType { let search = PublishRelay<String>() let selectIndex = PublishSubject<Int>() } let input: InputWrapper<Input> input.accept("GitHub", for: \.search)
  44. ,FZ1BUI͔Β*OQVU಺ͷ1VCMJTI3FMBZ5Λऔಘ͢Δ ͜ͷ··ฦͯ͠͠·͏ͱ1VCMJTI3FMZ5ͳͷͰ GVODBT0CTFSWBCMF ʹ΋ΞΫηεͰ͖ͯ͠·͏ InputWrapperͷ࢓૊Έ class InputWrapper<Input: InputType> { private

    let input: Input func accept<T>(_ value: T, for keyPath: KeyPath<Input, PublishRelay<T>>) { input[keyPath: keyPath].accept(value) } } struct Input: InputType { let search = PublishRelay<String>() let selectIndex = PublishSubject<Int>() } let input: InputWrapper<Input> input.accept("GitHub", for: \.search)
  45. ೖྗ͢Δ͚ͩͷঢ়ଶΛ࣮ݱ͢ΔͨΊʹ ಺෦ͰGVODBDDFQU @ Λ࣮ߦ͢Δ InputWrapperͷ࢓૊Έ class InputWrapper<Input: InputType> { private

    let input: Input func accept<T>(_ value: T, for keyPath: KeyPath<Input, PublishRelay<T>>) { input[keyPath: keyPath].accept(value) } } struct Input: InputType { let search = PublishRelay<String>() let selectIndex = PublishSubject<Int>() } let input: InputWrapper<Input> input.accept("GitHub", for: \.search)
  46. • Input • Output • State • Extra • Logic

    • UnioStream Unioͷߏ੒ཁૉ
  47. Output - Unioͷߏ੒ཁૉ struct Output: OutputType { let isHidden =

    BehaviorRelay(value: false) let title = BehaviorSubject(value: "title") }
  48. Output - Unioͷߏ੒ཁૉ struct Output: OutputType { let isHidden =

    BehaviorRelay(value: false) let title = BehaviorSubject(value: "title") } 7JFX.PEFM΁ͷग़ྗΛఆٛ͢Δ৔ॴ
  49. Output - Unioͷߏ੒ཁૉ let output: OutputWrapper<Output> let label: UILabel label.isHidden

    = output.value(for: \.isHideen) output.observable(for: \.isHideen).bind(to: label.rx.isHidden) struct Output: OutputType { let isHidden = BehaviorRelay(value: false) let title = BehaviorSubject(value: "title") }
  50. Output - Unioͷߏ੒ཁૉ let output: OutputWrapper<Output> let label: UILabel label.isHidden

    = output.value(for: \.isHideen) output.observable(for: \.isHideen).bind(to: label.rx.isHidden) struct Output: OutputType { let isHidden = BehaviorRelay(value: false) let title = BehaviorSubject(value: "title") } 6OJPͰ͸0VUQVU8SBQQFSʹ ϥοϓ͞Εͯެ։͞ΕΔ
  51. Output - Unioͷߏ੒ཁૉ let output: OutputWrapper<Output> let label: UILabel label.isHidden

    = output.value(for: \.isHideen) output.observable(for: \.isHideen).bind(to: label.rx.isHidden) struct Output: OutputType { let isHidden = BehaviorRelay(value: false) let title = BehaviorSubject(value: "title") } #FIBWJPS3FMBZͰఆٛ͞Ε͍ͯΔ͕WBMVF΍ GVODBT0CTFSWBCMF ͔࣮͠ߦͰ͖ͳ͍ʂ
  52. struct Output: OutputType { let isHidden = BehaviorRelay(value: false) let

    title = BehaviorSubject(value: "title") } let output: OutputWrapper<Output> let label: UILabel label.isHidden = output.value(for: \.isHideen) output.observable(for: \.isHideen).bind(to: label.rx.isHidden) class OutputWrapper<Output: OutputType> { private let output: Output func value<T>(for keyPath: KeyPath<Output, BehaviorRelay<T>>) -> T { return output[keyPath: keyPath].value } func observable<T>(for keyPath: KeyPath<Output, BehaviorRelay<T>>) -> Observable<T> { return output[keyPath: keyPath].asObservable() } } OutputWrapperͷ࢓૊Έ
  53. class OutputWrapper<Output: OutputType> { private let output: Output func value<T>(for

    keyPath: KeyPath<Output, BehaviorRelay<T>>) -> T { return output[keyPath: keyPath].value } func observable<T>(for keyPath: KeyPath<Output, BehaviorRelay<T>>) -> Observable<T> { return output[keyPath: keyPath].asObservable() } } QSJWBUFͰPVUQVU͕಺෦Ͱอ࣋͞Ε͍ͯΔ ͭ·Γɺ0VUQVU8SBQQFS֎͔ΒJOQVUࣗମ΍ ͦͷQSPQFSUZʹ௚઀ΞΫηεͰ͖ͳ͍ OutputWrapperͷ࢓૊Έ struct Output: OutputType { let isHidden = BehaviorRelay(value: false) let title = BehaviorSubject(value: "title") } let output: OutputWrapper<Output> let label: UILabel label.isHidden = output.value(for: \.isHideen) output.observable(for: \.isHideen).bind(to: label.rx.isHidden)
  54. class OutputWrapper<Output: OutputType> { private let output: Output func value<T>(for

    keyPath: KeyPath<Output, BehaviorRelay<T>>) -> T { return output[keyPath: keyPath].value } func observable<T>(for keyPath: KeyPath<Output, BehaviorRelay<T>>) -> Observable<T> { return output[keyPath: keyPath].asObservable() } } OutputWrapperͷ࢓૊Έ struct Output: OutputType { let isHidden = BehaviorRelay(value: false) let title = BehaviorSubject(value: "title") } let output: OutputWrapper<Output> let label: UILabel label.isHidden = output.value(for: \.isHideen) output.observable(for: \.isHideen).bind(to: label.rx.isHidden) 0VUQVUͰ#FIBWJPS3FMBZ5Ͱఆٛ͞Ε͍ͯΔ QSPQFSUZͷ,FZ1BUIΛड͚औΔ
  55. class OutputWrapper<Output: OutputType> { private let output: Output func value<T>(for

    keyPath: KeyPath<Output, BehaviorRelay<T>>) -> T { return output[keyPath: keyPath].value } func observable<T>(for keyPath: KeyPath<Output, BehaviorRelay<T>>) -> Observable<T> { return output[keyPath: keyPath].asObservable() } } OutputWrapperͷ࢓૊Έ struct Output: OutputType { let isHidden = BehaviorRelay(value: false) let title = BehaviorSubject(value: "title") } let output: OutputWrapper<Output> let label: UILabel label.isHidden = output.value(for: \.isHideen) output.observable(for: \.isHideen).bind(to: label.rx.isHidden) ,FZ1BUI͔Β0VUQVU಺ͷ#FIBWJPS3FMBZ5Λऔಘ͢Δ ͜ͷ··ฦͯ͠͠·͏ͱ#FIBWJPS3FMZ5ͳͷͰ GVODBDDFQU @ ʹ΋ΞΫηεͰ͖ͯ͠·͏
  56. class OutputWrapper<Output: OutputType> { private let output: Output func value<T>(for

    keyPath: KeyPath<Output, BehaviorRelay<T>>) -> T { return output[keyPath: keyPath].value } func observable<T>(for keyPath: KeyPath<Output, BehaviorRelay<T>>) -> Observable<T> { return output[keyPath: keyPath].asObservable() } } OutputWrapperͷ࢓૊Έ struct Output: OutputType { let isHidden = BehaviorRelay(value: false) let title = BehaviorSubject(value: "title") } let output: OutputWrapper<Output> let label: UILabel label.isHidden = output.value(for: \.isHideen) output.observable(for: \.isHideen).bind(to: label.rx.isHidden) ஋Λड͚औΔ͚ͩͷঢ়ଶΛ࣮ݱ͢Δ ͨΊʹ಺෦ͰWBMVFΛฦ͢
  57. class OutputWrapper<Output: OutputType> { private let output: Output func value<T>(for

    keyPath: KeyPath<Output, BehaviorRelay<T>>) -> T { return output[keyPath: keyPath].value } func observable<T>(for keyPath: KeyPath<Output, BehaviorRelay<T>>) -> Observable<T> { return output[keyPath: keyPath].asObservable() } } OutputWrapperͷ࢓૊Έ struct Output: OutputType { let isHidden = BehaviorRelay(value: false) let title = BehaviorSubject(value: "title") } let output: OutputWrapper<Output> let label: UILabel label.isHidden = output.value(for: \.isHideen) output.observable(for: \.isHideen).bind(to: label.rx.isHidden) 0CTFSWBCMFΛड͚औΔ͚ͩͷঢ়ଶΛ࣮ݱ͢ΔͨΊʹ ಺෦ͰGVODBT0CTFSWBCMF ͨ݁͠ՌΛฦ͢
  58. Output - Unioͷߏ੒ཁૉ let output: OutputWrapper<Output> let label: UILabel label.text

    = try? output.value(for: \.title) output.observable(for: \.title).bind(to: label.rx.text) struct Output: OutputType { let isHidden = BehaviorRelay(value: false) let title = BehaviorSubject(value: "title") }
  59. Output - Unioͷߏ੒ཁૉ let output: OutputWrapper<Output> let label: UILabel label.text

    = try? output.value(for: \.title) output.observable(for: \.title).bind(to: label.rx.text) struct Output: OutputType { let isHidden = BehaviorRelay(value: false) let title = BehaviorSubject(value: "title") } #FIBWJSP4VCKFDUͷ৔߹ɺ஋ʹΞΫηε͢Δࡍʹྫ֎Λ ౤͛Δ৔߹͕͋ΔͷͰɺUSZ͢Δ͜ͱͰΞΫηεͰ͖Δ
  60. • Input • Output • State • Extra • Logic

    • UnioStream Unioͷߏ੒ཁૉ
  61. State - Unioͷߏ੒ཁૉ struct State: StateType { let count =

    BehaviorRelay(value: 0) }
  62. State - Unioͷߏ੒ཁૉ struct State: StateType { let count =

    BehaviorRelay(value: 0) } 7JFX.PEFMͷ಺෦ঢ়ଶΛఆٛ͢Δ৔ॴ
  63. • Input • Output • State • Extra • Logic

    • UnioStream Unioͷߏ੒ཁૉ
  64. Extra - Unioͷߏ੒ཁૉ struct Extra: ExtraType { let userDefaults: UserDefaults

    let notificationCenter: NotificationCenter }
  65. Extra - Unioͷߏ੒ཁૉ struct Extra: ExtraType { let userDefaults: UserDefaults

    let notificationCenter: NotificationCenter } 7JFX.PEFMͷ֎෦ґଘΛఆٛ͢Δ৔ॴ
  66. • Input • Output • State • Extra • Logic

    • UnioStream Unioͷߏ੒ཁૉ
  67. Logic - Unioͷߏ੒ཁૉ struct Logic: LogicType { private let disposeBag

    = DisposeBag() func bind(from dependency: Dependency<Input, State, NoExtra>) -> Output { dependency.inputObservable(for: \.increment) .withLatestFrom(dependency.state.count) { $1 + 1 } .bind(to: dependency.state.count) .disposed(by: disposeBag) return Output(text: dependency.state.count.map(String.init)) } } struct Input: InputType { let increment = PublishRelay<Void>() } struct Output: OutputType { let text: Observable<String> } struct State: StateType { let count = BehaviorRelay(value: 0) }
  68. Logic - Unioͷߏ੒ཁૉ struct Logic: LogicType { private let disposeBag

    = DisposeBag() func bind(from dependency: Dependency<Input, State, NoExtra>) -> Output { dependency.inputObservable(for: \.increment) .withLatestFrom(dependency.state.count) { $1 + 1 } .bind(to: dependency.state.count) .disposed(by: disposeBag) return Output(text: dependency.state.count.map(String.init)) } } struct Input: InputType { let increment = PublishRelay<Void>() } struct Output: OutputType { let text: Observable<String> } struct State: StateType { let count = BehaviorRelay(value: 0) } *OQVUɾ4UBUFɾ&YUSBΛར༻ͯ͠ 0VUQVUΛੜ੒Ͱ͖Δ།Ұͷ৔ॴ
  69. Logic - Unioͷߏ੒ཁૉ struct Logic: LogicType { private let disposeBag

    = DisposeBag() func bind(from dependency: Dependency<Input, State, NoExtra>) -> Output { dependency.inputObservable(for: \.increment) .withLatestFrom(dependency.state.count) { $1 + 1 } .bind(to: dependency.state.count) .disposed(by: disposeBag) return Output(text: dependency.state.count.map(String.init)) } } struct Input: InputType { let increment = PublishRelay<Void>() } struct Output: OutputType { let text: Observable<String> } struct State: StateType { let count = BehaviorRelay(value: 0) } *OQVUɾ4UBUFɾ&YUSBʹΞΫηεͰ͖Δ
  70. Logic - Unioͷߏ੒ཁૉ struct Logic: LogicType { private let disposeBag

    = DisposeBag() func bind(from dependency: Dependency<Input, State, NoExtra>) -> Output { dependency.inputObservable(for: \.increment) .withLatestFrom(dependency.state.count) { $1 + 1 } .bind(to: dependency.state.count) .disposed(by: disposeBag) return Output(text: dependency.state.count.map(String.init)) } } struct Input: InputType { let increment = PublishRelay<Void>() } struct Output: OutputType { let text: Observable<String> } struct State: StateType { let count = BehaviorRelay(value: 0) } %FQFOEFODZΛհͯ͠ɺ*OQVUͷ೚ҙͷ1VCMJTI3FMBZʹ ରͯ͠0CTFSWBCMFͱͯ͠ΞΫηε͢Δ͜ͱ͕Ͱ͖Δ ΋ͪΖΜɺGVODBDDFQU @ ͷݺͼग़͠͸Ͱ͖ͳ͍
  71. Logic - Unioͷߏ੒ཁૉ struct Logic: LogicType { private let disposeBag

    = DisposeBag() func bind(from dependency: Dependency<Input, State, NoExtra>) -> Output { dependency.inputObservable(for: \.increment) .withLatestFrom(dependency.state.count) { $1 + 1 } .bind(to: dependency.state.count) .disposed(by: disposeBag) return Output(text: dependency.state.count.map(String.init)) } } struct Input: InputType { let increment = PublishRelay<Void>() } struct Output: OutputType { let text: Observable<String> } struct State: StateType { let count = BehaviorRelay(value: 0) } %FQFOEFODZΛհͯ͠ɺ4UBUFʹΞΫηεͰ͖Δ
  72. Logic - Unioͷߏ੒ཁૉ struct Logic: LogicType { private let disposeBag

    = DisposeBag() func bind(from dependency: Dependency<Input, State, NoExtra>) -> Output { dependency.inputObservable(for: \.increment) .withLatestFrom(dependency.state.count) { $1 + 1 } .bind(to: dependency.state.count) .disposed(by: disposeBag) return Output(text: dependency.state.count.map(String.init)) } } struct Input: InputType { let increment = PublishRelay<Void>() } struct Output: OutputType { let text: Observable<String> } struct State: StateType { let count = BehaviorRelay(value: 0) } *OQVUɾ4UBUFɾ&YUSBΛར༻ͯ͠0VUQVUΛੜ੒
  73. • Input • Output • State • Extra • Logic

    • UnioStream Unioͷߏ੒ཁૉ
  74. UnioStream - Unioͷߏ੒ཁૉ protocol CounterViewStreamType: AnyObject { var input: InputWrapper<CounterViewStream.Input>

    { get } var output: OutputWrapper<CounterViewStream.Output> { get } } final class CounterViewStream: UnioStream<CounterViewStream.Logic>, CounterViewStreamType { struct Input: InputType { let increment = PublishRelay<Void>() } struct Output: OutputType { let text: Observable<String> } struct State: StateType { let count = BehaviorRelay(value: 0) } struct Logic: LogicType { private let disposeBag = DisposeBag() func bind(from dependency: Dependency<Input, State, NoExtra>) -> Output { ... return Output(text: dependency.state.count.map(String.init)) } } }
  75. UnioStream - Unioͷߏ੒ཁૉ protocol CounterViewStreamType: AnyObject { var input: InputWrapper<CounterViewStream.Input>

    { get } var output: OutputWrapper<CounterViewStream.Output> { get } } final class CounterViewStream: UnioStream<CounterViewStream.Logic>, CounterViewStreamType { struct Input: InputType { let increment = PublishRelay<Void>() } struct Output: OutputType { let text: Observable<String> } struct State: StateType { let count = BehaviorRelay(value: 0) } struct Logic: LogicType { private let disposeBag = DisposeBag() func bind(from dependency: Dependency<Input, State, NoExtra>) -> Output { ... return Output(text: dependency.state.count.map(String.init)) } } } *OQVUɾ0VUQVUɾ4UBUFɾ&YUSBɾ-PHJDΛ ͭͳ͗߹Θͤͯ୯Ұํ޲ͷೖग़ྗΛ࣮ݱ͢Δ
  76. let stream: CounterViewStreamType let button: UIButton let label: UILabel button.rx.tap

    .bind(to: stream.input.accept(for: \.increment)) .disposed(by: disposeBag) stream.output.observable(for: \.text) .bind(to: label.rx.text) .disposed(by: disposeBag) UnioStream - Unioͷߏ੒ཁૉ
  77. let stream: CounterViewStreamType let button: UIButton let label: UILabel button.rx.tap

    .bind(to: stream.input.accept(for: \.increment)) .disposed(by: disposeBag) stream.output.observable(for: \.text) .bind(to: label.rx.text) .disposed(by: disposeBag) UnioStream - Unioͷߏ੒ཁૉ 6OJP4USFBN͕อ͍࣋ͯ͠Δ*OQVU8SBQQFSΛ հͯ͠ϘλϯͷλοϓΛೖྗͱͯ͠఻ୡ͢Δ
  78. let stream: CounterViewStreamType let button: UIButton let label: UILabel button.rx.tap

    .bind(to: stream.input.accept(for: \.increment)) .disposed(by: disposeBag) stream.output.observable(for: \.text) .bind(to: label.rx.text) .disposed(by: disposeBag) UnioStream - Unioͷߏ੒ཁૉ 6OJP4USFBN͕อ͍࣋ͯ͠Δ0VUQVU8SBQQFSΛ հͯ͠ग़ྗΛϥϕϧʹ൓ө͢Δ
  79. UnioStreamͷ࢓૊Έ open class UnioStream<Logic: LogicType> { public let input: InputWrapper<Logic.Input>

    public let output: OutputWrapper<Logic.Output> private let _state: Logic.State private let _extra: Logic.Extra private let _logic: Logic public init(input: Logic.Input, state: Logic.State, extra: Logic.Extra, logic: Logic) { let dependency = Dependency(input: input, state: state, extra: extra) let output = logic.bind(from: dependency) self.input = InputWrapper(input) self.output = OutputWrapper(output) self._state = state self._extra = extra self._logic = logic } }
  80. UnioStreamͷ࢓૊Έ open class UnioStream<Logic: LogicType> { public let input: InputWrapper<Logic.Input>

    public let output: OutputWrapper<Logic.Output> private let _state: Logic.State private let _extra: Logic.Extra private let _logic: Logic public init(input: Logic.Input, state: Logic.State, extra: Logic.Extra, logic: Logic) { let dependency = Dependency(input: input, state: state, extra: extra) let output = logic.bind(from: dependency) self.input = InputWrapper(input) self.output = OutputWrapper(output) self._state = state self._extra = extra self._logic = logic } } *OQVU8SBQQFSͱ0VUQVU8SBQQFSΛ QSPQFSUZͱͯ͠อ͍࣋ͯ͠Δ
  81. UnioStreamͷ࢓૊Έ open class UnioStream<Logic: LogicType> { public let input: InputWrapper<Logic.Input>

    public let output: OutputWrapper<Logic.Output> private let _state: Logic.State private let _extra: Logic.Extra private let _logic: Logic public init(input: Logic.Input, state: Logic.State, extra: Logic.Extra, logic: Logic) { let dependency = Dependency(input: input, state: state, extra: extra) let output = logic.bind(from: dependency) self.input = InputWrapper(input) self.output = OutputWrapper(output) self._state = state self._extra = extra self._logic = logic } } ॳظԽ࣌ʹ%FQFOEFODZ͕ੜ੒͞Εɺ-PHJDͷ GVODCJOE GSPN ͕౓͚ͩݺ͹ΕΔ ͦͯ͠ɺGVODCJOE GSPN ͔ΒPVUQVU͕ੜ੒͞ΕΔ
  82. UnioStreamͷ࢓૊Έ open class UnioStream<Logic: LogicType> { public let input: InputWrapper<Logic.Input>

    public let output: OutputWrapper<Logic.Output> private let _state: Logic.State private let _extra: Logic.Extra private let _logic: Logic public init(input: Logic.Input, state: Logic.State, extra: Logic.Extra, logic: Logic) { let dependency = Dependency(input: input, state: state, extra: extra) let output = logic.bind(from: dependency) self.input = InputWrapper(input) self.output = OutputWrapper(output) self._state = state self._extra = extra self._logic = logic } } JOQVUͱੜ੒͞ΕͨPVUQVUΛ΋ͱʹ 6OJP4USFBNͷೖग़ྗΛ֬ఆ͢Δ
  83. UnioͱViewModelͷൺֱ class SearchViewStream: UnioStream<SearchViewStream.Logic> { struct Input: InputType { let

    search = PublishRelay<Void>() let text = PublishRelay<String?>() } struct Output: OutputType { let repositories: Observable<[Repository]> } struct State: StateType { let repositories = BehaviorRelay<[Repository]>(value: []) } struct Extra: ExtraType { let apiAction = SearchAPIAction() } struct Logic: LogicType { let disposeBag = DisposeBag() func bind(from dependency: Dependency<Input, State, Extra>) -> Output { let state = dependency.state let extra = dependency.extra extra.apiAction.response .bind(to: state.repositories) .disposed(by: disposeBag) dependency.inputObservable(for: \.search) .withLatestFrom(dependency.inputObservable(for: \.text)) { $1 } .flatMap { $0.map(Observable.just) ?? .empty() } .subscribe(onNext: { extra.apiAction.execute($0) }) .disposed(by: disposeBag) return Output(repositories: state.repositories.asObservable()) } } } class SearchViewModel { let repositories: Observable<[Repository]> private let _repositories = BehaviorRelay<[Repository]>(value: []) private let _search = PublishRelay<Void>() private let _text = PublishRelay<String?>() private let disposeBag = DisposeBag() init() { let apiAction = SearchAPIAction() self.repositories = _repositories.asObservable() apiAction.response .bind(to: _repositories) .disposed(by: disposeBag) _search .withLatestFrom(_text) { $1 } .flatMap { $0.map(Observable.just) ?? .empty() } .subscribe(onNext: { apiAction.execute($0) }) .disposed(by: disposeBag) } func search() { _search.accept(()) } func setText(_ text: String?) { _text.accept(text) } }
  84. UnioͱViewModelͷൺֱ class SearchViewStream: UnioStream<SearchViewStream.Logic> { struct Input: InputType { let

    search = PublishRelay<Void>() let text = PublishRelay<String?>() } struct Output: OutputType { let repositories: Observable<[Repository]> } struct State: StateType { let repositories = BehaviorRelay<[Repository]>(value: []) } struct Extra: ExtraType { let apiAction = SearchAPIAction() } struct Logic: LogicType { let disposeBag = DisposeBag() func bind(from dependency: Dependency<Input, State, Extra>) -> Output { let state = dependency.state let extra = dependency.extra extra.apiAction.response .bind(to: state.repositories) .disposed(by: disposeBag) dependency.inputObservable(for: \.search) .withLatestFrom(dependency.inputObservable(for: \.text)) { $1 } .flatMap { $0.map(Observable.just) ?? .empty() } .subscribe(onNext: { extra.apiAction.execute($0) }) .disposed(by: disposeBag) return Output(repositories: state.repositories.asObservable()) } } } class SearchViewModel { let repositories: Observable<[Repository]> private let _repositories = BehaviorRelay<[Repository]>(value: []) private let _search = PublishRelay<Void>() private let _text = PublishRelay<String?>() private let disposeBag = DisposeBag() init() { let apiAction = SearchAPIAction() self.repositories = _repositories.asObservable() apiAction.response .bind(to: _repositories) .disposed(by: disposeBag) _search .withLatestFrom(_text) { $1 } .flatMap { $0.map(Observable.just) ?? .empty() } .subscribe(onNext: { apiAction.execute($0) }) .disposed(by: disposeBag) } func search() { _search.accept(()) } func setText(_ text: String?) { _text.accept(text) } }
  85. UnioͱViewModelͷൺֱ class SearchViewStream: UnioStream<SearchViewStream.Logic> { struct Input: InputType { let

    search = PublishRelay<Void>() let text = PublishRelay<String?>() } struct Output: OutputType { let repositories: Observable<[Repository]> } struct State: StateType { let repositories = BehaviorRelay<[Repository]>(value: []) } struct Extra: ExtraType { let apiAction = SearchAPIAction() } struct Logic: LogicType { let disposeBag = DisposeBag() func bind(from dependency: Dependency<Input, State, Extra>) -> Output { let state = dependency.state let extra = dependency.extra extra.apiAction.response .bind(to: state.repositories) .disposed(by: disposeBag) dependency.inputObservable(for: \.search) .withLatestFrom(dependency.inputObservable(for: \.text)) { $1 } .flatMap { $0.map(Observable.just) ?? .empty() } .subscribe(onNext: { extra.apiAction.execute($0) }) .disposed(by: disposeBag) return Output(repositories: state.repositories.asObservable()) } } } class SearchViewModel { let repositories: Observable<[Repository]> private let _repositories = BehaviorRelay<[Repository]>(value: []) private let _search = PublishRelay<Void>() private let _text = PublishRelay<String?>() private let disposeBag = DisposeBag() init() { let apiAction = SearchAPIAction() self.repositories = _repositories.asObservable() apiAction.response .bind(to: _repositories) .disposed(by: disposeBag) _search .withLatestFrom(_text) { $1 } .flatMap { $0.map(Observable.just) ?? .empty() } .subscribe(onNext: { apiAction.execute($0) }) .disposed(by: disposeBag) } func search() { _search.accept(()) } func setText(_ text: String?) { _text.accept(text) } }
  86. UnioͱViewModelͷൺֱ class SearchViewStream: UnioStream<SearchViewStream.Logic> { struct Input: InputType { let

    search = PublishRelay<Void>() let text = PublishRelay<String?>() } struct Output: OutputType { let repositories: Observable<[Repository]> } struct State: StateType { let repositories = BehaviorRelay<[Repository]>(value: []) } struct Extra: ExtraType { let apiAction = SearchAPIAction() } struct Logic: LogicType { let disposeBag = DisposeBag() func bind(from dependency: Dependency<Input, State, Extra>) -> Output { let state = dependency.state let extra = dependency.extra extra.apiAction.response .bind(to: state.repositories) .disposed(by: disposeBag) dependency.inputObservable(for: \.search) .withLatestFrom(dependency.inputObservable(for: \.text)) { $1 } .flatMap { $0.map(Observable.just) ?? .empty() } .subscribe(onNext: { extra.apiAction.execute($0) }) .disposed(by: disposeBag) return Output(repositories: state.repositories.asObservable()) } } } class SearchViewModel { let repositories: Observable<[Repository]> private let _repositories = BehaviorRelay<[Repository]>(value: []) private let _search = PublishRelay<Void>() private let _text = PublishRelay<String?>() private let disposeBag = DisposeBag() init() { let apiAction = SearchAPIAction() self.repositories = _repositories.asObservable() apiAction.response .bind(to: _repositories) .disposed(by: disposeBag) _search .withLatestFrom(_text) { $1 } .flatMap { $0.map(Observable.just) ?? .empty() } .subscribe(onNext: { apiAction.execute($0) }) .disposed(by: disposeBag) } func search() { _search.accept(()) } func setText(_ text: String?) { _text.accept(text) } }
  87. UnioͱViewModelͷൺֱ class SearchViewStream: UnioStream<SearchViewStream.Logic> { struct Input: InputType { let

    search = PublishRelay<Void>() let text = PublishRelay<String?>() } struct Output: OutputType { let repositories: Observable<[Repository]> } struct State: StateType { let repositories = BehaviorRelay<[Repository]>(value: []) } struct Extra: ExtraType { let apiAction = SearchAPIAction() } struct Logic: LogicType { let disposeBag = DisposeBag() func bind(from dependency: Dependency<Input, State, Extra>) -> Output { let state = dependency.state let extra = dependency.extra extra.apiAction.response .bind(to: state.repositories) .disposed(by: disposeBag) dependency.inputObservable(for: \.search) .withLatestFrom(dependency.inputObservable(for: \.text)) { $1 } .flatMap { $0.map(Observable.just) ?? .empty() } .subscribe(onNext: { extra.apiAction.execute($0) }) .disposed(by: disposeBag) return Output(repositories: state.repositories.asObservable()) } } } class SearchViewModel { let repositories: Observable<[Repository]> private let _repositories = BehaviorRelay<[Repository]>(value: []) private let _search = PublishRelay<Void>() private let _text = PublishRelay<String?>() private let disposeBag = DisposeBag() init() { let apiAction = SearchAPIAction() self.repositories = _repositories.asObservable() apiAction.response .bind(to: _repositories) .disposed(by: disposeBag) _search .withLatestFrom(_text) { $1 } .flatMap { $0.map(Observable.just) ?? .empty() } .subscribe(onNext: { apiAction.execute($0) }) .disposed(by: disposeBag) } func search() { _search.accept(()) } func setText(_ text: String?) { _text.accept(text) } }
  88. UnioͱViewModelͷൺֱ class SearchViewStream: UnioStream<SearchViewStream.Logic> { struct Input: InputType { let

    search = PublishRelay<Void>() let text = PublishRelay<String?>() } struct Output: OutputType { let repositories: Observable<[Repository]> } struct State: StateType { let repositories = BehaviorRelay<[Repository]>(value: []) } struct Extra: ExtraType { let apiAction = SearchAPIAction() } struct Logic: LogicType { let disposeBag = DisposeBag() func bind(from dependency: Dependency<Input, State, Extra>) -> Output { let state = dependency.state let extra = dependency.extra extra.apiAction.response .bind(to: state.repositories) .disposed(by: disposeBag) dependency.inputObservable(for: \.search) .withLatestFrom(dependency.inputObservable(for: \.text)) { $1 } .flatMap { $0.map(Observable.just) ?? .empty() } .subscribe(onNext: { extra.apiAction.execute($0) }) .disposed(by: disposeBag) return Output(repositories: state.repositories.asObservable()) } } } class SearchViewModel { let repositories: Observable<[Repository]> private let _repositories = BehaviorRelay<[Repository]>(value: []) private let _search = PublishRelay<Void>() private let _text = PublishRelay<String?>() private let disposeBag = DisposeBag() init() { let apiAction = SearchAPIAction() self.repositories = _repositories.asObservable() apiAction.response .bind(to: _repositories) .disposed(by: disposeBag) _search .withLatestFrom(_text) { $1 } .flatMap { $0.map(Observable.just) ?? .empty() } .subscribe(onNext: { apiAction.execute($0) }) .disposed(by: disposeBag) } func search() { _search.accept(()) } func setText(_ text: String?) { _text.accept(text) } }
  89. UnioͰͰ͖ΔΑ͏ʹͳͬͨ͜ͱ • ೖग़ྗɾ಺෦ঢ়ଶͷఆٛ৔ॴ͕໌ ֬ʹͳͬͨ • ྨࣅͨ͠ೖྗΛෳ਺ఆٛ͢Δඞཁ ͕ͳ͘ͳͬͨ • WrapperͱDependencyʹΑͬ ͯɺ୯Ұํ޲ͷೖྗΛܕͰറΔ͜

    ͱ͕Ͱ͖ΔΑ͏ʹͳͬͨ
  90. Model΋UnioStreamԽ class SearchViewStream: UnioStream<SearchViewStream.Logic> { struct Input: InputType { let

    search = PublishRelay<Void>() let text = PublishRelay<String?>() } struct Output: OutputType { let repositories: Observable<[Repository]> } struct State: StateType { let repositories = BehaviorRelay<[Repository]>(value: []) } struct Extra: ExtraType { let apiAction = SearchAPIAction() } struct Logic: LogicType { let disposeBag = DisposeBag() func bind(from dependency: Dependency<Input, State, Extra>) -> Output { let state = dependency.state let extra = dependency.extra extra.apiAction.response .bind(to: state.repositories) .disposed(by: disposeBag) dependency.inputObservable(for: \.search) .withLatestFrom(dependency.inputObservable(for: \.text)) { $1 } .flatMap { $0.map(Observable.just) ?? .empty() } .subscribe(onNext: { extra.apiAction.execute($0) }) .disposed(by: disposeBag) return Output(repositories: state.repositories.asObservable()) } } }
  91. Model΋UnioStreamԽ class SearchViewStream: UnioStream<SearchViewStream.Logic> { struct Input: InputType { let

    search = PublishRelay<Void>() let text = PublishRelay<String?>() } struct Output: OutputType { let repositories: Observable<[Repository]> } struct State: StateType { let repositories = BehaviorRelay<[Repository]>(value: []) } struct Extra: ExtraType { let apiAction = SearchAPIAction() } struct Logic: LogicType { let disposeBag = DisposeBag() func bind(from dependency: Dependency<Input, State, Extra>) -> Output { let state = dependency.state let extra = dependency.extra extra.apiAction.response .bind(to: state.repositories) .disposed(by: disposeBag) dependency.inputObservable(for: \.search) .withLatestFrom(dependency.inputObservable(for: \.text)) { $1 } .flatMap { $0.map(Observable.just) ?? .empty() } .subscribe(onNext: { extra.apiAction.execute($0) }) .disposed(by: disposeBag) return Output(repositories: state.repositories.asObservable()) } } }
  92. Model΋UnioStreamԽ class SearchViewStream: UnioStream<SearchViewStream.Logic> { struct Input: InputType { let

    search = PublishRelay<Void>() let text = PublishRelay<String?>() } struct Output: OutputType { let repositories: Observable<[Repository]> } struct State: StateType { let repositories = BehaviorRelay<[Repository]>(value: []) } struct Extra: ExtraType { let apiAction = SearchAPIAction() } struct Logic: LogicType { let disposeBag = DisposeBag() func bind(from dependency: Dependency<Input, State, Extra>) -> Output { let state = dependency.state let extra = dependency.extra extra.apiAction.response .bind(to: state.repositories) .disposed(by: disposeBag) dependency.inputObservable(for: \.search) .withLatestFrom(dependency.inputObservable(for: \.text)) { $1 } .flatMap { $0.map(Observable.just) ?? .empty() } .subscribe(onNext: { extra.apiAction.execute($0) }) .disposed(by: disposeBag) return Output(repositories: state.repositories.asObservable()) } } } protocol SearchAPIActionType { var response: Observable<[Repository]> { get } var error: Observable<Error> { get } func execute(_ text: String) }
  93. Model΋UnioStreamԽ protocol SearchAPIStreamType: AnyObject { var input: InputWrapper<SearchAPIStream.Input> { get

    } var output: OutputWrapper<SearchAPIStream.Output> { get } } class SearchAPIStream: UnioStream<SearchAPIStream.Logic>, SearchAPIStreamType { struct Input: InputType { let execute = PublishRelay<String>() } struct Output: OutputType { let response: Observable<[Repository]> let error: Observable<Error> } struct Extra: ExtraType { let session = URLSession.shared let decoder = JSONDecoder() } struct Logic: LogicType { func bind(from dependency: Dependency<Input, NoState, Extra>) -> Output { let extra = dependency.extra let result = dependency.inputObservable(for: \.execute) .flatMapLatest { query -> Observable<Event<[Repository]>> in let request = URLRequest(url: URL(string: "https://◦◦?q=\(query)")!) return extra.session.rx.data(request: request) .take(1) .map { try extra.decoder.decode([Repository].self, from:$0) } .materialize() } .share() return Output(response: result.flatMap { $0.element.map(Observable.just) ?? .empty() }, error: result.flatMap { $0.error.map(Observable.just) ?? .empty() }) } } } protocol SearchAPIActionType { var response: Observable<[Repository]> { get } var error: Observable<Error> { get } func execute(_ text: String) }
  94. Model΋UnioStreamԽ protocol SearchAPIStreamType: AnyObject { var input: InputWrapper<SearchAPIStream.Input> { get

    } var output: OutputWrapper<SearchAPIStream.Output> { get } } class SearchAPIStream: UnioStream<SearchAPIStream.Logic>, SearchAPIStreamType { struct Input: InputType { let execute = PublishRelay<String>() } struct Output: OutputType { let response: Observable<[Repository]> let error: Observable<Error> } struct Extra: ExtraType { let session = URLSession.shared let decoder = JSONDecoder() } struct Logic: LogicType { func bind(from dependency: Dependency<Input, NoState, Extra>) -> Output { let extra = dependency.extra let result = dependency.inputObservable(for: \.execute) .flatMapLatest { query -> Observable<Event<[Repository]>> in let request = URLRequest(url: URL(string: "https://◦◦?q=\(query)")!) return extra.session.rx.data(request: request) .take(1) .map { try extra.decoder.decode([Repository].self, from:$0) } .materialize() } .share() return Output(response: result.flatMap { $0.element.map(Observable.just) ?? .empty() }, error: result.flatMap { $0.error.map(Observable.just) ?? .empty() }) } } } protocol SearchAPIActionType { var response: Observable<[Repository]> { get } var error: Observable<Error> { get } func execute(_ text: String) }
  95. Model΋UnioStreamԽ protocol SearchAPIStreamType: AnyObject { var input: InputWrapper<SearchAPIStream.Input> { get

    } var output: OutputWrapper<SearchAPIStream.Output> { get } } class SearchAPIStream: UnioStream<SearchAPIStream.Logic>, SearchAPIStreamType { struct Input: InputType { let execute = PublishRelay<String>() } struct Output: OutputType { let response: Observable<[Repository]> let error: Observable<Error> } struct Extra: ExtraType { let session = URLSession.shared let decoder = JSONDecoder() } struct Logic: LogicType { func bind(from dependency: Dependency<Input, NoState, Extra>) -> Output { let extra = dependency.extra let result = dependency.inputObservable(for: \.execute) .flatMapLatest { query -> Observable<Event<[Repository]>> in let request = URLRequest(url: URL(string: "https://◦◦?q=\(query)")!) return extra.session.rx.data(request: request) .take(1) .map { try extra.decoder.decode([Repository].self, from:$0) } .materialize() } .share() return Output(response: result.flatMap { $0.element.map(Observable.just) ?? .empty() }, error: result.flatMap { $0.error.map(Observable.just) ?? .empty() }) } } } protocol SearchAPIActionType { var response: Observable<[Repository]> { get } var error: Observable<Error> { get } func execute(_ text: String) }
  96. Model΋UnioStreamԽ protocol SearchAPIStreamType: AnyObject { var input: InputWrapper<SearchAPIStream.Input> { get

    } var output: OutputWrapper<SearchAPIStream.Output> { get } } class SearchAPIStream: UnioStream<SearchAPIStream.Logic>, SearchAPIStreamType { struct Input: InputType { let execute = PublishRelay<String>() } struct Output: OutputType { let response: Observable<[Repository]> let error: Observable<Error> } struct Extra: ExtraType { let session = URLSession.shared let decoder = JSONDecoder() } struct Logic: LogicType { func bind(from dependency: Dependency<Input, NoState, Extra>) -> Output { let extra = dependency.extra let result = dependency.inputObservable(for: \.execute) .flatMapLatest { query -> Observable<Event<[Repository]>> in let request = URLRequest(url: URL(string: "https://◦◦?q=\(query)")!) return extra.session.rx.data(request: request) .take(1) .map { try extra.decoder.decode([Repository].self, from:$0) } .materialize() } .share() return Output(response: result.flatMap { $0.element.map(Observable.just) ?? .empty() }, error: result.flatMap { $0.error.map(Observable.just) ?? .empty() }) } } } protocol SearchAPIActionType { var response: Observable<[Repository]> { get } var error: Observable<Error> { get } func execute(_ text: String) } *OQVUͱ0VUQVUͷΈΛఆٛ͠ .PDLԽͰ͖ΔΑ͏ʹ͢Δ
  97. Model΋UnioStreamԽ class SearchViewStream: UnioStream<SearchViewStream.Logic> { struct Input: InputType { let

    search = PublishRelay<Void>() let text = PublishRelay<String?>() } struct Output: OutputType { let repositories: Observable<[Repository]> } struct State: StateType { let repositories = BehaviorRelay<[Repository]>(value: []) } struct Extra: ExtraType { let apiAction: SearchAPIStreamType } struct Logic: LogicType { let disposeBag = DisposeBag() func bind(from dependency: Dependency<Input, State, Extra>) -> Output { let state = dependency.state let apiAction = dependency.extra.apiAction apiAction.output.observable(for: \.response) .bind(to: state.repositories) .disposed(by: disposeBag) dependency.inputObservable(for: \.search) .withLatestFrom(dependency.inputObservable(for: \.text)) { $1 } .flatMap { $0.map(Observable.just) ?? .empty() } .bind(to: apiAction.input.accept(for: \.execute)) .disposed(by: disposeBag) return Output(repositories: state.repositories.asObservable()) } } }
  98. Model΋UnioStreamԽ class SearchViewStream: UnioStream<SearchViewStream.Logic> { struct Input: InputType { let

    search = PublishRelay<Void>() let text = PublishRelay<String?>() } struct Output: OutputType { let repositories: Observable<[Repository]> } struct State: StateType { let repositories = BehaviorRelay<[Repository]>(value: []) } struct Extra: ExtraType { let apiAction: SearchAPIStreamType } struct Logic: LogicType { let disposeBag = DisposeBag() func bind(from dependency: Dependency<Input, State, Extra>) -> Output { let state = dependency.state let apiAction = dependency.extra.apiAction apiAction.output.observable(for: \.response) .bind(to: state.repositories) .disposed(by: disposeBag) dependency.inputObservable(for: \.search) .withLatestFrom(dependency.inputObservable(for: \.text)) { $1 } .flatMap { $0.map(Observable.just) ?? .empty() } .bind(to: apiAction.input.accept(for: \.execute)) .disposed(by: disposeBag) return Output(repositories: state.repositories.asObservable()) } } } ςετ࣌ʹΛ.PDLΛ౉ͤΔΑ͏ʹ͢Δ
  99. Model΋UnioStreamԽ class SearchViewStream: UnioStream<SearchViewStream.Logic> { struct Input: InputType { let

    search = PublishRelay<Void>() let text = PublishRelay<String?>() } struct Output: OutputType { let repositories: Observable<[Repository]> } struct State: StateType { let repositories = BehaviorRelay<[Repository]>(value: []) } struct Extra: ExtraType { let apiAction: SearchAPIStreamType } struct Logic: LogicType { let disposeBag = DisposeBag() func bind(from dependency: Dependency<Input, State, Extra>) -> Output { let state = dependency.state let apiAction = dependency.extra.apiAction apiAction.output.observable(for: \.response) .bind(to: state.repositories) .disposed(by: disposeBag) dependency.inputObservable(for: \.search) .withLatestFrom(dependency.inputObservable(for: \.text)) { $1 } .flatMap { $0.map(Observable.just) ?? .empty() } .bind(to: apiAction.input.accept(for: \.execute)) .disposed(by: disposeBag) return Output(repositories: state.repositories.asObservable()) } } } PVUQVU͔ΒϨεϙϯεΛड͚औΔ
  100. Model΋UnioStreamԽ class SearchViewStream: UnioStream<SearchViewStream.Logic> { struct Input: InputType { let

    search = PublishRelay<Void>() let text = PublishRelay<String?>() } struct Output: OutputType { let repositories: Observable<[Repository]> } struct State: StateType { let repositories = BehaviorRelay<[Repository]>(value: []) } struct Extra: ExtraType { let apiAction: SearchAPIStreamType } struct Logic: LogicType { let disposeBag = DisposeBag() func bind(from dependency: Dependency<Input, State, Extra>) -> Output { let state = dependency.state let apiAction = dependency.extra.apiAction apiAction.output.observable(for: \.response) .bind(to: state.repositories) .disposed(by: disposeBag) dependency.inputObservable(for: \.search) .withLatestFrom(dependency.inputObservable(for: \.text)) { $1 } .flatMap { $0.map(Observable.just) ?? .empty() } .bind(to: apiAction.input.accept(for: \.execute)) .disposed(by: disposeBag) return Output(repositories: state.repositories.asObservable()) } } } JOQVU͔ΒϦΫΤετΛ࣮ߦ͢Δ
  101. UnioStreamͷςετ class SearchViewStream: UnioStream<SearchViewStream.Logic> { struct Input: InputType { let

    search = PublishRelay<Void>() let text = PublishRelay<String?>() } struct Output: OutputType { let repositories: Observable<[Repository]> } struct State: StateType { let repositories = BehaviorRelay<[Repository]>(value: []) } struct Extra: ExtraType { let apiAction: SearchAPIStreamType } struct Logic: LogicType { let disposeBag = DisposeBag() func bind(from dependency: Dependency<Input, State, Extra>) -> Output { let state = dependency.state let apiAction = dependency.extra.apiAction apiAction.output.observable(for: \.response) .bind(to: state.repositories) .disposed(by: disposeBag) dependency.inputObservable(for: \.search) .withLatestFrom(dependency.inputObservable(for: \.text)) { $1 } .flatMap { $0.map(Observable.just) ?? .empty() } .bind(to: apiAction.input.accept(for: \.execute)) .disposed(by: disposeBag) return Output(repositories: state.repositories.asObservable()) } } }
  102. UnioStreamͷςετ class SearchViewStream: UnioStream<SearchViewStream.Logic> { struct Input: InputType { let

    search = PublishRelay<Void>() let text = PublishRelay<String?>() } struct Output: OutputType { let repositories: Observable<[Repository]> } struct State: StateType { let repositories = BehaviorRelay<[Repository]>(value: []) } struct Extra: ExtraType { let apiAction: SearchAPIStreamType } struct Logic: LogicType { let disposeBag = DisposeBag() func bind(from dependency: Dependency<Input, State, Extra>) -> Output { let state = dependency.state let apiAction = dependency.extra.apiAction apiAction.output.observable(for: \.response) .bind(to: state.repositories) .disposed(by: disposeBag) dependency.inputObservable(for: \.search) .withLatestFrom(dependency.inputObservable(for: \.text)) { $1 } .flatMap { $0.map(Observable.just) ?? .empty() } .bind(to: apiAction.input.accept(for: \.execute)) .disposed(by: disposeBag) return Output(repositories: state.repositories.asObservable()) } } } ݕࡧจࣈྻ͕౉͍ͬͯΔঢ়ଶͰݕࡧ͕࣮ߦ͞ΕΔͱ "1*ͷFYFDVUF͕ݺ͹ΕΔςετ
  103. UnioStreamͷςετ protocol SearchAPIStreamType: AnyObject { var input: InputWrapper<SearchAPIStream.Input> { get

    } var output: OutputWrapper<SearchAPIStream.Output> { get } } class MockSearchAPIStream: SearchAPIStreamType { let input: InputWrapper<SearchAPIStream.Input> let output: OutputWrapper<SearchAPIStream.Output> let _input = SearchAPIStream.Input() let _response = PublishRelay<[Repository]>() let _error = PublishRelay<Error>() init() { self.input = InputWrapper(_input) let output = SearchAPIStream.Output( response: _response.asObservable(), error: _error.asObservable() ) self.output = OutputWrapper(output) } }
  104. UnioStreamͷςετ protocol SearchAPIStreamType: AnyObject { var input: InputWrapper<SearchAPIStream.Input> { get

    } var output: OutputWrapper<SearchAPIStream.Output> { get } } class MockSearchAPIStream: SearchAPIStreamType { let input: InputWrapper<SearchAPIStream.Input> let output: OutputWrapper<SearchAPIStream.Output> let _input = SearchAPIStream.Input() let _response = PublishRelay<[Repository]>() let _error = PublishRelay<Error>() init() { self.input = InputWrapper(_input) let output = SearchAPIStream.Output( response: _response.asObservable(), error: _error.asObservable() ) self.output = OutputWrapper(output) } } QSPUPDPMʹ߹ΘͤͯɺJOQVUͱPVUQVUΛఆٛ͠·͢
  105. UnioStreamͷςετ protocol SearchAPIStreamType: AnyObject { var input: InputWrapper<SearchAPIStream.Input> { get

    } var output: OutputWrapper<SearchAPIStream.Output> { get } } class MockSearchAPIStream: SearchAPIStreamType { let input: InputWrapper<SearchAPIStream.Input> let output: OutputWrapper<SearchAPIStream.Output> let _input = SearchAPIStream.Input() let _response = PublishRelay<[Repository]>() let _error = PublishRelay<Error>() init() { self.input = InputWrapper(_input) let output = SearchAPIStream.Output( response: _response.asObservable(), error: _error.asObservable() ) self.output = OutputWrapper(output) } } *OQVU͕࣮ߦ͞Εͨ͜ͱΛड͚औΔͨΊʹ *OQVU8SBQQFS͚ͩͰͳ͘*OQVU΋ఆٛ
  106. UnioStreamͷςετ protocol SearchAPIStreamType: AnyObject { var input: InputWrapper<SearchAPIStream.Input> { get

    } var output: OutputWrapper<SearchAPIStream.Output> { get } } class MockSearchAPIStream: SearchAPIStreamType { let input: InputWrapper<SearchAPIStream.Input> let output: OutputWrapper<SearchAPIStream.Output> let _input = SearchAPIStream.Input() let _response = PublishRelay<[Repository]>() let _error = PublishRelay<Error>() init() { self.input = InputWrapper(_input) let output = SearchAPIStream.Output( response: _response.asObservable(), error: _error.asObservable() ) self.output = OutputWrapper(output) } } QSPQFSUZͷ*OQVUΛ΋ͱʹ*OQVU8SBQQFSΛੜ੒
  107. UnioStreamͷςετ class SearchViewStreamTests: XCTestCase { var testTarget: SearchViewStream! var mockSearchAPIStream:

    MockSearchAPIStream! override func setUp() { self.mockSearchAPIStream = MockSearchAPIStream() self.testTarget = SearchViewStream( input: .init(), state: .init(), extra: .init(apiAction: mockSearchAPIStream), logic: .init() ) } func test_input_textʹจࣈྻΛ౉͠_searchΛ࣮ߦ͢Δͱ_execute͕ݺ͹ΕΔ() { let result = BehaviorRelay<String?>(value: nil) let disposable = mockSearchAPIStream._input.execute.bind(to: result) let expected = "test-query" testTarget.input.accept(expected, for: \.text) testTarget.input.accept((), for: \.search) XCTAssertEqual(result.value, expected) disposable.dispose() } }
  108. UnioStreamͷςετ class SearchViewStreamTests: XCTestCase { var testTarget: SearchViewStream! var mockSearchAPIStream:

    MockSearchAPIStream! override func setUp() { self.mockSearchAPIStream = MockSearchAPIStream() self.testTarget = SearchViewStream( input: .init(), state: .init(), extra: .init(apiAction: mockSearchAPIStream), logic: .init() ) } func test_input_textʹจࣈྻΛ౉͠_searchΛ࣮ߦ͢Δͱ_execute͕ݺ͹ΕΔ() { let result = BehaviorRelay<String?>(value: nil) let disposable = mockSearchAPIStream._input.execute.bind(to: result) let expected = "test-query" testTarget.input.accept(expected, for: \.text) testTarget.input.accept((), for: \.search) XCTAssertEqual(result.value, expected) disposable.dispose() } } ςετର৅ͷ6OJP4USFBNͱ.PDLԽͨ͠6OJP4USFBNΛ อ࣋͠ɺॳظԽ࣌ʹ&YUSBܦ༝Ͱ.PDLΛ஫ೖ͢Δ
  109. UnioStreamͷςετ class SearchViewStreamTests: XCTestCase { var testTarget: SearchViewStream! var mockSearchAPIStream:

    MockSearchAPIStream! override func setUp() { self.mockSearchAPIStream = MockSearchAPIStream() self.testTarget = SearchViewStream( input: .init(), state: .init(), extra: .init(apiAction: mockSearchAPIStream), logic: .init() ) } func test_input_textʹจࣈྻΛ౉͠_searchΛ࣮ߦ͢Δͱ_execute͕ݺ͹ΕΔ() { let result = BehaviorRelay<String?>(value: nil) let disposable = mockSearchAPIStream._input.execute.bind(to: result) let expected = "test-query" testTarget.input.accept(expected, for: \.text) testTarget.input.accept((), for: \.search) XCTAssertEqual(result.value, expected) disposable.dispose() } } *OQVUͷFYFDVUF͕࣮ߦ͞Εͨ৔߹ʹ஋Λड͚औΔ
  110. UnioStreamͷςετ class SearchViewStreamTests: XCTestCase { var testTarget: SearchViewStream! var mockSearchAPIStream:

    MockSearchAPIStream! override func setUp() { self.mockSearchAPIStream = MockSearchAPIStream() self.testTarget = SearchViewStream( input: .init(), state: .init(), extra: .init(apiAction: mockSearchAPIStream), logic: .init() ) } func test_input_textʹจࣈྻΛ౉͠_searchΛ࣮ߦ͢Δͱ_execute͕ݺ͹ΕΔ() { let result = BehaviorRelay<String?>(value: nil) let disposable = mockSearchAPIStream._input.execute.bind(to: result) let expected = "test-query" testTarget.input.accept(expected, for: \.text) testTarget.input.accept((), for: \.search) XCTAssertEqual(result.value, expected) disposable.dispose() } } จࣈྻΛ౉ͯ͠ɺݕࡧΛ࣮ߦ͢Δ
  111. UnioStreamͷςετ class SearchViewStreamTests: XCTestCase { var testTarget: SearchViewStream! var mockSearchAPIStream:

    MockSearchAPIStream! override func setUp() { self.mockSearchAPIStream = MockSearchAPIStream() self.testTarget = SearchViewStream( input: .init(), state: .init(), extra: .init(apiAction: mockSearchAPIStream), logic: .init() ) } func test_input_textʹจࣈྻΛ౉͠_searchΛ࣮ߦ͢Δͱ_execute͕ݺ͹ΕΔ() { let result = BehaviorRelay<String?>(value: nil) let disposable = mockSearchAPIStream._input.execute.bind(to: result) let expected = "test-query" testTarget.input.accept(expected, for: \.text) testTarget.input.accept((), for: \.search) XCTAssertEqual(result.value, expected) disposable.dispose() } } FYFDVUF͕ݺͼग़͞Εɺର৅΋จࣈྻ͕౉͞Ε͍ͯΔ
  112. UnioͰͰ͖ΔΑ͏ʹͳͬͨ͜ͱ • Extraͱͯ͠UnioStreamΛ౉͢͜ ͱͰɺMockԽ͕༰қʹͳΔ

  113. Unioͷෆศͳͱ͜Ζᶃ InputɾOutputɾStateɾExtra UnioStreamΛ ౎౓ఆٛ͠ͳ͍ͱ͍͚ͳ͍

  114. Unioͷෆศͳͱ͜Ζᶃ - վળࡦ xctemplateͰੜ੒ͯ͠͠·͏

  115. Unioͷෆศͳͱ͜Ζᶄ input.accept(for: \.hoge) output.observable(for: \.fuga) ͷΑ͏ʹKeyPathͰΞΫηε ͠ͳ͚Ε͹͍͚ͳ͍

  116. IUUQTUXJUUFSDPNBLLZJFTUBUVT 

  117. IUUQTHJUIVCDPNBQQMFTXJGUFWPMVUJPO CMPCNBTUFSQSPQPTBMTLFZQBUI EZOBNJDNFNCFSMPPLVQNE

  118. Input - KeyPath Dynamic Member Lookup struct Input: InputType {

    let search = PublishRelay<String>() } let input = InputWrapper(Input()) input.search("GitHub") input.search.onNext("Unio") let c: (String) -> Void = input.search let o: AnyObserver<String> = input.search
  119. Input - KeyPath Dynamic Member Lookup struct Input: InputType {

    let search = PublishRelay<String>() } let input = InputWrapper(Input()) input.search("GitHub") input.search.onNext("Unio") let c: (String) -> Void = input.search let o: AnyObserver<String> = input.search QSPQFSUZʹ௚઀ΞΫηε͍ͯ͠Δ͔ͷΑ͏ͳݺͼग़͕͠Ͱ͖Δ
  120. @dynamicMemberLookup class InputWrapper<Input: InputType> { private let input: T subscript<T>(dynamicMember

    keyPath: KeyPath<Input, PublishRelay<T>) -> (T) -> Void { return input[keyPath: keyPath].accept } subscript<T>(dynamicMember keyPath: KeyPath<Input, PublishRelay<T>>) -> AnyObserver<T> { let relay = input[keyPath: keyPath] return AnyObserver { $0.element.map(relay.accept) } } } Input - KeyPath Dynamic Member Lookup
  121. @dynamicMemberLookup class InputWrapper<Input: InputType> { private let input: T subscript<T>(dynamicMember

    keyPath: KeyPath<Input, PublishRelay<T>) -> (T) -> Void { return input[keyPath: keyPath].accept } subscript<T>(dynamicMember keyPath: KeyPath<Input, PublishRelay<T>>) -> AnyObserver<T> { let relay = input[keyPath: keyPath] return AnyObserver { $0.element.map(relay.accept) } } } Input - KeyPath Dynamic Member Lookup ,FZ1BUIEZOBNJD.FNCFS-PPLVQʹରԠ
  122. struct Output: OutputType { let title = BehaviorRelay(value: "title") }

    let output = OutputWrapper(Output()) let o: Observable<String> = output.title let v: String = output.title.value let p: Property<String> = output.title Output - KeyPath Dynamic Member Lookup
  123. struct Output: OutputType { let title = BehaviorRelay(value: "title") }

    let output = OutputWrapper(Output()) let o: Observable<String> = output.title let v: String = output.title.value let p: Property<String> = output.title Output - KeyPath Dynamic Member Lookup QSPQFSUZʹ௚઀ΞΫηε͍ͯ͠Δ͔ͷΑ͏ͳݺͼग़͕͠Ͱ͖Δ
  124. let stream: CounterViewStreamType let button: UIButton let label: UILabel button.rx.tap

    .bind(to: stream.input.accept(for: \.increment)) .disposed(by: disposeBag) stream.output.observable(for: \.text) .bind(to: label.rx.text) .disposed(by: disposeBag) Unio - KeyPath Dynamic Member Lookup
  125. let stream: CounterViewStreamType let button: UIButton let label: UILabel button.rx.tap

    .bind(to: stream.input.accept(for: \.increment)) .disposed(by: disposeBag) stream.output.observable(for: \.text) .bind(to: label.rx.text) .disposed(by: disposeBag) Unio - KeyPath Dynamic Member Lookup button.rx.tap .bind(to: stream.input.increment) .disposed(by: disposeBag) stream.output.text .bind(to: label.rx.text) .disposed(by: disposeBag)
  126. ࠷ޙʹ UnioɹͰ࣮૷Λറͬͯ ୯Ұํ޲ͷೖग़ྗͳMVVMΛ ࢼͯ͠Έ·ͤΜ͔

  127. ͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠