AndroidAPS/app/src/main/java/info/nightscout/androidaps/data/QuickWizard.java

93 lines
2.5 KiB
Java
Raw Normal View History

2017-12-26 00:27:34 +01:00
package info.nightscout.androidaps.data;
2016-10-20 23:50:31 +02:00
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2016-10-20 23:50:31 +02:00
import info.nightscout.androidaps.MainApp;
/**
* Created by mike on 12.10.2016.
*/
public class QuickWizard {
private static Logger log = LoggerFactory.getLogger(QuickWizard.class);
2016-10-20 23:50:31 +02:00
2017-12-26 00:27:34 +01:00
private JSONArray storage = new JSONArray();
2016-10-20 23:50:31 +02:00
public void setData(JSONArray newData) {
storage = newData;
}
public void save() {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
SharedPreferences.Editor editor = preferences.edit();
editor.putString("QuickWizard", storage.toString());
editor.apply();
}
public int size() {
return storage.length();
}
public QuickWizardEntry get(int position) {
try {
return new QuickWizardEntry((JSONObject) storage.get(position), position);
} catch (JSONException e) {
log.error("Unhandled exception", e);
2016-10-20 23:50:31 +02:00
}
return null;
}
public Boolean isActive() {
for (int i = 0; i < storage.length(); i++) {
try {
if (new QuickWizardEntry((JSONObject) storage.get(i), i).isActive()) return true;
} catch (JSONException e) {
log.error("Unhandled exception", e);
2016-10-20 23:50:31 +02:00
}
}
return false;
}
public QuickWizardEntry getActive() {
for (int i = 0; i < storage.length(); i++) {
QuickWizardEntry entry;
try {
entry = new QuickWizardEntry((JSONObject) storage.get(i), i);
} catch (JSONException e) {
continue;
}
if (entry.isActive()) return entry;
}
return null;
}
public QuickWizardEntry newEmptyItem() {
return new QuickWizardEntry();
}
public void addOrUpdate(QuickWizardEntry newItem) {
if (newItem.position == -1)
storage.put(newItem.storage);
else {
try {
storage.put(newItem.position, newItem.storage);
} catch (JSONException e) {
log.error("Unhandled exception", e);
2016-10-20 23:50:31 +02:00
}
}
save();
}
public void remove(int position) {
storage.remove(position);
save();
}
}