AndroidAPS/app/src/main/java/info/nightscout/androidaps/dialogs/ProfileSwitchDialog.kt

116 lines
5.8 KiB
Kotlin
Raw Normal View History

2019-12-20 18:55:54 +01:00
package info.nightscout.androidaps.dialogs
2019-12-18 20:59:33 +01:00
import android.os.Bundle
2019-12-18 23:22:03 +01:00
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
2019-12-18 20:59:33 +01:00
import android.widget.ArrayAdapter
import com.google.common.base.Joiner
import info.nightscout.androidaps.Constants
import info.nightscout.androidaps.R
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin
2019-12-27 19:20:38 +01:00
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunction
2019-12-18 23:22:03 +01:00
import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin
2019-12-18 20:59:33 +01:00
import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.HtmlHelper
import info.nightscout.androidaps.utils.OKDialog
2019-12-27 19:20:38 +01:00
import info.nightscout.androidaps.utils.resources.ResourceHelper
2019-12-20 23:05:35 +01:00
import kotlinx.android.synthetic.main.dialog_profileswitch.*
2019-12-19 23:45:27 +01:00
import kotlinx.android.synthetic.main.notes.*
2019-12-18 20:59:33 +01:00
import kotlinx.android.synthetic.main.okcancel.*
import java.text.DecimalFormat
import java.util.*
2019-12-27 19:20:38 +01:00
import javax.inject.Inject
2019-12-18 20:59:33 +01:00
class ProfileSwitchDialog : DialogFragmentWithDate() {
2019-12-27 19:20:38 +01:00
@Inject
lateinit var resourceHelper: ResourceHelper
@Inject
lateinit var profileFunction: ProfileFunction
2019-12-28 22:51:04 +01:00
@Inject
lateinit var treatmentsPlugin: TreatmentsPlugin
2019-12-18 23:22:03 +01:00
override fun onSaveInstanceState(savedInstanceState: Bundle) {
2019-12-18 20:59:33 +01:00
super.onSaveInstanceState(savedInstanceState)
savedInstanceState.putDouble("overview_profileswitch_duration", overview_profileswitch_duration.value)
savedInstanceState.putDouble("overview_profileswitch_percentage", overview_profileswitch_percentage.value)
savedInstanceState.putDouble("overview_profileswitch_timeshift", overview_profileswitch_timeshift.value)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
2019-12-20 23:05:35 +01:00
onCreateViewGeneral()
2019-12-20 18:55:54 +01:00
return inflater.inflate(R.layout.dialog_profileswitch, container, false)
2019-12-18 20:59:33 +01:00
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
overview_profileswitch_duration.setParams(savedInstanceState?.getDouble("overview_profileswitch_duration")
2019-12-20 23:05:35 +01:00
?: 0.0, 0.0, Constants.MAX_PROFILE_SWITCH_DURATION, 10.0, DecimalFormat("0"), false, ok)
2019-12-18 20:59:33 +01:00
overview_profileswitch_percentage.setParams(savedInstanceState?.getDouble("overview_profileswitch_percentage")
2019-12-20 23:05:35 +01:00
?: 100.0, Constants.CPP_MIN_PERCENTAGE.toDouble(), Constants.CPP_MAX_PERCENTAGE.toDouble(), 1.0, DecimalFormat("0"), false, ok)
2019-12-18 20:59:33 +01:00
overview_profileswitch_timeshift.setParams(savedInstanceState?.getDouble("overview_profileswitch_timeshift")
2019-12-20 23:05:35 +01:00
?: 0.0, Constants.CPP_MIN_TIMESHIFT.toDouble(), Constants.CPP_MAX_TIMESHIFT.toDouble(), 1.0, DecimalFormat("0"), false, ok)
2019-12-18 20:59:33 +01:00
// profile
context?.let { context ->
val profileStore = ConfigBuilderPlugin.getPlugin().activeProfileInterface?.profile
2019-12-20 23:05:35 +01:00
?: return
2019-12-18 20:59:33 +01:00
val profileList = profileStore.getProfileList()
val adapter = ArrayAdapter(context, R.layout.spinner_centered, profileList)
overview_profileswitch_profile.adapter = adapter
// set selected to actual profile
for (p in profileList.indices)
2019-12-27 19:20:38 +01:00
if (profileList[p] == profileFunction.getProfileName(false))
2019-12-18 20:59:33 +01:00
overview_profileswitch_profile.setSelection(p)
} ?: return
2019-12-18 23:22:03 +01:00
TreatmentsPlugin.getPlugin().getProfileSwitchFromHistory(DateUtil.now())?.let { ps ->
if (ps.isCPP) {
overview_profileswitch_reuselayout.visibility = View.VISIBLE
2019-12-27 19:20:38 +01:00
overview_profileswitch_reusebutton.text = resourceHelper.gs(R.string.reuse) + " " + ps.percentage + "% " + ps.timeshift + "h"
2019-12-18 23:22:03 +01:00
overview_profileswitch_reusebutton.setOnClickListener {
overview_profileswitch_percentage.value = ps.percentage.toDouble()
overview_profileswitch_timeshift.value = ps.timeshift.toDouble()
}
} else {
overview_profileswitch_reuselayout.visibility = View.GONE
}
}
2019-12-18 20:59:33 +01:00
}
2019-12-21 23:17:20 +01:00
override fun submit(): Boolean {
val profileStore = ConfigBuilderPlugin.getPlugin().activeProfileInterface?.profile
?: return false
2019-12-18 20:59:33 +01:00
val actions: LinkedList<String> = LinkedList()
val duration = overview_profileswitch_duration.value
if (duration > 0)
2019-12-27 19:20:38 +01:00
actions.add(resourceHelper.gs(R.string.duration) + ": " + resourceHelper.gs(R.string.format_hours, duration))
2019-12-18 20:59:33 +01:00
val profile = overview_profileswitch_profile.selectedItem.toString()
2019-12-27 19:20:38 +01:00
actions.add(resourceHelper.gs(R.string.profile) + ": " + profile)
2019-12-18 20:59:33 +01:00
val percent = overview_profileswitch_percentage.value.toInt()
if (percent != 100)
2019-12-27 19:20:38 +01:00
actions.add(resourceHelper.gs(R.string.percent) + ": " + percent + "%")
2019-12-18 20:59:33 +01:00
val timeShift = overview_profileswitch_timeshift.value.toInt()
if (timeShift != 0)
2019-12-27 19:20:38 +01:00
actions.add(resourceHelper.gs(R.string.careportal_newnstreatment_timeshift_label) + ": " + resourceHelper.gs(R.string.format_hours, timeShift.toDouble()))
2019-12-19 23:45:27 +01:00
val notes = notes.text.toString()
2019-12-18 20:59:33 +01:00
if (notes.isNotEmpty())
2019-12-27 19:20:38 +01:00
actions.add(resourceHelper.gs(R.string.careportal_newnstreatment_notes_label) + ": " + notes)
2019-12-18 20:59:33 +01:00
if (eventTimeChanged)
2019-12-27 19:20:38 +01:00
actions.add(resourceHelper.gs(R.string.time) + ": " + DateUtil.dateAndTimeString(eventTime))
2019-12-18 20:59:33 +01:00
activity?.let { activity ->
2019-12-27 19:20:38 +01:00
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.careportal_profileswitch), HtmlHelper.fromHtml(Joiner.on("<br/>").join(actions)), Runnable {
2019-12-28 22:51:04 +01:00
treatmentsPlugin.doProfileSwitch(profileStore, profile, duration.toInt(), percent, timeShift, eventTime)
2019-12-22 21:37:26 +01:00
})
2019-12-18 20:59:33 +01:00
}
2019-12-21 23:17:20 +01:00
return true
2019-12-18 20:59:33 +01:00
}
}