Skip to content

Instantly share code, notes, and snippets.

@keyboardr
Created November 19, 2017 10:41
Show Gist options
  • Save keyboardr/8259533ec74f703f9e999f103a6b1135 to your computer and use it in GitHub Desktop.
Save keyboardr/8259533ec74f703f9e999f103a6b1135 to your computer and use it in GitHub Desktop.
A LiveData sourced from Firebase Realtime Database
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);
}
}
@rostopira
Copy link

Nice! Rewrote it to Kotlin:

import androidx.lifecycle.LiveData
import com.google.firebase.database.*

class FirebaseLiveData<T> private constructor(
    private val reference: DatabaseReference,
    private val type: Class<T>?,
    private val typeIndicator: GenericTypeIndicator<T>?,
): LiveData<T>() {
    constructor(reference: DatabaseReference, type: Class<T>): this(reference, type, null)
    constructor(reference: DatabaseReference, type: GenericTypeIndicator<T>): this(reference, null, type)

    private val listener = object: ValueEventListener {
        override fun onDataChange(snapshot: DataSnapshot) {
            value = if (type != null)
                snapshot.getValue(type)
            else
                snapshot.getValue(typeIndicator!!)
        }
        override fun onCancelled(error: DatabaseError) {
            value = null
        }
    }

    override fun onActive() {
        super.onActive()
        reference.addValueEventListener(listener)
    }

    override fun onInactive() {
        super.onInactive()
        reference.removeEventListener(listener)
    }
}

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