diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/autotune/AutotuneFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/autotune/AutotuneFragment.kt index a3c6fa80c5..2b467cb500 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/autotune/AutotuneFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/autotune/AutotuneFragment.kt @@ -1,5 +1,6 @@ package info.nightscout.androidaps.plugins.general.autotune +import android.graphics.Paint import android.graphics.Typeface import android.os.Bundle import android.text.Editable @@ -80,7 +81,7 @@ class AutotuneFragment : DaggerFragment() { super.onViewCreated(view, savedInstanceState) sp.putBoolean(R.string.key_autotune_tune_insulin_curve, false) // put to false tune insulin curve sp.putBoolean(R.string.key_autotune_additional_log, false) // put to false additional log - autotunePlugin.lastRun = sp.getLong(R.string.key_autotune_last_run, 0) + autotunePlugin.loadLastRun() if (autotunePlugin.lastNbDays.isEmpty()) autotunePlugin.lastNbDays = sp.getInt(R.string.key_autotune_default_tune_days, 5).toString() val defaultValue = sp.getInt(R.string.key_autotune_default_tune_days, 5).toDouble() @@ -145,6 +146,7 @@ class AutotuneFragment : DaggerFragment() { autotunePlugin.tunedProfile?.profilename = localName autotunePlugin.updateProfile(autotunePlugin.tunedProfile) autotunePlugin.updateButtonVisibility = View.GONE + autotunePlugin.saveLastRun() uel.log( UserEntry.Action.STORE_PROFILE, UserEntry.Sources.Autotune, @@ -164,6 +166,7 @@ class AutotuneFragment : DaggerFragment() { autotunePlugin.tunedProfile?.profilename = "" autotunePlugin.updateProfile(autotunePlugin.pumpProfile) autotunePlugin.updateButtonVisibility = View.VISIBLE + autotunePlugin.saveLastRun() uel.log( UserEntry.Action.STORE_PROFILE, UserEntry.Sources.Autotune, @@ -252,6 +255,14 @@ class AutotuneFragment : DaggerFragment() { } } } + + binding.tuneLastrun.setOnClickListener { + if (!autotunePlugin.calculationRunning) { + autotunePlugin.loadLastRun() + updateGui() + } + } + binding.tuneLastrun.paintFlags = binding.tuneLastrun.paintFlags or Paint.UNDERLINE_TEXT_FLAG } @Synchronized @@ -264,7 +275,6 @@ class AutotuneFragment : DaggerFragment() { updateGui() }, { fabricPrivacy.logException(it) }) checkNewDay() - binding.tuneDays.value = autotunePlugin.lastNbDays.toDouble() updateGui() } @@ -277,6 +287,7 @@ class AutotuneFragment : DaggerFragment() { @Synchronized private fun updateGui() { _binding ?: return + binding.tuneDays.value = autotunePlugin.lastNbDays.toDouble() profileStore = activePlugin.activeProfileSource.profile ?: ProfileStore(injector, JSONObject(), dateUtil) profileName = if (binding.profileList.text.toString() == rh.gs(R.string.active)) "" else binding.profileList.text.toString() profileFunction.getProfile()?.let { currentProfile -> diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/autotune/AutotunePlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/autotune/AutotunePlugin.kt index 0bc1d3fb05..b0ec81d2d5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/autotune/AutotunePlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/autotune/AutotunePlugin.kt @@ -7,6 +7,7 @@ import info.nightscout.androidaps.data.LocalInsulin import info.nightscout.androidaps.data.ProfileSealed import info.nightscout.androidaps.database.entities.UserEntry import info.nightscout.androidaps.database.entities.ValueWithUnit +import info.nightscout.androidaps.extensions.pureProfileFromJson import info.nightscout.androidaps.interfaces.* import info.nightscout.androidaps.logging.UserEntryLogger import info.nightscout.androidaps.plugins.bus.RxBus @@ -16,6 +17,7 @@ import info.nightscout.androidaps.plugins.general.autotune.events.EventAutotuneU import info.nightscout.androidaps.plugins.profile.local.LocalProfilePlugin import info.nightscout.androidaps.plugins.profile.local.events.EventLocalProfileChanged import info.nightscout.androidaps.utils.DateUtil +import info.nightscout.androidaps.utils.JsonHelper import info.nightscout.androidaps.utils.MidnightTime import info.nightscout.androidaps.utils.T import info.nightscout.androidaps.utils.buildHelper.BuildHelper @@ -192,12 +194,12 @@ class AutotunePlugin @Inject constructor( tunedProfile?.let { lastRunSuccess = true - sp.putLong(R.string.key_autotune_last_run, lastRun) + saveLastRun() rxBus.send(EventAutotuneUpdateGui()) calculationRunning = false return result } - return "No Result" // should never occurs + return rh.gs(R.string.autotune_error) } private fun showResults(tunedProfile: ATProfile?, pumpProfile: ATProfile): String { @@ -300,6 +302,56 @@ class AutotunePlugin @Inject constructor( localProfilePlugin.storeSettings() } + fun saveLastRun() { + val json = JSONObject() + json.put("lastNbDays", lastNbDays) + json.put("lastRun",lastRun) + json.put("pumpProfile", pumpProfile.profile.toPureNsJson(dateUtil)) + json.put("pumpProfileName", pumpProfile.profilename) + json.put("pumpPeak", pumpProfile.peak) + json.put("pumpDia", pumpProfile.dia) + json.put("tunedProfile", tunedProfile?.profile?.toPureNsJson(dateUtil)) + json.put("tunedCircadianProfile", tunedProfile?.circadianProfile?.toPureNsJson(dateUtil)) + json.put("tunedProfileName", tunedProfile?.profilename) + json.put("tunedPeak", tunedProfile?.peak) + json.put("tunedDia", tunedProfile?.dia) + json.put("result", result) + json.put("updateButtonVisibility", updateButtonVisibility) + sp.putString(R.string.key_autotune_last_run, json.toString()) + } + + fun loadLastRun() { + result = "" + lastRunSuccess = false + try { + val json = JSONObject(sp.getString(R.string.key_autotune_last_run, "")) + lastNbDays = JsonHelper.safeGetString(json, "lastNbDays", "") + lastRun = JsonHelper.safeGetLong(json, "lastRun") + val pumpPeak = JsonHelper.safeGetInt(json, "pumpPeak") + val pumpDia = JsonHelper.safeGetDouble(json, "pumpDia") + var localInsulin = LocalInsulin("PumpInsulin", pumpPeak, pumpDia) + selectedProfile = JsonHelper.safeGetString(json, "pumpProfileName", "") + val profile = JsonHelper.safeGetJSONObject(json, "pumpProfile", null)?.let { pureProfileFromJson(it, dateUtil) } + ?: return + pumpProfile = ATProfile(ProfileSealed.Pure(profile), localInsulin, injector).also { it.profilename = selectedProfile } + val tunedPeak = JsonHelper.safeGetInt(json, "tunedPeak") + val tunedDia = JsonHelper.safeGetDouble(json, "tunedDia") + localInsulin = LocalInsulin("PumpInsulin", tunedPeak, tunedDia) + val tunedProfileName = JsonHelper.safeGetString(json, "tunedProfileName", "") + val tuned = JsonHelper.safeGetJSONObject(json, "tunedProfile", null)?.let { pureProfileFromJson(it, dateUtil) } + ?: return + val circadianTuned = JsonHelper.safeGetJSONObject(json, "tunedCircadianProfile", null)?.let { pureProfileFromJson(it, dateUtil) } + ?: return + tunedProfile = ATProfile(ProfileSealed.Pure(tuned), localInsulin, injector).also { atProfile -> + atProfile.profilename = tunedProfileName + atProfile.circadianProfile = ProfileSealed.Pure(circadianTuned) + } + result = JsonHelper.safeGetString(json, "result", "") + updateButtonVisibility = JsonHelper.safeGetInt(json, "updateButtonVisibility") + lastRunSuccess = true + } catch (e: Exception) { + } + } private fun log(message: String) { atLog("[Plugin] $message") diff --git a/app/src/main/res/layout/autotune_fragment.xml b/app/src/main/res/layout/autotune_fragment.xml index 003699e6ba..744b9a260e 100644 --- a/app/src/main/res/layout/autotune_fragment.xml +++ b/app/src/main/res/layout/autotune_fragment.xml @@ -140,7 +140,8 @@ android:gravity="start" android:paddingStart="5dp" android:paddingEnd="5dp" - android:textSize="14sp" /> + android:textSize="14sp" + android:textColor="@color/colorCalculatorButton"/>