Answer the question
In order to leave comments, you need to log in
How to implement network request state monitoring in MVVM?
I am writing a simple MVVM application.
The ViewModel has a field of type LiveData where I put the value of the fields of my NetworkState state marker class.
It looks like this:
enum class Status {
WAITING,
RUNNING,
SUCCESS,
FAILED
}
class NetworkState(val status: Status, val msg: String) {
companion object {
val WAITING:NetworkState
val LOADED: NetworkState
val LOADING: NetworkState
val ERROR: NetworkState
val API_LIMIT_EXCEEDED: NetworkState
val NO_INTERNET:NetworkState
init {
WAITING = NetworkState(Status.WAITING,"Waiting user request")
LOADED = NetworkState(Status.SUCCESS, "Success")
LOADING = NetworkState(Status.RUNNING, "Running")
ERROR = NetworkState(Status.FAILED, "Something went wrong")
API_LIMIT_EXCEEDED = NetworkState(Status.FAILED, "API limit exceeded")
NO_INTERNET = NetworkState(Status.FAILED,"No internet connection")
}
}
}
_networkState.postValue(NetworkState.NO_INTERNET)
_networkState.postValue(NetworkState.WAITING)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question