1
0

More than 3 years have passed since last update.

通信処理をRxで拡張して怠惰にプログレス表示

Last updated at Posted at 2021-09-04

よくあるパターンの通信関連のUI実装は楽をしたい

  • 通信中はプログレスを出します。終わったら消します。
  • 通信エラーの時は、エラー表示をします。

本当によく実装する内容なので、便利なものを作って、何も考えずに楽に実装して、細かいUI実装に専念していきたいです。
そのほうがみんな嬉しい。

こんな感じでできると嬉しい。

通信処理を行う実装イメージ


let progressTracker: PublishRelay<Bool> = .init()

// UIではこれをsubscribeする
let progress: Signal<Bool> 

someEvent
  .flatMap {
    // 通信処理
  }
  .trackProgress(progressTracker) // progressの表示/非表示
    .trackError(errorTracker) // エラーも似たように
  .toDoSomeOperator
...

UI実装箇所のイメージ

viewModel.progress
  .emit(to: SVProgressHUD.rx.isVisible)
 .dispose(by: disposeBag)

作ってみた

ちょっとgistを使ってみたかったのでgistに書いてみました。
https://gist.github.com/y-hakutaku/0424241d0863893a90f4ce9542a9f3b2

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0