Last active
September 21, 2017 04:58
-
-
Save Atternatt/376d11a1e08d41c719a8016cb58ffb97 to your computer and use it in GitHub Desktop.
Property delegation Examples
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
fun backgroundColorAnimator(): ReadWriteProperty<View, Int> = | |
object : ReadWriteProperty<View, Int> { | |
override fun getValue(thisRef: View, property: KProperty<*>): Int { | |
return (thisRef.background as? ColorDrawable)?.color ?: 0 | |
} | |
override fun setValue(thisRef: View, property: KProperty<*>, value: Int) { | |
ValueAnimator.ofObject(ArgbEvaluator(), getValue(thisRef, property), value).apply { | |
duration = 1000L | |
interpolator = AccelerateDecelerateInterpolator() | |
addUpdateListener { thisRef.setBackgroundColor(it.animatedValue as Int) } | |
}.start() | |
} | |
} | |
//View ahora tiene una propiedad bgColor que si la cmabiamos animará el background de la vista de forma animada | |
var View.bgColor: Int by backgroundColorAnimator() |
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
fun <Param> View.invalidator(initialValue: Param, afterChangeBlock: ((Param) -> Unit)? = {}): ReadWriteProperty<Any?, Param> = | |
object : ObservableProperty<Param>(initialValue) { | |
override fun afterChange(property: KProperty<*>, oldValue: Param, newValue: Param) { | |
afterChangeBlock?.invoke(newValue) | |
[email protected]() | |
} | |
} | |
fun <Param> View.layoutRequester(initialValue: Param, afterChangeBlock: ((Param) -> Unit)? = {}): ReadWriteProperty<Any?, Param> = | |
object : ObservableProperty<Param>(initialValue) { | |
override fun afterChange(property: KProperty<*>, oldValue: Param, newValue: Param) { | |
afterChangeBlock?.invoke(newValue) | |
[email protected]() | |
} | |
} |
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
fun <Param : Number> View.propertyAnimator(initValue: Param, beforAnimationBlock: ((Param) -> Param)? = null): ReadWriteProperty<Any?, Param> = | |
object : ReadWriteProperty<Any?, Param> { | |
private var innerValue: Param = initValue | |
private val persistedInitialValue = innerValue | |
override fun getValue(thisRef: Any?, property: KProperty<*>): Param { | |
return innerValue | |
} | |
@Suppress("UNCHECKED_CAST") | |
override fun setValue(thisRef: Any?, property: KProperty<*>, value: Param) { | |
val finalValue = beforAnimationBlock?.invoke(value) ?: value | |
obtainValueAnimator(persistedInitialValue, finalValue).apply { | |
duration = 1500L | |
interpolator = BounceInterpolator() | |
addUpdateListener { innerValue = it.animatedValue as Param; invalidate() } | |
}.start() | |
} | |
private fun obtainValueAnimator(innerValue: Param, value: Param): ValueAnimator { | |
return when (innerValue) { | |
is Float -> ValueAnimator.ofFloat(innerValue, value.toFloat()) | |
is Double -> ValueAnimator.ofFloat(innerValue.toFloat(), value.toFloat()) | |
is Int -> ValueAnimator.ofInt(innerValue, value.toInt()) | |
is Short -> ValueAnimator.ofInt(innerValue.toInt(), value.toInt()) | |
is Long -> ValueAnimator.ofInt(innerValue.toInt(), value.toInt()) | |
else -> ValueAnimator.ofFloat(innerValue.toFloat(), value.toFloat()) | |
} | |
} | |
} |
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
//estas propiedades están siendo definidas dentro de una View | |
var startingDegree: Int by invalidator(270) | |
var prefixText: String by layoutRequester("£") | |
var progress: Int by propertyAnimator(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment