Skip to content

Commit

Permalink
Make date prefix configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
ReallySmallSoftware committed Dec 3, 2017
1 parent af39d45 commit 372cb25
Show file tree
Hide file tree
Showing 8 changed files with 265 additions and 224 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1 +1,2 @@
.git.safe
/node_modules
47 changes: 47 additions & 0 deletions Gruntfile.js
@@ -0,0 +1,47 @@
module.exports = function(grunt) {

// Project configuration.
grunt.initConfig({
jshint: {
options: {
curly: true,
eqeqeq: true,
immed: true,
latedef: true,
newcap: true,
noarg: true,
sub: true,
undef: true,
boss: true,
eqnull: true,
node: true,
es5: true,
globals: {
jasmine: false,
describe: false,
beforeEach: false,
afterEach: false,
expect: false,
it: false,
spyOn: false,
$: false,
cordova: false,
launchnavigator: false,
window: false,
document: false,
ons: false,
navigator: false,
google: false,
FCMPlugin: false,
device: false,
plugins: false,
addFixture: false,
truncateSql: false
}
},
all: ['Gruntfile.js', 'www/**/*.js']
}
});

grunt.loadNpmTasks('grunt-contrib-jshint');
};
6 changes: 5 additions & 1 deletion package.json
Expand Up @@ -8,5 +8,9 @@
"ios"
]
},
"description": "A Google Firebase Firestore plugin"
"description": "A Google Firebase Firestore plugin",
"devDependencies": {
"grunt": "^1.0.1",
"grunt-contrib-jshint": "^1.1.0"
}
}
Expand Up @@ -8,10 +8,13 @@
import org.apache.cordova.CallbackContext;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class InitialiseHandler implements ActionHandler {

public static final String PERSIST = "persist";
public static final String DATE_PREFIX = "datePrefix";
private FirestorePlugin firestorePlugin;

public InitialiseHandler(FirestorePlugin firestorePlugin) {
Expand All @@ -25,11 +28,23 @@ public boolean handle(final JSONArray args, CallbackContext callbackContext) {
if (firestorePlugin.getDatabase() == null) {
Log.d(FirestorePlugin.TAG, "Initialising Firestore...");

final boolean persist = args.getBoolean(0);
final JSONObject options = args.getJSONObject(0);

FirebaseFirestore.setLoggingEnabled(true);
firestorePlugin.setDatabase(FirebaseFirestore.getInstance());

boolean persist = false;

if (options.has(PERSIST) && options.getBoolean(PERSIST)) {
persist = true;
}

if (options.has(DATE_PREFIX)) {
JSONDateWrapper.setDatePrefix(options.getString(DATE_PREFIX));
}

Log.d(FirestorePlugin.TAG, "Setting Firestore persistance to " + persist);

FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
.setPersistenceEnabled(persist)
.build();
Expand Down
Expand Up @@ -3,12 +3,19 @@
import java.util.Date;

public class JSONDateWrapper extends Date {

private static String datePrefix = "__DATE:";

public JSONDateWrapper(Date date) {
super(date.getTime());
}

public static void setDatePrefix(String datePrefix) {
JSONDateWrapper.datePrefix = datePrefix;
}

@Override
public String toString() {
return "__DATE(" + this.getTime() + ")";
return this.datePrefix + this.getTime();
}
}
Expand Up @@ -32,48 +32,6 @@ static JSONObject toJSON(Map<String, Object> values) throws JSONException {
return result;
}

// static Map<String, Object> jsonToMap(JSONObject json) throws JSONException {
// Map<String, Object> retMap = new HashMap<String, Object>();
//
// if (json != JSONObject.NULL) {
// retMap = toMap(json);
// }
// return retMap;
// }

// static Map<String, Object> toMap(JSONObject object) throws JSONException {
// Map<String, Object> map = new HashMap<String, Object>();
//
// Iterator<String> keysItr = object.keys();
// while (keysItr.hasNext()) {
// String key = keysItr.next();
// Object value = object.get(key);
//
// if (value instanceof JSONArray) {
// value = toList((JSONArray) value);
// } else if (value instanceof JSONObject) {
// value = toMap((JSONObject) value);
// }
// map.put(key, value);
// }
// return map;
// }
//
//
// static List<Object> toList(JSONArray array) throws JSONException {
// List<Object> list = new ArrayList<Object>();
// for (int i = 0; i < array.length(); i++) {
// Object value = array.get(i);
// if (value instanceof JSONArray) {
// value = toList((JSONArray) value);
// } else if (value instanceof JSONObject) {
// value = toMap((JSONObject) value);
// }
// list.add(value);
// }
// return list;
// }

static Object toSettable(Object value) {

Object result = value;
Expand Down
22 changes: 14 additions & 8 deletions www/browser/firestore.js
@@ -1,3 +1,5 @@
/* global firebase: false, Promise: false */

var PLUGIN_NAME = 'Firestore';

var loadJS = function(url, implementationCode, location) {
Expand All @@ -10,24 +12,26 @@ var loadJS = function(url, implementationCode, location) {
location.appendChild(scriptTag);
};

function Firestore(persist, firebaseOptions) {
function Firestore(options, resolve) {

var self = this;

var initialise = function() {

firebase.initializeApp(firebaseOptions);
firebase.initializeApp(options.browser);

if (persist) {
if (options.persist) {
firebase.firestore().enablePersistence().then(function() {
self.database = firebase.firestore();
resolve(self);
});
} else {
self.database = firebase.firestore();
resolve(self);
}
}
loadJS('https://www.gstatic.com/firebasejs/4.5.0/firebase.js', function() {
loadJS('https://www.gstatic.com/firebasejs/4.5.0/firebase-firestore.js', initialise, document.body);
};
loadJS('https://www.gstatic.com/firebasejs/4.7.0/firebase.js', function() {
loadJS('https://www.gstatic.com/firebasejs/4.7.0/firebase-firestore.js', initialise, document.body);
}, document.body);

}
Expand All @@ -37,7 +41,9 @@ Firestore.prototype.get = function() {
};

module.exports = {
initialise: function(persist, firebaseOptions) {
return new Firestore(persist, firebaseOptions);
initialise: function(options) {
return new Promise(function(resolve, reject) {
var db = new Firestore(options, resolve);
});
}
};

0 comments on commit 372cb25

Please sign in to comment.