Skip to content

Instantly share code, notes, and snippets.

@faogustavo
Created November 21, 2016 18:36
Show Gist options
  • Save faogustavo/5b71acfe4bdd5d0fd97c17b5e96626cd to your computer and use it in GitHub Desktop.
Save faogustavo/5b71acfe4bdd5d0fd97c17b5e96626cd to your computer and use it in GitHub Desktop.
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseException;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.MutableData;
import com.google.firebase.database.Transaction;
import com.google.firebase.database.ValueEventListener;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
public class FirebaseHandler {
public static class FindOne<T> {
public interface Callback<T> {
void onItemFound(T value);
void onError(DatabaseException error);
void onFoundNothing();
}
private DatabaseReference myDatabase;
private final Class<T> clazz;
public FindOne(Class<T> clazz, DatabaseReference database, String... params) {
this.myDatabase = database;
this.clazz = clazz;
for (String param : params) {
myDatabase = myDatabase.child(param);
}
}
public void execute(@NotNull final Callback<T> callback) {
System.out.println("executing");
myDatabase.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
if (databaseError != null) {
callback.onError(databaseError.toException());
return;
}
if (!b) {
callback.onError(new DatabaseException("Transactions was not successful."));
return;
}
if (dataSnapshot == null || dataSnapshot.getValue() == null) {
callback.onFoundNothing();
return;
}
try {
callback.onItemFound(dataSnapshot.getValue(clazz));
} catch (Exception ex) {
System.out.println(dataSnapshot.getValue());
callback.onError(new DatabaseException(ex.getMessage()));
}
}
});
}
}
public static class FindMany<T> {
public interface Callback<T> {
void onItemFound(List<T> values);
void onError(DatabaseException error);
void onFoundNothing();
}
private DatabaseReference myDatabase;
private final Class<T> clazz;
public FindMany(Class<T> clazz, DatabaseReference database, String... params) {
this.clazz = clazz;
this.myDatabase = database;
for (String param : params) {
myDatabase = myDatabase.child(param);
}
}
public void execute(final Callback<T> callback) {
myDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot == null) {
callback.onFoundNothing();
return;
}
try {
List data = new ArrayList<>();
for (DataSnapshot child : dataSnapshot.getChildren()) {
if (child.getValue() == null)
continue;
data.add(dataSnapshot.getValue(clazz));
}
callback.onItemFound(data);
} catch (Exception ex) {
callback.onError(new DatabaseException("Child cannot be converted to specified type"));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
callback.onError(databaseError.toException());
}
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment