Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Commit

Permalink
Use Gson to convert javaObject to jsObject #324
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyverbruggen committed Mar 23, 2017
1 parent b9ff3d6 commit 6b32e68
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
- iOS: 3.13.x
- Android: 10.2.x

### New
- [#324](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/issues/324) Use Gson to convert javaObject to jsObject

### Fixes
- [#331](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/issues/331) CI builds may file if no entitlements file exists

Expand Down
55 changes: 46 additions & 9 deletions firebase.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ firebase._facebookAccessToken = null;
var fbCallbackManager = null;
var GOOGLE_SIGNIN_INTENT_ID = 123;

var gson = new com.google.gson.Gson();
var gson = typeof(com.google.gson) === "undefined" ? null : new com.google.gson.Gson();

(function() {
if (typeof(com.google.firebase.messaging) === "undefined") {
Expand Down Expand Up @@ -109,7 +109,44 @@ firebase.toValue = function(val){
};

firebase.toJsObject = function(javaObj) {
return JSON.parse(gson.toJson(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);
}
};

firebase.toJsObjectLegacy = function(javaObj) {
if (javaObj === null || typeof javaObj != "object") {
return javaObj;
}

var node;
switch (javaObj.getClass().getName()) {
case 'java.lang.Boolean':
var str = String(javaObj);
return Boolean(!!(str == "True" || str == "true"));
case 'java.lang.String':
return String(javaObj);
case 'java.lang.Long':
case 'java.lang.Double':
return Number(String(javaObj));
case 'java.util.ArrayList':
node = [];
for (var i = 0; i < javaObj.size(); i++) {
node[i] = firebase.toJsObjectLegacy(javaObj.get(i));
}
break;
default:
node = {};
var iterator = javaObj.entrySet().iterator();
while (iterator.hasNext()) {
var item = iterator.next();
node[item.getKey()] = firebase.toJsObjectLegacy(item.getValue());
}
}
return node;
};

firebase.getCallbackData = function(type, snapshot) {
Expand Down Expand Up @@ -717,8 +754,8 @@ firebase.getAuthToken = function (arg) {
});

user.getToken(arg.forceRefresh)
.addOnSuccessListener(onSuccessListener)
.addOnFailureListener(onFailureListener);
.addOnSuccessListener(onSuccessListener)
.addOnFailureListener(onFailureListener);

} else {
reject("Log in first");
Expand Down Expand Up @@ -776,7 +813,7 @@ firebase.login = function (arg) {
console.log("Logging in the user failed. " + (task.getException() && task.getException().getReason ? task.getException().getReason() : task.getException()));
// also disconnect from Google otherwise ppl can't connect with a different account
if (firebase._mGoogleApiClient) {
com.google.android.gms.auth.api.Auth.GoogleSignInApi.revokeAccess(firebase._mGoogleApiClient);
com.google.android.gms.auth.api.Auth.GoogleSignInApi.revokeAccess(firebase._mGoogleApiClient);
}
reject("Logging in the user failed. " + (task.getException() && task.getException().getReason ? task.getException().getReason() : task.getException()));
} else {
Expand Down Expand Up @@ -1278,7 +1315,7 @@ firebase.update = function (path, val) {
return new Promise(function (resolve, reject) {
try {
if (typeof val == "object") {
firebase.instance.child(path).updateChildren(firebase.toHashMap(val));
firebase.instance.child(path).updateChildren(firebase.toHashMap(val));
} else {
var lastPartOfPath = path.lastIndexOf("/");
var pathPrefix = path.substring(0, lastPartOfPath);
Expand Down Expand Up @@ -1684,7 +1721,7 @@ firebase.subscribeToTopic = function(topicName){
firebase.unsubscribeFromTopic = function(topicName){
return new Promise(function (resolve, reject) {
try {

if (typeof(com.google.firebase.messaging) === "undefined") {
reject("Uncomment firebase-messaging in the plugin's include.gradle first");
return;
Expand All @@ -1694,14 +1731,14 @@ firebase.unsubscribeFromTopic = function(topicName){
reject("Can be run only after init");
return;
}

com.google.firebase.messaging.FirebaseMessaging.getInstance().unsubscribeFromTopic(topicName);
resolve();
} catch(ex){
console.log("Error in firebase.unsubscribeFromTopic: " + ex);
reject(ex);
}
});
});
};

firebase.sendCrashLog = function (arg) {
Expand Down
3 changes: 3 additions & 0 deletions scripts/postinstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -3067,6 +3067,9 @@ dependencies {
compile "com.google.firebase:firebase-database:10.2.+"
compile "com.google.firebase:firebase-auth:10.2.+"
// for converting Java objects to JS
compile "com.google.code.gson:gson:2.8.+"
// for reading google-services.json and configuration
def googlePlayServicesVersion = project.hasProperty('googlePlayServicesVersion') ? project.googlePlayServicesVersion : '10.2.+'
compile "com.google.android.gms:play-services-base:$googlePlayServicesVersion"
Expand Down

0 comments on commit 6b32e68

Please sign in to comment.