Skip to content

Instantly share code, notes, and snippets.

@leinardi
Created July 16, 2018 07:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leinardi/4cf623cba07673e51432a1602141e34a to your computer and use it in GitHub Desktop.
Save leinardi/4cf623cba07673e51432a1602141e34a to your computer and use it in GitHub Desktop.
import android.databinding.BaseObservable
import kotlin.properties.Delegates
fun <T> BaseObservable.dataBind(defaultValue: T, fieldId: Int, vararg fieldIds: Int, callback: ((oldValue: T, newValue: T) -> Unit)? = null) =
Delegates.observable(defaultValue) { _, old, new ->
if (old != new) {
callback?.invoke(old, new)
notifyPropertyChanged(fieldId)
fieldIds.forEach { notifyPropertyChanged(it) }
}
}
fun <T> BaseObservable.dataBindAction(noOpValue: T, fieldId: Int, callback: ((oldValue: T, newValue: T) -> Unit)? = null) =
Delegates.observable(noOpValue) { _, old, new ->
if (new != noOpValue) {
callback?.invoke(old, new)
notifyPropertyChanged(fieldId)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment