Home > front end >  ProgressBar not showing and observing in MVVM pattern
ProgressBar not showing and observing in MVVM pattern

Time:12-20

I am trying to observe a progressBar and everything seems ok to me but its not working.. when it gets to the viewmodel its getting null value..

This is the repository:

val isLoadingProgressBarMutableLiveData = MutableLiveData<Boolean>()

    fun getEmployeeListFromAPI(): MutableLiveData<List<Employee>> {

        isLoadingProgressBarMutableLiveData.value = true
        val apiRequest: APICallRequest = APIRequest.retrofitCallGetList


        apiRequest.callEmployeeList().enqueue(object : Callback<EmployeesListResult?> {
            override fun onResponse(
                call: Call<EmployeesListResult?>,
                response: Response<EmployeesListResult?>
            ) {
                Log.e("onResponse1", "${isLoadingProgressBarMutableLiveData.value}")
                if (response.isSuccessful) {
                    isLoadingProgressBarMutableLiveData.value = false
                    mutableListLiveData.value = response.body()?.getEmployeesListResult
                    Log.e("onResponse2", "${isLoadingProgressBarMutableLiveData.value}")
                    Log.e("onResponse", "Success!")
                    Log.e("Response:", "${response.body()}")
                }

            }

            override fun onFailure(call: Call<EmployeesListResult?>, t: Throwable) {
                Log.e("onFailure", "Failed getting list: ${t.message}")
                isLoadingProgressBarMutableLiveData.value = false
            }
        })

        return mutableListLiveData

    }

fun getLoadingState() : MutableLiveData<Boolean>{
    return isLoadingProgressBarMutableLiveData
}

"onResponse1" = true "onResponse2" = false

but when I move it to the ViewModel I get null ...

This is the ViewModel:

class MainViewModel : ViewModel() {

    fun getEmployeeListFromRepo() : LiveData<List<Employee>>{

        return MainRepository().getEmployeeListFromAPI()
    }

    fun showProgressBar(): LiveData<Boolean> {
        Log.e("Progress","ddd ${MainRepository().getLoadingState().value}")
        return MainRepository().getLoadingState()
    }


}

"ProgressBar" = is null

And in the activity:

mainViewModel.showProgressBar().observe(this, object : Observer<Boolean?> {
    override fun onChanged(isLoading: Boolean?) {
        Log.e("isLoadingProgressBar:", "Loading is...: $isLoading")
        if (isLoading == true){
            progressBar.visibility = View.VISIBLE
        }else{
            progressBar.visibility = View.GONE
        }
    }
})

CodePudding user response:

Buddy - every time you do MainRepository() you're creating a new repository and accessing that. You should have one repository you're working with.

class MainViewModel : ViewModel() {
    private val repository = MainRespository() // ONE repo

    fun getEmployeeListFromRepo() : LiveData<List<Employee>>{

        return repository.getEmployeeListFromAPI() // Get the live data to the ONE repo instead of creating a new one
    }

    fun showProgressBar(): LiveData<Boolean> {
        // Print and return the value from the ONE respository instead of creating
        // TWO new ones in this method
        Log.e("Progress","ddd ${respository.getLoadingState().value}")
        return respository.getLoadingState()
    }
}
  • Related