Added shared preferences

This commit is contained in:
Keith Holliday 2017-05-02 10:02:13 -06:00
parent c60724db89
commit 2ca3c4db1f

View file

@ -3,6 +3,7 @@ package com.habitrpg.android.habitica.helpers;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Environment;
import android.support.v7.preference.PreferenceManager;
import android.util.Log;
import org.json.JSONException;
@ -30,9 +31,11 @@ public class RemoteConfigManager {
private static RemoteConfigManager instance;
private Context context;
private static Boolean enableRepeatbles = false;
private String REMOTE_STRING_KEY = "remote-string";
private RemoteConfigManager(Context context) {
this.context = context;
loadFromPreferences();
new DownloadFileFromURL().execute("https://s3.amazonaws.com/habitica-assets/mobileApp/endpoint/config-ios.json");
}
@ -47,6 +50,22 @@ public class RemoteConfigManager {
return enableRepeatbles;
}
private void loadFromPreferences () {
String storedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
.getString(REMOTE_STRING_KEY, "");
if (storedPreferences.isEmpty()) {
return;
}
try {
JSONObject obj = new JSONObject(storedPreferences);
enableRepeatbles = obj.getBoolean("enableRepeatbles");
} catch (JSONException e) {
e.printStackTrace();
}
}
class DownloadFileFromURL extends AsyncTask<String, String, String> {
private String filename = "config.json";
@ -110,6 +129,9 @@ public class RemoteConfigManager {
e.printStackTrace();
}
PreferenceManager.getDefaultSharedPreferences(context)
.edit().putString(REMOTE_STRING_KEY, text.toString()).apply();
try {
JSONObject obj = new JSONObject(text.toString());
enableRepeatbles = obj.getBoolean("enableRepeatbles");