Created
November 19, 2019 18:09
-
-
Save nseidm1/2fbe7df95f36ccc81d7a76a8cbb910a9 to your computer and use it in GitHub Desktop.
Model Observable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface ModelObservable : Observable { | |
fun notifyChange() | |
fun notifyPropertyChanged(fieldId: Int) | |
} | |
/** | |
* A convenience class that implements an extension of the [Observable] interface and provides | |
* [.notifyPropertyChanged] and [.notifyChange] methods. | |
*/ | |
class ModelObservableImpl : ModelObservable { | |
@Transient | |
private var mCallbacks: PropertyChangeRegistry? = null | |
override fun addOnPropertyChangedCallback(callback: Observable.OnPropertyChangedCallback) { | |
synchronized(this) { | |
if (mCallbacks == null) { | |
mCallbacks = PropertyChangeRegistry() | |
} | |
} | |
mCallbacks!!.add(callback) | |
} | |
override fun removeOnPropertyChangedCallback(callback: Observable.OnPropertyChangedCallback) { | |
synchronized(this) { | |
if (mCallbacks == null) { | |
return | |
} | |
} | |
mCallbacks!!.remove(callback) | |
} | |
/** | |
* Notifies listeners that all properties of this instance have changed. | |
*/ | |
override fun notifyChange() { | |
synchronized(this) { | |
if (mCallbacks == null) { | |
return | |
} | |
} | |
mCallbacks!!.notifyCallbacks(this, 0, null) | |
} | |
/** | |
* Notifies listeners that a specific property has changed. The getter for the property | |
* that changes should be marked with [androidx.databinding.Bindable] to generate a field in | |
* `BR` to be used as `fieldId`. | |
* | |
* @param fieldId The generated BR id for the Bindable field. | |
*/ | |
override fun notifyPropertyChanged(fieldId: Int) { | |
synchronized(this) { | |
if (mCallbacks == null) { | |
return | |
} | |
} | |
mCallbacks!!.notifyCallbacks(this, fieldId, null) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment