Skip to content

Commit

Permalink
add the android implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
mamillastre committed Dec 20, 2024
1 parent 6cc4e1e commit cab4468
Show file tree
Hide file tree
Showing 6 changed files with 233 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.capawesome.capacitorjs.plugins.firebase.firestore.classes.constraints.QueryLimitConstraint;
import io.capawesome.capacitorjs.plugins.firebase.firestore.classes.constraints.QueryOrderByConstraint;
import io.capawesome.capacitorjs.plugins.firebase.firestore.classes.constraints.QueryStartAtConstraint;
import io.capawesome.capacitorjs.plugins.firebase.firestore.classes.fields.FirestoreField;
import io.capawesome.capacitorjs.plugins.firebase.firestore.interfaces.QueryNonFilterConstraint;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -45,7 +46,7 @@ public static JSObject createJSObjectFromMap(@Nullable Map<String, Object> map)
} else if (value instanceof Map) {
value = createJSObjectFromMap((Map<String, Object>) value);
}
object.put(key, value);
object.put(key, parseObject(value));
}
return object;
}
Expand All @@ -54,7 +55,7 @@ public static Object createObjectFromJSValue(Object value) throws JSONException
if (value.toString().equals("null")) {
return null;
} else if (value instanceof JSONObject) {
return createHashMapFromJSONObject((JSONObject) value);
return createObjectFromJSONObject((JSONObject) value);
} else if (value instanceof JSONArray) {
return createArrayListFromJSONArray((JSONArray) value);
} else {
Expand Down Expand Up @@ -108,7 +109,7 @@ private static ArrayList<Object> createArrayListFromJSONArray(JSONArray array) t
for (int x = 0; x < array.length(); x++) {
Object value = array.get(x);
if (value instanceof JSONObject) {
value = createHashMapFromJSONObject((JSONObject) value);
value = createObjectFromJSONObject((JSONObject) value);
} else if (value instanceof JSONArray) {
value = createArrayListFromJSONArray((JSONArray) value);
}
Expand All @@ -123,7 +124,7 @@ private static JSArray createJSArrayFromArrayList(ArrayList arrayList) {
if (value instanceof Map) {
value = createJSObjectFromMap((Map<String, Object>) value);
}
array.put(value);
array.put(parseObject(value));
}
return array;
}
Expand All @@ -134,4 +135,24 @@ public static JSObject createSnapshotMetadataResult(DocumentSnapshot snapshot) {
obj.put("hasPendingWrites", snapshot.getMetadata().hasPendingWrites());
return obj;
}

private static Object createObjectFromJSONObject(@NonNull JSONObject object) throws JSONException {
if (FirestoreField.isFirestoreField(object)) {
FirestoreField field = FirestoreField.fromJSONObject(object);
return field.getField();
}

return createHashMapFromJSONObject(object);
}

/**
* Parse an object to return it's firestore field value. Else return the same object.
*/
private static Object parseObject(@NonNull Object object) {
try {
return FirestoreField.fromObject(object).getJSObject();
} catch (Exception e) {}

return object;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.capawesome.capacitorjs.plugins.firebase.firestore.classes.fields;

import androidx.annotation.NonNull;

import org.json.JSONException;
import org.json.JSONObject;

import io.capawesome.capacitorjs.plugins.firebase.firestore.enums.FieldValueMethod;

public class FieldValue {

@NonNull
FieldValueMethod method;

public FieldValue(@NonNull FieldValueMethod method) {
this.method = method;
}

public static FieldValue fromJSONObject(@NonNull JSONObject value) throws JSONException {
return new FieldValue(
FieldValueMethod.fromString(value.getString("method"))
);
}

public Object getField() {
switch (method) {
case CREATE_SERVER_TIMESTAMP:
return com.google.firebase.firestore.FieldValue.serverTimestamp();
default:
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package io.capawesome.capacitorjs.plugins.firebase.firestore.classes.fields;

import androidx.annotation.NonNull;

import com.getcapacitor.JSObject;

import org.json.JSONException;
import org.json.JSONObject;

import io.capawesome.capacitorjs.plugins.firebase.firestore.enums.FirestoreFieldType;

public class FirestoreField {

@NonNull
private FirestoreFieldType type;

@NonNull
private JSONObject value;

private static final String FIRESTORE_FIELD_TYPE = "_capacitorFirestoreFieldType";
private static final String FIRESTORE_FIELD_VALUE = "_capacitorFirestoreFieldValue";

/**
* Is a JSONObject a serialized Firestore field
* @param firestoreFieldData
* @return
*/
public static boolean isFirestoreField(JSONObject firestoreFieldData) {
if (!firestoreFieldData.has(FIRESTORE_FIELD_TYPE)) {
return false;
}
return true;
}

public FirestoreField(FirestoreFieldType type, JSONObject value) {
this.type = type;
this.value = value;
}

public static FirestoreField fromJSONObject(JSONObject data) throws JSONException {
FirestoreFieldType type = FirestoreFieldType.fromString((String) data.get(FIRESTORE_FIELD_TYPE));
JSONObject value = (JSONObject) data.get(FIRESTORE_FIELD_VALUE);
return new FirestoreField(type, value);
}

public static FirestoreField fromObject(Object object) throws IllegalArgumentException {
if (object instanceof com.google.firebase.Timestamp) {
Timestamp timestamp = Timestamp.fromFirestore((com.google.firebase.Timestamp) object);
return new FirestoreField(FirestoreFieldType.TIMESTAMP, timestamp.getValue());
}
throw new IllegalArgumentException("The provided object is not a firestore field");
}

public Object getField() throws JSONException {
switch(type) {
case FIELD_VALUE:
return FieldValue.fromJSONObject(value).getField();
case TIMESTAMP:
return Timestamp.fromJSONObject(value).getField();
default:
return null;
}
}

public JSObject getJSObject() throws JSONException {
JSObject object = new JSObject();
object.put(FIRESTORE_FIELD_TYPE, type.toString());
object.put(FIRESTORE_FIELD_VALUE, JSObject.fromJSONObject(value));
return object;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.capawesome.capacitorjs.plugins.firebase.firestore.classes.fields;

import androidx.annotation.NonNull;

import org.json.JSONException;
import org.json.JSONObject;

public class Timestamp {

@NonNull
long seconds;

@NonNull
int nanoseconds;

public Timestamp(@NonNull long seconds, @NonNull int nanoseconds) {
this.seconds = seconds;
this.nanoseconds = nanoseconds;
}

public static Timestamp fromJSONObject(@NonNull JSONObject value) throws JSONException {
return new Timestamp(
((Number) value.get("seconds")).longValue(),
(int) value.get("nanoseconds")
);
}

public static Timestamp fromFirestore(@NonNull com.google.firebase.Timestamp timestamp) {
return new Timestamp(
timestamp.getSeconds(),
timestamp.getNanoseconds()
);
}

@NonNull
public JSONObject getValue() {
JSONObject value = new JSONObject();
try {
value.put("seconds", seconds);
value.put("nanoseconds", nanoseconds);
} catch (JSONException e) {}
return value;
}

public com.google.firebase.Timestamp getField() throws JSONException {
return new com.google.firebase.Timestamp(
seconds,
nanoseconds
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.capawesome.capacitorjs.plugins.firebase.firestore.enums;

public enum FieldValueMethod {
CREATE_SERVER_TIMESTAMP("serverTimestamp");

private String value;

private FieldValueMethod(String value) {
this.value = value;
}

private String getValue() {
return value;
}

@Override
public String toString() {
return this.getValue();
}

public static FieldValueMethod fromString(String value) {
for(FieldValueMethod v : values())
if(v.getValue().equalsIgnoreCase(value)) return v;
throw new IllegalArgumentException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.capawesome.capacitorjs.plugins.firebase.firestore.enums;

public enum FirestoreFieldType {
FIELD_VALUE("fieldvalue"),
TIMESTAMP("timestamp");

private String value;

private FirestoreFieldType(String value) {
this.value = value;
}

private String getValue() {
return value;
}

@Override
public String toString() {
return this.getValue();
}

public static FirestoreFieldType fromString(String value) {
for(FirestoreFieldType v : values())
if(v.getValue().equalsIgnoreCase(value)) return v;
throw new IllegalArgumentException();
}
}

0 comments on commit cab4468

Please sign in to comment.