Simplify ViewState by removing Reloading state and just using Loading state

57 views Asked by At

I do not want to hide RecyclerView when I am refreshing using SwipeRefreshLayout. As a result I introduced Reloading state in my ViewState class :

sealed class Resource<out T> {
    class Loading<out T> : Resource<T>()
    class Reloading<out T> : Resource<T>()
    data class Success<out T>(val data: T?) : Resource<T>()
    data class Failure<out T>(val cause: String?) : Resource<T>()
}

And here is my binding Adapters :

    @JvmStatic
    @BindingAdapter("refreshing")
    fun setSwipeRefreshLayout(view: SwipeRefreshLayout, resource: Resource<*>?) {
        view.isRefreshing = resource is Resource.Loading || resource is Resource.Reloading
        view.isEnabled = resource !is Resource.Failure
    }

    @JvmStatic
    @BindingAdapter("showError")
    fun showError(view: View, resource: Resource<*>?) {
        view.visibility = if (resource is Resource.Failure) View.VISIBLE else View.GONE
    }

    @JvmStatic
    @BindingAdapter("showData")
    fun showData(view: RecyclerView, resource: Resource<*>?) {
        view.visibility = if (resource is Resource.Reloading || resource is Resource.Success)
            View.VISIBLE else View.GONE

    }

Is there any solution to simplify ViewState classes and remove Reloading state (by just using Loading state)? Source code can be found here : https://github.com/AliRezaeiii/SavingGoals-Cache

1

There are 1 answers

0
lbasek On BEST ANSWER

The most easiest way to cut off Reloading state and add boolean indicator to Loading state:

sealed class Resource<out T> {
    data class Loading<out T>(val isRefreshing: Boolean) : Resource<T>()
    data class Success<out T>(val data: T?) : Resource<T>()
    data class Failure<out T>(val cause: String?) : Resource<T>()
}