https://kotlinlang.org logo
Title
t

Tim Malseed

12/02/2021, 11:28 AM
Hey, just wanted to run this by you guys and see what comes out.. I wanted a way to be able to define some ‘flags’ in the app - basically a bunch of compile-time defined, observable state holders. So, I came up with the following:
data class Flag<T : Any>(val key: String, val description: String, val defaultValue: T)

class FlagManager() {

    private val flagMap = mutableMapOf<Flag<out Any>, MutableStateFlow<Any>>()

    fun registerFlag(flag: Flag<Any>) {
        if (flagMap.containsKey(flag)) {
            throw IllegalStateException("Flag cannot be registered more than once")
        }
        flagMap[flag] = MutableStateFlow(flag.defaultValue)
    }

    fun <T : Any> getFlagState(flag: Flag<T>): StateFlow<T> {
        if (!flagMap.containsKey(flag)) {
            throw IllegalStateException("Flag not registered")
        }

        return (flagMap[flag] as MutableStateFlow<T>).asStateFlow()
    }

    fun <T : Any> updateFlagState(flag: Flag<T>, value: T) {
        if (!flagMap.containsKey(flag)) {
            throw IllegalStateException("Flag not registered")
        }

        (flagMap[flag] as MutableStateFlow<T>).value = value
    }
}
Whenever I write some code that holds StateFlows in an array or map or something, I always get nervous that I’ve over-engineered the solution.
I haven’t actually tested this code yet. But I just wanted to see what the Kotlin people think.. Has this wheel already been invented?
t

Tgo1014

12/02/2021, 11:43 AM
We do kinda the same here (the only difference is on get, if it doesn’t exist we create and return instead of exception) and it works fine. I’ll be watching this thread to see if there’s some better way to do it haha
👍 1
t

Tim Malseed

12/02/2021, 11:50 AM
Yeah, that would definitely simplify things. The register call isn't really necessary.