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

239 lines
11 KiB
Kotlin
Raw Normal View History

2019-12-20 18:55:54 +01:00
package info.nightscout.androidaps.dialogs
2019-12-19 20:30:35 +01:00
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.StringRes
import com.google.common.base.Joiner
2020-03-10 18:58:27 +01:00
import dagger.android.HasAndroidInjector
2019-12-19 20:30:35 +01:00
import info.nightscout.androidaps.Constants
import info.nightscout.androidaps.MainApp
import info.nightscout.androidaps.R
import info.nightscout.androidaps.data.Profile
2021-03-08 20:10:02 +01:00
import info.nightscout.androidaps.database.AppRepository
import info.nightscout.androidaps.database.entities.TherapyEvent
import info.nightscout.androidaps.database.transactions.InsertTherapyEventIfNewTransaction
import info.nightscout.androidaps.database.entities.UserEntry.*
2021-01-21 17:21:29 +01:00
import info.nightscout.androidaps.databinding.DialogCareBinding
2020-05-07 09:54:36 +02:00
import info.nightscout.androidaps.interfaces.ProfileFunction
2021-03-08 20:10:02 +01:00
import info.nightscout.androidaps.logging.LTag
2021-02-09 17:57:28 +01:00
import info.nightscout.androidaps.logging.UserEntryLogger
2019-12-19 20:30:35 +01:00
import info.nightscout.androidaps.plugins.general.nsclient.NSUpload
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatusProvider
2019-12-20 23:05:35 +01:00
import info.nightscout.androidaps.utils.HtmlHelper
2021-03-08 20:10:02 +01:00
import info.nightscout.androidaps.utils.T
2019-12-20 23:05:35 +01:00
import info.nightscout.androidaps.utils.Translator
2020-05-10 20:10:08 +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-03-08 20:10:02 +01:00
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.plusAssign
2019-12-19 20:30:35 +01:00
import java.text.DecimalFormat
import java.util.*
2019-12-27 19:20:38 +01:00
import javax.inject.Inject
2019-12-19 20:30:35 +01:00
class CareDialog : DialogFragmentWithDate() {
2021-01-21 17:21:29 +01:00
2020-03-10 18:58:27 +01:00
@Inject lateinit var injector: HasAndroidInjector
2019-12-30 00:53:44 +01:00
@Inject lateinit var mainApp: MainApp
@Inject lateinit var resourceHelper: ResourceHelper
2019-12-31 00:37:36 +01:00
@Inject lateinit var profileFunction: ProfileFunction
2020-05-09 10:52:20 +02:00
@Inject lateinit var nsUpload: NSUpload
2020-04-27 23:55:06 +02:00
@Inject lateinit var translator: Translator
2021-02-09 17:57:28 +01:00
@Inject lateinit var uel: UserEntryLogger
2021-03-08 20:10:02 +01:00
@Inject lateinit var repository: AppRepository
@Inject lateinit var glucoseStatusProvider: GlucoseStatusProvider
2021-03-08 20:10:02 +01:00
private val disposable = CompositeDisposable()
2019-12-27 19:20:38 +01:00
2019-12-19 20:30:35 +01:00
enum class EventType {
BGCHECK,
SENSOR_INSERT,
2020-01-13 19:38:10 +01:00
BATTERY_CHANGE,
NOTE,
2020-05-10 20:10:08 +02:00
EXERCISE,
QUESTION,
ANNOUNCEMENT
2019-12-19 20:30:35 +01:00
}
private var options: EventType = EventType.BGCHECK
private var valuesWithUnit = mutableListOf<ValueWithUnit>()
2020-05-10 20:10:08 +02:00
2019-12-19 20:30:35 +01:00
@StringRes
private var event: Int = R.string.none
fun setOptions(options: EventType, @StringRes event: Int): CareDialog {
this.options = options
this.event = event
return this
}
2021-01-21 17:21:29 +01:00
private var _binding: DialogCareBinding? = null
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!
2019-12-19 20:30:35 +01:00
override fun onSaveInstanceState(savedInstanceState: Bundle) {
super.onSaveInstanceState(savedInstanceState)
2021-01-21 17:21:29 +01:00
savedInstanceState.putDouble("bg", binding.bg.value)
savedInstanceState.putDouble("duration", binding.duration.value)
2020-03-04 22:51:50 +01:00
savedInstanceState.putInt("event", event)
savedInstanceState.putInt("options", options.ordinal)
2019-12-19 20:30:35 +01:00
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
2021-01-21 17:21:29 +01:00
savedInstanceState: Bundle?): View {
2019-12-20 23:05:35 +01:00
onCreateViewGeneral()
2021-01-21 17:21:29 +01:00
_binding = DialogCareBinding.inflate(inflater, container, false)
return binding.root
2019-12-19 20:30:35 +01:00
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
2020-03-04 22:51:50 +01:00
savedInstanceState?.let {
event = savedInstanceState.getInt("event", R.string.error)
options = EventType.values()[savedInstanceState.getInt("options", 0)]
}
2021-01-21 17:21:29 +01:00
binding.icon.setImageResource(when (options) {
2020-06-19 13:59:39 +02:00
EventType.BGCHECK -> R.drawable.ic_cp_bgcheck
EventType.SENSOR_INSERT -> R.drawable.ic_cp_cgm_insert
EventType.BATTERY_CHANGE -> R.drawable.ic_cp_pump_battery
EventType.NOTE -> R.drawable.ic_cp_note
EventType.EXERCISE -> R.drawable.ic_cp_exercise
EventType.QUESTION -> R.drawable.ic_cp_question
EventType.ANNOUNCEMENT -> R.drawable.ic_cp_announcement
2019-12-19 20:30:35 +01:00
})
2021-01-21 17:21:29 +01:00
binding.title.text = resourceHelper.gs(when (options) {
2019-12-20 23:05:35 +01:00
EventType.BGCHECK -> R.string.careportal_bgcheck
EventType.SENSOR_INSERT -> R.string.careportal_cgmsensorinsert
2019-12-19 20:30:35 +01:00
EventType.BATTERY_CHANGE -> R.string.careportal_pumpbatterychange
2020-01-13 19:38:10 +01:00
EventType.NOTE -> R.string.careportal_note
EventType.EXERCISE -> R.string.careportal_exercise
2020-05-10 20:10:08 +02:00
EventType.QUESTION -> R.string.careportal_question
EventType.ANNOUNCEMENT -> R.string.careportal_announcement
2019-12-19 20:30:35 +01:00
})
when (options) {
2020-05-10 20:10:08 +02:00
EventType.QUESTION,
EventType.ANNOUNCEMENT,
2021-03-08 20:10:02 +01:00
EventType.BGCHECK -> {
2021-01-21 17:21:29 +01:00
binding.durationLayout.visibility = View.GONE
2020-01-13 19:38:10 +01:00
}
2020-02-05 15:58:29 +01:00
2019-12-19 20:30:35 +01:00
EventType.SENSOR_INSERT,
EventType.BATTERY_CHANGE -> {
2021-01-21 17:21:29 +01:00
binding.bgLayout.visibility = View.GONE
binding.bgsource.visibility = View.GONE
binding.durationLayout.visibility = View.GONE
2019-12-19 20:30:35 +01:00
}
2020-02-05 15:58:29 +01:00
2020-01-13 19:38:10 +01:00
EventType.NOTE,
2021-03-08 20:10:02 +01:00
EventType.EXERCISE -> {
2021-01-21 17:21:29 +01:00
binding.bgLayout.visibility = View.GONE
binding.bgsource.visibility = View.GONE
2019-12-19 20:30:35 +01:00
}
}
val bg = Profile.fromMgdlToUnits(glucoseStatusProvider.glucoseStatusData?.glucose
2019-12-31 00:37:36 +01:00
?: 0.0, profileFunction.getUnits())
2019-12-19 20:30:35 +01:00
val bgTextWatcher: TextWatcher = object : TextWatcher {
override fun afterTextChanged(s: Editable) {}
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
2021-01-21 17:21:29 +01:00
if (binding.sensor.isChecked) binding.meter.isChecked = true
2019-12-19 20:30:35 +01:00
}
}
2019-12-31 00:37:36 +01:00
if (profileFunction.getUnits() == Constants.MMOL) {
2021-01-21 17:21:29 +01:00
binding.bgunits.text = resourceHelper.gs(R.string.mmol)
binding.bg.setParams(savedInstanceState?.getDouble("bg")
?: bg, 2.0, 30.0, 0.1, DecimalFormat("0.0"), false, binding.okcancel.ok, bgTextWatcher)
2019-12-22 02:05:50 +01:00
} else {
2021-01-21 17:21:29 +01:00
binding.bgunits.text = resourceHelper.gs(R.string.mgdl)
binding.bg.setParams(savedInstanceState?.getDouble("bg")
?: bg, 36.0, 500.0, 1.0, DecimalFormat("0"), false, binding.okcancel.ok, bgTextWatcher)
2019-12-22 02:05:50 +01:00
}
2021-01-21 17:21:29 +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)
if (options == EventType.NOTE || options == EventType.QUESTION || options == EventType.ANNOUNCEMENT || options == EventType.EXERCISE)
2021-01-21 17:21:29 +01:00
binding.notesLayout.root.visibility = View.VISIBLE // independent to preferences
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
2019-12-19 20:30:35 +01:00
}
2019-12-21 23:17:20 +01:00
override fun submit(): Boolean {
2021-03-08 20:10:02 +01:00
val enteredBy = sp.getString("careportal_enteredby", "AndroidAPS")
2019-12-31 00:37:36 +01:00
val unitResId = if (profileFunction.getUnits() == Constants.MGDL) R.string.mgdl else R.string.mmol
2019-12-19 20:30:35 +01:00
2021-03-08 20:10:02 +01:00
eventTime -= eventTime % 1000
val therapyEvent = TherapyEvent(
timestamp = eventTime,
type = when (options) {
EventType.BGCHECK -> TherapyEvent.Type.FINGER_STICK_BG_VALUE
EventType.SENSOR_INSERT -> TherapyEvent.Type.SENSOR_CHANGE
EventType.BATTERY_CHANGE -> TherapyEvent.Type.PUMP_BATTERY_CHANGE
EventType.NOTE -> TherapyEvent.Type.NOTE
EventType.EXERCISE -> TherapyEvent.Type.EXERCISE
EventType.QUESTION -> TherapyEvent.Type.QUESTION
EventType.ANNOUNCEMENT -> TherapyEvent.Type.ANNOUNCEMENT
},
units = profileFunction.getUnits()
)
2019-12-19 20:30:35 +01:00
val actions: LinkedList<String> = LinkedList()
2020-05-10 20:10:08 +02:00
if (options == EventType.BGCHECK || options == EventType.QUESTION || options == EventType.ANNOUNCEMENT) {
2021-03-08 20:10:02 +01:00
val meterType =
2019-12-20 23:05:35 +01:00
when {
2021-03-08 20:10:02 +01:00
binding.meter.isChecked -> TherapyEvent.MeterType.FINGER
binding.sensor.isChecked -> TherapyEvent.MeterType.SENSOR
else -> TherapyEvent.MeterType.MANUAL
2019-12-20 23:05:35 +01:00
}
2021-03-08 20:10:02 +01:00
actions.add(resourceHelper.gs(R.string.careportal_newnstreatment_glucosetype) + ": " + translator.translate(meterType.text))
2021-01-21 17:21:29 +01:00
actions.add(resourceHelper.gs(R.string.treatments_wizard_bg_label) + ": " + Profile.toCurrentUnitsString(profileFunction, binding.bg.value) + " " + resourceHelper.gs(unitResId))
2021-03-08 20:10:02 +01:00
therapyEvent.glucoseType = meterType
therapyEvent.glucose = binding.bg.value
valuesWithUnit.add(ValueWithUnit(binding.bg.value.toDouble(), profileFunction.getUnits()))
valuesWithUnit.add(ValueWithUnit(meterType, Units.CPEvent))
2019-12-19 20:30:35 +01:00
}
2020-01-13 19:38:10 +01:00
if (options == EventType.NOTE || options == EventType.EXERCISE) {
2021-01-21 17:21:29 +01:00
actions.add(resourceHelper.gs(R.string.careportal_newnstreatment_duration_label) + ": " + resourceHelper.gs(R.string.format_mins, binding.duration.value.toInt()))
2021-03-08 20:10:02 +01:00
therapyEvent.duration = T.mins(binding.duration.value.toLong()).msecs()
if (!binding.duration.value.equals(0.0)) valuesWithUnit.add(ValueWithUnit(binding.duration.value.toInt(), Units.M))
2020-01-13 19:38:10 +01:00
}
2021-01-21 17:21:29 +01:00
val notes = binding.notesLayout.notes.text.toString()
2019-12-19 20:30:35 +01:00
if (notes.isNotEmpty()) {
2021-02-22 18:04:30 +01:00
actions.add(resourceHelper.gs(R.string.notes_label) + ": " + notes)
2021-03-08 20:10:02 +01:00
therapyEvent.note = notes
2019-12-19 20:30:35 +01:00
}
if (eventTimeChanged) {
2020-05-07 23:40:59 +02:00
actions.add(resourceHelper.gs(R.string.time) + ": " + dateUtil.dateAndTimeString(eventTime))
valuesWithUnit.add(0, ValueWithUnit(eventTime, Units.Timestamp))
}
2019-12-19 20:30:35 +01:00
2021-03-08 20:10:02 +01:00
therapyEvent.enteredBy = enteredBy
2019-12-19 20:30:35 +01:00
activity?.let { activity ->
2021-01-21 17:21:29 +01:00
OKDialog.showConfirmation(activity, resourceHelper.gs(event), HtmlHelper.fromHtml(Joiner.on("<br/>").join(actions)), {
2021-03-08 20:10:02 +01:00
disposable += repository.runTransactionForResult(InsertTherapyEventIfNewTransaction(therapyEvent)).subscribe({ result ->
result.inserted.forEach { nsUpload.uploadEvent(it) }
}, {
aapsLogger.error(LTag.BGSOURCE, "Error while saving therapy event", it)
})
valuesWithUnit.add(if (eventTimeChanged) 1 else 0, ValueWithUnit(therapyEvent.type.text, Units.CPEvent))
uel.log(Action.CAREPORTAL, notes, valuesWithUnit)
2019-12-22 21:37:26 +01:00
}, null)
2019-12-19 20:30:35 +01:00
}
2019-12-21 23:17:20 +01:00
return true
2019-12-19 20:30:35 +01:00
}
}