@@ -20,7 +20,7 @@ let fbCallbackManager = null;
2020const GOOGLE_SIGNIN_INTENT_ID = 123 ;
2121const REQUEST_INVITE_INTENT_ID = 48 ;
2222
23- const gson = lazy ( ( ) => typeof ( com . google . gson ) === "undefined" ? null : new com . google . gson . Gson ( ) ) ;
23+ // const gson = lazy(() => typeof(com.google.gson) === "undefined" ? null : new com.google.gson.Gson());
2424const messagingEnabled = lazy ( ( ) => typeof ( com . google . firebase . messaging ) !== "undefined" ) ;
2525const dynamicLinksEnabled = lazy ( ( ) => typeof ( com . google . android . gms . appinvite ) !== "undefined" ) ;
2626
@@ -167,13 +167,18 @@ firebase.toValue = val => {
167167 return returnVal ;
168168} ;
169169
170+ // no longer using Gson as fi Firestore's DocumentReference isn't serialized
170171firebase . toJsObject = javaObj => {
171- if ( gson ( ) !== null ) {
172- return JSON . parse ( gson ( ) . toJson ( javaObj ) ) ;
173- } else {
174- // temp fallback for folks not having fetched gson yet in their build for some reason
175- return firebase . toJsObjectLegacy ( javaObj ) ;
176- }
172+ // if (gson() !== null) {
173+ // try {
174+ // return JSON.parse(gson().toJson(javaObj)); // this may fail if fi a DocumentReference is encountered
175+ // } catch (ignore) {
176+ // return firebase.toJsObjectLegacy(javaObj);
177+ // }
178+ // } else {
179+ // fallback for folks not having fetched gson yet in their build for some reason
180+ return firebase . toJsObjectLegacy ( javaObj ) ;
181+ // }
177182} ;
178183
179184firebase . toJsObjectLegacy = javaObj => {
@@ -191,18 +196,33 @@ firebase.toJsObjectLegacy = javaObj => {
191196 case 'java.lang.Long' :
192197 case 'java.lang.Double' :
193198 return Number ( String ( javaObj ) ) ;
199+ case 'java.util.Date' :
200+ return new Date ( javaObj ) ;
201+ case 'com.google.firebase.firestore.GeoPoint' :
202+ return {
203+ "latitude" : javaObj . getLatitude ( ) ,
204+ "longitude" : javaObj . getLongitude ( )
205+ } ;
206+ case 'com.google.firebase.firestore.DocumentReference' :
207+ const path : string = javaObj . getPath ( ) ;
208+ const lastSlashIndex = path . lastIndexOf ( "/" ) ;
209+ return firebase . firestore . _getDocumentReference ( javaObj , path . substring ( 0 , lastSlashIndex ) , path . substring ( lastSlashIndex + 1 ) ) ;
194210 case 'java.util.ArrayList' :
195211 node = [ ] ;
196212 for ( let i = 0 ; i < javaObj . size ( ) ; i ++ ) {
197213 node [ i ] = firebase . toJsObjectLegacy ( javaObj . get ( i ) ) ;
198214 }
199215 break ;
200216 default :
201- node = { } ;
202- const iterator = javaObj . entrySet ( ) . iterator ( ) ;
203- while ( iterator . hasNext ( ) ) {
204- const item = iterator . next ( ) ;
205- node [ item . getKey ( ) ] = firebase . toJsObjectLegacy ( item . getValue ( ) ) ;
217+ try {
218+ node = { } ;
219+ const iterator = javaObj . entrySet ( ) . iterator ( ) ;
220+ while ( iterator . hasNext ( ) ) {
221+ const item = iterator . next ( ) ;
222+ node [ item . getKey ( ) ] = firebase . toJsObjectLegacy ( item . getValue ( ) ) ;
223+ }
224+ } catch ( e ) {
225+ console . log ( "PLEASE REPORT THIS AT https://github.com/NativeScript/NativeScript/issues: Tried to serialize an unsupported type: javaObj.getClass().getName(), error: " + e ) ;
206226 }
207227 }
208228 return node ;
@@ -2306,9 +2326,21 @@ firebase.firestore.onCollectionSnapshot = (colRef: com.google.firebase.firestore
23062326 return ( ) => listener . remove ( ) ;
23072327} ;
23082328
2329+ firebase . firestore . _getDocumentReference = ( javaObj , collectionPath , documentPath ) : firestore . DocumentReference => {
2330+ return {
2331+ id : javaObj . getId ( ) ,
2332+ collection : cp => firebase . firestore . collection ( `${ collectionPath } /${ documentPath } /${ cp } ` ) ,
2333+ set : ( data : any , options ?: firestore . SetOptions ) => firebase . firestore . set ( collectionPath , javaObj . getId ( ) , data , options ) ,
2334+ get : ( ) => firebase . firestore . getDocument ( collectionPath , javaObj . getId ( ) ) ,
2335+ update : ( data : any ) => firebase . firestore . update ( collectionPath , javaObj . getId ( ) , data ) ,
2336+ delete : ( ) => firebase . firestore . delete ( collectionPath , javaObj . getId ( ) ) ,
2337+ onSnapshot : ( callback : ( doc : DocumentSnapshot ) => void ) => firebase . firestore . onDocumentSnapshot ( javaObj , callback ) ,
2338+ android : javaObj
2339+ } ;
2340+ } ;
2341+
23092342firebase . firestore . doc = ( collectionPath : string , documentPath ?: string ) : firestore . DocumentReference => {
23102343 try {
2311-
23122344 if ( typeof ( com . google . firebase . firestore ) === "undefined" ) {
23132345 console . log ( "Make sure firebase-firestore is in the plugin's include.gradle" ) ;
23142346 return null ;
@@ -2317,17 +2349,7 @@ firebase.firestore.doc = (collectionPath: string, documentPath?: string): firest
23172349 const db = com . google . firebase . firestore . FirebaseFirestore . getInstance ( ) ;
23182350 const colRef : com . google . firebase . firestore . CollectionReference = db . collection ( collectionPath ) ;
23192351 const docRef : com . google . firebase . firestore . DocumentReference = documentPath ? colRef . document ( documentPath ) : colRef . document ( ) ;
2320-
2321- return {
2322- id : docRef . getId ( ) ,
2323- collection : cp => firebase . firestore . collection ( `${ collectionPath } /${ documentPath } /${ cp } ` ) ,
2324- set : ( data : any , options ?: firestore . SetOptions ) => firebase . firestore . set ( collectionPath , docRef . getId ( ) , data , options ) ,
2325- get : ( ) => firebase . firestore . getDocument ( collectionPath , docRef . getId ( ) ) ,
2326- update : ( data : any ) => firebase . firestore . update ( collectionPath , docRef . getId ( ) , data ) ,
2327- delete : ( ) => firebase . firestore . delete ( collectionPath , docRef . getId ( ) ) ,
2328- onSnapshot : ( callback : ( doc : DocumentSnapshot ) => void ) => firebase . firestore . onDocumentSnapshot ( docRef , callback )
2329- } ;
2330-
2352+ return firebase . firestore . _getDocumentReference ( docRef , collectionPath , documentPath ) ;
23312353 } catch ( ex ) {
23322354 console . log ( "Error in firebase.firestore.doc: " + ex ) ;
23332355 return null ;
0 commit comments