Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions android/src/firebase/database/FirebaseDatabaseModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import androidx.annotation.Nullable;

import com.google.android.gms.common.util.ArrayUtils;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
Expand All @@ -32,6 +34,7 @@
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -83,6 +86,20 @@ public void queryFirestore(KrollDict kd) {
Log.w(TAG, "Error getting documents.", task.getException());
}
});
/*dr.addSnapshotListener((value, error) -> {
if (error != null) {
Log.w(TAG, "Listen failed.", error);
return;
}
String source = value != null && value.getMetadata().hasPendingWrites()
? "Local" : "Server";

if (value != null && value.exists()) {
Log.i(TAG, source + " data: " + value.getData());
} else {
Log.i(TAG, source + " data: null");
}
});*/
} else {
cr.get().addOnCompleteListener(task -> {
if (task.isSuccessful()) {
Expand All @@ -109,6 +126,63 @@ public void queryFirestore(KrollDict kd) {
}
}

@Kroll.method
public void updateFirestore(KrollDict kd) {
databaseFirestore = FirebaseFirestore.getInstance();
String col = kd.getString("collection");
String doc = kd.getString("document");

if (!col.isEmpty() && !doc.isEmpty()) {
Map<String, Object> dataMap = new HashMap<>();
KrollDict data = kd.getKrollDict("data");
for (Map.Entry<String, Object> entry : data.entrySet()) {
dataMap.put(entry.getKey(), entry.getValue());
}

databaseFirestore.collection(col).document(doc)
.set(dataMap)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "DocumentSnapshot successfully written!");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG, "Error writing document", e);
}
});
} else {
Log.w(TAG, "Please set 'collection' and 'document'");
}
}

@Kroll.method
public void deleteFirestore(KrollDict kd) {
databaseFirestore = FirebaseFirestore.getInstance();
String col = kd.getString("collection");
String doc = kd.getString("document");
if (!col.isEmpty() && !doc.isEmpty()) {
databaseFirestore.collection(col).document(doc)
.delete()
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "DocumentSnapshot successfully deleted!");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG, "Error deleting document", e);
}
});
} else {
Log.w(TAG, "Please set 'collection' and 'document'");
}
}

private JSONObject mapToJSON(Map<String, Object> map) throws JSONException {
JSONObject obj = new JSONObject();

Expand Down