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

220 lines
10 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
import info.nightscout.androidaps.db.CareportalEvent
import info.nightscout.androidaps.db.Source
2020-05-07 09:54:36 +02:00
import info.nightscout.androidaps.interfaces.ProfileFunction
2019-12-19 20:30:35 +01:00
import info.nightscout.androidaps.plugins.general.nsclient.NSUpload
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus
2019-12-20 23:05:35 +01:00
import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.HtmlHelper
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
2019-12-20 18:55:54 +01:00
import kotlinx.android.synthetic.main.dialog_care.*
2019-12-19 23:45:27 +01:00
import kotlinx.android.synthetic.main.notes.*
2019-12-20 23:05:35 +01:00
import kotlinx.android.synthetic.main.okcancel.*
2019-12-19 20:30:35 +01:00
import org.json.JSONObject
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() {
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
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
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
}
override fun onSaveInstanceState(savedInstanceState: Bundle) {
super.onSaveInstanceState(savedInstanceState)
savedInstanceState.putDouble("actions_care_bg", actions_care_bg.value)
2020-01-13 19:38:10 +01:00
savedInstanceState.putDouble("actions_care_duration", actions_care_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?,
savedInstanceState: Bundle?): View? {
2019-12-20 23:05:35 +01:00
onCreateViewGeneral()
2019-12-20 18:55:54 +01:00
return inflater.inflate(R.layout.dialog_care, container, false)
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)]
}
2019-12-19 20:30:35 +01:00
actions_care_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
})
2019-12-27 19:20:38 +01:00
actions_care_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,
2020-01-13 19:38:10 +01:00
EventType.BGCHECK -> {
action_care_duration_layout.visibility = View.GONE
}
2020-02-05 15:58:29 +01:00
2019-12-19 20:30:35 +01:00
EventType.SENSOR_INSERT,
EventType.BATTERY_CHANGE -> {
action_care_bg_layout.visibility = View.GONE
actions_care_bgsource.visibility = View.GONE
2020-01-13 19:38:10 +01:00
action_care_duration_layout.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,
2020-02-05 15:58:29 +01:00
EventType.EXERCISE -> {
2020-01-13 19:38:10 +01:00
action_care_bg_layout.visibility = View.GONE
actions_care_bgsource.visibility = View.GONE
2019-12-19 20:30:35 +01:00
}
}
2020-12-06 15:09:32 +01:00
val bg = Profile.fromMgdlToUnits(GlucoseStatus(injector).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) {
if (actions_care_sensor.isChecked) actions_care_meter.isChecked = true
}
}
2019-12-31 00:37:36 +01:00
if (profileFunction.getUnits() == Constants.MMOL) {
2019-12-27 19:20:38 +01:00
actions_care_bgunits.text = resourceHelper.gs(R.string.mmol)
2019-12-19 20:30:35 +01:00
actions_care_bg.setParams(savedInstanceState?.getDouble("actions_care_bg")
2019-12-22 02:05:50 +01:00
?: bg, 2.0, 30.0, 0.1, DecimalFormat("0.0"), false, ok, bgTextWatcher)
} else {
2019-12-27 19:20:38 +01:00
actions_care_bgunits.text = resourceHelper.gs(R.string.mgdl)
2019-12-19 20:30:35 +01:00
actions_care_bg.setParams(savedInstanceState?.getDouble("actions_care_bg")
2019-12-22 02:05:50 +01:00
?: bg, 36.0, 500.0, 1.0, DecimalFormat("0"), false, ok, bgTextWatcher)
}
2020-01-13 19:38:10 +01:00
actions_care_duration.setParams(savedInstanceState?.getDouble("actions_care_duration")
?: 0.0, 0.0, Constants.MAX_PROFILE_SWITCH_DURATION, 10.0, DecimalFormat("0"), false, ok)
2020-05-10 20:10:08 +02:00
if (options == EventType.NOTE || options == EventType.QUESTION || options == EventType.ANNOUNCEMENT)
2020-01-13 19:38:10 +01:00
notes_layout?.visibility = View.VISIBLE // independent to preferences
2019-12-19 20:30:35 +01:00
}
2019-12-21 23:17:20 +01:00
override fun submit(): Boolean {
2019-12-30 00:53:44 +01:00
val enteredBy = sp.getString("careportal_enteredby", "")
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
val json = JSONObject()
val actions: LinkedList<String> = LinkedList()
2020-05-10 20:10:08 +02:00
if (options == EventType.BGCHECK || options == EventType.QUESTION || options == EventType.ANNOUNCEMENT) {
2019-12-19 20:30:35 +01:00
val type =
2019-12-20 23:05:35 +01:00
when {
actions_care_meter.isChecked -> CareportalEvent.FINGER
actions_care_sensor.isChecked -> CareportalEvent.SENSOR
else -> CareportalEvent.MANUAL
2019-12-20 23:05:35 +01:00
}
2020-04-27 23:55:06 +02:00
actions.add(resourceHelper.gs(R.string.careportal_newnstreatment_glucosetype) + ": " + translator.translate(type))
2020-03-10 18:58:27 +01:00
actions.add(resourceHelper.gs(R.string.treatments_wizard_bg_label) + ": " + Profile.toCurrentUnitsString(profileFunction, actions_care_bg.value) + " " + resourceHelper.gs(unitResId))
2019-12-19 20:30:35 +01:00
json.put("glucose", actions_care_bg.value)
json.put("glucoseType", type)
}
2020-01-13 19:38:10 +01:00
if (options == EventType.NOTE || options == EventType.EXERCISE) {
2020-02-05 17:40:13 +01:00
actions.add(resourceHelper.gs(R.string.careportal_newnstreatment_duration_label) + ": " + resourceHelper.gs(R.string.format_mins, actions_care_duration.value.toInt()))
2020-01-13 19:38:10 +01:00
json.put("duration", actions_care_duration.value.toInt())
}
2019-12-19 23:45:27 +01:00
val notes = notes.text.toString()
2019-12-19 20:30:35 +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-19 20:30:35 +01:00
json.put("notes", notes)
}
eventTime -= eventTime % 1000
2019-12-19 23:45:27 +01:00
if (eventTimeChanged)
2020-05-07 23:40:59 +02:00
actions.add(resourceHelper.gs(R.string.time) + ": " + dateUtil.dateAndTimeString(eventTime))
2019-12-19 20:30:35 +01:00
json.put("created_at", DateUtil.toISOString(eventTime))
json.put("mills", eventTime)
2019-12-19 20:30:35 +01:00
json.put("eventType", when (options) {
2019-12-20 23:05:35 +01:00
EventType.BGCHECK -> CareportalEvent.BGCHECK
EventType.SENSOR_INSERT -> CareportalEvent.SENSORCHANGE
2019-12-19 20:30:35 +01:00
EventType.BATTERY_CHANGE -> CareportalEvent.PUMPBATTERYCHANGE
2020-01-13 19:38:10 +01:00
EventType.NOTE -> CareportalEvent.NOTE
EventType.EXERCISE -> CareportalEvent.EXERCISE
2020-05-10 20:10:08 +02:00
EventType.QUESTION -> CareportalEvent.QUESTION
EventType.ANNOUNCEMENT -> CareportalEvent.ANNOUNCEMENT
2019-12-19 20:30:35 +01:00
})
2019-12-31 00:37:36 +01:00
json.put("units", profileFunction.getUnits())
2019-12-19 20:30:35 +01:00
if (enteredBy.isNotEmpty())
json.put("enteredBy", enteredBy)
activity?.let { activity ->
2019-12-27 19:20:38 +01:00
OKDialog.showConfirmation(activity, resourceHelper.gs(event), HtmlHelper.fromHtml(Joiner.on("<br/>").join(actions)), Runnable {
2020-04-24 22:39:56 +02:00
val careportalEvent = CareportalEvent(injector)
careportalEvent.date = eventTime
careportalEvent.source = Source.USER
careportalEvent.eventType = when (options) {
EventType.BGCHECK -> CareportalEvent.BGCHECK
EventType.SENSOR_INSERT -> CareportalEvent.SENSORCHANGE
EventType.BATTERY_CHANGE -> CareportalEvent.PUMPBATTERYCHANGE
2020-01-13 19:38:10 +01:00
EventType.NOTE -> CareportalEvent.NOTE
EventType.EXERCISE -> CareportalEvent.EXERCISE
2020-05-10 20:10:08 +02:00
EventType.QUESTION -> CareportalEvent.QUESTION
EventType.ANNOUNCEMENT -> CareportalEvent.ANNOUNCEMENT
}
careportalEvent.json = json.toString()
2020-02-05 15:58:29 +01:00
aapsLogger.debug("USER ENTRY: CAREPORTAL ${careportalEvent.eventType} json: ${careportalEvent.json}")
MainApp.getDbHelper().createOrUpdate(careportalEvent)
2020-05-09 10:52:20 +02:00
nsUpload.uploadCareportalEntryToNS(json)
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
}
}