Move CSV generation to UserEntryPresentationHelper

This commit is contained in:
Philoul 2021-04-02 16:37:01 +02:00
parent 0b00f4b401
commit c0aedc7736
3 changed files with 94 additions and 83 deletions

View file

@ -9,6 +9,7 @@ import info.nightscout.androidaps.interfaces.ProfileFunction
import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.DecimalFormatter
import info.nightscout.androidaps.utils.Translator
import info.nightscout.androidaps.utils.UserEntryPresentationHelper
import info.nightscout.androidaps.utils.resources.ResourceHelper
import info.nightscout.androidaps.utils.storage.Storage
import java.io.File
@ -23,6 +24,7 @@ class ClassicPrefsFormat @Inject constructor(
private var dateUtil: DateUtil,
private var translator: Translator,
private var profileFunction: ProfileFunction,
private var userEntryPresentationHelper: UserEntryPresentationHelper,
private var storage: Storage
) : PrefsFormat {
@ -79,7 +81,7 @@ class ClassicPrefsFormat @Inject constructor(
fun saveCsv(file: File, userEntries: List<UserEntry>) {
try {
val contents = UserEntriesToCsv(userEntries)
val contents = userEntryPresentationHelper.UserEntriesToCsv(userEntries)
storage.putFileContents(file, contents)
} catch (e: FileNotFoundException) {
throw PrefFileNotFoundError(file.absolutePath)
@ -87,84 +89,4 @@ class ClassicPrefsFormat @Inject constructor(
throw PrefIOError(file.absolutePath)
}
}
fun UserEntriesToCsv(userEntries: List<UserEntry>): String {
val userEntryHeader = resourceHelper.gs(R.string.ue_csv_header,
csvString(R.string.ue_timestamp),
csvString(R.string.date),
csvString(R.string.ue_utc_offset),
csvString(R.string.ue_action),
csvString(R.string.eventtype),
csvString(R.string.ue_source),
csvString(R.string.careportal_note),
csvString(R.string.ue_formated_string),
csvString(R.string.event_time_label),
csvString(Units.fromText(profileFunction.getUnits())),
csvString(Units.G),
csvString(Units.U),
csvString(Units.U_H),
csvString(Units.Percent),
csvString(Units.H),
csvString(Units.M),
csvString(R.string.ue_none)
) + "\n"
return userEntryHeader + userEntries.joinToString("\n") { entry ->
var timestampRec = "" + entry.timestamp
var dateTimestampRev = dateUtil.dateAndTimeAndSecondsString(entry.timestamp)
var utcOffset = dateUtil.timeString(entry.utcOffset)
var action = csvString(entry.action)
var therapyEvent = ""
var source = ""
var note = csvString(entry.note)
var formatedString = ""
var timestamp = ""
var bg = ""
var g = ""
var u = ""
var uh = ""
var percent = ""
var h = ""
var m = ""
var other = ""
/*
for (v in entry.values) {
when (v.unit) {
Units.Timestamp -> timestamp = dateUtil.dateAndTimeAndSecondsString(v.lValue)
Units.TherapyEvent -> therapyEvent = if (therapyEvent == "") translator.translate(v.sValue) else therapyEvent + " / " + translator.translate(v.sValue) //Todo update with XXXValueWithUnit
Units.Source -> source = csvString(v.sValue)
Units.R_String -> if (v.iValue != 0) { //Formated string lValue is the number of parameters, up to 3
var rStringParam = v.lValue.toInt()
var tempString = ""
when (rStringParam) { //
0 -> tempString = resourceHelper.gs(v.iValue)
1 -> tempString = resourceHelper.gs(v.iValue, entry.values[entry.values.indexOf(v)+1].value())
2 -> tempString = resourceHelper.gs(v.iValue, entry.values[entry.values.indexOf(v)+1].value(), entry.values[entry.values.indexOf(v)+2].value())
3 -> tempString = resourceHelper.gs(v.iValue, entry.values[entry.values.indexOf(v)+1].value(), entry.values[entry.values.indexOf(v)+2].value(), entry.values[entry.values.indexOf(v)+3].value())
}
formatedString = if (formatedString == "") tempString else formatedString + " / " + tempString
}
Units.Mg_Dl -> bg = if (profileFunction.getUnits()==Constants.MGDL) DecimalFormatter.to0Decimal(v.dValue) else DecimalFormatter.to1Decimal(v.dValue/Constants.MMOLL_TO_MGDL)
Units.Mmol_L -> bg = if (profileFunction.getUnits()==Constants.MGDL) DecimalFormatter.to0Decimal(v.dValue*Constants.MMOLL_TO_MGDL) else DecimalFormatter.to1Decimal(v.dValue)
Units.G -> g = v.iValue.toString()
Units.U -> u = DecimalFormatter.to2Decimal(v.dValue)
Units.U_H -> uh = DecimalFormatter.to2Decimal(v.dValue)
Units.Percent -> percent = v.iValue.toString()
Units.H -> h = v.iValue.toString()
Units.M -> m = v.iValue.toString()
else -> other = if (other == "") v.value().toString() else other + " / " + v.value().toString()
}
}
*/
therapyEvent = csvString(therapyEvent)
formatedString = csvString(formatedString)
other = csvString(other)
timestampRec + ";" + dateTimestampRev + ";" + utcOffset + ";" + action + ";" + therapyEvent + ";" + source + ";" + note + ";" + formatedString + ";" + timestamp + ";" + bg + ";" + g + ";" + u + ";" + uh + ";" + percent + ";" + h + ";" + m + ";" + other
}
}
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(unit: Units): String = "\"" + translator.translate(unit).replace("\"", "\"\"") + "\""
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

@ -109,4 +109,91 @@ class UserEntryPresentationHelper @Inject constructor(
XXXValueWithUnit.UNKNOWN -> ""
null -> ""
}
fun UserEntriesToCsv(userEntries: List<UserEntry>): String {
return getCsvHeader() + userEntries.joinToString("\n") { entry -> getCsvEntry(entry) }
}
private fun getCsvHeader() = resourceHelper.gs(R.string.ue_csv_header,
csvString(R.string.ue_timestamp),
csvString(R.string.date),
csvString(R.string.ue_utc_offset),
csvString(R.string.ue_action),
csvString(R.string.eventtype),
csvString(R.string.ue_source),
csvString(R.string.careportal_note),
csvString(R.string.ue_formated_string),
csvString(R.string.event_time_label),
csvString(Units.fromText(profileFunction.getUnits())),
csvString(Units.G),
csvString(Units.U),
csvString(Units.U_H),
csvString(Units.Percent),
csvString(Units.H),
csvString(Units.M),
csvString(R.string.ue_none)
) + "\n"
private fun getCsvEntry(entry: UserEntry): String {
val fullvalueWithUnitList = ArrayList<XXXValueWithUnit?>(entry.values)
var timestampRec = "" + entry.timestamp
var dateTimestampRev = dateUtil.dateAndTimeAndSecondsString(entry.timestamp)
var utcOffset = dateUtil.timeString(entry.utcOffset)
var action = csvString(entry.action)
var therapyEvent = ""
var source = translator.translate(entry.source)
var note = csvString(entry.note)
var stringResource = ""
var timestamp = ""
var bg = ""
var gram = ""
var insulin = ""
var unitPerHour = ""
var percent = ""
var hour = ""
var minute = ""
var other = ""
for (valueWithUnit in entry.values) {
if (valueWithUnit is XXXValueWithUnit.StringResource) fullvalueWithUnitList.addAll(valueWithUnit.params)
}
for (valueWithUnit in fullvalueWithUnitList.filter { it != null }) {
when (valueWithUnit) {
is XXXValueWithUnit.Gram -> gram = valueWithUnit.value.toString()
is XXXValueWithUnit.Hour -> hour = valueWithUnit.value.toString()
is XXXValueWithUnit.Minute -> minute = valueWithUnit.value.toString()
is XXXValueWithUnit.Percent -> percent = valueWithUnit.value.toString()
is XXXValueWithUnit.Insulin -> insulin = DecimalFormatter.to2Decimal(valueWithUnit.value)
is XXXValueWithUnit.UnitPerHour -> unitPerHour = DecimalFormatter.to2Decimal(valueWithUnit.value)
is XXXValueWithUnit.SimpleInt -> other = if (other == "") valueWithUnit.value.toString() else other + " / " + valueWithUnit.value.toString()
is XXXValueWithUnit.SimpleString -> other = if (other == "") valueWithUnit.value else other + " / " + valueWithUnit.value
is XXXValueWithUnit.StringResource -> stringResource = if (stringResource == "") resourceHelper.gs(valueWithUnit.value, valueWithUnit.params) else stringResource + " / " + resourceHelper.gs(valueWithUnit.value, valueWithUnit.params)
is XXXValueWithUnit.TherapyEventMeterType -> therapyEvent = if (therapyEvent == "") translator.translate(valueWithUnit.value) else therapyEvent + " / " + translator.translate(valueWithUnit.value)
is XXXValueWithUnit.TherapyEventTTReason -> therapyEvent = if (therapyEvent == "") translator.translate(valueWithUnit.value) else therapyEvent + " / " + translator.translate(valueWithUnit.value)
is XXXValueWithUnit.TherapyEventType -> therapyEvent = if (therapyEvent == "") translator.translate(valueWithUnit.value) else therapyEvent + " / " + translator.translate(valueWithUnit.value)
is XXXValueWithUnit.Timestamp -> timestamp = dateUtil.dateAndTimeAndSecondsString(valueWithUnit.value)
is XXXValueWithUnit.Mgdl -> {
bg = if (profileFunction.getUnits() == Constants.MGDL) DecimalFormatter.to0Decimal(valueWithUnit.value)
else DecimalFormatter.to1Decimal(valueWithUnit.value / Constants.MMOLL_TO_MGDL)
}
is XXXValueWithUnit.Mmoll -> {
bg = if (profileFunction.getUnits() == Constants.MGDL) DecimalFormatter.to0Decimal(valueWithUnit.value)
else DecimalFormatter.to1Decimal(valueWithUnit.value * Constants.MMOLL_TO_MGDL)
}
}
}
therapyEvent = csvString(therapyEvent)
stringResource = csvString(stringResource)
other = csvString(other)
return timestampRec + ";" + dateTimestampRev + ";" + utcOffset + ";" + action + ";" + therapyEvent + ";" + source + ";" + note + ";" + stringResource + ";" + timestamp + ";" + bg + ";" + gram + ";" + insulin + ";" + unitPerHour + ";" + percent + ";" + hour + ";" + minute + ";" + other
}
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(unit: Units): String = "\"" + translator.translate(unit).replace("\"", "\"\"") + "\""
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

@ -4,6 +4,7 @@ import info.nightscout.androidaps.TestBase
import info.nightscout.androidaps.interfaces.ProfileFunction
import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.Translator
import info.nightscout.androidaps.utils.UserEntryPresentationHelper
import info.nightscout.androidaps.utils.resources.ResourceHelper
import org.junit.Assert
import org.junit.Test
@ -22,13 +23,14 @@ class ClassicPrefsFormatTest : TestBase() {
@Mock lateinit var dateUtil: DateUtil
@Mock lateinit var translator: Translator
@Mock lateinit var profileFunction: ProfileFunction
@Mock lateinit var userEntryPresentationHelper: UserEntryPresentationHelper
@Mock lateinit var file: MockedFile
@Test
fun preferenceLoadingTest() {
val test = "key1::val1\nkeyB::valB"
val classicFormat = ClassicPrefsFormat(resourceHelper, dateUtil, translator, profileFunction, SingleStringStorage(test))
val classicFormat = ClassicPrefsFormat(resourceHelper, dateUtil, translator, profileFunction, userEntryPresentationHelper, SingleStringStorage(test))
val prefs = classicFormat.loadPreferences(getMockedFile(), "")
Assert.assertEquals(prefs.values.size, 2)
@ -40,7 +42,7 @@ class ClassicPrefsFormatTest : TestBase() {
@Test
fun preferenceSavingTest() {
val storage = SingleStringStorage("")
val classicFormat = ClassicPrefsFormat(resourceHelper, dateUtil, translator, profileFunction, storage)
val classicFormat = ClassicPrefsFormat(resourceHelper, dateUtil, translator, profileFunction, userEntryPresentationHelper, storage)
val prefs = Prefs(
mapOf(
"key1" to "A",