NSC: better select matching records when switching between v1 and v3 plugin

This commit is contained in:
Milos Kozak 2023-09-23 15:27:42 +02:00
parent f42bc33e6d
commit 8c6157b8f6
12 changed files with 74 additions and 21 deletions

View file

@ -233,6 +233,7 @@ class NSClientFragment : DaggerFragment(), MenuProvider, PluginFragment {
}
private fun updateLog() {
_binding?.recyclerview?.recycledViewPool?.clear()
_binding?.recyclerview?.swapAdapter(RecyclerViewAdapter(nsClientPlugin?.listLog ?: arrayListOf()), true)
}

View file

@ -21,9 +21,14 @@ fun BolusCalculatorResult.toJson(isAdd: Boolean, dateUtil: DateUtil, profileUtil
.also { if (isAdd && interfaceIDs.nightscoutId != null) it.put("_id", interfaceIDs.nightscoutId) }
fun BolusCalculatorResult.Companion.fromJson(jsonObject: JSONObject): BolusCalculatorResult? {
val timestamp = JsonHelper.safeGetLongAllowNull(jsonObject, "mills", null) ?: return null
val timestamp =
JsonHelper.safeGetLongAllowNull(jsonObject, "mills", null)
?: JsonHelper.safeGetLongAllowNull(jsonObject, "date", null)
?: return null
val isValid = JsonHelper.safeGetBoolean(jsonObject, "isValid", true)
val id = JsonHelper.safeGetStringAllowNull(jsonObject, "_id", null) ?: return null
val id = JsonHelper.safeGetStringAllowNull(jsonObject, "identifier", null)
?: JsonHelper.safeGetStringAllowNull(jsonObject, "_id", null)
?: return null
val bcrString = JsonHelper.safeGetStringAllowNull(jsonObject, "bolusCalculatorResult", null) ?: return null
if (timestamp == 0L) return null

View file

@ -26,12 +26,17 @@ fun Bolus.toJson(isAdd: Boolean, dateUtil: DateUtil): JSONObject =
}
fun Bolus.Companion.fromJson(jsonObject: JSONObject): Bolus? {
val timestamp = JsonHelper.safeGetLongAllowNull(jsonObject, "mills", null) ?: return null
val timestamp =
JsonHelper.safeGetLongAllowNull(jsonObject, "mills", null)
?: JsonHelper.safeGetLongAllowNull(jsonObject, "date", null)
?: return null
val amount = JsonHelper.safeGetDoubleAllowNull(jsonObject, "insulin") ?: return null
val type = Bolus.Type.fromString(JsonHelper.safeGetString(jsonObject, "type"))
val isValid = JsonHelper.safeGetBoolean(jsonObject, "isValid", true)
val notes = JsonHelper.safeGetStringAllowNull(jsonObject, "notes", null)
val id = JsonHelper.safeGetStringAllowNull(jsonObject, "_id", null) ?: return null
val id = JsonHelper.safeGetStringAllowNull(jsonObject, "identifier", null)
?: JsonHelper.safeGetStringAllowNull(jsonObject, "_id", null)
?: return null
val pumpId = JsonHelper.safeGetLongAllowNull(jsonObject, "pumpId", null)
val pumpType = InterfaceIDs.PumpType.fromString(JsonHelper.safeGetStringAllowNull(jsonObject, "pumpType", null))
val pumpSerial = JsonHelper.safeGetStringAllowNull(jsonObject, "pumpSerial", null)

View file

@ -23,12 +23,17 @@ fun Carbs.toJson(isAdd: Boolean, dateUtil: DateUtil): JSONObject =
}
fun Carbs.Companion.fromJson(jsonObject: JSONObject): Carbs? {
val timestamp = JsonHelper.safeGetLongAllowNull(jsonObject, "mills", null) ?: return null
val timestamp =
JsonHelper.safeGetLongAllowNull(jsonObject, "mills", null)
?: JsonHelper.safeGetLongAllowNull(jsonObject, "date", null)
?: return null
val duration = JsonHelper.safeGetLong(jsonObject, "duration")
val amount = JsonHelper.safeGetDoubleAllowNull(jsonObject, "carbs") ?: return null
val notes = JsonHelper.safeGetStringAllowNull(jsonObject, "notes", null)
val isValid = JsonHelper.safeGetBoolean(jsonObject, "isValid", true)
val id = JsonHelper.safeGetStringAllowNull(jsonObject, "_id", null) ?: return null
val id = JsonHelper.safeGetStringAllowNull(jsonObject, "identifier", null)
?: JsonHelper.safeGetStringAllowNull(jsonObject, "_id", null)
?: return null
val pumpId = JsonHelper.safeGetLongAllowNull(jsonObject, "pumpId", null)
val pumpType = InterfaceIDs.PumpType.fromString(JsonHelper.safeGetStringAllowNull(jsonObject, "pumpType", null))
val pumpSerial = JsonHelper.safeGetStringAllowNull(jsonObject, "pumpSerial", null)

View file

@ -31,13 +31,18 @@ fun EffectiveProfileSwitch.toJson(isAdd: Boolean, dateUtil: DateUtil): JSONObjec
}
fun EffectiveProfileSwitch.Companion.fromJson(jsonObject: JSONObject, dateUtil: DateUtil): EffectiveProfileSwitch? {
val timestamp = JsonHelper.safeGetLongAllowNull(jsonObject, "mills", null) ?: return null
val timestamp =
JsonHelper.safeGetLongAllowNull(jsonObject, "mills", null)
?: JsonHelper.safeGetLongAllowNull(jsonObject, "date", null)
?: return null
val originalTimeshift = JsonHelper.safeGetLong(jsonObject, "originalTimeshift")
val originalDuration = JsonHelper.safeGetLong(jsonObject, "originalDuration")
val originalEnd = JsonHelper.safeGetLong(jsonObject, "originalEnd")
val originalPercentage = JsonHelper.safeGetInt(jsonObject, "originalPercentage", 100)
val isValid = JsonHelper.safeGetBoolean(jsonObject, "isValid", true)
val id = JsonHelper.safeGetStringAllowNull(jsonObject, "_id", null)
val id = JsonHelper.safeGetStringAllowNull(jsonObject, "identifier", null)
?: JsonHelper.safeGetStringAllowNull(jsonObject, "_id", null)
?: return null
val originalProfileName = JsonHelper.safeGetStringAllowNull(jsonObject, "originalProfileName", null) ?: return null
val originalCustomizedName = JsonHelper.safeGetStringAllowNull(jsonObject, "originalCustomizedName", null) ?: return null
val profileJson = JsonHelper.safeGetStringAllowNull(jsonObject, "profileJson", null) ?: return null

View file

@ -40,7 +40,10 @@ fun ExtendedBolus.toRealJson(isAdd: Boolean, dateUtil: DateUtil): JSONObject =
}
fun ExtendedBolus.Companion.extendedBolusFromJson(jsonObject: JSONObject): ExtendedBolus? {
val timestamp = JsonHelper.safeGetLongAllowNull(jsonObject, "mills", null) ?: return null
val timestamp =
JsonHelper.safeGetLongAllowNull(jsonObject, "mills", null)
?: JsonHelper.safeGetLongAllowNull(jsonObject, "date", null)
?: return null
if (JsonHelper.safeGetIntAllowNull(jsonObject, "splitNow") != 0) return null
if (JsonHelper.safeGetIntAllowNull(jsonObject, "splitExt") != 100) return null
val amount = JsonHelper.safeGetDoubleAllowNull(jsonObject, "enteredinsulin") ?: return null
@ -48,7 +51,9 @@ fun ExtendedBolus.Companion.extendedBolusFromJson(jsonObject: JSONObject): Exten
val durationInMilliseconds = JsonHelper.safeGetLongAllowNull(jsonObject, "durationInMilliseconds")
val isValid = JsonHelper.safeGetBoolean(jsonObject, "isValid", true)
val isEmulatingTempBasal = JsonHelper.safeGetBoolean(jsonObject, "isEmulatingTempBasal", false)
val id = JsonHelper.safeGetStringAllowNull(jsonObject, "_id", null) ?: return null
val id = JsonHelper.safeGetStringAllowNull(jsonObject, "identifier", null)
?: JsonHelper.safeGetStringAllowNull(jsonObject, "_id", null)
?: return null
val pumpId = JsonHelper.safeGetLongAllowNull(jsonObject, "pumpId", null)
val endPumpId = JsonHelper.safeGetLongAllowNull(jsonObject, "endId", null)
val pumpType = InterfaceIDs.PumpType.fromString(JsonHelper.safeGetStringAllowNull(jsonObject, "pumpType", null))

View file

@ -16,7 +16,9 @@ fun Food.Companion.fromJson(jsonObject: JSONObject): Food? {
val energy = JsonHelper.safeGetIntAllowNull(jsonObject, "energy")
val protein = JsonHelper.safeGetIntAllowNull(jsonObject, "protein")
val fat = JsonHelper.safeGetIntAllowNull(jsonObject, "fat")
val id = JsonHelper.safeGetStringAllowNull(jsonObject, "_id", null) ?: return null
val id = JsonHelper.safeGetStringAllowNull(jsonObject, "identifier", null)
?: JsonHelper.safeGetStringAllowNull(jsonObject, "_id", null)
?: return null
val isValid = JsonHelper.safeGetBoolean(jsonObject, "isValid", true)
val food = Food(

View file

@ -37,11 +37,16 @@ fun OfflineEvent.toJson(isAdd: Boolean, dateUtil: DateUtil): JSONObject =
}
*/
fun OfflineEvent.Companion.fromJson(jsonObject: JSONObject): OfflineEvent? {
val timestamp = JsonHelper.safeGetLongAllowNull(jsonObject, "mills", null) ?: return null
val timestamp =
JsonHelper.safeGetLongAllowNull(jsonObject, "mills", null)
?: JsonHelper.safeGetLongAllowNull(jsonObject, "date", null)
?: return null
val duration = JsonHelper.safeGetLong(jsonObject, "duration")
val durationInMilliseconds = JsonHelper.safeGetLongAllowNull(jsonObject, "durationInMilliseconds")
val isValid = JsonHelper.safeGetBoolean(jsonObject, "isValid", true)
val id = JsonHelper.safeGetStringAllowNull(jsonObject, "_id", null)
val id = JsonHelper.safeGetStringAllowNull(jsonObject, "identifier", null)
?: JsonHelper.safeGetStringAllowNull(jsonObject, "_id", null)
?: return null
val pumpId = JsonHelper.safeGetLongAllowNull(jsonObject, "pumpId", null)
val pumpType = InterfaceIDs.PumpType.fromString(JsonHelper.safeGetStringAllowNull(jsonObject, "pumpType", null))
val pumpSerial = JsonHelper.safeGetStringAllowNull(jsonObject, "pumpSerial", null)

View file

@ -52,13 +52,18 @@ fun ProfileSwitch.toJson(isAdd: Boolean, dateUtil: DateUtil, decimalFormatter: D
}
*/
fun ProfileSwitch.Companion.fromJson(jsonObject: JSONObject, dateUtil: DateUtil, activePlugin: ActivePlugin): ProfileSwitch? {
val timestamp = JsonHelper.safeGetLongAllowNull(jsonObject, "mills", null) ?: return null
val timestamp =
JsonHelper.safeGetLongAllowNull(jsonObject, "mills", null)
?: JsonHelper.safeGetLongAllowNull(jsonObject, "date", null)
?: return null
val duration = JsonHelper.safeGetLong(jsonObject, "duration")
val originalDuration = JsonHelper.safeGetLongAllowNull(jsonObject, "originalDuration")
val timeshift = JsonHelper.safeGetLong(jsonObject, "timeshift")
val percentage = JsonHelper.safeGetInt(jsonObject, "percentage", 100)
val isValid = JsonHelper.safeGetBoolean(jsonObject, "isValid", true)
val id = JsonHelper.safeGetStringAllowNull(jsonObject, "_id", null)
val id = JsonHelper.safeGetStringAllowNull(jsonObject, "identifier", null)
?: JsonHelper.safeGetStringAllowNull(jsonObject, "_id", null)
?: return null
val profileName = JsonHelper.safeGetStringAllowNull(jsonObject, "profile", null) ?: return null
val originalProfileName = JsonHelper.safeGetStringAllowNull(jsonObject, "originalProfileName", null)
val profileJson = JsonHelper.safeGetStringAllowNull(jsonObject, "profileJson", null)

View file

@ -33,14 +33,19 @@ fun TemporaryBasal.toJson(isAdd: Boolean, profile: Profile?, dateUtil: DateUtil)
}
fun TemporaryBasal.Companion.temporaryBasalFromJson(jsonObject: JSONObject): TemporaryBasal? {
val timestamp = JsonHelper.safeGetLongAllowNull(jsonObject, "mills", null) ?: return null
val timestamp =
JsonHelper.safeGetLongAllowNull(jsonObject, "mills", null)
?: JsonHelper.safeGetLongAllowNull(jsonObject, "date", null)
?: return null
val percent = JsonHelper.safeGetDoubleAllowNull(jsonObject, "percent")
val absolute = JsonHelper.safeGetDoubleAllowNull(jsonObject, "absolute")
val duration = JsonHelper.safeGetLongAllowNull(jsonObject, "duration") ?: return null
val durationInMilliseconds = JsonHelper.safeGetLongAllowNull(jsonObject, "durationInMilliseconds")
val type = fromString(JsonHelper.safeGetString(jsonObject, "type"))
val isValid = JsonHelper.safeGetBoolean(jsonObject, "isValid", true)
val id = JsonHelper.safeGetStringAllowNull(jsonObject, "_id", null) ?: return null
val id = JsonHelper.safeGetStringAllowNull(jsonObject, "identifier", null)
?: JsonHelper.safeGetStringAllowNull(jsonObject, "_id", null)
?: return null
val pumpId = JsonHelper.safeGetLongAllowNull(jsonObject, "pumpId", null)
val endPumpId = JsonHelper.safeGetLongAllowNull(jsonObject, "endId", null)
val pumpType = InterfaceIDs.PumpType.fromString(JsonHelper.safeGetStringAllowNull(jsonObject, "pumpType", null))

View file

@ -11,7 +11,10 @@ import org.json.JSONObject
fun TemporaryTarget.Companion.fromJson(jsonObject: JSONObject, profileUtil: ProfileUtil): TemporaryTarget? {
val units = GlucoseUnit.fromText(JsonHelper.safeGetString(jsonObject, "units", GlucoseUnit.MGDL.asText))
val timestamp = JsonHelper.safeGetLongAllowNull(jsonObject, "mills", null) ?: return null
val timestamp =
JsonHelper.safeGetLongAllowNull(jsonObject, "mills", null)
?: JsonHelper.safeGetLongAllowNull(jsonObject, "date", null)
?: return null
val duration = JsonHelper.safeGetLongAllowNull(jsonObject, "duration", null) ?: return null
val durationInMilliseconds = JsonHelper.safeGetLongAllowNull(jsonObject, "durationInMilliseconds")
var low = JsonHelper.safeGetDouble(jsonObject, "targetBottom")
@ -22,7 +25,9 @@ fun TemporaryTarget.Companion.fromJson(jsonObject: JSONObject, profileUtil: Prof
?: return null else ""
// this string can be localized from NS, it will not work in this case CUSTOM will be used
val reason = TemporaryTarget.Reason.fromString(reasonString)
val id = JsonHelper.safeGetStringAllowNull(jsonObject, "_id", null) ?: return null
val id = JsonHelper.safeGetStringAllowNull(jsonObject, "identifier", null)
?: JsonHelper.safeGetStringAllowNull(jsonObject, "_id", null)
?: return null
val isValid = JsonHelper.safeGetBoolean(jsonObject, "isValid", true)
if (timestamp == 0L) return null

View file

@ -23,7 +23,10 @@ fun therapyEventFromNsMbg(mbg: NSMbg) =
fun TherapyEvent.Companion.fromJson(jsonObject: JSONObject): TherapyEvent? {
val glucoseUnit = if (JsonHelper.safeGetString(jsonObject, "units", GlucoseUnit.MGDL.asText) == GlucoseUnit.MGDL.asText) TherapyEvent.GlucoseUnit.MGDL else TherapyEvent.GlucoseUnit.MMOL
val timestamp = JsonHelper.safeGetLongAllowNull(jsonObject, "mills", null) ?: return null
val timestamp =
JsonHelper.safeGetLongAllowNull(jsonObject, "mills", null)
?: JsonHelper.safeGetLongAllowNull(jsonObject, "date", null)
?: return null
val type = TherapyEvent.Type.fromString(JsonHelper.safeGetString(jsonObject, "eventType", TherapyEvent.Type.NONE.text))
val duration = JsonHelper.safeGetLong(jsonObject, "duration")
val durationInMilliseconds = JsonHelper.safeGetLongAllowNull(jsonObject, "durationInMilliseconds")
@ -31,7 +34,9 @@ fun TherapyEvent.Companion.fromJson(jsonObject: JSONObject): TherapyEvent? {
val glucoseType = TherapyEvent.MeterType.fromString(JsonHelper.safeGetString(jsonObject, "glucoseType"))
val enteredBy = JsonHelper.safeGetStringAllowNull(jsonObject, "enteredBy", null)
val note = JsonHelper.safeGetStringAllowNull(jsonObject, "notes", null)
val id = JsonHelper.safeGetStringAllowNull(jsonObject, "_id", null) ?: return null
val id = JsonHelper.safeGetStringAllowNull(jsonObject, "identifier", null)
?: JsonHelper.safeGetStringAllowNull(jsonObject, "_id", null)
?: return null
val isValid = JsonHelper.safeGetBoolean(jsonObject, "isValid", true)
if (timestamp == 0L) return null