Safe delete Units enum class in UserEntry

This commit is contained in:
Philoul 2021-04-03 16:35:57 +02:00
parent 2a03d1d7ca
commit 8d8abd65a7
5 changed files with 30 additions and 60 deletions

View file

@ -1,14 +1,13 @@
package info.nightscout.androidaps; package info.nightscout.androidaps;
import info.nightscout.androidaps.utils.T; import info.nightscout.androidaps.utils.T;
import info.nightscout.androidaps.database.entities.UserEntry.Units;
/** /**
* Created by mike on 07.06.2016. * Created by mike on 07.06.2016.
*/ */
public class Constants { public class Constants {
public static final String MGDL = Units.Mg_Dl.getText(); // This is Nightscout's representation public static final String MGDL = "mg/dl"; // This is Nightscout's representation
public static final String MMOL = Units.Mmol_L.getText(); public static final String MMOL = "mmol";
public static final double MMOLL_TO_MGDL = 18; // 18.0182; public static final double MMOLL_TO_MGDL = 18; // 18.0182;
public static final double MGDL_TO_MMOLL = 1 / MMOLL_TO_MGDL; public static final double MGDL_TO_MMOLL = 1 / MMOLL_TO_MGDL;

View file

@ -3,11 +3,8 @@ package info.nightscout.androidaps.plugins.general.maintenance.formats
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.UserEntry import info.nightscout.androidaps.database.entities.UserEntry
import info.nightscout.androidaps.database.entities.UserEntry.Action
import info.nightscout.androidaps.database.entities.UserEntry.Units
import info.nightscout.androidaps.interfaces.ProfileFunction 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.Translator import info.nightscout.androidaps.utils.Translator
import info.nightscout.androidaps.utils.UserEntryPresentationHelper import info.nightscout.androidaps.utils.UserEntryPresentationHelper
import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.resources.ResourceHelper

View file

@ -5,7 +5,7 @@ import info.nightscout.androidaps.database.entities.TemporaryTarget
import info.nightscout.androidaps.database.entities.TherapyEvent import info.nightscout.androidaps.database.entities.TherapyEvent
import info.nightscout.androidaps.database.entities.UserEntry.Action import info.nightscout.androidaps.database.entities.UserEntry.Action
import info.nightscout.androidaps.database.entities.UserEntry.Sources import info.nightscout.androidaps.database.entities.UserEntry.Sources
import info.nightscout.androidaps.database.entities.UserEntry.Units import info.nightscout.androidaps.database.entities.XXXValueWithUnit
import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.resources.ResourceHelper
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@ -92,18 +92,16 @@ class Translator @Inject internal constructor(
Action.UNKNOWN -> resourceHelper.gs(R.string.unknown) Action.UNKNOWN -> resourceHelper.gs(R.string.unknown)
} }
fun translate(units: Units): String = when(units) { fun translate(units: XXXValueWithUnit?): String = when(units) {
Units.Mg_Dl -> resourceHelper.gs(R.string.mgdl) is XXXValueWithUnit.Gram -> resourceHelper.gs(R.string.shortgram)
Units.Mmol_L -> resourceHelper.gs(R.string.mmol) is XXXValueWithUnit.Hour -> resourceHelper.gs(R.string.shorthour)
Units.U -> resourceHelper.gs(R.string.insulin_unit_shortname) is XXXValueWithUnit.Insulin -> resourceHelper.gs(R.string.insulin_unit_shortname)
Units.U_H -> resourceHelper.gs(R.string.profile_ins_units_per_hour) is XXXValueWithUnit.Mgdl -> resourceHelper.gs(R.string.mgdl)
Units.G -> resourceHelper.gs(R.string.shortgram) is XXXValueWithUnit.Minute -> resourceHelper.gs(R.string.shortminute)
Units.M -> resourceHelper.gs(R.string.shortminute) is XXXValueWithUnit.Mmoll -> resourceHelper.gs(R.string.mmol)
Units.H -> resourceHelper.gs(R.string.shorthour) is XXXValueWithUnit.Percent -> resourceHelper.gs(R.string.shortpercent)
Units.Percent -> resourceHelper.gs(R.string.shortpercent) is XXXValueWithUnit.UnitPerHour -> resourceHelper.gs(R.string.profile_ins_units_per_hour)
Units.None -> "" else -> ""
else -> resourceHelper.gs(R.string.unknown)
} }
fun translate(meterType: TherapyEvent.MeterType): String = when(meterType) { fun translate(meterType: TherapyEvent.MeterType): String = when(meterType) {

View file

@ -7,7 +7,6 @@ import info.nightscout.androidaps.core.R
import info.nightscout.androidaps.database.entities.UserEntry import info.nightscout.androidaps.database.entities.UserEntry
import info.nightscout.androidaps.database.entities.UserEntry.Action import info.nightscout.androidaps.database.entities.UserEntry.Action
import info.nightscout.androidaps.database.entities.UserEntry.Sources import info.nightscout.androidaps.database.entities.UserEntry.Sources
import info.nightscout.androidaps.database.entities.UserEntry.Units
import info.nightscout.androidaps.database.entities.UserEntry.ColorGroup import info.nightscout.androidaps.database.entities.UserEntry.ColorGroup
import info.nightscout.androidaps.database.entities.XXXValueWithUnit import info.nightscout.androidaps.database.entities.XXXValueWithUnit
import info.nightscout.androidaps.interfaces.ProfileFunction import info.nightscout.androidaps.interfaces.ProfileFunction
@ -85,12 +84,12 @@ class UserEntryPresentationHelper @Inject constructor(
list.joinToString(separator = " ", transform = this::toPresentationString) list.joinToString(separator = " ", transform = this::toPresentationString)
private fun toPresentationString(valueWithUnit: XXXValueWithUnit?): String = when (valueWithUnit) { private fun toPresentationString(valueWithUnit: XXXValueWithUnit?): String = when (valueWithUnit) {
is XXXValueWithUnit.Gram -> "${valueWithUnit.value}${translator.translate(Units.G)}" is XXXValueWithUnit.Gram -> "${valueWithUnit.value}${translator.translate(valueWithUnit)}"
is XXXValueWithUnit.Hour -> "${valueWithUnit.value}${translator.translate(Units.H)}" is XXXValueWithUnit.Hour -> "${valueWithUnit.value}${translator.translate(valueWithUnit)}"
is XXXValueWithUnit.Minute -> "${valueWithUnit.value}${translator.translate(Units.M)}" is XXXValueWithUnit.Minute -> "${valueWithUnit.value}${translator.translate(valueWithUnit)}"
is XXXValueWithUnit.Percent -> "${valueWithUnit.value}${translator.translate(Units.Percent)}" is XXXValueWithUnit.Percent -> "${valueWithUnit.value}${translator.translate(valueWithUnit)}"
is XXXValueWithUnit.Insulin -> DecimalFormatter.to2Decimal(valueWithUnit.value) + translator.translate(UserEntry.Units.U) is XXXValueWithUnit.Insulin -> DecimalFormatter.to2Decimal(valueWithUnit.value) + translator.translate(valueWithUnit)
is XXXValueWithUnit.UnitPerHour -> DecimalFormatter.to2Decimal(valueWithUnit.value) + translator.translate(UserEntry.Units.U_H) is XXXValueWithUnit.UnitPerHour -> DecimalFormatter.to2Decimal(valueWithUnit.value) + translator.translate(valueWithUnit)
is XXXValueWithUnit.SimpleInt -> valueWithUnit.value.toString() is XXXValueWithUnit.SimpleInt -> valueWithUnit.value.toString()
is XXXValueWithUnit.SimpleString -> valueWithUnit.value is XXXValueWithUnit.SimpleString -> valueWithUnit.value
// is XXXValueWithUnit.StringResource -> resourceHelper.gs(valueWithUnit.value, valueWithUnit.params.map{ it.value() }.toTypedArray()) //Todo Fix StringResource with Param // is XXXValueWithUnit.StringResource -> resourceHelper.gs(valueWithUnit.value, valueWithUnit.params.map{ it.value() }.toTypedArray()) //Todo Fix StringResource with Param
@ -101,13 +100,13 @@ class UserEntryPresentationHelper @Inject constructor(
is XXXValueWithUnit.Timestamp -> dateUtil.dateAndTimeAndSecondsString(valueWithUnit.value) is XXXValueWithUnit.Timestamp -> dateUtil.dateAndTimeAndSecondsString(valueWithUnit.value)
is XXXValueWithUnit.Mgdl -> { is XXXValueWithUnit.Mgdl -> {
if (profileFunction.getUnits() == Constants.MGDL) DecimalFormatter.to0Decimal(valueWithUnit.value) + translator.translate(Units.Mg_Dl) if (profileFunction.getUnits() == Constants.MGDL) DecimalFormatter.to0Decimal(valueWithUnit.value) + translator.translate(valueWithUnit)
else DecimalFormatter.to1Decimal(valueWithUnit.value / Constants.MMOLL_TO_MGDL) + translator.translate(Units.Mmol_L) else DecimalFormatter.to1Decimal(valueWithUnit.value / Constants.MMOLL_TO_MGDL) + translator.translate(valueWithUnit)
} }
is XXXValueWithUnit.Mmoll -> { is XXXValueWithUnit.Mmoll -> {
if (profileFunction.getUnits() == Constants.MGDL) DecimalFormatter.to0Decimal(valueWithUnit.value) + translator.translate(Units.Mmol_L) if (profileFunction.getUnits() == Constants.MGDL) DecimalFormatter.to0Decimal(valueWithUnit.value) + translator.translate(valueWithUnit)
else DecimalFormatter.to1Decimal(valueWithUnit.value * Constants.MMOLL_TO_MGDL) + translator.translate(Units.Mg_Dl) else DecimalFormatter.to1Decimal(valueWithUnit.value * Constants.MMOLL_TO_MGDL) + translator.translate(valueWithUnit)
} }
XXXValueWithUnit.UNKNOWN -> "" XXXValueWithUnit.UNKNOWN -> ""
@ -128,13 +127,13 @@ class UserEntryPresentationHelper @Inject constructor(
csvString(R.string.careportal_note), csvString(R.string.careportal_note),
csvString(R.string.ue_formated_string), csvString(R.string.ue_formated_string),
csvString(R.string.event_time_label), csvString(R.string.event_time_label),
csvString(Units.fromText(profileFunction.getUnits())), csvString(if (profileFunction.getUnits() == Constants.MGDL) R.string.mgdl else R.string.mmol ),
csvString(Units.G), csvString(R.string.shortgram),
csvString(Units.U), csvString(R.string.insulin_unit_shortname),
csvString(Units.U_H), csvString(R.string.profile_ins_units_per_hour),
csvString(Units.Percent), csvString(R.string.shortpercent),
csvString(Units.H), csvString(R.string.shorthour),
csvString(Units.M), csvString(R.string.shortminute),
csvString(R.string.ue_none) csvString(R.string.ue_none)
) + "\n" ) + "\n"
@ -198,7 +197,6 @@ class UserEntryPresentationHelper @Inject constructor(
private fun saveString(id: Int): String = if (id != 0) resourceHelper.gs(id) else "" private fun saveString(id: Int): String = if (id != 0) resourceHelper.gs(id) else ""
private fun csvString(action: Action): String = "\"" + translator.translate(action).replace("\"", "\"\"") + "\"" private fun csvString(action: Action): String = "\"" + translator.translate(action).replace("\"", "\"\"") + "\""
private fun csvString(unit: Units): String = "\"" + translator.translate(unit).replace("\"", "\"\"") + "\""
private fun csvString(id: Int): String = if (id != 0) "\"" + resourceHelper.gs(id).replace("\"", "\"\"") + "\"" else "" private fun csvString(id: Int): String = if (id != 0) "\"" + resourceHelper.gs(id).replace("\"", "\"\"") + "\"" else ""
private fun csvString(s: String): String = if (s != "") "\"" + s.replace("\"", "\"\"") + "\"" else "" private fun csvString(s: String): String = if (s != "") "\"" + s.replace("\"", "\"\"") + "\"" else ""
} }

View file

@ -97,28 +97,6 @@ data class UserEntry(
} }
} }
enum class Units(val text: String) {
None (""), //Int or String
Mg_Dl ("mg/dl"), //Double
Mmol_L ("mmol"), //Double
Timestamp("Timestamp"), //long
U ("U"), //Double
U_H ("U/h"), //Double
G ("g"), //Int
M ("m"), //Int
H ("h"), //Int
Percent ("%"), //Int
TherapyEvent ("TherapyEvent"), //String (All enum key translated by Translator function, mainly The
R_String ("R.string"), //Int
Source ("Source") //String
;
companion object {
fun fromString(unit: String?) = values().firstOrNull { it.name == unit } ?: None
fun fromText(unit: String?) = values().firstOrNull { it.text == unit } ?: None
}
}
enum class Sources() { enum class Sources() {
TreatmentDialog, TreatmentDialog,
InsulinDialog, InsulinDialog,