Last active
July 28, 2017 06:43
-
-
Save PaulWoitaschek/ea893f26dd1fe1b30c0fcc286685ea00 to your computer and use it in GitHub Desktop.
Controller Argument Delegation. An extensible mechanism for seriaizing values to the Controllers bundle.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* An abstract argument delegate that uses the property to infer the key name for the bundle. | |
*/ | |
abstract class ControllerArgumentDelegate<T : Any> : ReadWriteProperty<Controller, T> { | |
private var value: T? = null | |
override final fun getValue(thisRef: Controller, property: KProperty<*>): T { | |
if (value == null) { | |
val key = property.name | |
value = fromBundle(thisRef.args, key) | |
} | |
return value ?: throw IllegalStateException("Property ${property.name} could not be read") | |
} | |
override final fun setValue(thisRef: Controller, property: KProperty<*>, value: T) { | |
val bundle = thisRef.args | |
val key = property.name | |
toBundle(bundle, key, value) | |
} | |
abstract fun toBundle(bundle: Bundle, key: String, value: T) | |
abstract fun fromBundle(bundle: Bundle, key: String): T | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EnumArgumentDelegate<E : Enum<E>>(private val clazz: Class<E>) : ControllerArgumentDelegate<E>() { | |
override fun toBundle(bundle: Bundle, key: String, value: E) { | |
bundle.putString(key, value.name) | |
} | |
override fun fromBundle(bundle: Bundle, key: String): E { | |
val stringValue = bundle.getString(key) | |
return java.lang.Enum.valueOf(clazz, stringValue) | |
} | |
} | |
inline fun <reified E : Enum<E>> enumArgumentDelegate() = EnumArgumentDelegate(E::class.java) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class StringArgumentDelegate : ControllerArgumentDelegate<String>() { | |
override fun toBundle(bundle: Bundle, key: String, value: String) { | |
bundle.putString(key, value) | |
} | |
override fun fromBundle(bundle: Bundle, key: String): String = bundle.getString(key) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample: