Fix Build

This commit is contained in:
Philoul 2021-03-28 16:25:37 +02:00
parent 7db2e112ee
commit b9ffb1ca13
5 changed files with 22 additions and 63 deletions

View file

@ -3,7 +3,6 @@ package info.nightscout.androidaps.utils.extensions
import dagger.Reusable import dagger.Reusable
import info.nightscout.androidaps.Constants import info.nightscout.androidaps.Constants
import info.nightscout.androidaps.core.R import info.nightscout.androidaps.core.R
import info.nightscout.androidaps.database.entities.Translator
import info.nightscout.androidaps.database.entities.UserEntry import info.nightscout.androidaps.database.entities.UserEntry
import info.nightscout.androidaps.database.entities.UserEntry.ColorGroup import info.nightscout.androidaps.database.entities.UserEntry.ColorGroup
import info.nightscout.androidaps.database.entities.UserEntry.Units import info.nightscout.androidaps.database.entities.UserEntry.Units
@ -12,6 +11,7 @@ import info.nightscout.androidaps.interfaces.ProfileFunction
import info.nightscout.androidaps.utils.DateUtil import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.DecimalFormatter import info.nightscout.androidaps.utils.DecimalFormatter
import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.resources.ResourceHelper
import info.nightscout.androidaps.utils.Translator
import javax.inject.Inject import javax.inject.Inject
@Reusable @Reusable

View file

@ -17,7 +17,7 @@ object SealedClassHelper {
override fun <T : Any> create(gson: Gson, type: TypeToken<T>): TypeAdapter<T> { override fun <T : Any> create(gson: Gson, type: TypeToken<T>): TypeAdapter<T> {
val kClass = Reflection.getOrCreateKotlinClass(type.rawType) val kClass = Reflection.getOrCreateKotlinClass(type.rawType)
return if (kClass.sealedSubclasses.any()) { return if (kClass.sealedSubclasses.any()) {
SealedClassTypeAdapter<T>(kClass, gson) SealedClassTypeAdapter(kClass, gson)
} else } else
gson.getDelegateAdapter(this, type) gson.getDelegateAdapter(this, type)
} }
@ -26,25 +26,26 @@ object SealedClassHelper {
private class SealedClassTypeAdapter<T : Any>(private val kClass: KClass<Any>, val gson: Gson) : TypeAdapter<T>() { private class SealedClassTypeAdapter<T : Any>(private val kClass: KClass<Any>, val gson: Gson) : TypeAdapter<T>() {
override fun read(jsonReader: JsonReader): T? { override fun read(jsonReader: JsonReader): T? {
jsonReader.beginObject() //start reading the object jsonReader.beginObject()
val nextName = jsonReader.nextName() //get the name on the object val nextName = jsonReader.nextName()
val innerClass = kClass.sealedSubclasses.firstOrNull { val innerClass = kClass.sealedSubclasses.firstOrNull { it.simpleName == nextName }
it.simpleName!!.contains(nextName) ?: throw Exception("$nextName is not a child of the sealed class ${kClass.qualifiedName}")
}
?: throw Exception("$nextName is not found to be a data class of the sealed class ${kClass.qualifiedName}")
val x = gson.fromJson<T>(jsonReader, innerClass.javaObjectType) val x = gson.fromJson<T>(jsonReader, innerClass.javaObjectType)
jsonReader.endObject() jsonReader.endObject()
//if there a static object, actually return that back to ensure equality and such! // if there a static object, actually return that
return innerClass.objectInstance as T? ?: x return innerClass.objectInstance as T? ?: x
} }
override fun write(out: JsonWriter, value: T) { override fun write(out: JsonWriter, value: T) {
val jsonString = gson.toJson(value) val jsonString = gson.toJson(value)
out.beginObject()
val name = value.javaClass.canonicalName val name = value.javaClass.canonicalName
out.name(name.splitToSequence(".").last()).jsonValue(jsonString) if (name != null) {
out.endObject() out.beginObject()
out.name(name.splitToSequence(".").last()).jsonValue(jsonString)
out.endObject()
}
} }
} }
}
} inline fun <reified T> Gson.fromJson(json: String): T = fromJson(json, object : TypeToken<T>() {}.type)

View file

