- Define the custom property delegate called ObservableVetoableDelegate as a subclass of the ObservableProperty class:
class ObservableVetoable<T>(initialValue: T,
val updatePrecondition: (old: T, new: T)
-> Boolean,
val updateListener: (old: T, new: T)
-> Unit) :
ObservableProperty<T>(initialValue = initialValue) {
override fun beforeChange(property: KProperty<*>,
oldValue: T,
newValue: T): Boolean =
updatePrecondition(oldValue, newValue)
override fun afterChange(property: KProperty<*>,
oldValue: T,
newValue: T) =
updateListener(oldValue, newValue)
}
- Define the initialTemperature, updatePrecondition, and updateListener arguments required by the ObservableVetoable class:
val initialTemperature = 1
val updatePrecondition: (Int, Int) -> Boolean =
{ oldValue, newValue -> Math.abs(oldValue - newValue) >= 10 }
val updateListener: (Int, Int) -> Unit = { _, newValue -> println(newValue) }
- Declare the temperature: Int variable by delegating it to the ObservableVetoable class instance:
var temperature: Int by ObservableVetoable(initialTemperature,
updatePrecondition,
updateListener)