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

Serialize process with screen transitions.

417.72KI
February 28, 2019

Serialize process with screen transitions.

potatotips #59

417.72KI

February 28, 2019
Tweet

More Decks by 417.72KI

Other Decks in Technology

Transcript

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

    View full-size slide

  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

    View full-size slide

  3. ࿩͢͜ͱ
    w ՝୊ͱΞϓϩʔνྫ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  6. ී௨ͷෳࡶͳॲཧϑϩʔ
    func connectA() -> Single
    func connectB(resultOfA: String) -> Single
    func connectC(resultOfB: String) -> Single
    func doSomething() {
    connectA()
    .flatMap(connectB)
    .flatMap(connectC)
    .subscribe(onSuccess: {
    // ࠷ऴ݁ՌͰԿ͔͢Δ
    }, onError: { print($0) })
    .disposed(by: disposeBag)
    }

    View full-size slide

  7. ࠓճͷ՝୊ ྫ

    "ͱ௨৴͢Δ
    "ͷ݁ՌΛ8FC7JFXʹૹͬͯ
    Ϣʔβʔʹૢ࡞ͤ͞Δ
    Ϣʔβʔ͕ૢ࡞ͨ݁͠ՌΛ
    ϑοΫͯ͠$ͱ௨৴͢Δ
    8FC7JFX$POUSPMMFS
    QSFTFOU7JFX$POUSPMMFS
    8FC7JFX%FMFHBUF
    EJTNJTT7JFX$POUSPMMFS
    8FC7JFX$POUSPMMFS
    %FMFHBUF

    View full-size slide

  8. ࠓճͷ՝୊ ྫ

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

    View full-size slide

  9. 8FC7JFX$POUSPMMFSʹ
    0CTFSWFSΛஔ͚͹
    ௚ྻԽͰ͖ΔͷͰ͸ʁ

    View full-size slide

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

    View full-size slide

  11. ݺͼग़͢7$ʹ
    0CTFSWFSΛஔ͘
    class WebViewController: UIViewController {
    var param: String!
    private let resultSubject = PublishSubject()
    // ҎԼུ
    }
    extension WebViewController {
    var result: Maybe {
    return resultSubject.asMaybe()
    }
    }

    View full-size slide

  12. ݺͼग़͢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()
    }
    }

    View full-size slide

  13. ݺͼग़͠ॲཧΛ
    QSPUPDPMʹ͢Δ
    protocol WebViewOpenable {
    /// paramΛWebViewʹૹͬͯ݁ՌΛड͚औΔ
    func requestToWeb(withParam param: String) -> Maybe
    }

    View full-size slide

  14. ݺͼग़͠ॲཧΛ
    QSPUPDPMʹ͢Δ
    extension WebViewOpenable where Self: UIViewController {
    func requestToWeb(withParam param: String) -> Maybe {
    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
    }
    }

    View full-size slide

  15. ݩͷίʔυ
    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)
    }

    View full-size slide

  16. ௚ྻԽ͞Εͨίʔυ
    func doSomething() {
    connectA()
    .flatMap(requestToWeb)
    .flatMap(connectC)
    .subscribe(onSuccess: {
    // ࠷ऴ݁ՌͰԿ͔͢Δ
    print($0)
    }, onError: { print($0) })
    .disposed(by: disposeBag)
    }

    View full-size slide

  17. ௚ྻԽ͞Εͨίʔυ
    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)
    }

    View full-size slide

  18. ݁Ռ
    w ը໘ભҠ͕བྷΉΑ͏ͳॲཧϑϩʔ΋ભҠઌʹ
    0CTFSWFSΛஔ͘͜ͱͰqBU.BQCJOEͰܨ͛Β
    ΕΔΑ͏ʹͳͬͨ
    w ॲཧதʹը໘ભҠ͕ൃੜ͢Δ͜ͱΛϑϩʔͷத
    Ͱҙࣝ͠ͳͯ͘΋ྑ͘ͳͬͨ
    w 1VCMJTI4VCKFDUJT༏ल

    View full-size slide

  19. 4BNQMF
    w IUUQTHJUIVCDPN,*
    3Y4DSFFO5SBOTJUJPO4BNQMF

    View full-size slide