Created
November 19, 2017 10:41
-
-
Save keyboardr/8259533ec74f703f9e999f103a6b1135 to your computer and use it in GitHub Desktop.
A LiveData sourced from Firebase Realtime Database
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
import android.arch.lifecycle.LiveData; | |
import android.support.annotation.NonNull; | |
import android.util.Log; | |
import com.google.firebase.database.DataSnapshot; | |
import com.google.firebase.database.DatabaseError; | |
import com.google.firebase.database.DatabaseReference; | |
import com.google.firebase.database.GenericTypeIndicator; | |
import com.google.firebase.database.ValueEventListener; | |
public class FirebaseLiveData<T> extends LiveData<T> { | |
private static final String TAG = "FirebaseLiveData"; | |
@NonNull | |
private final DatabaseReference reference; | |
private final Class<T> type; | |
private final GenericTypeIndicator<T> typeIndicator; | |
private ValueEventListener listener = new ValueEventListener() { | |
@Override | |
public void onDataChange(DataSnapshot dataSnapshot) { | |
if (type != null) { | |
setValue(dataSnapshot.getValue(type)); | |
} else if (typeIndicator != null) { | |
setValue(dataSnapshot.getValue(typeIndicator)); | |
} else { | |
Log.w(TAG, "no type specified"); | |
//noinspection unchecked | |
setValue((T) dataSnapshot.getValue()); | |
} | |
} | |
@Override | |
public void onCancelled(DatabaseError databaseError) { | |
setValue(null); | |
} | |
}; | |
public FirebaseLiveData(@NonNull DatabaseReference reference, Class<T> type) { | |
this.reference = reference; | |
this.type = type; | |
typeIndicator = null; | |
} | |
public FirebaseLiveData(@NonNull DatabaseReference reference, | |
GenericTypeIndicator<T> typeIndicator) { | |
this.reference = reference; | |
this.type = null; | |
this.typeIndicator = typeIndicator; | |
} | |
@Override | |
protected void onActive() { | |
super.onActive(); | |
reference.addValueEventListener(listener); | |
} | |
@Override | |
protected void onInactive() { | |
super.onInactive(); | |
reference.removeEventListener(listener); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice! Rewrote it to Kotlin: