Merge pull request #1811 from Philoul/Autotune/SaveLastRun
Autotune UI Improvement
This commit is contained in:
commit
ac09866c3d
3 changed files with 69 additions and 5 deletions
|
@ -1,5 +1,6 @@
|
||||||
package info.nightscout.androidaps.plugins.general.autotune
|
package info.nightscout.androidaps.plugins.general.autotune
|
||||||
|
|
||||||
|
import android.graphics.Paint
|
||||||
import android.graphics.Typeface
|
import android.graphics.Typeface
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.text.Editable
|
import android.text.Editable
|
||||||
|
@ -80,7 +81,7 @@ class AutotuneFragment : DaggerFragment() {
|
||||||
super.onViewCreated(view, savedInstanceState)
|
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_tune_insulin_curve, false) // put to false tune insulin curve
|
||||||
sp.putBoolean(R.string.key_autotune_additional_log, false) // put to false additional log
|
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())
|
if (autotunePlugin.lastNbDays.isEmpty())
|
||||||
autotunePlugin.lastNbDays = sp.getInt(R.string.key_autotune_default_tune_days, 5).toString()
|
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()
|
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.tunedProfile?.profilename = localName
|
||||||
autotunePlugin.updateProfile(autotunePlugin.tunedProfile)
|
autotunePlugin.updateProfile(autotunePlugin.tunedProfile)
|
||||||
autotunePlugin.updateButtonVisibility = View.GONE
|
autotunePlugin.updateButtonVisibility = View.GONE
|
||||||
|
autotunePlugin.saveLastRun()
|
||||||
uel.log(
|
uel.log(
|
||||||
UserEntry.Action.STORE_PROFILE,
|
UserEntry.Action.STORE_PROFILE,
|
||||||
UserEntry.Sources.Autotune,
|
UserEntry.Sources.Autotune,
|
||||||
|
@ -164,6 +166,7 @@ class AutotuneFragment : DaggerFragment() {
|
||||||
autotunePlugin.tunedProfile?.profilename = ""
|
autotunePlugin.tunedProfile?.profilename = ""
|
||||||
autotunePlugin.updateProfile(autotunePlugin.pumpProfile)
|
autotunePlugin.updateProfile(autotunePlugin.pumpProfile)
|
||||||
autotunePlugin.updateButtonVisibility = View.VISIBLE
|
autotunePlugin.updateButtonVisibility = View.VISIBLE
|
||||||
|
autotunePlugin.saveLastRun()
|
||||||
uel.log(
|
uel.log(
|
||||||
UserEntry.Action.STORE_PROFILE,
|
UserEntry.Action.STORE_PROFILE,
|
||||||
UserEntry.Sources.Autotune,
|
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
|
@Synchronized
|
||||||
|
@ -264,7 +275,6 @@ class AutotuneFragment : DaggerFragment() {
|
||||||
updateGui()
|
updateGui()
|
||||||
}, { fabricPrivacy.logException(it) })
|
}, { fabricPrivacy.logException(it) })
|
||||||
checkNewDay()
|
checkNewDay()
|
||||||
binding.tuneDays.value = autotunePlugin.lastNbDays.toDouble()
|
|
||||||
updateGui()
|
updateGui()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -277,6 +287,7 @@ class AutotuneFragment : DaggerFragment() {
|
||||||
@Synchronized
|
@Synchronized
|
||||||
private fun updateGui() {
|
private fun updateGui() {
|
||||||
_binding ?: return
|
_binding ?: return
|
||||||
|
binding.tuneDays.value = autotunePlugin.lastNbDays.toDouble()
|
||||||
profileStore = activePlugin.activeProfileSource.profile ?: ProfileStore(injector, JSONObject(), dateUtil)
|
profileStore = activePlugin.activeProfileSource.profile ?: ProfileStore(injector, JSONObject(), dateUtil)
|
||||||
profileName = if (binding.profileList.text.toString() == rh.gs(R.string.active)) "" else binding.profileList.text.toString()
|
profileName = if (binding.profileList.text.toString() == rh.gs(R.string.active)) "" else binding.profileList.text.toString()
|
||||||
profileFunction.getProfile()?.let { currentProfile ->
|
profileFunction.getProfile()?.let { currentProfile ->
|
||||||
|
|
|
@ -7,6 +7,7 @@ import info.nightscout.androidaps.data.LocalInsulin
|
||||||
import info.nightscout.androidaps.data.ProfileSealed
|
import info.nightscout.androidaps.data.ProfileSealed
|
||||||
import info.nightscout.androidaps.database.entities.UserEntry
|
import info.nightscout.androidaps.database.entities.UserEntry
|
||||||
import info.nightscout.androidaps.database.entities.ValueWithUnit
|
import info.nightscout.androidaps.database.entities.ValueWithUnit
|
||||||
|
import info.nightscout.androidaps.extensions.pureProfileFromJson
|
||||||
import info.nightscout.androidaps.interfaces.*
|
import info.nightscout.androidaps.interfaces.*
|
||||||
import info.nightscout.androidaps.logging.UserEntryLogger
|
import info.nightscout.androidaps.logging.UserEntryLogger
|
||||||
import info.nightscout.androidaps.plugins.bus.RxBus
|
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.LocalProfilePlugin
|
||||||
import info.nightscout.androidaps.plugins.profile.local.events.EventLocalProfileChanged
|
import info.nightscout.androidaps.plugins.profile.local.events.EventLocalProfileChanged
|
||||||
import info.nightscout.androidaps.utils.DateUtil
|
import info.nightscout.androidaps.utils.DateUtil
|
||||||
|
import info.nightscout.androidaps.utils.JsonHelper
|
||||||
import info.nightscout.androidaps.utils.MidnightTime
|
import info.nightscout.androidaps.utils.MidnightTime
|
||||||
import info.nightscout.androidaps.utils.T
|
import info.nightscout.androidaps.utils.T
|
||||||
import info.nightscout.androidaps.utils.buildHelper.BuildHelper
|
import info.nightscout.androidaps.utils.buildHelper.BuildHelper
|
||||||
|
@ -192,12 +194,12 @@ class AutotunePlugin @Inject constructor(
|
||||||
|
|
||||||
tunedProfile?.let {
|
tunedProfile?.let {
|
||||||
lastRunSuccess = true
|
lastRunSuccess = true
|
||||||
sp.putLong(R.string.key_autotune_last_run, lastRun)
|
saveLastRun()
|
||||||
rxBus.send(EventAutotuneUpdateGui())
|
rxBus.send(EventAutotuneUpdateGui())
|
||||||
calculationRunning = false
|
calculationRunning = false
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
return "No Result" // should never occurs
|
return rh.gs(R.string.autotune_error)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun showResults(tunedProfile: ATProfile?, pumpProfile: ATProfile): String {
|
private fun showResults(tunedProfile: ATProfile?, pumpProfile: ATProfile): String {
|
||||||
|
@ -300,6 +302,56 @@ class AutotunePlugin @Inject constructor(
|
||||||
localProfilePlugin.storeSettings()
|
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) {
|
private fun log(message: String) {
|
||||||
atLog("[Plugin] $message")
|
atLog("[Plugin] $message")
|
||||||
|
|
|
@ -140,7 +140,8 @@
|
||||||
android:gravity="start"
|
android:gravity="start"
|
||||||
android:paddingStart="5dp"
|
android:paddingStart="5dp"
|
||||||
android:paddingEnd="5dp"
|
android:paddingEnd="5dp"
|
||||||
android:textSize="14sp" />
|
android:textSize="14sp"
|
||||||
|
android:textColor="@color/colorCalculatorButton"/>
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue