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
|
2021-01-21 16:38:42 +01:00
|
|
|
import info.nightscout.androidaps.databinding.DialogProfileswitchBinding
|
2020-03-16 21:40:29 +01:00
|
|
|
import info.nightscout.androidaps.interfaces.ActivePluginProvider
|
2020-05-07 09:54:36 +02:00
|
|
|
import info.nightscout.androidaps.interfaces.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
|
2020-04-08 14:03:57 +02:00
|
|
|
import info.nightscout.androidaps.utils.alertDialogs.OKDialog
|
2019-12-27 19:20:38 +01:00
|
|
|
import info.nightscout.androidaps.utils.resources.ResourceHelper
|
2019-12-18 20:59:33 +01:00
|
|
|
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() {
|
2021-01-21 16:38:42 +01:00
|
|
|
|
2019-12-31 00:37:36 +01:00
|
|
|
@Inject lateinit var resourceHelper: ResourceHelper
|
|
|
|
@Inject lateinit var profileFunction: ProfileFunction
|
|
|
|
@Inject lateinit var treatmentsPlugin: TreatmentsPlugin
|
2020-03-16 21:40:29 +01:00
|
|
|
@Inject lateinit var activePlugin: ActivePluginProvider
|
2019-12-28 22:51:04 +01:00
|
|
|
|
2021-01-21 16:38:42 +01:00
|
|
|
private var profileIndex: Int? = null
|
|
|
|
|
|
|
|
private var _binding: DialogProfileswitchBinding? = null
|
|
|
|
|
|
|
|
// This property is only valid between onCreateView and
|
|
|
|
// onDestroyView.
|
|
|
|
private val binding get() = _binding!!
|
2020-06-22 22:19:27 +02:00
|
|
|
|
2019-12-18 23:22:03 +01:00
|
|
|
override fun onSaveInstanceState(savedInstanceState: Bundle) {
|
2019-12-18 20:59:33 +01:00
|
|
|
super.onSaveInstanceState(savedInstanceState)
|
2021-01-21 16:38:42 +01:00
|
|
|
savedInstanceState.putDouble("duration", binding.duration.value)
|
|
|
|
savedInstanceState.putDouble("percentage", binding.percentage.value)
|
|
|
|
savedInstanceState.putDouble("timeshift", binding.timeshift.value)
|
2019-12-18 20:59:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
|
2021-01-21 16:38:42 +01:00
|
|
|
savedInstanceState: Bundle?): View {
|
2019-12-20 23:05:35 +01:00
|
|
|
onCreateViewGeneral()
|
2020-06-22 22:19:27 +02:00
|
|
|
arguments?.let { bundle ->
|
|
|
|
profileIndex = bundle.getInt("profileIndex", 0)
|
|
|
|
}
|
2021-01-21 16:38:42 +01:00
|
|
|
_binding = DialogProfileswitchBinding.inflate(inflater, container, false)
|
|
|
|
return binding.root
|
2019-12-18 20:59:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
|
|
|
|
2021-01-21 16:38:42 +01:00
|
|
|
binding.duration.setParams(savedInstanceState?.getDouble("duration")
|
|
|
|
?: 0.0, 0.0, Constants.MAX_PROFILE_SWITCH_DURATION, 10.0, DecimalFormat("0"), false, binding.okcancel.ok)
|
|
|
|
binding.percentage.setParams(savedInstanceState?.getDouble("percentage")
|
|
|
|
?: 100.0, Constants.CPP_MIN_PERCENTAGE.toDouble(), Constants.CPP_MAX_PERCENTAGE.toDouble(), 5.0, DecimalFormat("0"), false, binding.okcancel.ok)
|
|
|
|
binding.timeshift.setParams(savedInstanceState?.getDouble("timeshift")
|
|
|
|
?: 0.0, Constants.CPP_MIN_TIMESHIFT.toDouble(), Constants.CPP_MAX_TIMESHIFT.toDouble(), 1.0, DecimalFormat("0"), false, binding.okcancel.ok)
|
2019-12-18 20:59:33 +01:00
|
|
|
|
|
|
|
// profile
|
|
|
|
context?.let { context ->
|
2020-03-16 21:40:29 +01:00
|
|
|
val profileStore = activePlugin.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)
|
2021-01-21 16:38:42 +01:00
|
|
|
binding.profile.adapter = adapter
|
2019-12-18 20:59:33 +01:00
|
|
|
// set selected to actual profile
|
2020-06-22 22:19:27 +02:00
|
|
|
if (profileIndex != null)
|
2021-01-21 16:38:42 +01:00
|
|
|
binding.profile.setSelection(profileIndex as Int)
|
2020-06-22 22:19:27 +02:00
|
|
|
else
|
|
|
|
for (p in profileList.indices)
|
|
|
|
if (profileList[p] == profileFunction.getProfileName(false))
|
2021-01-21 16:38:42 +01:00
|
|
|
binding.profile.setSelection(p)
|
2019-12-18 20:59:33 +01:00
|
|
|
} ?: return
|
2019-12-18 23:22:03 +01:00
|
|
|
|
2019-12-31 00:37:36 +01:00
|
|
|
treatmentsPlugin.getProfileSwitchFromHistory(DateUtil.now())?.let { ps ->
|
2019-12-18 23:22:03 +01:00
|
|
|
if (ps.isCPP) {
|
2021-01-21 16:38:42 +01:00
|
|
|
binding.reuselayout.visibility = View.VISIBLE
|
|
|
|
binding.reusebutton.text = resourceHelper.gs(R.string.reuse_profile_pct_hours, ps.percentage, ps.timeshift)
|
|
|
|
binding.reusebutton.setOnClickListener {
|
|
|
|
binding.percentage.value = ps.percentage.toDouble()
|
|
|
|
binding.timeshift.value = ps.timeshift.toDouble()
|
2019-12-18 23:22:03 +01:00
|
|
|
}
|
|
|
|
} else {
|
2021-01-21 16:38:42 +01:00
|
|
|
binding.reuselayout.visibility = View.GONE
|
2019-12-18 23:22:03 +01:00
|
|
|
}
|
|
|
|
}
|
2019-12-18 20:59:33 +01:00
|
|
|
}
|
|
|
|
|
2021-01-21 16:38:42 +01:00
|
|
|
override fun onDestroyView() {
|
|
|
|
super.onDestroyView()
|
|
|
|
_binding = null
|
|
|
|
}
|
|
|
|
|
2019-12-21 23:17:20 +01:00
|
|
|
override fun submit(): Boolean {
|
2021-01-21 16:38:42 +01:00
|
|
|
if (_binding == null) return false
|
2020-03-16 21:40:29 +01:00
|
|
|
val profileStore = activePlugin.activeProfileInterface.profile
|
2019-12-21 23:17:20 +01:00
|
|
|
?: return false
|
2019-12-18 20:59:33 +01:00
|
|
|
|
|
|
|
val actions: LinkedList<String> = LinkedList()
|
2021-01-21 16:38:42 +01:00
|
|
|
val duration = binding.duration.value?.toInt() ?: return false
|
2019-12-18 20:59:33 +01:00
|
|
|
if (duration > 0)
|
2020-03-10 19:20:22 +01:00
|
|
|
actions.add(resourceHelper.gs(R.string.duration) + ": " + resourceHelper.gs(R.string.format_mins, duration))
|
2021-01-21 16:38:42 +01:00
|
|
|
val profile = binding.profile.selectedItem.toString()
|
2019-12-27 19:20:38 +01:00
|
|
|
actions.add(resourceHelper.gs(R.string.profile) + ": " + profile)
|
2021-01-21 16:38:42 +01:00
|
|
|
val percent = binding.percentage.value.toInt()
|
2019-12-18 20:59:33 +01:00
|
|
|
if (percent != 100)
|
2019-12-27 19:20:38 +01:00
|
|
|
actions.add(resourceHelper.gs(R.string.percent) + ": " + percent + "%")
|
2021-01-21 16:38:42 +01:00
|
|
|
val timeShift = binding.timeshift.value.toInt()
|
2019-12-18 20:59:33 +01:00
|
|
|
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()))
|
2021-01-21 16:38:42 +01:00
|
|
|
val notes = binding.notesLayout.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)
|
2020-05-07 23:40:59 +02:00
|
|
|
actions.add(resourceHelper.gs(R.string.time) + ": " + dateUtil.dateAndTimeString(eventTime))
|
2019-12-18 20:59:33 +01:00
|
|
|
|
|
|
|
activity?.let { activity ->
|
2021-01-21 16:38:42 +01:00
|
|
|
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.careportal_profileswitch), HtmlHelper.fromHtml(Joiner.on("<br/>").join(actions)), {
|
2020-02-28 18:20:31 +01:00
|
|
|
aapsLogger.debug("USER ENTRY: PROFILE SWITCH $profile percent: $percent timeshift: $timeShift duration: $duration")
|
2020-03-10 19:20:22 +01:00
|
|
|
treatmentsPlugin.doProfileSwitch(profileStore, profile, duration, 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
|
|
|
}
|
|
|
|
}
|