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
|
|
|
|
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
|
2020-01-02 18:25:49 +01:00
|
|
|
import info.nightscout.androidaps.db.Source
|
2019-12-31 00:37:36 +01:00
|
|
|
import info.nightscout.androidaps.plugins.configBuilder.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.OKDialog
|
|
|
|
import info.nightscout.androidaps.utils.Translator
|
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() {
|
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
|
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,
|
|
|
|
EXERCISE
|
2019-12-19 20:30:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private var options: EventType = EventType.BGCHECK
|
|
|
|
@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)
|
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)
|
|
|
|
|
|
|
|
actions_care_icon.setImageResource(when (options) {
|
2019-12-20 23:05:35 +01:00
|
|
|
EventType.BGCHECK -> R.drawable.icon_cp_bgcheck
|
|
|
|
EventType.SENSOR_INSERT -> R.drawable.icon_cp_cgm_insert
|
2019-12-19 20:30:35 +01:00
|
|
|
EventType.BATTERY_CHANGE -> R.drawable.icon_cp_pump_battery
|
2020-01-13 19:38:10 +01:00
|
|
|
EventType.NOTE -> R.drawable.icon_cp_note
|
|
|
|
EventType.EXERCISE -> R.drawable.icon_cp_exercise
|
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
|
2019-12-19 20:30:35 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
when (options) {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
val bg = Profile.fromMgdlToUnits(GlucoseStatus.getGlucoseStatusData()?.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)
|
|
|
|
if (options == EventType.NOTE)
|
|
|
|
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()
|
|
|
|
if (options == EventType.BGCHECK) {
|
|
|
|
val type =
|
2019-12-20 23:05:35 +01:00
|
|
|
when {
|
|
|
|
actions_care_meter.isChecked -> "Finger"
|
|
|
|
actions_care_sensor.isChecked -> "Sensor"
|
|
|
|
else -> "Manual"
|
|
|
|
}
|
2019-12-27 19:20:38 +01:00
|
|
|
actions.add(resourceHelper.gs(R.string.careportal_newnstreatment_glucosetype) + ": " + Translator.translate(type))
|
|
|
|
actions.add(resourceHelper.gs(R.string.treatments_wizard_bg_label) + ": " + Profile.toCurrentUnitsString(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) {
|
|
|
|
actions.add(MainApp.gs(R.string.careportal_newnstreatment_duration_label) + ": " + MainApp.gs(R.string.format_mins, actions_care_duration.value.toInt()))
|
|
|
|
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)
|
|
|
|
}
|
2020-01-02 18:25:49 +01:00
|
|
|
eventTime -= eventTime % 1000
|
|
|
|
|
2019-12-19 23:45:27 +01:00
|
|
|
if (eventTimeChanged)
|
2019-12-27 19:20:38 +01: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))
|
2020-01-02 18:25:49 +01:00
|
|
|
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
|
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-01-02 18:25:49 +01:00
|
|
|
val careportalEvent = CareportalEvent()
|
|
|
|
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-01-02 18:25:49 +01:00
|
|
|
}
|
|
|
|
careportalEvent.json = json.toString()
|
2020-02-05 15:58:29 +01:00
|
|
|
aapsLogger.debug("USER ENTRY: CAREPORTAL ${careportalEvent.eventType} json: ${careportalEvent.json}")
|
2020-01-02 18:25:49 +01:00
|
|
|
MainApp.getDbHelper().createOrUpdate(careportalEvent)
|
2019-12-19 20:30:35 +01: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
|
|
|
}
|
|
|
|
}
|