diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f168753..5d3c9d11 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ ### Fixes - [#601](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/issues/601) Error using admob +- [#604](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/issues/604) Date saved to Firestore get retrieved as strings ## 5.1.2 (2018, January 8) diff --git a/demo-ng/app/item/items.component.ts b/demo-ng/app/item/items.component.ts index 692cb8e0..b0e89b82 100644 --- a/demo-ng/app/item/items.component.ts +++ b/demo-ng/app/item/items.component.ts @@ -133,6 +133,9 @@ export class ItemsComponent implements OnInit { .then((querySnapshot: firestore.QuerySnapshot) => { querySnapshot.forEach(doc => { console.log(`${doc.id} => ${JSON.stringify(doc.data())}`); + // since there's a reference stored here, we can use that to retrieve its data + const docRef: firestore.DocumentReference = doc.data().ref2sf; + docRef.get().then(res => console.log("docref.get: " + JSON.stringify(res.data()))); }); }) .catch(err => console.log("Get failed, error" + err)); diff --git a/demo-ng/package.json b/demo-ng/package.json index 48c9d772..7073c5e2 100644 --- a/demo-ng/package.json +++ b/demo-ng/package.json @@ -24,7 +24,7 @@ "angularfire2": "^5.0.0-rc.4", "firebase": "^4.6.2", "nativescript-angular": "~4.4.0", - "nativescript-plugin-firebase": "5.1.2", + "nativescript-plugin-firebase": "5.1.3", "nativescript-theme-core": "~1.0.2", "reflect-metadata": "~0.1.8", "rxjs": "~5.4.2", diff --git a/demo/package.json b/demo/package.json index bd397e56..3ef2db24 100644 --- a/demo/package.json +++ b/demo/package.json @@ -1,15 +1,15 @@ { "nativescript": { "id": "org.nativescript.firebasedemo", - "tns-android": { + "tns-ios": { "version": "3.4.0" }, - "tns-ios": { + "tns-android": { "version": "3.4.0" } }, "dependencies": { - "nativescript-plugin-firebase": "5.1.2", + "nativescript-plugin-firebase": "5.1.3", "nativescript-theme-core": "^1.0.4", "nativescript-unit-test-runner": "^0.3.4", "tns-core-modules": "~3.4.0" diff --git a/src/firebase.android.ts b/src/firebase.android.ts index a8b8154a..9ce0c18f 100755 --- a/src/firebase.android.ts +++ b/src/firebase.android.ts @@ -20,7 +20,7 @@ let fbCallbackManager = null; const GOOGLE_SIGNIN_INTENT_ID = 123; const REQUEST_INVITE_INTENT_ID = 48; -const gson = lazy(() => typeof(com.google.gson) === "undefined" ? null : new com.google.gson.Gson()); +// const gson = lazy(() => typeof(com.google.gson) === "undefined" ? null : new com.google.gson.Gson()); const messagingEnabled = lazy(() => typeof(com.google.firebase.messaging) !== "undefined"); const dynamicLinksEnabled = lazy(() => typeof(com.google.android.gms.appinvite) !== "undefined"); @@ -167,13 +167,18 @@ firebase.toValue = val => { return returnVal; }; +// no longer using Gson as fi Firestore's DocumentReference isn't serialized firebase.toJsObject = javaObj => { - if (gson() !== null) { - return JSON.parse(gson().toJson(javaObj)); - } else { - // temp fallback for folks not having fetched gson yet in their build for some reason - return firebase.toJsObjectLegacy(javaObj); - } + // if (gson() !== null) { + // try { + // return JSON.parse(gson().toJson(javaObj)); // this may fail if fi a DocumentReference is encountered + // } catch (ignore) { + // return firebase.toJsObjectLegacy(javaObj); + // } + // } else { + // fallback for folks not having fetched gson yet in their build for some reason + return firebase.toJsObjectLegacy(javaObj); + // } }; firebase.toJsObjectLegacy = javaObj => { @@ -191,6 +196,17 @@ firebase.toJsObjectLegacy = javaObj => { case 'java.lang.Long': case 'java.lang.Double': return Number(String(javaObj)); + case 'java.util.Date': + return new Date(javaObj); + case 'com.google.firebase.firestore.GeoPoint': + return { + "latitude": javaObj.getLatitude(), + "longitude": javaObj.getLongitude() + }; + case 'com.google.firebase.firestore.DocumentReference': + const path: string = javaObj.getPath(); + const lastSlashIndex = path.lastIndexOf("/"); + return firebase.firestore._getDocumentReference(javaObj, path.substring(0, lastSlashIndex), path.substring(lastSlashIndex + 1)); case 'java.util.ArrayList': node = []; for (let i = 0; i < javaObj.size(); i++) { @@ -198,11 +214,15 @@ firebase.toJsObjectLegacy = javaObj => { } break; default: - node = {}; - const iterator = javaObj.entrySet().iterator(); - while (iterator.hasNext()) { - const item = iterator.next(); - node[item.getKey()] = firebase.toJsObjectLegacy(item.getValue()); + try { + node = {}; + const iterator = javaObj.entrySet().iterator(); + while (iterator.hasNext()) { + const item = iterator.next(); + node[item.getKey()] = firebase.toJsObjectLegacy(item.getValue()); + } + } catch (e) { + console.log("PLEASE REPORT THIS AT https://github.com/NativeScript/NativeScript/issues: Tried to serialize an unsupported type: javaObj.getClass().getName(), error: " + e); } } return node; @@ -2306,9 +2326,21 @@ firebase.firestore.onCollectionSnapshot = (colRef: com.google.firebase.firestore return () => listener.remove(); }; +firebase.firestore._getDocumentReference = (javaObj, collectionPath, documentPath): firestore.DocumentReference => { + return { + id: javaObj.getId(), + collection: cp => firebase.firestore.collection(`${collectionPath}/${documentPath}/${cp}`), + set: (data: any, options?: firestore.SetOptions) => firebase.firestore.set(collectionPath, javaObj.getId(), data, options), + get: () => firebase.firestore.getDocument(collectionPath, javaObj.getId()), + update: (data: any) => firebase.firestore.update(collectionPath, javaObj.getId(), data), + delete: () => firebase.firestore.delete(collectionPath, javaObj.getId()), + onSnapshot: (callback: (doc: DocumentSnapshot) => void) => firebase.firestore.onDocumentSnapshot(javaObj, callback), + android: javaObj + }; +}; + firebase.firestore.doc = (collectionPath: string, documentPath?: string): firestore.DocumentReference => { try { - if (typeof(com.google.firebase.firestore) === "undefined") { console.log("Make sure firebase-firestore is in the plugin's include.gradle"); return null; @@ -2317,17 +2349,7 @@ firebase.firestore.doc = (collectionPath: string, documentPath?: string): firest const db = com.google.firebase.firestore.FirebaseFirestore.getInstance(); const colRef: com.google.firebase.firestore.CollectionReference = db.collection(collectionPath); const docRef: com.google.firebase.firestore.DocumentReference = documentPath ? colRef.document(documentPath) : colRef.document(); - - return { - id: docRef.getId(), - collection: cp => firebase.firestore.collection(`${collectionPath}/${documentPath}/${cp}`), - set: (data: any, options?: firestore.SetOptions) => firebase.firestore.set(collectionPath, docRef.getId(), data, options), - get: () => firebase.firestore.getDocument(collectionPath, docRef.getId()), - update: (data: any) => firebase.firestore.update(collectionPath, docRef.getId(), data), - delete: () => firebase.firestore.delete(collectionPath, docRef.getId()), - onSnapshot: (callback: (doc: DocumentSnapshot) => void) => firebase.firestore.onDocumentSnapshot(docRef, callback) - }; - + return firebase.firestore._getDocumentReference(docRef, collectionPath, documentPath); } catch (ex) { console.log("Error in firebase.firestore.doc: " + ex); return null; diff --git a/src/firebase.d.ts b/src/firebase.d.ts index baa36f3e..ccde5fe9 100644 --- a/src/firebase.d.ts +++ b/src/firebase.d.ts @@ -808,6 +808,8 @@ export namespace firestore { update: (document: any) => Promise; delete: () => Promise; onSnapshot(callback: (doc: DocumentSnapshot) => void): () => void; + android?: any; + ios?: any; } export interface Query { diff --git a/src/firebase.ios.ts b/src/firebase.ios.ts index 55b738bf..7bbb4193 100755 --- a/src/firebase.ios.ts +++ b/src/firebase.ios.ts @@ -581,6 +581,17 @@ firebase.toJsObject = objCObj => { case 'Date': node[key] = new Date(val); break; + case 'FIRDocumentReference': + const path = (val).path; + const lastSlashIndex = path.lastIndexOf("/"); + node[key] = firebase.firestore._getDocumentReference(val, path.substring(0, lastSlashIndex), path.substring(lastSlashIndex + 1)); + break; + case 'FIRGeoPoint': + node[key] = { + latitude: (val).latitude, + longitude: (val).longitude + }; + break; default: console.log("Please report this at https://github.com/EddyVerbruggen/nativescript-plugin-firebase/issues: iOS toJsObject is missing a converter for class '" + types.getClass(val) + "'. Casting to String as a fallback."); node[key] = String(val); @@ -2285,6 +2296,19 @@ firebase.firestore.onCollectionSnapshot = (colRef: FIRCollectionReference, callb } }; +firebase.firestore._getDocumentReference = (fIRDocumentReference, collectionPath, documentPath): firestore.DocumentReference => { + return { + id: fIRDocumentReference.documentID, + collection: cp => firebase.firestore.collection(`${collectionPath}/${documentPath}/${cp}`), + set: (data: any, options?: firestore.SetOptions) => firebase.firestore.set(collectionPath, fIRDocumentReference.documentID, data, options), + get: () => firebase.firestore.getDocument(collectionPath, fIRDocumentReference.documentID), + update: (data: any) => firebase.firestore.update(collectionPath, fIRDocumentReference.documentID, data), + delete: () => firebase.firestore.delete(collectionPath, fIRDocumentReference.documentID), + onSnapshot: (callback: (doc: DocumentSnapshot) => void) => firebase.firestore.onDocumentSnapshot(fIRDocumentReference, callback), + ios: fIRDocumentReference + }; +}; + firebase.firestore.doc = (collectionPath: string, documentPath?: string): firestore.DocumentReference => { try { if (typeof(FIRFirestore) === "undefined") { @@ -2294,17 +2318,7 @@ firebase.firestore.doc = (collectionPath: string, documentPath?: string): firest const fIRCollectionReference = FIRFirestore.firestore().collectionWithPath(collectionPath); const fIRDocumentReference = documentPath ? fIRCollectionReference.documentWithPath(documentPath) : fIRCollectionReference.documentWithAutoID(); - - return { - id: fIRDocumentReference.documentID, - collection: cp => firebase.firestore.collection(`${collectionPath}/${documentPath}/${cp}`), - set: (data: any, options?: firestore.SetOptions) => firebase.firestore.set(collectionPath, fIRDocumentReference.documentID, data, options), - get: () => firebase.firestore.getDocument(collectionPath, fIRDocumentReference.documentID), - update: (data: any) => firebase.firestore.update(collectionPath, fIRDocumentReference.documentID, data), - delete: () => firebase.firestore.delete(collectionPath, fIRDocumentReference.documentID), - onSnapshot: (callback: (doc: DocumentSnapshot) => void) => firebase.firestore.onDocumentSnapshot(fIRDocumentReference, callback) - }; - + return firebase.firestore._getDocumentReference(fIRDocumentReference, collectionPath, documentPath); } catch (ex) { console.log("Error in firebase.firestore.doc: " + ex); return null;