How to do it...

  1. 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)
}

  1. 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) }
  1. Declare the temperature: Int variable by delegating it to the ObservableVetoable class instance:
var temperature: Int by ObservableVetoable(initialTemperature, 
updatePrecondition,
updateListener)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.115.39