带有密封类的 MutableLiveData

问题描述

我有这些反馈类,我想使用密封类

折射

反馈课

data class Single(
    var type: TYPE? = null,var part: PART? = null,var status: STATUS? = null,var hardware: HARDWARE? = null,var software: SOFTWARE? = null
)

data class Multiple(
    var type: TYPE? = null,var hardware: List<HARDWARE>? = null,var software: List<SOFTWARE>? = null
)

视图模型

class Sharedviewmodel : viewmodel() {

    private val single: mutablelivedata<Single> = mutablelivedata()
    private val multiple: mutablelivedata<Multiple> = mutablelivedata()

    var isSingle = false
        private set

    val isIssue: Boolean
        get() = single.value?.type === TYPE.ISSUE
    

    val isHardware: Boolean
        get() = single.value?.part === PART.HARDWARE


    val isReproductible: Boolean
        get() = single.value?.status === STATUS.REPRODUCTIBLE


    fun setType(type: TYPE?) {
        this.single.value =
            this.single.value?.apply { this.type = type } ?: Single(type = type)

        this.multiple.value =
            this.multiple.value?.apply { this.type = type } ?: Multiple(type = type)
    }

    fun setPart(part: PART?) {
        this.single.value =
            this.single.value?.apply {
                this.part = part
                this.hardware = null
                this.software = null
            } ?: Single(part = part)


        this.multiple.value =
            this.multiple.value?.apply {
                this.part = part
                this.hardware = null
                this.software = null
            } ?: Multiple(part = part)
    }

    fun setStatus(status: STATUS?) {
        this.single.value =
            this.single.value?.apply { this.status = status } ?: Single(status = status)

        this.multiple.value =
            this.multiple.value?.apply { this.status = status }
                ?: Multiple(status = status)
    }

    fun setSoftware(software: SOFTWARE?) {
        this.isSingle = true
        this.single.value =
            this.single.value?.apply { this.software = software }
                ?: Single(software = software)
    }

    fun setMultipleSoftware(software: List<SOFTWARE>?) {
        this.isSingle = false
        this.multiple.value =
            this.multiple.value?.apply { this.software = software } ?: Multiple(
                software = software
            )
    }

    fun setHardware(hardware: HARDWARE?) {
        this.isSingle = true
        this.single.value =
            this.single.value?.apply { this.hardware = hardware }
                ?: Single(hardware = hardware)
    }

    fun setMultipleHardware(hardware: List<HARDWARE>?) {
        this.isSingle = false
        this.multiple.value =
            this.multiple.value?.apply { this.hardware = hardware } ?: Multiple(
                hardware = hardware
            )
    }

    fun getFeedback(): mutablelivedata<Single> = this.single

    fun getMultiple(): mutablelivedata<Multiple> = this.multiple
}

一个问题:在这里使用密封类是个好主意吗?

第二个问题:在给定这个新类的情况下,是否可以使用密封类在视图模型中仅使用一个 mutablelivedata 进行折射?

新类:

sealed class Feedback {
    data class Single(
        var type: TYPE? = null,var software: SOFTWARE? = null
    )

    data class Multiple(
        var type: TYPE? = null,var software: List<SOFTWARE>? = null
    )
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)