Live Data Observer called only once _ Android

Viewed 24

Live Data Observer called only once. It is not updating the data from server when api is called again to update UI.

Here is my activity:

class LoginActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
     super.onCreate(savedInstanceState)
         onClick(){
             callAPI(binding.edtEMail.text.toString(), binding.edtPasswords.text.toString())
         }
     }

    private fun callAPI(userName: String, password: String) {
        var factory = object : ViewModelProvider.Factory {
            override fun <T : ViewModel> create(modelClass: Class<T>): T {
                return LoginViewModel(
                    networkAvailable,
                    application,
                    getLoginUseCase,
                    userName,
                    password
                ) as T
            }
        }

        val loginViewModel: LoginViewModel by lazy {
            ViewModelProvider(this, factory)[LoginViewModel::class.java]
        }

        loginViewModel.loginMainEntity.observe(this, Observer {
            when (it) {
                is Resource.Success -> {
                    it.data?.let { it ->
                        val intent = Intent(this, MainActivity::class.java)
                        startActivity(intent)
                        finish()
                    }
                }
                is Resource.Error -> {
                    it.message?.let { it ->
                        when (it) {
                            getString(R.string.invalid_login) -> {
                                Toast.makeText(this, R.string.error_user_pass, Toast.LENGTH_LONG)
                                    .show()
                            }
                            getString(R.string.service_not_available) -> {
                                Toast.makeText(
                                    this,
                                    R.string.error_service_not_available,
                                    Toast.LENGTH_LONG
                                )
                                    .show()
                            }
                            else -> {
                                Toast.makeText(this, it, Toast.LENGTH_LONG)
                                    .show()
                            }
                        }
                    }
                }
            }
        })
    }
}

And here is LoginViewModel:

class LoginViewModel constructor
    (
    private val networkAvailable: NetworkAvailable,
    private val app: Application,
    private val getLoginUseCase: GetLoginUseCase,
    private val userName: String,
    private val password: String
) : ViewModel() {

    private val _loginMainEntity = MutableLiveData<Resource<LoginMainEntity>>()
    val loginMainEntity: LiveData<Resource<LoginMainEntity>>
        get() = _loginMainEntity

    init {
        loginValues()
    }

    private fun loginValues() {
        viewModelScope.launch {
            try {
                _loginMainEntity.postValue(Resource.Loading())
            } catch (e: Exception) {
                _loginMainEntity.postValue(Resource.Error(app.resources.getString(R.string.unknown)))
            }
            try {
                if (networkAvailable.isNetworkConnected()) {
                    try {
                        _loginMainEntity.postValue(getLoginUseCase.execute(userName, password))
                    } catch (e: Exception) {
                        _loginMainEntity.postValue(Resource.Error(app.resources.getString(R.string.error_api)))
                    }
                } else if (!networkAvailable.isNetworkConnected()) {
                    try {
                        _loginMainEntity.postValue(Resource.Error(app.resources.getString(R.string.error_api_network)))
                    } catch (e: Exception) {
                        _loginMainEntity.postValue(Resource.Error(app.resources.getString(R.string.error_api_network)))
                    }
                }
            } catch (e: Exception) {
                try {
                    _loginMainEntity.postValue(Resource.Error(app.resources.getString(R.string.error_api)))
                } catch (e: Exception) {
                    _loginMainEntity.postValue(Resource.Error(app.resources.getString(R.string.error_api)))
                }
            }
        }
    }
}
0 Answers
Related