Issue
I have following Base class :
open class BaseViewModel<T>(
private val schedulerProvider: BaseSchedulerProvider,
private val requestObservable: Observable<T>
) : ViewModel() {
private val compositeDisposable = CompositeDisposable()
private val _liveData = MutableLiveData<Resource<T>>()
val liveData: LiveData<Resource<T>>
get() = _liveData
protected fun sendRequest() {
_liveData.postValue(Resource.Loading)
composeObservable { requestObservable }.subscribe({
_liveData.postValue(Resource.Success(it))
}) {
_liveData.postValue(Resource.Failure(it.localizedMessage))
Timber.e(it)
}.also { compositeDisposable.add(it) }
}
}
In one of my derived classes I have following logic :
class DetailViewModel(api: QapitalService, schedulerProvider: BaseSchedulerProvider,
currencyFormatter: CurrencyFormatterDefault, goal: SavingsGoal
) : BaseViewModel<DetailWrapper>(schedulerProvider,
Observable.zip(api.requestFeeds(goal.id).map { it.wrapper },
api.requestSavingRules().map { it.wrapper },
BiFunction<List<Feed>, List<SavingsRule>, DetailWrapper>
{ feeds, savingRules -> DetailWrapper(feeds,
savingRules.joinToString { it.type },
feeds.asWeekSumText(currencyFormatter)
) })) {
init {
sendRequest()
}
class DetailWrapper(
val feeds: List<Feed>,
val savingRules: String,
val weekSumText: String)
}
As you see in the sendRequest() method, I have postValue(Loading). I believe it is on the main thread and there is no need for PostValue rather I can setValue.
But by using setValue when I click on Home hardware button and resume the app again, ProgressBar appears again. Why that happens?
When I use PostValue, progressBar does not appear when I resume the app.
I have made some other similar projects and I did not face which the issue that I explained above, but that projects were a single Activity and multiple fragments using Navigation component. In the above project I have multiple Activities with multiple fragments which are getting attached to the Activities layout.
Full source code can be found : https://github.com/AliRezaeiii/SavingGoals-Cache
Hore is my progressBar in my fragment_detail layout :
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:showLoading="@{vm.liveData}" />
And here is showLoading
binding adapter :
@BindingAdapter("showLoading")
fun showLoading(view: ProgressBar, resource: Resource<*>?) {
view.visibility = if (resource is Resource.Loading) View.VISIBLE else View.GONE
}
Solution
I just upgraded my project using a single Activity and multiple fragments using Navigation component, and the problem that I explained in the question resolved.
As I mentioned I was using multiple Activities with multiple fragments, as a result I reported that in Google issue tracker
Answered By - Ali
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.