Skip to content

Instantly share code, notes, and snippets.

@carotkut94
Last active January 2, 2021 14:28
Show Gist options
  • Save carotkut94/2ba06c99e3d9552ee4571c4646eca719 to your computer and use it in GitHub Desktop.
Save carotkut94/2ba06c99e3d9552ee4571c4646eca719 to your computer and use it in GitHub Desktop.
import android.app.Application
import androidx.databinding.Observable
import androidx.databinding.PropertyChangeRegistry
import androidx.lifecycle.AndroidViewModel
abstract class ObservableViewModel(app: Application): AndroidViewModel(app), Observable {
@delegate:Transient
private val mCallBacks: PropertyChangeRegistry by lazy { PropertyChangeRegistry() }
override fun addOnPropertyChangedCallback(callback: Observable.OnPropertyChangedCallback) {
mCallBacks.add(callback)
}
override fun removeOnPropertyChangedCallback(callback: Observable.OnPropertyChangedCallback) {
mCallBacks.remove(callback)
}
fun notifyChange() {
mCallBacks.notifyChange(this, 0)
}
fun notifyChange(viewId:Int){
mCallBacks.notifyChange(this, viewId)
}
}
@stillie
Copy link

stillie commented Jun 11, 2019

file name is .java but its Kotlin :)

@carotkut94
Copy link
Author

Thanks for point that out, fixed it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment