From c78399ded08319c1dff39a036b0492fabf501d89 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Fri, 16 Jun 2017 09:06:35 +0200 Subject: [PATCH] cache created objects in profilestore --- .../androidaps/data/ProfileStore.java | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/data/ProfileStore.java b/app/src/main/java/info/nightscout/androidaps/data/ProfileStore.java index bcc778485b..3e2217454f 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/ProfileStore.java +++ b/app/src/main/java/info/nightscout/androidaps/data/ProfileStore.java @@ -1,6 +1,7 @@ package info.nightscout.androidaps.data; import android.support.annotation.Nullable; +import android.support.v4.util.ArrayMap; import org.json.JSONException; import org.json.JSONObject; @@ -21,6 +22,8 @@ public class ProfileStore { private JSONObject json = null; private String units = Constants.MGDL; + ArrayMap cachedObjects = new ArrayMap<>(); + public ProfileStore(JSONObject json) { this.json = json; getDefaultProfile(); // initialize units @@ -37,10 +40,14 @@ public class ProfileStore { String defaultProfileName = json.getString("defaultProfile"); JSONObject store = json.getJSONObject("store"); if (store.has(defaultProfileName)) { - if (store.has("units")) - units = store.getString("units"); - profile = new Profile(store.getJSONObject(defaultProfileName), units); - units = profile.getUnits(); + profile = cachedObjects.get(defaultProfileName); + if (profile == null) { + if (store.has("units")) + units = store.getString("units"); + profile = new Profile(store.getJSONObject(defaultProfileName), units); + units = profile.getUnits(); + cachedObjects.put(defaultProfileName, profile); + } } } catch (JSONException e) { e.printStackTrace(); @@ -73,10 +80,14 @@ public class ProfileStore { try { JSONObject store = json.getJSONObject("store"); if (store.has(profileName)) { - String units = null; - if (json.has("units")) - units = json.getString("units"); - profile = new Profile(store.getJSONObject(profileName), units); + profile = cachedObjects.get(profileName); + if (profile == null) { + if (store.has("units")) + units = store.getString("units"); + profile = new Profile(store.getJSONObject(profileName), units); + units = profile.getUnits(); + cachedObjects.put(profileName, profile); + } } } catch (JSONException e) { e.printStackTrace();