AndroidAPS/app/src/main/java/info/nightscout/androidaps/activities/ProfileHelperActivity.kt

296 lines
15 KiB
Kotlin
Raw Normal View History

2020-07-06 21:14:00 +02:00
package info.nightscout.androidaps.activities
import android.content.res.ColorStateList
2020-07-06 21:14:00 +02:00
import android.os.Bundle
2020-07-14 16:51:13 +02:00
import android.text.Editable
import android.text.TextWatcher
import android.view.Menu
import android.widget.PopupMenu
2020-07-06 21:14:00 +02:00
import info.nightscout.androidaps.R
2020-07-14 16:51:13 +02:00
import info.nightscout.androidaps.data.Profile
2020-07-06 21:14:00 +02:00
import info.nightscout.androidaps.data.defaultProfile.DefaultProfile
2020-07-16 17:30:04 +02:00
import info.nightscout.androidaps.data.defaultProfile.DefaultProfileDPV
2021-01-22 14:16:59 +01:00
import info.nightscout.androidaps.databinding.ActivityProfilehelperBinding
2020-07-14 16:51:13 +02:00
import info.nightscout.androidaps.db.ProfileSwitch
2020-07-06 21:14:00 +02:00
import info.nightscout.androidaps.dialogs.ProfileViewerDialog
2020-07-14 16:51:13 +02:00
import info.nightscout.androidaps.interfaces.ActivePluginProvider
import info.nightscout.androidaps.interfaces.DatabaseHelperInterface
2020-07-06 21:14:00 +02:00
import info.nightscout.androidaps.interfaces.ProfileFunction
import info.nightscout.androidaps.logging.AAPSLogger
2020-07-14 16:51:13 +02:00
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.plugins.profile.local.LocalProfilePlugin
import info.nightscout.androidaps.plugins.profile.local.events.EventLocalProfileChanged
2020-07-06 21:14:00 +02:00
import info.nightscout.androidaps.utils.DateUtil
2020-07-14 16:51:13 +02:00
import info.nightscout.androidaps.utils.T
2020-07-06 21:14:00 +02:00
import info.nightscout.androidaps.utils.ToastUtils
2020-07-14 16:51:13 +02:00
import info.nightscout.androidaps.utils.alertDialogs.OKDialog
import info.nightscout.androidaps.utils.extensions.toVisibility
2020-07-06 21:14:00 +02:00
import info.nightscout.androidaps.utils.stats.TddCalculator
import java.text.DecimalFormat
import javax.inject.Inject
class ProfileHelperActivity : NoSplashAppCompatActivity() {
2021-01-18 22:47:55 +01:00
2020-07-06 21:14:00 +02:00
@Inject lateinit var aapsLogger: AAPSLogger
@Inject lateinit var tddCalculator: TddCalculator
@Inject lateinit var profileFunction: ProfileFunction
@Inject lateinit var defaultProfile: DefaultProfile
2020-07-16 17:30:04 +02:00
@Inject lateinit var defaultProfileDPV: DefaultProfileDPV
2020-07-14 16:51:13 +02:00
@Inject lateinit var localProfilePlugin: LocalProfilePlugin
@Inject lateinit var rxBus: RxBusWrapper
@Inject lateinit var dateUtil: DateUtil
@Inject lateinit var activePlugin: ActivePluginProvider
@Inject lateinit var databaseHelper: DatabaseHelperInterface
enum class ProfileType {
MOTOL_DEFAULT,
2020-07-16 17:30:04 +02:00
DPV_DEFAULT,
2020-07-14 16:51:13 +02:00
CURRENT,
AVAILABLE_PROFILE,
PROFILE_SWITCH
}
2020-07-16 17:30:04 +02:00
private var tabSelected = 0
private val typeSelected = arrayOf(ProfileType.MOTOL_DEFAULT, ProfileType.CURRENT)
2020-07-14 16:51:13 +02:00
2020-07-16 17:30:04 +02:00
private val ageUsed = arrayOf(15.0, 15.0)
private val weightUsed = arrayOf(0.0, 0.0)
private val tddUsed = arrayOf(0.0, 0.0)
private val pctUsed = arrayOf(32.0, 32.0)
2020-07-14 16:51:13 +02:00
2020-07-16 17:30:04 +02:00
private lateinit var profileList: ArrayList<CharSequence>
private val profileUsed = arrayOf(0, 0)
2020-07-14 16:51:13 +02:00
2020-07-16 17:30:04 +02:00
private lateinit var profileSwitch: List<ProfileSwitch>
private val profileSwitchUsed = arrayOf(0, 0)
2020-07-06 21:14:00 +02:00
2021-01-22 14:16:59 +01:00
private lateinit var binding: ActivityProfilehelperBinding
2020-07-06 21:14:00 +02:00
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
2021-01-22 14:16:59 +01:00
binding = ActivityProfilehelperBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.menu1.setOnClickListener {
2020-07-16 17:30:04 +02:00
switchTab(0, typeSelected[0])
2020-07-14 16:51:13 +02:00
}
2021-01-22 14:16:59 +01:00
binding.menu2.setOnClickListener {
2020-07-16 17:30:04 +02:00
switchTab(1, typeSelected[1])
2020-07-14 16:51:13 +02:00
}
2020-07-06 21:14:00 +02:00
2021-01-22 14:16:59 +01:00
binding.profiletype.setOnClickListener {
PopupMenu(this, binding.profiletype).apply {
2020-07-14 16:51:13 +02:00
menuInflater.inflate(R.menu.menu_profilehelper, menu)
setOnMenuItemClickListener { item ->
2021-01-22 14:16:59 +01:00
binding.profiletype.setText(item.title)
2020-07-14 16:51:13 +02:00
when (item.itemId) {
2021-01-22 14:16:59 +01:00
R.id.menu_default -> switchTab(tabSelected, ProfileType.MOTOL_DEFAULT)
R.id.menu_default_dpv -> switchTab(tabSelected, ProfileType.DPV_DEFAULT)
R.id.menu_current -> switchTab(tabSelected, ProfileType.CURRENT)
R.id.menu_available -> switchTab(tabSelected, ProfileType.AVAILABLE_PROFILE)
2020-07-16 17:30:04 +02:00
R.id.menu_profileswitch -> switchTab(tabSelected, ProfileType.PROFILE_SWITCH)
2020-07-14 16:51:13 +02:00
}
true
}
show()
}
}
// Active profile
profileList = activePlugin.activeProfileInterface.profile?.getProfileList() ?: ArrayList()
2021-01-22 14:16:59 +01:00
binding.availableProfileList.setOnClickListener {
PopupMenu(this, binding.availableProfileList).apply {
2020-07-14 16:51:13 +02:00
var order = 0
for (name in profileList) menu.add(Menu.NONE, order, order++, name)
setOnMenuItemClickListener { item ->
2021-01-22 14:16:59 +01:00
binding.availableProfileList.setText(item.title)
2020-07-14 16:51:13 +02:00
profileUsed[tabSelected] = item.itemId
true
}
show()
}
}
2020-07-06 21:14:00 +02:00
2020-07-14 16:51:13 +02:00
// Profile switch
profileSwitch = databaseHelper.getProfileSwitchData(dateUtil._now() - T.months(2).msecs(), true)
2021-01-22 14:16:59 +01:00
binding.profileswitchList.setOnClickListener {
PopupMenu(this, binding.profileswitchList).apply {
2020-07-14 16:51:13 +02:00
var order = 0
for (name in profileSwitch) menu.add(Menu.NONE, order, order++, name.customizedName)
setOnMenuItemClickListener { item ->
2021-01-22 14:16:59 +01:00
binding.profileswitchList.setText(item.title)
2020-07-14 16:51:13 +02:00
profileSwitchUsed[tabSelected] = item.itemId
true
}
show()
}
}
// Default profile
2021-01-22 14:16:59 +01:00
binding.copytolocalprofile.setOnClickListener {
2020-07-16 17:30:04 +02:00
val age = ageUsed[tabSelected]
val weight = weightUsed[tabSelected]
val tdd = tddUsed[tabSelected]
val pct = pctUsed[tabSelected]
val profile = if (typeSelected[tabSelected] == ProfileType.MOTOL_DEFAULT) defaultProfile.profile(age, tdd, weight, profileFunction.getUnits())
else defaultProfileDPV.profile(age, tdd, pct / 100.0, profileFunction.getUnits())
profile?.let {
2020-07-14 16:51:13 +02:00
OKDialog.showConfirmation(this, resourceHelper.gs(R.string.careportal_profileswitch), resourceHelper.gs(R.string.copytolocalprofile), Runnable {
2020-08-10 13:06:27 +02:00
localProfilePlugin.addProfile(localProfilePlugin.copyFrom(it, "DefaultProfile" + dateUtil.dateAndTimeAndSecondsString(dateUtil._now())))
2020-07-14 16:51:13 +02:00
rxBus.send(EventLocalProfileChanged())
})
}
}
2021-01-22 14:16:59 +01:00
binding.age.setParams(0.0, 1.0, 18.0, 1.0, DecimalFormat("0"), false, null)
binding.weight.setParams(0.0, 0.0, 150.0, 1.0, DecimalFormat("0"), false, null, object : TextWatcher {
2020-07-14 16:51:13 +02:00
override fun afterTextChanged(s: Editable) {}
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
2021-01-22 14:16:59 +01:00
binding.tddRow.visibility = (binding.weight.value == 0.0).toVisibility()
2020-07-06 21:14:00 +02:00
}
2020-07-14 16:51:13 +02:00
})
2021-01-22 14:16:59 +01:00
binding.tdd.setParams(0.0, 0.0, 200.0, 1.0, DecimalFormat("0"), false, null, object : TextWatcher {
2020-07-14 16:51:13 +02:00
override fun afterTextChanged(s: Editable) {}
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
2021-01-22 14:16:59 +01:00
binding.weightRow.visibility = (binding.tdd.value == 0.0).toVisibility()
2020-07-06 21:14:00 +02:00
}
2020-07-14 16:51:13 +02:00
})
2021-01-22 14:16:59 +01:00
binding.basalpctfromtdd.setParams(32.0, 32.0, 37.0, 1.0, DecimalFormat("0"), false, null)
2020-07-16 17:30:04 +02:00
2021-01-22 14:16:59 +01:00
binding.tdds.text = tddCalculator.stats()
2020-07-14 16:51:13 +02:00
// Current profile
2021-01-22 14:16:59 +01:00
binding.currentProfileText.text = profileFunction.getProfileName()
2020-07-14 16:51:13 +02:00
// General
2021-01-22 14:16:59 +01:00
binding.compareprofile.setOnClickListener {
2020-07-16 17:30:04 +02:00
storeValues()
for (i in 0..1) {
if (typeSelected[i] == ProfileType.MOTOL_DEFAULT) {
if (ageUsed[i] < 1 || ageUsed[i] > 18) {
ToastUtils.showToastInUiThread(this, R.string.invalidage)
return@setOnClickListener
}
if ((weightUsed[i] < 5 || weightUsed[i] > 150) && tddUsed[i] == 0.0) {
ToastUtils.showToastInUiThread(this, R.string.invalidweight)
return@setOnClickListener
}
if ((tddUsed[i] < 5 || tddUsed[i] > 150) && weightUsed[i] == 0.0) {
ToastUtils.showToastInUiThread(this, R.string.invalidweight)
return@setOnClickListener
}
2020-07-14 16:51:13 +02:00
}
2020-07-16 17:30:04 +02:00
if (typeSelected[i] == ProfileType.DPV_DEFAULT) {
if (ageUsed[i] < 1 || ageUsed[i] > 18) {
ToastUtils.showToastInUiThread(this, R.string.invalidage)
return@setOnClickListener
}
if (tddUsed[i] < 5 || tddUsed[i] > 150) {
ToastUtils.showToastInUiThread(this, R.string.invalidweight)
return@setOnClickListener
}
if ((pctUsed[i] < 32 || pctUsed[i] > 37)) {
ToastUtils.showToastInUiThread(this, R.string.invalidpct)
return@setOnClickListener
}
2020-07-14 16:51:13 +02:00
}
2020-07-06 21:14:00 +02:00
}
2020-07-14 16:51:13 +02:00
2020-07-16 17:30:04 +02:00
getProfile(ageUsed[0], tddUsed[0], weightUsed[0], pctUsed[0] / 100.0, 0)?.let { profile0 ->
getProfile(ageUsed[1], tddUsed[1], weightUsed[1], pctUsed[1] / 100.0, 1)?.let { profile1 ->
2020-07-09 18:48:28 +02:00
ProfileViewerDialog().also { pvd ->
pvd.arguments = Bundle().also {
it.putLong("time", DateUtil.now())
it.putInt("mode", ProfileViewerDialog.Mode.PROFILE_COMPARE.ordinal)
2020-07-14 16:51:13 +02:00
it.putString("customProfile", profile0.data.toString())
it.putString("customProfile2", profile1.data.toString())
it.putString("customProfileUnits", profileFunction.getUnits())
2020-07-16 17:30:04 +02:00
it.putString("customProfileName", getProfileName(ageUsed[0], tddUsed[0], weightUsed[0], pctUsed[0] / 100.0, 0) + "\n" + getProfileName(ageUsed[1], tddUsed[1], weightUsed[1], pctUsed[1] / 100.0, 1))
2020-07-09 18:48:28 +02:00
}
}.show(supportFragmentManager, "ProfileViewDialog")
return@setOnClickListener
}
2020-07-06 21:14:00 +02:00
}
2020-07-09 18:48:28 +02:00
ToastUtils.showToastInUiThread(this, R.string.invalidinput)
2020-07-06 21:14:00 +02:00
}
2020-07-16 17:30:04 +02:00
switchTab(0, typeSelected[0], false)
2020-07-14 16:51:13 +02:00
}
2020-07-16 17:30:04 +02:00
private fun getProfile(age: Double, tdd: Double, weight: Double, basalPct: Double, tab: Int): Profile? =
2021-01-18 11:06:37 +01:00
try { // profile must not exist
when (typeSelected[tab]) {
2021-01-22 14:16:59 +01:00
ProfileType.MOTOL_DEFAULT -> defaultProfile.profile(age, tdd, weight, profileFunction.getUnits())
ProfileType.DPV_DEFAULT -> defaultProfileDPV.profile(age, tdd, basalPct, profileFunction.getUnits())
ProfileType.CURRENT -> profileFunction.getProfile()?.convertToNonCustomizedProfile()
2021-01-18 11:06:37 +01:00
ProfileType.AVAILABLE_PROFILE -> activePlugin.activeProfileInterface.profile?.getSpecificProfile(profileList[profileUsed[tab]].toString())
2021-01-22 14:16:59 +01:00
ProfileType.PROFILE_SWITCH -> profileSwitch[profileSwitchUsed[tab]].profileObject?.convertToNonCustomizedProfile()
2021-01-18 11:06:37 +01:00
}
2021-01-18 22:47:55 +01:00
} catch (e: Exception) {
null
}
2020-07-14 16:51:13 +02:00
2020-07-16 17:30:04 +02:00
private fun getProfileName(age: Double, tdd: Double, weight: Double, basalSumPct: Double, tab: Int): String =
2020-07-14 16:51:13 +02:00
when (typeSelected[tab]) {
2021-01-22 14:16:59 +01:00
ProfileType.MOTOL_DEFAULT -> if (tdd > 0) resourceHelper.gs(R.string.formatwithtdd, age, tdd) else resourceHelper.gs(R.string.formatwithweight, age, weight)
ProfileType.DPV_DEFAULT -> resourceHelper.gs(R.string.formatwittddandpct, age, tdd, (basalSumPct * 100).toInt())
ProfileType.CURRENT -> profileFunction.getProfileName()
2020-07-14 16:51:13 +02:00
ProfileType.AVAILABLE_PROFILE -> profileList[profileUsed[tab]].toString()
2021-01-22 14:16:59 +01:00
ProfileType.PROFILE_SWITCH -> profileSwitch[profileSwitchUsed[tab]].customizedName
2020-07-14 16:51:13 +02:00
}
2020-07-16 17:30:04 +02:00
private fun storeValues() {
2021-01-22 14:16:59 +01:00
ageUsed[tabSelected] = binding.age.value
weightUsed[tabSelected] = binding.weight.value
tddUsed[tabSelected] = binding.tdd.value
pctUsed[tabSelected] = binding.basalpctfromtdd.value
2020-07-16 17:30:04 +02:00
}
2020-07-14 16:51:13 +02:00
2020-07-16 17:30:04 +02:00
private fun switchTab(tab: Int, newContent: ProfileType, storeOld: Boolean = true) {
setBackgroundColorOnSelected(tab)
// Store values for selected tab. listBox values are stored on selection change
if (storeOld) storeValues()
2020-07-14 16:51:13 +02:00
tabSelected = tab
2020-07-16 17:30:04 +02:00
typeSelected[tabSelected] = newContent
2021-01-22 14:16:59 +01:00
binding.profiletypeTitle.defaultHintTextColor = ColorStateList.valueOf(resourceHelper.gc(if (tab == 0) R.color.tabBgColorSelected else R.color.examinedProfile))
2020-07-14 16:51:13 +02:00
2020-07-16 17:30:04 +02:00
// show new content
2021-01-22 14:16:59 +01:00
binding.profiletype.setText(
2020-07-16 17:30:04 +02:00
when (typeSelected[tabSelected]) {
ProfileType.MOTOL_DEFAULT -> resourceHelper.gs(R.string.motoldefaultprofile)
ProfileType.DPV_DEFAULT -> resourceHelper.gs(R.string.dpvdefaultprofile)
ProfileType.CURRENT -> resourceHelper.gs(R.string.currentprofile)
ProfileType.AVAILABLE_PROFILE -> resourceHelper.gs(R.string.availableprofile)
ProfileType.PROFILE_SWITCH -> resourceHelper.gs(R.string.careportal_profileswitch)
})
2021-01-22 14:16:59 +01:00
binding.defaultProfile.visibility = (newContent == ProfileType.MOTOL_DEFAULT || newContent == ProfileType.DPV_DEFAULT).toVisibility()
binding.currentProfile.visibility = (newContent == ProfileType.CURRENT).toVisibility()
binding.availableProfile.visibility = (newContent == ProfileType.AVAILABLE_PROFILE).toVisibility()
binding.profileSwitch.visibility = (newContent == ProfileType.PROFILE_SWITCH).toVisibility()
2020-07-14 16:51:13 +02:00
2020-07-16 17:30:04 +02:00
// restore selected values
2021-01-22 14:16:59 +01:00
binding.age.value = ageUsed[tabSelected]
binding.weight.value = weightUsed[tabSelected]
binding.tdd.value = tddUsed[tabSelected]
binding.basalpctfromtdd.value = pctUsed[tabSelected]
2020-07-16 17:30:04 +02:00
2021-01-22 14:16:59 +01:00
binding.basalpctfromtddRow.visibility = (newContent == ProfileType.DPV_DEFAULT).toVisibility()
2020-07-16 17:30:04 +02:00
if (profileList.isNotEmpty())
2021-01-22 14:16:59 +01:00
binding.availableProfileList.setText(profileList[profileUsed[tabSelected]].toString())
2020-07-16 17:30:04 +02:00
if (profileSwitch.isNotEmpty())
2021-01-22 14:16:59 +01:00
binding.profileswitchList.setText(profileSwitch[profileSwitchUsed[tabSelected]].customizedName)
2020-07-14 16:51:13 +02:00
}
private fun setBackgroundColorOnSelected(tab: Int) {
2021-01-22 14:16:59 +01:00
binding.menu1.setBackgroundColor(resourceHelper.gc(if (tab == 1) R.color.defaultbackground else R.color.tabBgColorSelected))
binding.menu2.setBackgroundColor(resourceHelper.gc(if (tab == 0) R.color.defaultbackground else R.color.examinedProfile))
2020-07-06 21:14:00 +02:00
}
}