diff --git a/app/src/main/java/info/nightscout/androidaps/data/ProfileStore.java b/app/src/main/java/info/nightscout/androidaps/data/ProfileStore.java deleted file mode 100644 index c7113d1c0f..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/data/ProfileStore.java +++ /dev/null @@ -1,108 +0,0 @@ -package info.nightscout.androidaps.data; - -import androidx.annotation.Nullable; -import androidx.collection.ArrayMap; - -import org.json.JSONException; -import org.json.JSONObject; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.ArrayList; -import java.util.Iterator; - -/** - * Created by mike on 01.06.2017. - */ - -public class ProfileStore { - private static Logger log = LoggerFactory.getLogger(ProfileStore.class); - private JSONObject json; - - private ArrayMap cachedObjects = new ArrayMap<>(); - - public ProfileStore(JSONObject json) { - this.json = json; - getDefaultProfile(); - } - - public JSONObject getData() { - return json; - } - - @Nullable - public Profile getDefaultProfile() { - Profile profile = null; - try { - String defaultProfileName = json.getString("defaultProfile"); - JSONObject store = json.getJSONObject("store"); - if (store.has(defaultProfileName)) { - profile = cachedObjects.get(defaultProfileName); - if (profile == null) { - if (store.has("units")) { - String units = store.getString("units"); - profile = new Profile(store.getJSONObject(defaultProfileName), units); - cachedObjects.put(defaultProfileName, profile); - } - } - } - } catch (JSONException e) { - log.error("Unhandled exception", e); - } - return profile; - } - - @Nullable - public String getDefaultProfileName() { - String defaultProfileName = null; - try { - defaultProfileName = json.getString("defaultProfile"); - JSONObject store = json.getJSONObject("store"); - if (store.has(defaultProfileName)) { - return defaultProfileName; - } - } catch (JSONException e) { - log.error("Unhandled exception", e); - } - return defaultProfileName; - } - - @Nullable - public Profile getSpecificProfile(String profileName) { - Profile profile = null; - try { - JSONObject store = json.getJSONObject("store"); - if (store.has(profileName)) { - profile = cachedObjects.get(profileName); - if (profile == null) { - JSONObject profileObject = store.getJSONObject(profileName); - if (profileObject!= null && profileObject.has("units")) { - profile = new Profile(profileObject, profileObject.getString("units")); - cachedObjects.put(profileName, profile); - } - } - } - } catch (JSONException e) { - log.error("Unhandled exception", e); - } - return profile; - } - - public ArrayList getProfileList() { - ArrayList ret = new ArrayList<>(); - - JSONObject store; - try { - store = json.getJSONObject("store"); - Iterator keys = store.keys(); - - while (keys.hasNext()) { - String profileName = (String) keys.next(); - ret.add(profileName); - } - } catch (JSONException e) { - log.error("Unhandled exception", e); - } - return ret; - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/data/ProfileStore.kt b/app/src/main/java/info/nightscout/androidaps/data/ProfileStore.kt new file mode 100644 index 0000000000..0fa0bf277f --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/data/ProfileStore.kt @@ -0,0 +1,61 @@ +package info.nightscout.androidaps.data + +import androidx.collection.ArrayMap +import org.json.JSONException +import org.json.JSONObject +import org.slf4j.LoggerFactory +import java.util.* + +class ProfileStore(val data: JSONObject) { + private val log = LoggerFactory.getLogger(ProfileStore::class.java) + + private val cachedObjects = ArrayMap() + + private fun getStore(): JSONObject? { + try { + if (data.has("store")) return data.getJSONObject("store") + } catch (e: JSONException) { + log.error("Unhandled exception", e) + } + return null + } + + fun getDefaultProfile(): Profile? = getDefaultProfileName()?.let { getSpecificProfile(it) } + + fun getDefaultProfileName(): String? { + val defaultProfileName = data.getString("defaultProfile") + return getStore()?.has(defaultProfileName)?.let { defaultProfileName } + } + + fun getProfileList(): ArrayList { + val ret = ArrayList() + getStore()?.keys()?.let { keys -> + while (keys.hasNext()) { + val profileName = keys.next() as String + ret.add(profileName) + } + } + return ret + } + + fun getSpecificProfile(profileName: String): Profile? { + var profile: Profile? = null + try { + getStore()?.let { store -> + if (store.has(profileName)) { + profile = cachedObjects[profileName] + if (profile == null) { + val profileObject = store.getJSONObject(profileName) + if (profileObject != null && profileObject.has("units")) { + profile = Profile(profileObject, profileObject.getString("units")) + cachedObjects[profileName] = profile + } + } + } + } + } catch (e: JSONException) { + log.error("Unhandled exception", e) + } + return profile + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/WizardDialog.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/WizardDialog.kt index d0db643787..46462b0dfc 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/WizardDialog.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/WizardDialog.kt @@ -214,7 +214,7 @@ class WizardDialog : DialogFragment() { } val profileList: ArrayList - profileList = profileStore.profileList + profileList = profileStore.getProfileList() profileList.add(0, MainApp.gs(R.string.active)) context?.let { context -> val adapter = ArrayAdapter(context, R.layout.spinner_centered, profileList) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt index 1b49e92863..7e1e971a24 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt @@ -47,7 +47,7 @@ class LocalProfileFragment : Fragment() { } private fun sumLabel(): String { - val profile = LocalProfilePlugin.createProfileStore().defaultProfile + val profile = LocalProfilePlugin.createProfileStore().getDefaultProfile() val sum = profile?.baseBasalSum() ?: 0.0 return " ∑" + DecimalFormatter.to2Decimal(sum) + MainApp.gs(R.string.insulin_unit_shortname) } @@ -80,7 +80,7 @@ class LocalProfileFragment : Fragment() { // Spinner spinner = SpinnerHelper(view?.findViewById(R.id.localprofile_spinner)) - val profileList: ArrayList = LocalProfilePlugin.profile?.profileList + val profileList: ArrayList = LocalProfilePlugin.profile?.getProfileList() ?: ArrayList() context?.let { context -> val adapter = ArrayAdapter(context, R.layout.spinner_centered, profileList) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfilePlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfilePlugin.kt index 79b474fffd..49da7b6f8f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfilePlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfilePlugin.kt @@ -63,7 +63,7 @@ object LocalProfilePlugin : PluginBase(PluginDescription() @Synchronized fun isValidEditState(): Boolean { - return createProfileStore().defaultProfile?.isValid(MainApp.gs(R.string.localprofile), false) + return createProfileStore().getDefaultProfile()?.isValid(MainApp.gs(R.string.localprofile), false) ?: false } @@ -107,6 +107,7 @@ object LocalProfilePlugin : PluginBase(PluginDescription() val LOCAL_PROFILE_NUMBERED = LOCAL_PROFILE + "_" + i + "_" p.name = SP.getString(LOCAL_PROFILE_NUMBERED + "name", LOCAL_PROFILE + i) + if (isExistingName(p.name)) continue p.mgdl = SP.getBoolean(LOCAL_PROFILE_NUMBERED + "mgdl", false) p.dia = SP.getDouble(LOCAL_PROFILE_NUMBERED + "dia", Constants.defaultDIA) try { @@ -162,9 +163,17 @@ object LocalProfilePlugin : PluginBase(PluginDescription() profiles.add(p) } isEdited = false + numOfProfiles = profiles.size createAndStoreConvertedProfile() } + private fun isExistingName(name: String?) : Boolean { + for (p in profiles) { + if (p.name == name) return true + } + return false + } + @Synchronized private fun doConversion() { // conversion from 2.3 to 2.4 format if (L.isEnabled(L.PROFILE)) @@ -347,7 +356,7 @@ object LocalProfilePlugin : PluginBase(PluginDescription() } override fun getProfileName(): String { - return DecimalFormatter.to2Decimal(rawProfile?.defaultProfile?.percentageBasalSum() + return DecimalFormatter.to2Decimal(rawProfile?.getDefaultProfile()?.percentageBasalSum() ?: 0.0) + "U " } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/profile/ns/NSProfileFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/profile/ns/NSProfileFragment.kt index 7a76283074..7b377c9078 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/profile/ns/NSProfileFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/profile/ns/NSProfileFragment.kt @@ -119,7 +119,7 @@ class NSProfileFragment : Fragment() { profileview_noprofile.visibility = View.VISIBLE NSProfilePlugin.getPlugin().profile?.let { profileStore -> - val profileList = profileStore.profileList + val profileList = profileStore.getProfileList() val adapter = ArrayAdapter(context!!, R.layout.spinner_centered, profileList) nsprofile_spinner.adapter = adapter // set selected to actual profile diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/ProfileViewerDialog.kt b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/ProfileViewerDialog.kt index a27ebfb656..be9adece61 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/ProfileViewerDialog.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/ProfileViewerDialog.kt @@ -58,7 +58,7 @@ class ProfileViewerDialog : DialogFragment() { profileview_datelayout.visibility = View.VISIBLE } Mode.PUMP_PROFILE -> { - profile = (ConfigBuilderPlugin.getPlugin().activePump as ProfileInterface?)?.profile?.defaultProfile + profile = (ConfigBuilderPlugin.getPlugin().activePump as ProfileInterface?)?.profile?.getDefaultProfile() profileName = (ConfigBuilderPlugin.getPlugin().activePump as ProfileInterface?)?.profileName date = "" profileview_reload.visibility = View.VISIBLE