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
|
2021-10-04 22:40:02 +02:00
|
|
|
import android.text.Editable
|
|
|
|
import android.text.TextWatcher
|
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-09-15 18:38:27 +02:00
|
|
|
import info.nightscout.androidaps.data.ProfileSealed
|
2021-04-29 20:42:45 +02:00
|
|
|
import info.nightscout.androidaps.database.AppRepository
|
2021-10-04 22:40:02 +02:00
|
|
|
import info.nightscout.androidaps.database.entities.TemporaryTarget
|
2021-03-31 00:18:55 +02:00
|
|
|
import info.nightscout.androidaps.database.entities.UserEntry.Action
|
|
|
|
import info.nightscout.androidaps.database.entities.UserEntry.Sources
|
2021-04-29 20:42:45 +02:00
|
|
|
import info.nightscout.androidaps.database.entities.ValueWithUnit
|
2021-10-04 22:40:02 +02:00
|
|
|
import info.nightscout.androidaps.database.transactions.InsertAndCancelCurrentTemporaryTargetTransaction
|
2021-01-21 16:38:42 +01:00
|
|
|
import info.nightscout.androidaps.databinding.DialogProfileswitchBinding
|
2021-10-04 22:40:02 +02:00
|
|
|
import info.nightscout.androidaps.extensions.toVisibility
|
2021-04-14 00:45:30 +02:00
|
|
|
import info.nightscout.androidaps.interfaces.ActivePlugin
|
2021-09-15 18:38:27 +02:00
|
|
|
import info.nightscout.androidaps.interfaces.Config
|
2021-10-04 22:40:02 +02:00
|
|
|
import info.nightscout.androidaps.interfaces.Profile
|
2020-05-07 09:54:36 +02:00
|
|
|
import info.nightscout.androidaps.interfaces.ProfileFunction
|
2021-12-10 15:19:19 +01:00
|
|
|
import info.nightscout.shared.logging.LTag
|
2021-02-09 17:57:28 +01:00
|
|
|
import info.nightscout.androidaps.logging.UserEntryLogger
|
2021-10-15 14:56:22 +02:00
|
|
|
import info.nightscout.androidaps.plugins.bus.RxBus
|
2021-10-04 22:40:02 +02:00
|
|
|
import info.nightscout.androidaps.utils.DefaultValueHelper
|
2021-09-15 18:38:27 +02:00
|
|
|
import info.nightscout.androidaps.utils.HardLimits
|
2019-12-18 20:59:33 +01:00
|
|
|
import info.nightscout.androidaps.utils.HtmlHelper
|
2021-10-25 18:09:53 +02:00
|
|
|
import info.nightscout.androidaps.utils.T
|
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
|
2022-02-10 19:11:58 +01:00
|
|
|
import io.reactivex.rxjava3.disposables.CompositeDisposable
|
|
|
|
import io.reactivex.rxjava3.kotlin.plusAssign
|
2019-12-18 20:59:33 +01:00
|
|
|
import java.text.DecimalFormat
|
|
|
|
import java.util.*
|
2021-10-04 22:40:02 +02:00
|
|
|
import java.util.concurrent.TimeUnit
|
2019-12-27 19:20:38 +01:00
|
|
|
import javax.inject.Inject
|
2021-11-02 09:19:56 +01:00
|
|
|
import kotlin.collections.ArrayList
|
2019-12-18 20:59:33 +01:00
|
|
|
|
|
|
|
class ProfileSwitchDialog : DialogFragmentWithDate() {
|
2021-01-21 16:38:42 +01:00
|
|
|
|
2021-11-04 10:56:12 +01:00
|
|
|
@Inject lateinit var rh: ResourceHelper
|
2019-12-31 00:37:36 +01:00
|
|
|
@Inject lateinit var profileFunction: ProfileFunction
|
2021-04-14 00:45:30 +02:00
|
|
|
@Inject lateinit var activePlugin: ActivePlugin
|
2021-04-29 20:42:45 +02:00
|
|
|
@Inject lateinit var repository: AppRepository
|
2021-02-09 17:57:28 +01:00
|
|
|
@Inject lateinit var uel: UserEntryLogger
|
2021-09-15 18:38:27 +02:00
|
|
|
@Inject lateinit var config: Config
|
|
|
|
@Inject lateinit var hardLimits: HardLimits
|
2021-10-15 14:56:22 +02:00
|
|
|
@Inject lateinit var rxBus: RxBus
|
2021-10-04 22:40:02 +02:00
|
|
|
@Inject lateinit var defaultValueHelper: DefaultValueHelper
|
2019-12-28 22:51:04 +01:00
|
|
|
|
2021-01-21 16:38:42 +01:00
|
|
|
private var profileIndex: Int? = null
|
|
|
|
|
2021-04-29 20:42:45 +02:00
|
|
|
private val disposable = CompositeDisposable()
|
|
|
|
|
2021-01-21 16:38:42 +01:00
|
|
|
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
|
|
|
|
2021-10-04 22:40:02 +02:00
|
|
|
private val textWatcher: TextWatcher = object : TextWatcher {
|
|
|
|
override fun afterTextChanged(s: Editable) {
|
|
|
|
val isDuration = binding.duration.value > 0
|
|
|
|
val isLowerPercentage = binding.percentage.value < 100
|
|
|
|
binding.ttLayout.visibility = (isDuration && isLowerPercentage).toVisibility()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
|
|
|
|
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-10-25 18:09:53 +02:00
|
|
|
override fun onCreateView(
|
|
|
|
inflater: LayoutInflater, container: ViewGroup?,
|
|
|
|
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-10-25 18:09:53 +02:00
|
|
|
binding.duration.setParams(
|
|
|
|
savedInstanceState?.getDouble("duration")
|
|
|
|
?: 0.0, 0.0, Constants.MAX_PROFILE_SWITCH_DURATION, 10.0, DecimalFormat("0"), false, binding.okcancel.ok,
|
|
|
|
textWatcher
|
|
|
|
)
|
|
|
|
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, textWatcher
|
|
|
|
)
|
|
|
|
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 ->
|
2021-04-14 22:58:21 +02:00
|
|
|
val profileStore = activePlugin.activeProfileSource.profile
|
2019-12-20 23:05:35 +01:00
|
|
|
?: return
|
2021-11-02 09:19:56 +01:00
|
|
|
val profileListToCheck = profileStore.getProfileList()
|
|
|
|
val profileList = ArrayList<CharSequence>()
|
|
|
|
for (profileName in profileListToCheck) {
|
|
|
|
val profileToCheck = activePlugin.activeProfileSource.profile?.getSpecificProfile(profileName.toString())
|
2021-11-04 10:56:12 +01:00
|
|
|
if (profileToCheck != null && ProfileSealed.Pure(profileToCheck).isValid("ProfileSwitch", activePlugin.activePump, config, rh, rxBus, hardLimits, false).isValid)
|
2021-11-02 09:19:56 +01:00
|
|
|
profileList.add(profileName)
|
|
|
|
}
|
|
|
|
if (profileList.isEmpty()) {
|
|
|
|
dismiss()
|
|
|
|
return
|
|
|
|
}
|
2019-12-18 20:59:33 +01:00
|
|
|
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)
|
2021-04-29 20:42:45 +02:00
|
|
|
if (profileList[p] == profileFunction.getOriginalProfileName())
|
2021-01-21 16:38:42 +01:00
|
|
|
binding.profile.setSelection(p)
|
2021-11-02 09:19:56 +01:00
|
|
|
}
|
2019-12-18 23:22:03 +01:00
|
|
|
|
2021-04-29 20:42:45 +02:00
|
|
|
profileFunction.getProfile()?.let { profile ->
|
2021-10-25 18:09:53 +02:00
|
|
|
if (profile is ProfileSealed.EPS)
|
|
|
|
if (profile.value.originalPercentage != 100 || profile.value.originalTimeshift != 0L) {
|
|
|
|
binding.reuselayout.visibility = View.VISIBLE
|
2021-11-04 10:56:12 +01:00
|
|
|
binding.reusebutton.text = rh.gs(R.string.reuse_profile_pct_hours, profile.value.originalPercentage, T.msecs(profile.value.originalTimeshift).hours().toInt())
|
2021-10-25 18:09:53 +02:00
|
|
|
binding.reusebutton.setOnClickListener {
|
|
|
|
binding.percentage.value = profile.value.originalPercentage.toDouble()
|
2022-02-24 15:26:54 +01:00
|
|
|
binding.timeshift.value = T.msecs(profile.value.originalTimeshift).hours().toDouble()
|
2021-10-25 18:09:53 +02:00
|
|
|
}
|
2019-12-18 23:22:03 +01:00
|
|
|
}
|
|
|
|
}
|
2021-10-04 22:40:02 +02:00
|
|
|
binding.ttLayout.visibility = View.GONE
|
2022-02-12 08:26:07 +01:00
|
|
|
binding.duration.editText?.id?.let { binding.durationLabel.labelFor = it }
|
|
|
|
binding.percentage.editText?.id?.let { binding.percentageLabel.labelFor = it }
|
|
|
|
binding.timeshift.editText?.id?.let { binding.timeshiftLabel.labelFor = it }
|
2019-12-18 20:59:33 +01:00
|
|
|
}
|
|
|
|
|
2021-01-21 16:38:42 +01:00
|
|
|
override fun onDestroyView() {
|
|
|
|
super.onDestroyView()
|
2021-04-29 20:42:45 +02:00
|
|
|
disposable.clear()
|
2021-01-21 16:38:42 +01:00
|
|
|
_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
|
2021-04-14 22:58:21 +02:00
|
|
|
val profileStore = activePlugin.activeProfileSource.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-11-23 19:53:45 +01:00
|
|
|
val duration = binding.duration.value.toInt()
|
2021-04-29 20:42:45 +02:00
|
|
|
if (duration > 0L)
|
2021-11-04 10:56:12 +01:00
|
|
|
actions.add(rh.gs(R.string.duration) + ": " + rh.gs(R.string.format_mins, duration))
|
2021-04-29 20:42:45 +02:00
|
|
|
val profileName = binding.profile.selectedItem.toString()
|
2021-11-04 10:56:12 +01:00
|
|
|
actions.add(rh.gs(R.string.profile) + ": " + profileName)
|
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)
|
2021-11-04 10:56:12 +01:00
|
|
|
actions.add(rh.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)
|
2021-11-04 10:56:12 +01:00
|
|
|
actions.add(rh.gs(R.string.careportal_newnstreatment_timeshift_label) + ": " + rh.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())
|
2021-11-04 10:56:12 +01:00
|
|
|
actions.add(rh.gs(R.string.notes_label) + ": " + notes)
|
2019-12-18 20:59:33 +01:00
|
|
|
if (eventTimeChanged)
|
2021-11-04 10:56:12 +01:00
|
|
|
actions.add(rh.gs(R.string.time) + ": " + dateUtil.dateAndTimeString(eventTime))
|
2019-12-18 20:59:33 +01:00
|
|
|
|
2021-10-04 22:40:02 +02:00
|
|
|
val isTT = binding.duration.value > 0 && binding.percentage.value < 100 && binding.tt.isChecked
|
|
|
|
val target = defaultValueHelper.determineActivityTT()
|
|
|
|
val units = profileFunction.getUnits()
|
|
|
|
if (isTT)
|
2021-11-04 10:56:12 +01:00
|
|
|
actions.add(rh.gs(R.string.careportal_temporarytarget) + ": " + rh.gs(R.string.activity))
|
2021-10-04 22:40:02 +02:00
|
|
|
|
2019-12-18 20:59:33 +01:00
|
|
|
activity?.let { activity ->
|
2021-10-31 17:44:04 +01:00
|
|
|
val ps = profileFunction.buildProfileSwitch(profileStore, profileName, duration, percent, timeShift, eventTime) ?: return@let
|
2021-11-04 10:56:12 +01:00
|
|
|
val validity = ProfileSealed.PS(ps).isValid(rh.gs(R.string.careportal_profileswitch), activePlugin.activePump, config, rh, rxBus, hardLimits, false)
|
2021-09-15 18:38:27 +02:00
|
|
|
if (validity.isValid)
|
2021-11-04 10:56:12 +01:00
|
|
|
OKDialog.showConfirmation(activity, rh.gs(R.string.careportal_profileswitch), HtmlHelper.fromHtml(Joiner.on("<br/>").join(actions)), {
|
2021-10-31 17:44:04 +01:00
|
|
|
if (profileFunction.createProfileSwitch(
|
2021-11-02 09:19:56 +01:00
|
|
|
profileStore,
|
|
|
|
profileName = profileName,
|
|
|
|
durationInMinutes = duration,
|
|
|
|
percentage = percent,
|
|
|
|
timeShiftInHours = timeShift,
|
|
|
|
timestamp = eventTime
|
|
|
|
)
|
|
|
|
) {
|
2021-10-31 17:44:04 +01:00
|
|
|
uel.log(Action.PROFILE_SWITCH,
|
|
|
|
Sources.ProfileSwitchDialog,
|
|
|
|
notes,
|
|
|
|
ValueWithUnit.Timestamp(eventTime).takeIf { eventTimeChanged },
|
|
|
|
ValueWithUnit.SimpleString(profileName),
|
|
|
|
ValueWithUnit.Percent(percent),
|
|
|
|
ValueWithUnit.Hour(timeShift).takeIf { timeShift != 0 },
|
|
|
|
ValueWithUnit.Minute(duration).takeIf { duration != 0 })
|
|
|
|
if (percent == 90 && duration == 10) sp.putBoolean(R.string.key_objectiveuseprofileswitch, true)
|
|
|
|
if (isTT) {
|
|
|
|
disposable += repository.runTransactionForResult(
|
|
|
|
InsertAndCancelCurrentTemporaryTargetTransaction(
|
|
|
|
timestamp = eventTime,
|
|
|
|
duration = TimeUnit.MINUTES.toMillis(duration.toLong()),
|
|
|
|
reason = TemporaryTarget.Reason.ACTIVITY,
|
|
|
|
lowTarget = Profile.toMgdl(target, profileFunction.getUnits()),
|
|
|
|
highTarget = Profile.toMgdl(target, profileFunction.getUnits())
|
|
|
|
)
|
|
|
|
).subscribe({ result ->
|
|
|
|
result.inserted.forEach { aapsLogger.debug(LTag.DATABASE, "Inserted temp target $it") }
|
|
|
|
result.updated.forEach { aapsLogger.debug(LTag.DATABASE, "Updated temp target $it") }
|
|
|
|
}, {
|
|
|
|
aapsLogger.error(LTag.DATABASE, "Error while saving temporary target", it)
|
|
|
|
})
|
|
|
|
uel.log(
|
|
|
|
Action.TT, Sources.TTDialog, ValueWithUnit.Timestamp(eventTime).takeIf { eventTimeChanged }, ValueWithUnit.TherapyEventTTReason(
|
|
|
|
TemporaryTarget.Reason.ACTIVITY
|
|
|
|
), ValueWithUnit.fromGlucoseUnit(target, units.asText), ValueWithUnit.Minute(duration)
|
2021-10-25 18:09:53 +02:00
|
|
|
)
|
2021-10-31 17:44:04 +01:00
|
|
|
}
|
2021-10-04 22:40:02 +02:00
|
|
|
}
|
2021-09-15 18:38:27 +02:00
|
|
|
})
|
|
|
|
else {
|
|
|
|
OKDialog.show(
|
|
|
|
activity,
|
2021-11-04 10:56:12 +01:00
|
|
|
rh.gs(R.string.careportal_profileswitch),
|
2021-09-15 18:38:27 +02:00
|
|
|
HtmlHelper.fromHtml(Joiner.on("<br/>").join(validity.reasons))
|
|
|
|
)
|
|
|
|
return false
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|