@ -1,18 +1,14 @@
package info.nightscout.androidaps.utils.serialisation package info.nightscout.androidaps.utils.serialisation
import com.google.gson.reflect.TypeToken
import info.nightscout.androidaps.database.entities.XXXValueWithUnit import info.nightscout.androidaps.database.entities.XXXValueWithUnit
import info.nightscout.androidaps.utils.ValueWithUnitWrapper
object ValueWithUnitSerialiser { object ValueWithUnitSerialiser {
fun toSealedClassJson(list: List<XXXValueWithUnit>): String = fun toSealedClassJson(list: List<XXXValueWithUnit>): String = list.map(::ValueWithUnitWrapper)
list.map { ValueWithUnitWrapper(it) }.let { SealedClassHelper.gson.toJson(it) } .let(SealedClassHelper.gson::toJson)
fun fromJson(string: String): List<XXXValueWithUnit> { fun fromJson(string: String): List<XXXValueWithUnit> = SealedClassHelper.gson
val itemType = object : TypeToken<List<ValueWithUnitWrapper>>() {}.type .fromJson<List<ValueWithUnitWrapper>>(string).map { it.wrapped }
return SealedClassHelper.gson.fromJson<List<ValueWithUnitWrapper>>(string, itemType).map { it.wrapped }
}
private class ValueWithUnitWrapper(val wrapped: XXXValueWithUnit)
} }

View file

@ -16,7 +16,7 @@ data class UserEntry(
override var utcOffset: Long = TimeZone.getDefault().getOffset(timestamp).toLong(), override var utcOffset: Long = TimeZone.getDefault().getOffset(timestamp).toLong(),
var action: Action, var action: Action,
var remark: String, var remark: String,
val sources: Sources, // val sources: Sources,
var values: MutableList<ValueWithUnit> var values: MutableList<ValueWithUnit>
) : DBEntry, DBEntryWithTime { ) : DBEntry, DBEntryWithTime {
enum class Action (val colorGroup: ColorGroup) { enum class Action (val colorGroup: ColorGroup) {

View file

@ -65,6 +65,7 @@ sealed class XXXValueWithUnit {
*/ */
// just do develop in this file. Remove when done. // just do develop in this file. Remove when done.
/*
interface Translator { interface Translator {
fun translate(units: UserEntry.Units): String fun translate(units: UserEntry.Units): String
@ -72,43 +73,4 @@ interface Translator {
fun translate(type: TherapyEvent.Type): String fun translate(type: TherapyEvent.Type): String
fun translate(reason: TemporaryTarget.Reason): String fun translate(reason: TemporaryTarget.Reason): String
} }
*/
enum class Sources(val text: String) {
@SerializedName("TreatmentDialog") TreatmentDialog ("TreatmentDialog"),
@SerializedName("InsulinDialog") InsulinDialog ("InsulinDialog"),
@SerializedName("CarbDialog") CarbDialog ("CarbDialog"),
@SerializedName("WizardDialog") WizardDialog ("WizardDialog"),
@SerializedName("QuickWizard") QuickWizard ("QuickWizard"),
@SerializedName("ExtendedBolusDialog") ExtendedBolusDialog ("ExtendedBolusDialog"),
@SerializedName("TTDialog") TTDialog ("TTDialog"),
@SerializedName("ProfileSwitchDialog") ProfileSwitchDialog ("ProfileSwitchDialog"),
@SerializedName("LoopDialog") LoopDialog ("LoopDialog"),
@SerializedName("TempBasalDialog") TempBasalDialog ("TempBasalDialog"),
@SerializedName("CalibrationDialog") CalibrationDialog ("CalibrationDialog"),
@SerializedName("FillDialog") FillDialog ("FillDialog"),
@SerializedName("BgCheck") BgCheck ("BgCheck"),
@SerializedName("SensorInsert") SensorInsert ("SensorInsert"),
@SerializedName("BatteryChange") BatteryChange ("BatteryChange"),
@SerializedName("Note") Note ("Note"),
@SerializedName("Exercise") Exercise ("Exercise"),
@SerializedName("Question") Question ("Question"),
@SerializedName("Announcement") Announcement ("Announcement"),
@SerializedName("Actions") Actions ("Actions"), //From Actions plugin
@SerializedName("Automation") Automation ("Automation"), //From Automation plugin
@SerializedName("LocalProfile") LocalProfile ("LocalProfile"), //From LocalProfile plugin
@SerializedName("Loop") Loop ("Loop"), //From Loop plugin
@SerializedName("Maintenance") Maintenance ("Maintenance"), //From Maintenance plugin
@SerializedName("NSClient") NSClient ("NSClient"), //From NSClient plugin
@SerializedName("Pump") Pump ("Pump"), //From Pump plugin (for example from pump history)
@SerializedName("SMS") SMS ("SMS"), //From SMS plugin
@SerializedName("Treatments") Treatments ("Treatments"), //From Treatments plugin
@SerializedName("Wear") Wear ("Wear"), //From Wear plugin
@SerializedName("Food") Food ("Food"), //From Food plugin
@SerializedName("Unknown") Unknown ("Unknown") //if necessary
;
companion object {
fun fromString(source: String?) = values().firstOrNull { it.name == source } ?: Unknown
fun fromText(source: String?) = values().firstOrNull { it.text == source } ?: Unknown
}
}