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-10-04 22:40:02 +02:00
|
|
|
import info.nightscout.androidaps.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
|
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
|
2021-04-29 20:42:45 +02:00
|
|
|
import io.reactivex.disposables.CompositeDisposable
|
2021-10-04 22:40:02 +02:00
|
|
|
import io.reactivex.rxkotlin.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
|
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
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
2021-10-04 22:40:02 +02:00
|
|
|
?: 0.0, 0.0, Constants.MAX_PROFILE_SWITCH_DURATION, 10.0, DecimalFormat("0"), false, binding.okcancel.ok,
|
|
|
|
textWatcher)
|
2021-01-21 16:38:42 +01:00
|
|
|
binding.percentage.setParams(savedInstanceState?.getDouble("percentage")
|
2021-10-04 22:40:02 +02:00
|
|
|
?: 100.0, Constants.CPP_MIN_PERCENTAGE.toDouble(), Constants.CPP_MAX_PERCENTAGE.toDouble(), 5.0,
|
|
|
|
DecimalFormat("0"), false, binding.okcancel.ok, textWatcher)
|
2021-01-21 16:38:42 +01:00
|
|
|
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
|
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)
|
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)
|
2019-12-18 20:59:33 +01:00
|
|
|
} ?: return
|
2019-12-18 23:22:03 +01:00
|
|
|
|
2021-04-29 20:42:45 +02:00
|
|
|
profileFunction.getProfile()?.let { profile ->
|
|
|
|
if (profile.percentage != 100 || profile.timeshift != 0) {
|
2021-01-21 16:38:42 +01:00
|
|
|
binding.reuselayout.visibility = View.VISIBLE
|
2021-04-29 20:42:45 +02:00
|
|
|
binding.reusebutton.text = resourceHelper.gs(R.string.reuse_profile_pct_hours, profile.percentage, profile.timeshift)
|
2021-01-21 16:38:42 +01:00
|
|
|
binding.reusebutton.setOnClickListener {
|
2021-04-29 20:42:45 +02:00
|
|
|
binding.percentage.value = profile.percentage.toDouble()
|
|
|
|
binding.timeshift.value = profile.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
|
|
|
}
|
|
|
|
}
|
2021-10-04 22:40:02 +02:00
|
|
|
binding.ttLayout.visibility = View.GONE
|
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-01-21 16:38:42 +01:00
|
|
|
val duration = binding.duration.value?.toInt() ?: return false
|
2021-04-29 20:42:45 +02:00
|
|
|
if (duration > 0L)
|
2020-03-10 19:20:22 +01:00
|
|
|
actions.add(resourceHelper.gs(R.string.duration) + ": " + resourceHelper.gs(R.string.format_mins, duration))
|
2021-04-29 20:42:45 +02:00
|
|
|
val profileName = binding.profile.selectedItem.toString()
|
|
|
|
actions.add(resourceHelper.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)
|
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())
|
2021-02-22 18:04:30 +01:00
|
|
|
actions.add(resourceHelper.gs(R.string.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
|
|
|
|
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)
|
|
|
|
actions.add(resourceHelper.gs(R.string.careportal_temporarytarget) + ": " + resourceHelper.gs(R.string.activity))
|
|
|
|
|
2019-12-18 20:59:33 +01:00
|
|
|
activity?.let { activity ->
|
2021-09-15 18:38:27 +02:00
|
|
|
val ps = profileFunction.buildProfileSwitch(profileStore, profileName, duration, percent, timeShift, eventTime)
|
|
|
|
val validity = ProfileSealed.PS(ps).isValid(resourceHelper.gs(R.string.careportal_profileswitch), activePlugin.activePump, config, resourceHelper, rxBus, hardLimits)
|
|
|
|
if (validity.isValid)
|
|
|
|
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.careportal_profileswitch), HtmlHelper.fromHtml(Joiner.on("<br/>").join(actions)), {
|
|
|
|
profileFunction.createProfileSwitch(profileStore,
|
|
|
|
profileName = profileName,
|
|
|
|
durationInMinutes = duration,
|
|
|
|
percentage = percent,
|
|
|
|
timeShiftInHours = timeShift,
|
|
|
|
timestamp = eventTime)
|
|
|
|
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 })
|
2021-10-18 20:10:18 +02:00
|
|
|
if (percent == 90 && duration == 10) sp.putBoolean(R.string.key_objectiveuseprofileswitch, true)
|
2021-10-04 22:40:02 +02:00
|
|
|
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-09-15 18:38:27 +02:00
|
|
|
})
|
|
|
|
else {
|
|
|
|
OKDialog.show(
|
|
|
|
activity,
|
|
|
|
resourceHelper.gs(R.string.careportal_profileswitch),
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|