cache created objects in profilestore

This commit is contained in:
Milos Kozak 2017-06-16 09:06:35 +02:00
parent 49ef46936b
commit c78399ded0

View file

@ -1,6 +1,7 @@
package info.nightscout.androidaps.data; package info.nightscout.androidaps.data;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.util.ArrayMap;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
@ -21,6 +22,8 @@ public class ProfileStore {
private JSONObject json = null; private JSONObject json = null;
private String units = Constants.MGDL; private String units = Constants.MGDL;
ArrayMap<String, Profile> cachedObjects = new ArrayMap<>();
public ProfileStore(JSONObject json) { public ProfileStore(JSONObject json) {
this.json = json; this.json = json;
getDefaultProfile(); // initialize units getDefaultProfile(); // initialize units
@ -37,10 +40,14 @@ public class ProfileStore {
String defaultProfileName = json.getString("defaultProfile"); String defaultProfileName = json.getString("defaultProfile");
JSONObject store = json.getJSONObject("store"); JSONObject store = json.getJSONObject("store");
if (store.has(defaultProfileName)) { if (store.has(defaultProfileName)) {
if (store.has("units")) profile = cachedObjects.get(defaultProfileName);
units = store.getString("units"); if (profile == null) {
profile = new Profile(store.getJSONObject(defaultProfileName), units); if (store.has("units"))
units = profile.getUnits(); units = store.getString("units");
profile = new Profile(store.getJSONObject(defaultProfileName), units);
units = profile.getUnits();
cachedObjects.put(defaultProfileName, profile);
}
} }
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
@ -73,10 +80,14 @@ public class ProfileStore {
try { try {
JSONObject store = json.getJSONObject("store"); JSONObject store = json.getJSONObject("store");
if (store.has(profileName)) { if (store.has(profileName)) {
String units = null; profile = cachedObjects.get(profileName);
if (json.has("units")) if (profile == null) {
units = json.getString("units"); if (store.has("units"))
profile = new Profile(store.getJSONObject(profileName), units); units = store.getString("units");
profile = new Profile(store.getJSONObject(profileName), units);
units = profile.getUnits();
cachedObjects.put(profileName, profile);
}
} }
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();