如何从视图模型获取数据时显示ProgressDialog

问题描述 投票:1回答:1

我想说明ProgressDialog而取出由ViewModel数据,当我获取数据,第一次它工作正常,但是当我想从API刷新数据的ProgressDialog开始,不停止

我创建MutableLiveData<Boolean>()并试图管理权限,但它不工作

这是我从刷新我的活动我的数据

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) }
}
android android-architecture-components android-livedata android-mvvm
1个回答
0
投票

我没有测试你的代码,但我觉得你的问题是在功能getNorthTrams()在视图模型。

当你获取数据,电车未初始化,您的API调用是发生在只有onCompleted,你设置isNorthUpdating.value =假的第一次。此代码的工作。

但是,当你刷新数据,电车已经被初始化。因此,对于isNorthUpdating.value =假,这是造成进度对话框,不排除任何情况下。

所以,我认为你应该在你的视图模型处理其他案件。

    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调用的一些错误。

热门问题
推荐问题
最新问题