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. 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
  2. ී௨ͷෳࡶͳॲཧϑϩʔ 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) }
  3. ࠓճͷ՝୊ ྫ 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) }
  4. ݺͼग़͢7$ʹ 0CTFSWFSΛஔ͘ class WebViewController: UIViewController { var param: String! private

    let resultSubject = PublishSubject<String>() // ҎԼུ } extension WebViewController { var result: Maybe<String> { return resultSubject.asMaybe() } }
  5. ݺͼग़͢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() } }
  6. ݺͼग़͠ॲཧΛ 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 } }
  7. ݩͷίʔυ 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) }
  8. ௚ྻԽ͞Εͨίʔυ func doSomething() { connectA() .flatMap(requestToWeb) .flatMap(connectC) .subscribe(onSuccess: { //

    ࠷ऴ݁ՌͰԿ͔͢Δ print($0) }, onError: { print($0) }) .disposed(by: disposeBag) }
  9. ௚ྻԽ͞Εͨίʔυ 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) }