通过删除重新加载状态并仅使用加载状态来简化 ViewState
Simplify ViewState by removing Reloading state and just using Loading state
我不想在使用 SwipeRefreshLayout 刷新时隐藏 RecyclerView。结果,我在我的 ViewState class 中引入了 Reloading 状态:
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>()
}
这是我的绑定适配器:
@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
}
是否有任何解决方案可以简化 ViewState classes 并删除 Reloading 状态(仅使用 Loading 状态)?
源代码可以在这里找到:https://github.com/AliRezaeiii/SavingGoals-Cache
截断Reloading state
并向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>()
}
我不想在使用 SwipeRefreshLayout 刷新时隐藏 RecyclerView。结果,我在我的 ViewState class 中引入了 Reloading 状态:
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>()
}
这是我的绑定适配器:
@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
}
是否有任何解决方案可以简化 ViewState classes 并删除 Reloading 状态(仅使用 Loading 状态)? 源代码可以在这里找到:https://github.com/AliRezaeiii/SavingGoals-Cache
截断Reloading state
并向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>()
}