Skip to content

Commit

Permalink
suites support for Android
Browse files Browse the repository at this point in the history
  • Loading branch information
apla committed Jul 10, 2016
1 parent 6006181 commit e56013a
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 26 deletions.
40 changes: 22 additions & 18 deletions src/android/AppPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.json.JSONStringer;
import org.json.JSONTokener;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
Expand Down Expand Up @@ -71,17 +72,27 @@ public void onPause(boolean multitasking) {
.unregisterOnSharedPreferenceChangeListener(this);
}

// TODO: use custom shared preference like in ios suite
// SharedPreferences sharedPrefs = cordova.getActivity().getSharedPreferences(PREF_UNIQUE_ID, Context.MODE_PRIVATE);

@Override
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
// String result = "";

JSONObject options = new JSONObject();
if (args.length() > 0)
options = args.optJSONObject (0);

String suiteName = options.optString("suiteName");

SharedPreferences sharedPrefs;
if (suiteName != null) {
sharedPrefs = cordova.getActivity().getSharedPreferences(suiteName, Context.MODE_PRIVATE);
} else {
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(cordova.getActivity());
}

if (action.equals ("show")) {
return this.showPreferencesActivity(callbackContext);
} else if (action.equals("clearAll")) {
return this.clearAll(callbackContext);
return this.clearAll(sharedPrefs, callbackContext);
} else if (action.equals("watch")) {
watchChanges = options.optBoolean("subscribe", true);

Expand All @@ -95,7 +106,6 @@ public boolean execute(String action, JSONArray args, final CallbackContext call
return true;
}

JSONObject options = args.getJSONObject (0);
String key = options.getString("key");
String dict = options.optString("dict");
String type = options.optString("type");
Expand All @@ -105,20 +115,19 @@ public boolean execute(String action, JSONArray args, final CallbackContext call


if (action.equals("fetch")) {
return this.fetchValueByKey(key, callbackContext);
return this.fetchValueByKey(sharedPrefs, key, callbackContext);
} else if (action.equals("store")) {
String value = options.getString("value");
return this.storeValueByKey(key, type, value, callbackContext);
return this.storeValueByKey(sharedPrefs, key, type, value, callbackContext);
} else if (action.equals("remove")) {
return this.removeValueByKey(key, callbackContext);
return this.removeValueByKey(sharedPrefs, key, callbackContext);
}
// callbackContext.sendPluginResult(new PluginResult (PluginResult.Status.JSON_EXCEPTION));
return false;
}

private boolean clearAll (final CallbackContext callbackContext) {
private boolean clearAll (final SharedPreferences sharedPrefs, final CallbackContext callbackContext) {
cordova.getThreadPool().execute(new Runnable() {public void run() {
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(cordova.getActivity());

Editor editor = sharedPrefs.edit();
editor.clear();
Expand Down Expand Up @@ -157,10 +166,9 @@ private boolean showPreferencesActivity (final CallbackContext callbackContext)
return true;
}

private boolean fetchValueByKey(final String key, final CallbackContext callbackContext) {
private boolean fetchValueByKey(final SharedPreferences sharedPrefs, final String key, final CallbackContext callbackContext) {
cordova.getThreadPool().execute(new Runnable() {public void run() {

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(cordova.getActivity());
String returnVal = null;
if (sharedPrefs.contains(key)) {
Object obj = sharedPrefs.getAll().get(key);
Expand Down Expand Up @@ -205,11 +213,9 @@ private boolean fetchValueByKey(final String key, final CallbackContext callback
return true;
}

private boolean removeValueByKey(final String key, final CallbackContext callbackContext) {
private boolean removeValueByKey(final SharedPreferences sharedPrefs, final String key, final CallbackContext callbackContext) {
cordova.getThreadPool().execute(new Runnable() { public void run() {

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(cordova.getActivity());

if (sharedPrefs.contains(key)) {
Editor editor = sharedPrefs.edit();
editor.remove(key);
Expand All @@ -236,11 +242,9 @@ private boolean removeValueByKey(final String key, final CallbackContext callbac
return true;
}

private boolean storeValueByKey(final String key, final String type, final String value, final CallbackContext callbackContext) {
private boolean storeValueByKey(final SharedPreferences sharedPrefs, final String key, final String type, final String value, final CallbackContext callbackContext) {
cordova.getThreadPool().execute(new Runnable() {public void run() {

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(cordova.getActivity());

Editor editor = sharedPrefs.edit();
// editor.putString(key, value);

Expand Down
6 changes: 3 additions & 3 deletions src/ios/AppPreferences.m
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ - (NSDictionary*)validateOptions:(CDVInvokedUrlCommand*)command

- (id)getStoreForOptions:(NSDictionary*)options
{
NSString *suiteName = [options objectForKey:@"iosSuiteName"];
NSString *cloudSync = [options objectForKey:@"cloudSync"];
NSString *suiteName = [options objectForKey:@"suiteName"];
NSString *cloudSync = [options objectForKey:@"cloudSync"];

id dataStore = nil;

Expand Down Expand Up @@ -259,7 +259,7 @@ - (void)clearAll:(CDVInvokedUrlCommand*)command
return;

NSString *settingsDict = [options objectForKey:@"dict"];
NSString *suiteName = [options objectForKey:@"iosSuiteName"];
NSString *suiteName = [options objectForKey:@"suiteName"];
NSString *cloudSync = [options objectForKey:@"cloudSync"];

id dataStore = [self getStoreForOptions:options];
Expand Down
49 changes: 44 additions & 5 deletions www/apppreferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,19 @@ AppPreferences.prototype.clearAll = platform.clearAll || function (
var argCount = 0;
var promise = promiseCheck.apply (this, [argCount].concat ([].slice.call(arguments)));

var args = {};

for (var k in this.defaultArgs) {
args[k] = this.defaultArgs[k];
}

var nativeExec = function (resolve, reject) {

if (platform.nativeClearAll) {
return platform.nativeClearAll (resolve, reject, args);
}

return platform.nativeExec (resolve, reject, "AppPreferences", "clearAll", []);
return platform.nativeExec (resolve, reject, "AppPreferences", "clearAll", [args]);
}

if (promise) {
Expand Down Expand Up @@ -335,17 +341,50 @@ AppPreferences.prototype.watch = platform.watch || function (
};

/**
* Return iOS Suite configuration context
* Return named configuration context
* In iOS you'll get a suite configuration, on Android — named file
* Supports: Android, iOS
* @param {String} suiteName suite name
* @returns {AppPreferences} AppPreferences object, bound to that suite
*/

AppPreferences.prototype.iosSuite = function (suiteName) {
var appPrefsSuite = new AppPreferences ({iosSuiteName: suiteName});
AppPreferences.prototype.iosSuite =
AppPreferences.prototype.suite =
function (suiteName) {

return appPrefsSuite;
var appPrefs = new AppPreferences ({
iosSuiteName: suiteName, // deprecated, remove when ios code is ready
suiteName: suiteName,
});

return appPrefs;
}

/**
* Return cloud synchronized configuration context
* Currently supports Windows and iOS/macOS
* @returns {AppPreferences} AppPreferences object, bound to that suite
*/

AppPreferences.prototype.cloudSync = function () {
var appPrefs = new AppPreferences ({cloudSync: true});

return appPrefs;
}

/**
* Return default configuration context
* Currently supports Windows and iOS/macOS
* @returns {AppPreferences} AppPreferences object, bound to that suite
*/

AppPreferences.prototype.defaults = function () {
var appPrefs = new AppPreferences ();

return appPrefs;
}


// WIP: functions to bind selected preferences to the form

function setFormFields (formEl, fieldsData) {
Expand Down

1 comment on commit e56013a

@apla
Copy link
Owner Author

@apla apla commented on e56013a Jul 13, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#97

Please sign in to comment.