manueldidonna / pokemon-save-editor-android

A work-in-progress pokémon save editor for Android
GNU General Public License v3.0
0 stars 0 forks source link

Support future Pokemon properties #2

Closed manueldidonna closed 4 years ago

manueldidonna commented 4 years ago

Many properties have been added from one generation to the next. I don't want to create specific pokemon implementation for each gen.

sealed class Property<out T> {
    object Nothing: Property<Nothing>()
    data class Value<T>(val value: T): Property<T>()
}

inline fun <T> T.asProperty(): Property<T> = Property.Value(this)
inline fun <T> Property<T>.valueOrNull(): Property.Value<T>? = this as? Property.Value<T>

interface Pokemon {
    val heldItem: Property<Int> 
        get() = Property.Nothing
}

internal class Gen2Pokemon: Pokemon {
    override val heldItem = itemId.asProperty()
}

// From UI
@Composable
fun ShowInfo(pokemon: Pokemon) {
    pokemon.heldItem.valueOrNull()?.let {
        ShowHeldItem(it.value)
    }
}