Serialize process with screen transitions.

7a78f23eef1b0e883ef44c229a54f0bb?s=47 417.72KI
February 28, 2019

Serialize process with screen transitions.

potatotips #59

7a78f23eef1b0e883ef44c229a54f0bb?s=128

417.72KI

February 28, 2019
Tweet

Transcript

  1. ը໘ભҠ͕བྷΉ஋ͷड͚౉͠Λ 3Y4XJGUͰ௚ྻԽͯ͠Έͨ !QPUBUPUJQT

  2. struct Me { let name = "Takuhiro Muta" let aka

    = "417.72KI" let experienceYears = 5 let company = "iRidge inc." let twitter = "417_72ki" let qiita = "417_72ki" let gitHub = "417-72KI" let like = [ "BEMANI", "Real Escape Game", "Bang Dream!", "LoveLive! Sunshine!!", "etc…" ] } TFMGEFTDSJQUJPO
  3. ࿩͢͜ͱ w ՝୊ͱΞϓϩʔνྫ

  4. ࿩͞ͳ͍͜ͱ w 3Y4XJGUʹ͍ͭͯ w ࢓૊Έͱ͔ܕͱ͔ w ֤छΞʔΩςΫνϟʹͲ͏ద༻͢Δ͔

  5. ී௨ͷෳࡶͳॲཧϑϩʔ "ͱ௨৴͢Δ "ͷ݁ՌΛ࢖ͬͯ#ͱ௨৴͢Δ #ͷ݁ՌΛ࢖ͬͯ$ͱ௨৴͢Δ

  6. ී௨ͷෳࡶͳॲཧϑϩʔ func connectA() -> Single<String> func connectB(resultOfA: String) -> Single<String>

    func connectC(resultOfB: String) -> Single<String> func doSomething() { connectA() .flatMap(connectB) .flatMap(connectC) .subscribe(onSuccess: { // ࠷ऴ݁ՌͰԿ͔͢Δ }, onError: { print($0) }) .disposed(by: disposeBag) }
  7. None
  8. ࠓճͷ՝୊ ྫ "ͱ௨৴͢Δ "ͷ݁ՌΛ8FC7JFXʹૹͬͯ Ϣʔβʔʹૢ࡞ͤ͞Δ Ϣʔβʔ͕ૢ࡞ͨ݁͠ՌΛ ϑοΫͯ͠$ͱ௨৴͢Δ 8FC7JFX$POUSPMMFS QSFTFOU7JFX$POUSPMMFS 8FC7JFX%FMFHBUF

    EJTNJTT7JFX$POUSPMMFS 8FC7JFX$POUSPMMFS %FMFHBUF
  9. ࠓճͷ՝୊ ྫ func doSomething() { connectA() .subscribe(onSuccess: { [unowned self]

    in let webView = // initialize WebViewController webView.delegate = self webView.param = $0 self.present(webView, animated: true) }, onError: { print($0) }) .disposed(by: disposeBag) } // MARK: - WebViewControllerDelegate func webViewDidRespondSomething(_ value: String) { connectC(resultOfB: value) .subscribe(onSuccess: { // ࠷ऴ݁ՌͰԿ͔͢Δ print($0) }, onError: { print($0) }) .disposed(by: disposeBag) }
  10. None
  11. None
  12. 8FC7JFX$POUSPMMFSʹ 0CTFSWFSΛஔ͚͹ ௚ྻԽͰ͖ΔͷͰ͸ʁ

  13. None
  14. ΍ͬͨ͜ͱ w ݺͼग़͢7$ʹ0CTFSWFSΛஔ͘ w ݺͼग़͠ॲཧΛQSPUPDPMʹ͢Δ

  15. ݺͼग़͢7$ʹ 0CTFSWFSΛஔ͘ class WebViewController: UIViewController { var param: String! private

    let resultSubject = PublishSubject<String>() // ҎԼུ } extension WebViewController { var result: Maybe<String> { return resultSubject.asMaybe() } }
  16. ݺͼग़͢7$ʹ 0CTFSWFSΛஔ͘ // MARK: - WKNavigationDelegate extension WebViewController: WKNavigationDelegate {

    override func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) { guard let result = doSomething(fromRequest: navigationAction.request) else { return } decisionHandler(.cancel) dismiss(animated: true) { [unowned self] in self.resultSubject.onNext(result) self.resultSubject.onCompleted() } }
  17. ݺͼग़͠ॲཧΛ QSPUPDPMʹ͢Δ protocol WebViewOpenable { /// paramΛWebViewʹૹͬͯ݁ՌΛड͚औΔ func requestToWeb(withParam param:

    String) -> Maybe<String> }
  18. ݺͼग़͠ॲཧΛ QSPUPDPMʹ͢Δ extension WebViewOpenable where Self: UIViewController { func requestToWeb(withParam

    param: String) -> Maybe<String> { guard let nav = R.storyboard.webView.instantiateInitialViewController(), let webView = nav.viewControllers.first as? WebView else { assertionFailure("Something wrong in WebView.storyboard") return Observable.error(CommonError.unknown) } webView.param = param defer { present(nav, animated: true) } return webView.result } }
  19. ݩͷίʔυ func doSomething() { connectA() .subscribe(onSuccess: { [unowned self] in

    let webView = // initialize WebViewController webView.delegate = self webView.param = $0 self.present(webView, animated: true) }, onError: { print($0) }) .disposed(by: disposeBag) } // MARK: - WebViewControllerDelegate func webViewDidRespondSomething(_ value: String) { connectC(resultOfB: value) .subscribe(onSuccess: { // ࠷ऴ݁ՌͰԿ͔͢Δ print($0) }, onError: { print($0) }) .disposed(by: disposeBag) }
  20. ௚ྻԽ͞Εͨίʔυ func doSomething() { connectA() .flatMap(requestToWeb) .flatMap(connectC) .subscribe(onSuccess: { //

    ࠷ऴ݁ՌͰԿ͔͢Δ print($0) }, onError: { print($0) }) .disposed(by: disposeBag) }
  21. ௚ྻԽ͞Εͨίʔυ func doSomething() { connectA() .flatMap { [unowned self] in

    self.requestToWeb(withParam: $0) } .flatMap { [unowned self] in self.connectC(resultOfB: $0) } .bind(to: resultLabel.rx.text) .disposed(by: disposeBag) }
  22. ݁Ռ w ը໘ભҠ͕བྷΉΑ͏ͳॲཧϑϩʔ΋ભҠઌʹ 0CTFSWFSΛஔ͘͜ͱͰqBU.BQCJOEͰܨ͛Β ΕΔΑ͏ʹͳͬͨ w ॲཧதʹը໘ભҠ͕ൃੜ͢Δ͜ͱΛϑϩʔͷத Ͱҙࣝ͠ͳͯ͘΋ྑ͘ͳͬͨ w 1VCMJTI4VCKFDUJT༏ल

  23. 4BNQMF w IUUQTHJUIVCDPN,* 3Y4DSFFO5SBOTJUJPO4BNQMF

  24. None