I want to show a loading indicator at the beginning of the first api call when list of items is being fetched from the server. Everything is okay if data is fetched successfully. That means the loading indicator got invisible if data loaded successfully. The code is given bellow.
must use this code in view model
protected open fun <T> Single<T>.asPageLoadEventSource(eventId: Int = Random.nextInt()): Single<T> {
var emitter: SingleEmitter<Any>? = null
val single = Single.create<Any> { emitter = it }
pageLoadEventSingles[eventId] = single
return this.doOnSuccess { emitter?.onSuccess(it as Any) }
.doOnError { emitter?.tryOnError(it) }
}
view model
private fun getIncomingStiDest() {
val queries = mapOf(
"last_status" to stiDest,
"type" to incoming
)
getStiDestActionLiveData.value = Resource(Status.LOADING)
getDashboardCounterUseCase.execute(queries).asPageLoadEventSource().subscribe({
getStiDestActionLiveData.value = Resource(Status.SUCCESS, it)
}, {
getStiDestActionLiveData.value = Resource(Status.ERROR, it)
}).collect()
}