Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(firestore): add timestamp & fieldvalue management #677

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
15 changes: 14 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,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 @@ -44,7 +45,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 @@ -53,7 +54,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 @@ -107,7 +108,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 @@ -122,8 +123,28 @@ 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;
}

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();
}
}
2 changes: 1 addition & 1 deletion packages/firestore/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"rimraf": "3.0.2",
"rollup": "2.77.2",
"swiftlint": "1.0.1",
"typescript": "4.1.5"
"typescript": "4.9.5"
},
"peerDependencies": {
"@capacitor/core": "^6.0.0",
Expand Down
Loading
Loading