android 带有MVVM的Sealed类的枚举

ssm49v7z  于 2023-05-27  发布在  Android
关注(0)|答案(3)|浏览(133)

请检查以下代码:

sealed interface ResourceV2<out T> {

enum class Status {
    SUCCESS,
    ERROR,
    LOADING
}

data class Success<T>(val data: T, val message: String, val code: Int, val state: Boolean) : ResourceV2<T>

data class Error<T>(val data: T? = null, val message: String, val code: Int, val state: Boolean) : ResourceV2<T>

data class Loading<T>(val state: Boolean) : ResourceV2<T>

}
在视图中访问如下:

Resource.Status.SUCCESS

我觉得不应该使用ENUM,应该有一些其他的方法来做干净的MVVM架构。请建议一个答案,包括我的资源类应该是什么样子,以及我如何在视图中访问它。

OLDER资源类:

data class Resource<out T>(val status: Status, val data: T?, val message: String?, val code: Int, val state: Boolean) {

    enum class Status {
        SUCCESS,
        ERROR,
        LOADING
    }

    companion object {
        fun <T> success(data: T, message: String, code: Int, state: Boolean): Resource<T> {
            return Resource(Status.SUCCESS, data, message, code, state)
        }

        fun <T> error(data: T? = null, message: String, code: Int, state: Boolean): Resource<T> {
            return Resource(Status.ERROR, data, message, code, state)
        }

        fun <T> loading(state: Boolean): Resource<T> {
            return Resource(Status.LOADING, null, null, NetworkConstant.NetworkValues.DEFAULT_ERROR_CODE, state)
        }
    }
}
hm2xizp9

hm2xizp91#

这就是Resource类的外观

sealed class Resource<T>(val data:T? = null,val message :String? = null,val state: Boolean) {

    class Success<T>(data:T,val state: Boolean):Resource<T>(data,state)
    class Error<T>(data: T?=null,message: String,val state: Boolean):Resource<T>(data,message,state)
    class Loading(val state: Boolean) : Resource(state)

}
viewModel.cartData.collect{
    setLoadingState(it.state)
       when(it){
                is Resource.Success -> {
                    
                }
                is Resource.Loading -> {
                    
                }
                is Resource.Error -> {
                    
                }
            }
}
cl25kdpy

cl25kdpy2#

在所有情况下都有state: Boolean,因此可以在密封类或接口中定义它。
我更喜欢使用密封的接口而不是密封的类,除非真的需要一些使用公共属性的最终函数。
Loading可以在没有泛型类型的情况下定义,因为它没有任何T属性。这使得它的示例化和使用更简单一些。

sealed interface Resource<out T> {
    val state: Boolean

    data class Success<out T>(val data: T, val message: String, val code: Int, override val state: Boolean): Resource<T>
    data class Error<out T>(val data: T? = null, val message: String, val code: Int, override val state: Boolean): Resource<T>
    data class Loading(override val state: Boolean): Resource<Nothing>
}

when语句中使用is检查来收集这些,如Kotlin文档中关于密封类的演示。
在我看来,Error有一个data属性,而Success有一个message属性,这似乎有点不寻常,但在您的用例中可能是有意义的。

njthzxwz

njthzxwz3#

如果不想调用Resource.Status.SUCCESS,请从密封类中删除枚举。
您当前的密封类如下所示

sealed class ResourceV2<out T> {
    data class Success<T>(val data: T, val message: String, val code: Int, val state: Boolean) : ResourceV2<T>()
    data class Error<T>(val data: T? = null, val message: String, val code: Int, val state: Boolean) : ResourceV2<T>()
    data class Loading<T>(val state: Boolean) : ResourceV2<T>()
}

并在观察者中直接访问它们,如下所示-

when (resource) {
    is ResourceV2.Success -> {
        val data = resource.data
        val message = resource.message
        val code = resource.code
        val state = resource.state
    }
    is ResourceV2.Error -> {
        val data = resource.data
        val message = resource.message
        val code = resource.code
        val state = resource.state
    }
    is ResourceV2.Loading -> {
        val state = resource.state
    }
}

有关更多详细信息,请执行https://developer.android.com/kotlin/flow/stateflow-and-sharedflow

相关问题