gpt4 book ai didi

android - 从 ViewModel 获取数据时如何显示 ProgressDialog

转载 作者:行者123 更新时间:2023-11-30 05:06:29 26 4
gpt4 key购买 nike

我要显示ProgressDialogViewModel 获取数据时当我第一次获取数据时它工作正常,但是当我想从 API 刷新数据时 ProgressDialog有始无终

我创建 MutableLiveData<Boolean>()并尝试管理可见性,但它不起作用

这就是我从 Activity 中刷新数据的方式

private fun loadNorthTram() {
val model =
ViewModelProviders.of(this@MainActivity).get(MyViewModelNorth::class.java)

model.isNorthUpdating.observe(
this@MainActivity,
Observer { b ->
if (b!!)
AppUtil.showProgressSpinner(this@MainActivity)
else
AppUtil.dismissProgressDialog()
})

model.getNorthTrams().observe(this@MainActivity, Observer
{
if (it != null) {
setData(it)
}
})
}

下面是我的 ViewModel 类

class MyViewModelNorth : ViewModel() {

private lateinit var mtoken: String
private val apiService: ApiInterface = ApiClient.client.create(ApiInterface::class.java)
private lateinit var trams: MutableLiveData<TramModel>
val isNorthUpdating = MutableLiveData<Boolean>().default(false)

fun getNorthTrams(): MutableLiveData<TramModel> {
isNorthUpdating.value = true
if (!::trams.isInitialized) {
trams = MutableLiveData()
callTokenAPI()
}
return trams
}

private fun callTokenAPI() {
val tokenObservable: Observable<TokenModel> = apiService.fetchToken()
tokenObservable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnNext { self ->
mtoken = self.responseObject[0].DeviceToken
callTramAPI()
}
.subscribe(getTokenObserver())
}

private fun callTramAPI() {
val apiService: ApiInterface = ApiClient.client.create(ApiInterface::class.java)
val observable: Observable<TramModel> = apiService.fetchTrams(AppUtil.NORTH_TRAMS, mtoken)
observable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(getTramObserver())
}

private fun getTokenObserver(): Observer<TokenModel> {
return object : Observer<TokenModel> {
override fun onComplete() {}
override fun onSubscribe(d: Disposable) {}

override fun onNext(tokenModel: TokenModel) {}

override fun onError(e: Throwable) {
if (e is HttpException) {
val errorBody = e.response().errorBody()
HttpErrorUtil(e.code()).handelError()
}
}
}
}

private fun getTramObserver(): Observer<TramModel> {
return object : Observer<TramModel> {
override fun onComplete() {
isNorthUpdating.value = false
}

override fun onSubscribe(d: Disposable) {}

override fun onNext(t: TramModel) {
if (!t.hasError && t.hasResponse)
trams.value = t
else if (t.errorMessage.isBlank())
applicationContext().showToast(t.errorMessage)
else
applicationContext().showToast(applicationContext().getString(R.string.something_wrong))
}

override fun onError(e: Throwable) {

if (e is HttpException) {
val errorBody = e.response().errorBody()
HttpErrorUtil(e.code()).handelError()
}
}
}
}

public fun getIsNothUpdating(): LiveData<Boolean> {
return isNorthUpdating
}

fun <T : Any?> MutableLiveData<T>.default(initialValue: T) = apply { setValue(initialValue) }
}

最佳答案

我没有测试过您的代码,但我认为您的问题出在 View 模型中的函数 getNorthTrams() 中。

当您第一次获取数据时,trams 未初始化,您的 api 调用正在进行并且仅在 onCompleted 时设置 isNorthUpdating.value = false。此代码有效。

但是当你刷新数据时,trams 已经被初始化了。因此,不存在 isNorthUpdating.value = false 的情况,这会导致进度对话框无法关闭。

所以我认为您应该在 View 模型中处理其他情况。

    fun getNorthTrams(): MutableLiveData<TramModel> {
isNorthUpdating.value = true
if (!::trams.isInitialized) {
trams = MutableLiveData()
callTokenAPI()
}else{
//do your thing for refresh
isNorthUpdating.value = false
}
return trams
}

此外,在api调用中,如果发生错误,您应该将isNorthUpdating设置为false并显示一些错误信息。否则,即使在 api 调用中发生某些错误,进度对话框也会始终显示。

关于android - 从 ViewModel 获取数据时如何显示 ProgressDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54501018/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com