Simple delegate for properties

This commit is contained in:
Denis Karmyshakov 2018-04-27 14:02:29 +03:00
parent 90c7c25f6c
commit e2107e9990
1 changed files with 16 additions and 0 deletions

View File

@ -0,0 +1,16 @@
package ru.touchin.roboswag.components.extensions
import kotlin.properties.Delegates
import kotlin.properties.ObservableProperty
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
/**
* Simple observable delegate only for notification of new value.
*/
inline fun <T> Delegates.observable(
initialValue: T,
crossinline onChange: (newValue: T) -> Unit
): ReadWriteProperty<Any?, T> = object : ObservableProperty<T>(initialValue) {
override fun afterChange(property: KProperty<*>, oldValue: T, newValue: T) = onChange(newValue)
}