Skip to content

Instantly share code, notes, and snippets.

@carotkut94
Created January 28, 2019 10:13
Show Gist options
  • Save carotkut94/f8a0ca733d284fed50982ca9ae490372 to your computer and use it in GitHub Desktop.
Save carotkut94/f8a0ca733d284fed50982ca9ae490372 to your computer and use it in GitHub Desktop.
package androidx.databinding;
import androidx.annotation.NonNull;
/**
* A convenience class that implements {@link android.databinding.Observable} interface and provides
* {@link #notifyPropertyChanged(int)} and {@link #notifyChange} methods.
*/
public class BaseObservable implements Observable {
private transient PropertyChangeRegistry mCallbacks;
public BaseObservable() {
}
@Override
public void addOnPropertyChangedCallback(@NonNull OnPropertyChangedCallback callback) {
synchronized (this) {
if (mCallbacks == null) {
mCallbacks = new PropertyChangeRegistry();
}
}
mCallbacks.add(callback);
}
@Override
public void removeOnPropertyChangedCallback(@NonNull OnPropertyChangedCallback callback) {
synchronized (this) {
if (mCallbacks == null) {
return;
}
}
mCallbacks.remove(callback);
}
/**
* Notifies listeners that all properties of this instance have changed.
*/
public void 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 {@link Bindable} to generate a field in
* <code>BR</code> to be used as <code>fieldId</code>.
*
* @param fieldId The generated BR id for the Bindable field.
*/
public void notifyPropertyChanged(int fieldId) {
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