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
|
|
|
|
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions
|
|
|
|
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-27 19:20:38 +01:00
|
|
|
|
2019-12-19 20:30:35 +01:00
|
|
|
enum class EventType {
|
|
|
|
BGCHECK,
|
|
|
|
SENSOR_INSERT,
|
|
|
|
BATTERY_CHANGE
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
})
|
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
|
|
|
|
})
|
|
|
|
|
|
|
|
when (options) {
|
|
|
|
EventType.SENSOR_INSERT,
|
|
|
|
EventType.BATTERY_CHANGE -> {
|
|
|
|
action_care_bg_layout.visibility = View.GONE
|
|
|
|
actions_care_bgsource.visibility = View.GONE
|
|
|
|
}
|
2019-12-20 23:05:35 +01:00
|
|
|
|
|
|
|
else -> {
|
2019-12-19 20:30:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
val bg = Profile.fromMgdlToUnits(GlucoseStatus.getGlucoseStatusData()?.glucose
|
2019-12-20 23:05:35 +01:00
|
|
|
?: 0.0, ProfileFunctions.getSystemUnits())
|
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-22 02:05:50 +01:00
|
|
|
if (ProfileFunctions.getSystemUnits() == 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)
|
|
|
|
}
|
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-19 20:30:35 +01:00
|
|
|
val unitResId = if (ProfileFunctions.getSystemUnits() == Constants.MGDL) R.string.mgdl else R.string.mmol
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
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)
|
|
|
|
}
|
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))
|
|
|
|
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
|
|
|
|
})
|
|
|
|
json.put("units", ProfileFunctions.getSystemUnits())
|
|
|
|
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 {
|
2019-12-19 20:30:35 +01:00
|
|
|
MainApp.getDbHelper().createCareportalEventFromJsonIfNotExists(json)
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|