Clean and simplify code

Add comment in UserEntry (data type for each Unit)
This commit is contained in:
Philoul 2021-03-09 21:09:59 +01:00
parent f3d6fff565
commit 01a1321cbb
4 changed files with 42 additions and 34 deletions

View file

@ -116,9 +116,9 @@ class TreatmentsUserEntryFragment : DaggerFragment() {
} }
Units.Mg_Dl -> valuesWithUnitString += if (profileFunction.getUnits()==Constants.MGDL) DecimalFormatter.to0Decimal(v.dValue) + resourceHelper.gs(Units.Mg_Dl.stringId()) + separator else DecimalFormatter.to1Decimal(v.dValue/Constants.MMOLL_TO_MGDL) + resourceHelper.gs(Units.Mmol_L.stringId()) + separator Units.Mg_Dl -> valuesWithUnitString += if (profileFunction.getUnits()==Constants.MGDL) DecimalFormatter.to0Decimal(v.dValue) + resourceHelper.gs(Units.Mg_Dl.stringId()) + separator else DecimalFormatter.to1Decimal(v.dValue/Constants.MMOLL_TO_MGDL) + resourceHelper.gs(Units.Mmol_L.stringId()) + separator
Units.Mmol_L -> valuesWithUnitString += if (profileFunction.getUnits()==Constants.MGDL) DecimalFormatter.to0Decimal(v.dValue*Constants.MMOLL_TO_MGDL) + resourceHelper.gs(Units.Mg_Dl.stringId()) + separator else DecimalFormatter.to1Decimal(v.dValue) + resourceHelper.gs(Units.Mmol_L.stringId()) + separator Units.Mmol_L -> valuesWithUnitString += if (profileFunction.getUnits()==Constants.MGDL) DecimalFormatter.to0Decimal(v.dValue*Constants.MMOLL_TO_MGDL) + resourceHelper.gs(Units.Mg_Dl.stringId()) + separator else DecimalFormatter.to1Decimal(v.dValue) + resourceHelper.gs(Units.Mmol_L.stringId()) + separator
Units.G -> valuesWithUnitString += v.iValue.toString() + resourceHelper.gs(Units.G.stringId()) + separator Units.U_H, Units.U -> valuesWithUnitString += DecimalFormatter.to2Decimal(v.dValue) + resourceHelper.gs(v.unit.stringId()) + separator
Units.U_H -> valuesWithUnitString += DecimalFormatter.to2Decimal(v.dValue) + resourceHelper.gs(Units.U_H.stringId()) + separator Units.G, Units.M, Units.H, Units.Percent -> valuesWithUnitString += v.iValue.toString() + resourceHelper.gs(v.unit.stringId()) + separator
else -> valuesWithUnitString += if (v.dValue != 0.0 || v.iValue != 0 || v.sValue != "") { v.value().toString() + if (!v.unit.stringId().equals(0)) resourceHelper.gs(v.unit.stringId()) + separator else separator } else "" else -> valuesWithUnitString += if (v.iValue != 0 || v.sValue != "") { v.value().toString() + if (!v.unit.stringId().equals(0)) resourceHelper.gs(v.unit.stringId()) + separator else separator } else ""
} }
} }
if (current.values.size > 0) if (current.values.size > 0)

View file

