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-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-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-01-21 16:38:42 +01:00
|
|
|
import info.nightscout.androidaps.databinding.DialogProfileswitchBinding
|
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
|
2020-05-07 09:54:36 +02:00
|
|
|
import info.nightscout.androidaps.interfaces.ProfileFunction
|
2021-02-09 17:57:28 +01:00
|
|
|
import info.nightscout.androidaps.logging.UserEntryLogger
|
2021-09-15 18:38:27 +02:00
|
|
|
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
|
|
|
|
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
|
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
|
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
|
|
|
|
@Inject lateinit var rxBus: RxBusWrapper
|
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
|
|
|
|
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 ->
|
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
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
|
|
|
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 })
|
|
|
|
})
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|