diff --git a/app/src/main/java/info/nightscout/androidaps/dialogs/CalibrationDialog.kt b/app/src/main/java/info/nightscout/androidaps/dialogs/CalibrationDialog.kt index 5e2b29dd03..4c129c301c 100644 --- a/app/src/main/java/info/nightscout/androidaps/dialogs/CalibrationDialog.kt +++ b/app/src/main/java/info/nightscout/androidaps/dialogs/CalibrationDialog.kt @@ -10,10 +10,12 @@ import info.nightscout.androidaps.Constants import info.nightscout.androidaps.R import info.nightscout.androidaps.data.Profile import info.nightscout.androidaps.database.entities.UserEntry +import info.nightscout.androidaps.database.entities.UserEntry.* import info.nightscout.androidaps.databinding.DialogCalibrationBinding import info.nightscout.androidaps.interfaces.ProfileFunction import info.nightscout.androidaps.logging.UserEntryLogger import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus +import info.nightscout.androidaps.utils.extensions.fromStringKey import info.nightscout.androidaps.utils.HtmlHelper import info.nightscout.androidaps.utils.XdripCalibrations import info.nightscout.androidaps.utils.alertDialogs.OKDialog @@ -78,7 +80,7 @@ class CalibrationDialog : DialogFragmentWithDate() { if (bg > 0) { activity?.let { activity -> OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.overview_calibration), HtmlHelper.fromHtml(Joiner.on("
").join(actions)), { - uel.log(UserEntry.Action.CALIBRATION, d1 = bg) + uel.log(Action.CALIBRATION, d1 = ValueWithUnit(bg, if (units == Constants.MMOL) Units.Mg_Dl else Units.Mmol_L)) xdripCalibrations.sendIntent(bg) }) } diff --git a/core/src/main/java/info/nightscout/androidaps/logging/UserEntryLogger.kt b/core/src/main/java/info/nightscout/androidaps/logging/UserEntryLogger.kt index f2270ac57d..eb2dae411b 100644 --- a/core/src/main/java/info/nightscout/androidaps/logging/UserEntryLogger.kt +++ b/core/src/main/java/info/nightscout/androidaps/logging/UserEntryLogger.kt @@ -2,6 +2,7 @@ package info.nightscout.androidaps.logging import info.nightscout.androidaps.database.AppRepository import info.nightscout.androidaps.database.entities.UserEntry +import info.nightscout.androidaps.database.entities.UserEntry.* import info.nightscout.androidaps.database.transactions.UserEntryTransaction import info.nightscout.androidaps.utils.rx.AapsSchedulers import io.reactivex.disposables.CompositeDisposable @@ -19,7 +20,7 @@ class UserEntryLogger @Inject constructor( private val compositeDisposable = CompositeDisposable() - fun log(action: UserEntry.Action, s: String = "", d1: Double = 0.0, d2: Double = 0.0, i1: Int = 0, i2: Int = 0) { + fun log(action: UserEntry.Action, s: String = "", d1: ValueWithUnit = ValueWithUnit(0.0,Units.None), d2: ValueWithUnit = ValueWithUnit(0.0,Units.None), i1: ValueWithUnit = ValueWithUnit(0,Units.None), i2: ValueWithUnit = ValueWithUnit(0,Units.None)) { compositeDisposable += repository.runTransaction(UserEntryTransaction( action = action, s = s, @@ -31,8 +32,8 @@ class UserEntryLogger @Inject constructor( .subscribeOn(aapsSchedulers.io) .observeOn(aapsSchedulers.io) .subscribeBy( - onError = { aapsLogger.debug("ERRORED USER ENTRY: $action $s ${if (d1 == 0.0) d1 else ""} ${if (d2 == 0.0) d2 else ""} ${if (i1 == 0) i1 else ""} ${if (i2 == 0) i2 else ""}") }, - onComplete = { aapsLogger.debug("USER ENTRY: $action $s ${if (d1 == 0.0) d1 else ""} ${if (d2 == 0.0) d2 else ""} ${if (i1 == 0) i1 else ""} ${if (i2 == 0) i2 else ""}") } + onError = { aapsLogger.debug("ERRORED USER ENTRY: $action $s ${if (d1.dValue != 0.0) d1 else ""} ${if (d2.dValue != 0.0) d2 else ""} ${if (i1.iValue != 0) i1 else ""} ${if (i2.iValue != 0) i2 else ""}") }, + onComplete = { aapsLogger.debug("USER ENTRY: $action $s ${if (d1.dValue != 0.0) d1 else ""} ${if (d2.dValue != 0.0) d2 else ""} ${if (i1.iValue != 0) i1 else ""} ${if (i2.iValue != 0) i2 else ""}") } ) } } \ No newline at end of file diff --git a/core/src/main/java/info/nightscout/androidaps/utils/extensions/UnitsStrings.kt b/core/src/main/java/info/nightscout/androidaps/utils/extensions/UnitsStrings.kt new file mode 100644 index 0000000000..7d7d306044 --- /dev/null +++ b/core/src/main/java/info/nightscout/androidaps/utils/extensions/UnitsStrings.kt @@ -0,0 +1,36 @@ +package info.nightscout.androidaps.utils.extensions + +import info.nightscout.androidaps.Constants +import info.nightscout.androidaps.core.R +import info.nightscout.androidaps.database.entities.UserEntry +import info.nightscout.androidaps.utils.resources.ResourceHelper + +fun UserEntry.Units.stringId(): Int { + return when { + this == UserEntry.Units.Mg_Dl -> R.string.mgdl + this == UserEntry.Units.Mmol_L -> R.string.mmol + this == UserEntry.Units.U -> R.string.insulin_unit_shortname + this == UserEntry.Units.U_H -> R.string.profile_ins_units_per_hour + this == UserEntry.Units.G -> R.string.shortgram + this == UserEntry.Units.M -> R.string.shortminute + this == UserEntry.Units.H -> R.string.shorthour + this == UserEntry.Units.Percent -> R.string.percent + else -> 0 + } +} + +fun UserEntry.Units.stringkey(): String { + return when { + this == UserEntry.Units.Mg_Dl -> Constants.MGDL + this == UserEntry.Units.Mmol_L -> Constants.MMOL + this == UserEntry.Units.U -> UserEntry.Units.U.name + this == UserEntry.Units.U_H -> UserEntry.Units.U_H.name + this == UserEntry.Units.G -> UserEntry.Units.G.name + this == UserEntry.Units.M -> UserEntry.Units.M.name + this == UserEntry.Units.H -> UserEntry.Units.H.name + this == UserEntry.Units.Percent -> "%" + else -> "" + } +} + +fun UserEntry.Units.Companion(source: String?) = UserEntry.Units.values().firstOrNull { it.stringkey() == source } ?: UserEntry.Units.None \ No newline at end of file diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index 21c17997ba..04b3dc9e6e 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -92,6 +92,8 @@ Event type mg/dl mmol/l + g + %% Advanced Settings Bluetooth BT Watchdog diff --git a/database/src/main/java/info/nightscout/androidaps/database/Converters.kt b/database/src/main/java/info/nightscout/androidaps/database/Converters.kt index 29bd3db50c..5cfb99a71c 100644 --- a/database/src/main/java/info/nightscout/androidaps/database/Converters.kt +++ b/database/src/main/java/info/nightscout/androidaps/database/Converters.kt @@ -16,6 +16,23 @@ class Converters { @TypeConverter fun toAction(action: String?) = action?.let { UserEntry.Action.fromString(it) } + @TypeConverter + fun fromValueWithUnit(valueWithUnit: UserEntry.ValueWithUnit?): String? { + if (valueWithUnit == null) return null + val jsonObject = JSONObject() + jsonObject.put("dValue", valueWithUnit.dValue) + jsonObject.put("iValue", valueWithUnit.iValue) + jsonObject.put("unit", valueWithUnit.unit.name) + return jsonObject.toString() + } + + @TypeConverter + fun toValueWithUnit(jsonString: String?): UserEntry.ValueWithUnit? { + if (jsonString == null) return null + val jsonObject = JSONObject(jsonString) + return UserEntry.ValueWithUnit(jsonObject.getDouble("dValue"), jsonObject.getInt("iValue"),UserEntry.Units.fromString(jsonObject.getString("unit")) ) + } + @TypeConverter fun fromBolusType(bolusType: Bolus.Type?) = bolusType?.name diff --git a/database/src/main/java/info/nightscout/androidaps/database/DatabaseModule.kt b/database/src/main/java/info/nightscout/androidaps/database/DatabaseModule.kt index b36e16ff5d..32dbba2511 100644 --- a/database/src/main/java/info/nightscout/androidaps/database/DatabaseModule.kt +++ b/database/src/main/java/info/nightscout/androidaps/database/DatabaseModule.kt @@ -33,4 +33,10 @@ open class DatabaseModule { database.execSQL("CREATE TABLE IF NOT EXISTS userEntry (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `timestamp` INTEGER NOT NULL, `utcOffset` INTEGER NOT NULL, `action` TEXT NOT NULL, `s` TEXT NOT NULL, `d1` REAL NOT NULL, `d2` REAL NOT NULL, `i1` INTEGER NOT NULL, `i2` INTEGER NOT NULL)") } } + + private val migration3to4 = object : Migration(3, 4) { + override fun migrate(database: SupportSQLiteDatabase) { + database.execSQL("CREATE TABLE IF NOT EXISTS userEntry (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `timestamp` INTEGER NOT NULL, `utcOffset` INTEGER NOT NULL, `action` TEXT NOT NULL, `s` TEXT NOT NULL, `d1` TEXT NOT NULL, `d2` TEXT NOT NULL, `i1` TEXT NOT NULL, `i2` TEXT NOT NULL)") + } + } } \ No newline at end of file diff --git a/database/src/main/java/info/nightscout/androidaps/database/entities/UserEntry.kt b/database/src/main/java/info/nightscout/androidaps/database/entities/UserEntry.kt index 2ee5f03c7d..84c2649b05 100644 --- a/database/src/main/java/info/nightscout/androidaps/database/entities/UserEntry.kt +++ b/database/src/main/java/info/nightscout/androidaps/database/entities/UserEntry.kt @@ -17,10 +17,10 @@ data class UserEntry( override var utcOffset: Long = TimeZone.getDefault().getOffset(timestamp).toLong(), var action: Action, var s: String, - var d1: Double, - var d2: Double, - var i1: Int, - var i2: Int + var d1: ValueWithUnit, + var d2: ValueWithUnit, + var i1: ValueWithUnit, + var i2: ValueWithUnit ) : DBEntry, DBEntryWithTime { enum class Action () { @SerializedName("BOLUS") BOLUS, @@ -123,4 +123,26 @@ data class UserEntry( fun fromString(source: String?) = UserEntry.Action.values().firstOrNull { it.name == source } ?: UserEntry.Action.UNKNOWN } } + data class ValueWithUnit (val dValue: Double, val iValue: Int, val unit: Units) { + constructor(value:Int, unit:Units) : this(0.0, value, unit) + constructor(value:Double, unit:Units) : this(value,0, unit) + + + } + enum class Units { + @SerializedName("None") None, + @SerializedName("Mg_Dl") Mg_Dl, + @SerializedName("Mmol_L") Mmol_L, + @SerializedName("U") U, + @SerializedName("U_H") U_H, + @SerializedName("G") G, + @SerializedName("M") M, + @SerializedName("H") H, + @SerializedName("Percent") Percent + ; + + companion object { + fun fromString(unit: String?) = UserEntry.Units.values().firstOrNull { it.name == unit } ?: UserEntry.Units.None + } + } } \ No newline at end of file diff --git a/database/src/main/java/info/nightscout/androidaps/database/transactions/UserEntryTransaction.kt b/database/src/main/java/info/nightscout/androidaps/database/transactions/UserEntryTransaction.kt index c4d4ce2755..0b633d013b 100644 --- a/database/src/main/java/info/nightscout/androidaps/database/transactions/UserEntryTransaction.kt +++ b/database/src/main/java/info/nightscout/androidaps/database/transactions/UserEntryTransaction.kt @@ -1,14 +1,15 @@ package info.nightscout.androidaps.database.transactions import info.nightscout.androidaps.database.entities.UserEntry +import info.nightscout.androidaps.database.entities.UserEntry.* class UserEntryTransaction( - val action: UserEntry.Action, + val action: Action, val s: String = "", - val d1: Double = 0.0, - val d2: Double = 0.0, - val i1: Int = 0, - val i2: Int = 0, + val d1: ValueWithUnit = ValueWithUnit(0.0, Units.None), + val d2: ValueWithUnit = ValueWithUnit(0.0, Units.None), + val i1: ValueWithUnit = ValueWithUnit(0, Units.None), + val i2: ValueWithUnit = ValueWithUnit(0, Units.None) ) : Transaction() { override fun run() {