@ -89,36 +89,43 @@ class ClassicPrefsFormat @Inject constructor(
} }
fun UserEntriesToCsv(userEntries: List<UserEntry>): String { fun UserEntriesToCsv(userEntries: List<UserEntry>): String {
val userEntryHeader = "Date;UTC Offset;Action;Note;Value;Unit\n" val userEntryHeader = resourceHelper.gs(R.string.ue_csv_header) + "\n"
return userEntryHeader + userEntries.joinToString("\n") { entry -> return userEntryHeader + userEntries.joinToString("\n") { entry ->
if (entry.values.size > 0) { if (entry.values.size > 0) {
entry.values.joinToString("\n") { value -> entry.values.joinToString("\n") { value ->
dateUtil.dateAndTimeAndSecondsString(entry.timestamp) + ";" + entry.timestamp.toString() + ";" +
dateUtil.dateAndTimeAndSecondsString(entry.timestamp) + ";" +
dateUtil.timeString(entry.utcOffset) + ";" + dateUtil.timeString(entry.utcOffset) + ";" +
"\"" + resourceHelper.gs(entry.action.stringId()) + "\";" + csvString(entry.action.stringId()) + ";" +
if (entry.s != "") {"\""+ entry.s.replace("\"", "\"\"") + "\";" } else { ";" } + csvString(entry.s) + ";" +
valueWithUnitToCsv(value) } valueWithUnitToCsv(value)
}
} else { } else {
dateUtil.dateAndTimeAndSecondsString(entry.timestamp) + ";" + entry.timestamp.toString() + ";" +
dateUtil.dateAndTimeAndSecondsString(entry.timestamp) + ";" +
dateUtil.timeString(entry.utcOffset) + ";" + dateUtil.timeString(entry.utcOffset) + ";" +
"\"" + resourceHelper.gs(entry.action.stringId()) + "\";" + csvString(entry.action.stringId()) + ";" +
if (entry.s != "") {"\""+ entry.s.replace("\"", "\"\"") + "\";" } else { ";;" } csvString(entry.s) + ";;"
} }
} }
} }
fun valueWithUnitToCsv(v: ValueWithUnit): String { fun valueWithUnitToCsv(v: ValueWithUnit): String {
return when (v.unit) { return when (v.unit) {
Units.Timestamp -> dateUtil.dateAndTimeAndSecondsString(v.lValue) + ";" + resourceHelper.gs(R.string.date) Units.Timestamp -> dateUtil.dateAndTimeAndSecondsString(v.lValue) + ";" + csvString(R.string.date)
Units.CPEvent -> translator.translate(v.sValue) + ";" Units.CPEvent -> csvString(translator.translate(v.sValue)) + ";"
Units.R_String -> "\"" + resourceHelper.gs(v.iValue).replace("\"", "\"\"") + "\";" Units.R_String -> if (v.lValue.toInt() == 0) csvString(v.iValue) + ";" else ";" //If lValue > 0 it's a formated string, so hidden for
Units.Mg_Dl -> if (profileFunction.getUnits()==Constants.MGDL) DecimalFormatter.to0Decimal(v.dValue) + ";" + resourceHelper.gs(Units.Mg_Dl.stringId()) else DecimalFormatter.to1Decimal(v.dValue/Constants.MMOLL_TO_MGDL) + ";" + resourceHelper.gs(Units.Mmol_L.stringId()) Units.Mg_Dl -> if (profileFunction.getUnits()==Constants.MGDL) DecimalFormatter.to0Decimal(v.dValue) + ";" + csvString(Units.Mg_Dl.stringId()) else DecimalFormatter.to1Decimal(v.dValue/Constants.MMOLL_TO_MGDL) + ";" + csvString(Units.Mmol_L.stringId())
Units.Mmol_L -> if (profileFunction.getUnits()==Constants.MGDL) DecimalFormatter.to0Decimal(v.dValue*Constants.MMOLL_TO_MGDL) + ";" + resourceHelper.gs(Units.Mg_Dl.stringId()) else DecimalFormatter.to1Decimal(v.dValue) + ";" + resourceHelper.gs(Units.Mmol_L.stringId()) Units.Mmol_L -> if (profileFunction.getUnits()==Constants.MGDL) DecimalFormatter.to0Decimal(v.dValue*Constants.MMOLL_TO_MGDL) + ";" + csvString(Units.Mg_Dl.stringId()) else DecimalFormatter.to1Decimal(v.dValue) + ";" + csvString(Units.Mmol_L.stringId())
Units.G -> v.iValue.toString() + ";" + resourceHelper.gs(Units.G.stringId()) Units.U_H, Units.U -> DecimalFormatter.to2Decimal(v.dValue) + ";" + csvString(v.unit.stringId())
Units.U_H -> DecimalFormatter.to2Decimal(v.dValue) + ";" + resourceHelper.gs(Units.U_H.stringId()) Units.G, Units.M, Units.H, Units.Percent
else -> if (v.sValue != "") {"\""+ v.sValue.replace("\"", "\"\"") + if (!v.unit.stringId().equals(0)) "\";\"" + resourceHelper.gs(v.unit.stringId()).replace("\"", "\"\"") + "\"" else "\";"} -> v.iValue.toString() + ";" + csvString(v.unit.stringId())
else if (v.dValue != 0.0 || v.iValue != 0) { v.value().toString() + if (!v.unit.stringId().equals(0)) ";" + resourceHelper.gs(v.unit.stringId()) else ";" } else -> if (v.sValue != "") { csvString(v.sValue) + ";" + csvString(v.unit.stringId())}
else ";" else if (v.iValue != 0) { v.iValue.toString() + ";" + csvString(v.unit.stringId())}
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 ""
} }

View file

@ -489,6 +489,7 @@
<string name="uel_unknown">UNKNOWN</string> <string name="uel_unknown">UNKNOWN</string>
<string name="formated_string">Formated string</string> <string name="formated_string">Formated string</string>
<string name="ue_export_to_csv">Export User Entries to Excel (csv)</string> <string name="ue_export_to_csv">Export User Entries to Excel (csv)</string>
<string name="ue_csv_header">"Timestamp;Date;UTC Offset;Action;Note;Value;Unit"</string>
<plurals name="days"> <plurals name="days">
<item quantity="one">%1$d day</item> <item quantity="one">%1$d day</item>

View file

@ -139,19 +139,19 @@ data class UserEntry(
} }
} }
enum class Units(val text: String) { enum class Units(val text: String) {
@SerializedName("None") None (""), @SerializedName("None") None (""), //Int or String
@SerializedName("Mg_Dl") Mg_Dl ("mg/dl"), @SerializedName("Mg_Dl") Mg_Dl ("mg/dl"), //Double
@SerializedName("Mmol_L") Mmol_L ("mmol"), @SerializedName("Mmol_L") Mmol_L ("mmol"), //Double
@SerializedName("Timestamp") Timestamp("Timestamp"), @SerializedName("Timestamp") Timestamp("Timestamp"), //long
@SerializedName("U") U ("U"), @SerializedName("U") U ("U"), //Double
@SerializedName("U_H") U_H ("U/h"), @SerializedName("U_H") U_H ("U/h"), //Double
@SerializedName("G") G ("g"), @SerializedName("G") G ("g"), //Int
@SerializedName("M") M ("m"), @SerializedName("M") M ("m"), //Int
@SerializedName("H") H ("h"), @SerializedName("H") H ("h"), //Int
@SerializedName("Percent") Percent ("%"), @SerializedName("Percent") Percent ("%"), //Int
@SerializedName("CPEvent") CPEvent ("CPEvent"), @SerializedName("CPEvent") CPEvent ("CPEvent"), //String (CareportalEvent.Event => Translated by Translator function)
@SerializedName("TT_Reason") TT_Reason ("TTReason"), @SerializedName("TT_Reason") TT_Reason ("TTReason"), //TemporaryTarget.Reason => String (to Improve for translation)
@SerializedName("R_String") R_String ("R.string") @SerializedName("R_String") R_String ("R.string") //Int
; ;
companion object { companion object {