-
-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6cc4e1e
commit cab4468
Showing
6 changed files
with
233 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
.../java/io/capawesome/capacitorjs/plugins/firebase/firestore/classes/fields/FieldValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
...a/io/capawesome/capacitorjs/plugins/firebase/firestore/classes/fields/FirestoreField.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...n/java/io/capawesome/capacitorjs/plugins/firebase/firestore/classes/fields/Timestamp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...ain/java/io/capawesome/capacitorjs/plugins/firebase/firestore/enums/FieldValueMethod.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...n/java/io/capawesome/capacitorjs/plugins/firebase/firestore/enums/FirestoreFieldType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |