Skip to content

Instantly share code, notes, and snippets.

@tiiime
Last active January 20, 2022 03:26
Show Gist options
  • Save tiiime/8979ee70f828247c6d0d3920572b4238 to your computer and use it in GitHub Desktop.
Save tiiime/8979ee70f828247c6d0d3920572b4238 to your computer and use it in GitHub Desktop.
delegate set operate, run specify action

Usage

var drawGravity: Int by InvalidateDelegate(Gravity.LEFT)

private inner class InvalidateDelegate<T>(t: T) : SetActionDelegate<T>(t, {
    invalidate()
})
open class SetActionDelegate<T>(
var t: T,
inline val action: () -> Unit
) {
operator fun getValue(any: Any, property: KProperty<*>): T = t
operator fun setValue(any: Any, property: KProperty<*>, i: T) {
if (t != i) {
t = i
action()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment