You can achieve this with doOnNext and PublishSubject . First create a theme and some values:
public static final int STATUS_UPLOADING = 0; public static final int STATUS_TOKEN = 1; public static final int STATUS_SUBMITTING = 2; public static final int STATUS_DONE = 3; PublishSubject<Integer> status = PublishSubject.create(); public Observable<Integer> getStatusStream() { return status; }
Then, when you do your download, just submit the value to this question every time:
status.onNext(STATUS_UPLOADING); return api.uploadFile() .doOnNext(o -> status.onNext(STATUS_TOKEN)) .flatMap(o -> api.createToken()) .doOnNext(o -> status.onNext(STATUS_SUBMITTING)) .flatMap(o -> api.submitOrder()) .doOnNext(o -> status.onNext(STATUS_DONE))
Then you can subscribe to Subject and update your interface:
model.getStatusStream() .subscribeOn(AndroidSchedulers.mainThread()) .subscribe( status -> { view().setMessage(status); }, Throwable.printStackTrace );
Alternatively, depending on how you want to architect your application, you can simply invoke update view calls with doOnNext each time. You will probably need to use observeOn to switch between the main and background threads each time.
Jahnold
source share