From c259aa345c56fb546d1e41b3239fdf3f98ee61c3 Mon Sep 17 00:00:00 2001 From: Philoul Date: Sun, 7 Mar 2021 10:43:33 +0100 Subject: [PATCH] Csv format (not xml) modified to Excel default remove unnecessary " and use ; as separator --- .../general/maintenance/ImportExportPrefs.kt | 2 +- .../fragments/TreatmentsUserEntryFragment.kt | 3 +- .../layout/treatments_user_entry_fragment.xml | 2 +- .../maintenance/PrefFileListProvider.kt | 8 +-- .../maintenance/formats/ClassicPrefsFormat.kt | 61 ++++++++----------- .../utils/extensions/UserEntryExt.kt | 5 -- core/src/main/res/values/strings.xml | 2 +- .../androidaps/database/entities/UserEntry.kt | 7 --- 8 files changed, 32 insertions(+), 58 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/ImportExportPrefs.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/ImportExportPrefs.kt index 23e7ec93c9..57ac66033e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/ImportExportPrefs.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/ImportExportPrefs.kt @@ -364,7 +364,7 @@ class ImportExportPrefs @Inject constructor( log.debug("XXXXX " + entries.size) prefFileList.ensureExportDirExists() val newFile = prefFileList.newExportXmlFile() - log.debug("XXXXX " + classicPrefsFormat.returnXml(entries)) + log.debug("XXXXX " + classicPrefsFormat.UserEntriesToCsv(entries)) askToConfirmExport(activity, newFile) { password -> try { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsUserEntryFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsUserEntryFragment.kt index 9442a069e3..d6cddf75b1 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsUserEntryFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsUserEntryFragment.kt @@ -18,7 +18,6 @@ import info.nightscout.androidaps.interfaces.ImportExportPrefsInterface import info.nightscout.androidaps.interfaces.ProfileFunction import info.nightscout.androidaps.logging.UserEntryLogger import info.nightscout.androidaps.plugins.bus.RxBusWrapper -import info.nightscout.androidaps.plugins.general.nsclient.events.EventNSClientRestart import info.nightscout.androidaps.utils.DateUtil import info.nightscout.androidaps.utils.DecimalFormatter import info.nightscout.androidaps.utils.FabricPrivacy @@ -61,7 +60,7 @@ class TreatmentsUserEntryFragment : DaggerFragment() { binding.recyclerview.layoutManager = LinearLayoutManager(view.context) binding.ueExportToXml.setOnClickListener { activity?.let { activity -> - OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.ue_export_to_xml) + "?") { + OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.ue_export_to_csv) + "?") { uel.log(Action.TREATMENTS_NS_REFRESH) importExportPrefs.exportUserEntriesXml(activity, repository.getAllUserEntries()) } diff --git a/app/src/main/res/layout/treatments_user_entry_fragment.xml b/app/src/main/res/layout/treatments_user_entry_fragment.xml index dd8ba9f295..f4b98829b8 100644 --- a/app/src/main/res/layout/treatments_user_entry_fragment.xml +++ b/app/src/main/res/layout/treatments_user_entry_fragment.xml @@ -12,7 +12,7 @@ android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:drawableStart="@drawable/ic_header_export" - android:text="@string/ue_export_to_xml" /> + android:text="@string/ue_export_to_csv" /> ) { try { - val contents = userEntries.joinToString("\n") { entry -> - if (entry.values.size > 0) { - dateUtil.dateAndTimeAndSecondsString(entry.timestamp) + "," + - dateUtil.dateAndTimeAndSecondsString(entry.utcOffset) + "," + - resourceHelper.gs(entry.action.stringId()) + "," + - "\""+ entry.s.replace("\"", "\\\"") + "\"," + - entry.values.joinToString(",") { value -> valueWithUnitToStringXml(value) } + "\n" - } else { - dateUtil.dateAndTimeAndSecondsString(entry.timestamp) + "," + - dateUtil.dateAndTimeAndSecondsString(entry.utcOffset) + "," + - resourceHelper.gs(entry.action.stringId()) + "," + - "\""+ entry.s.replace("\"", "\\\"") + "\",,\n" - } - } + val contents = UserEntriesToCsv(userEntries) storage.putFileContents(file, contents) } catch (e: FileNotFoundException) { throw PrefFileNotFoundError(file.absolutePath) @@ -101,37 +88,37 @@ class ClassicPrefsFormat @Inject constructor( } } - fun returnXml(userEntries: List): String { - - return userEntries.joinToString("\n") { entry -> + fun UserEntriesToCsv(userEntries: List): String { + val userEntryHeader = "Date;UTC Offset;Action;Note;Value;Unit\n" + return userEntryHeader + userEntries.joinToString("\n") { entry -> if (entry.values.size > 0) { entry.values.joinToString("\n") { value -> - "\"" + dateUtil.dateAndTimeAndSecondsString(entry.timestamp) + "\"," + - "\"" + dateUtil.timeString(entry.utcOffset) + "\"," + - "\"" + resourceHelper.gs(entry.action.stringId()) + "\"," + - if (entry.s != "") {"\""+ entry.s.replace("\"", "\\\"") + "\"," } else { "," } + - valueWithUnitToStringXml(value) } + dateUtil.dateAndTimeAndSecondsString(entry.timestamp) + ";" + + dateUtil.timeString(entry.utcOffset) + ";" + + "\"" + resourceHelper.gs(entry.action.stringId()) + "\";" + + if (entry.s != "") {"\""+ entry.s.replace("\"", "\"\"") + "\";" } else { ";" } + + valueWithUnitToCsv(value) } } else { - "\"" + dateUtil.dateAndTimeAndSecondsString(entry.timestamp) + "\"," + - "\"" + dateUtil.timeString(entry.utcOffset) + "\"," + - "\"" + resourceHelper.gs(entry.action.stringId()) + "\"," + - if (entry.s != "") {"\""+ entry.s.replace("\"", "\\\"") + "\"," } else { ",," } + dateUtil.dateAndTimeAndSecondsString(entry.timestamp) + ";" + + dateUtil.timeString(entry.utcOffset) + ";" + + "\"" + resourceHelper.gs(entry.action.stringId()) + "\";" + + if (entry.s != "") {"\""+ entry.s.replace("\"", "\"\"") + "\";" } else { ";;" } } } } - fun valueWithUnitToStringXml(v: ValueWithUnit): String { + fun valueWithUnitToCsv(v: ValueWithUnit): String { return when (v.unit) { - Units.Timestamp -> "\"" + dateUtil.dateAndTimeAndSecondsString(v.lValue) + "\",\"" + resourceHelper.gs(R.string.date) + "\"" - Units.CPEvent -> "\"" + translator.translate(v.sValue) + "\"," - Units.R_String -> "\"" + resourceHelper.gs(v.iValue) + "\"," - 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.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.G -> v.iValue.toString() + ",\"" + resourceHelper.gs(Units.G.stringId()) + "\"" - Units.U_H -> DecimalFormatter.to2Decimal(v.dValue) + ",\"" + resourceHelper.gs(Units.U_H.stringId()) + "\"" - else -> if (v.sValue != "") {"\""+ v.sValue.replace("\"", "\\\"") + if (!v.unit.stringId().equals(0)) "\",\"" + resourceHelper.gs(v.unit.stringId()) + "\"" else "\","} - 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 "," + Units.Timestamp -> dateUtil.dateAndTimeAndSecondsString(v.lValue) + ";" + resourceHelper.gs(R.string.date) + Units.CPEvent -> translator.translate(v.sValue) + ";" + Units.R_String -> "\"" + resourceHelper.gs(v.iValue).replace("\"", "\"\"") + "\";" + 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.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.G -> v.iValue.toString() + ";" + resourceHelper.gs(Units.G.stringId()) + Units.U_H -> DecimalFormatter.to2Decimal(v.dValue) + ";" + resourceHelper.gs(Units.U_H.stringId()) + else -> if (v.sValue != "") {"\""+ v.sValue.replace("\"", "\"\"") + if (!v.unit.stringId().equals(0)) "\";\"" + resourceHelper.gs(v.unit.stringId()).replace("\"", "\"\"") + "\"" else "\";"} + 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 ";" } } } \ No newline at end of file diff --git a/core/src/main/java/info/nightscout/androidaps/utils/extensions/UserEntryExt.kt b/core/src/main/java/info/nightscout/androidaps/utils/extensions/UserEntryExt.kt index 5aadcceff9..223ab8fb46 100644 --- a/core/src/main/java/info/nightscout/androidaps/utils/extensions/UserEntryExt.kt +++ b/core/src/main/java/info/nightscout/androidaps/utils/extensions/UserEntryExt.kt @@ -229,8 +229,3 @@ fun Units.stringId(): Int { else -> 0 } } - -fun UserEntry.toXml(): String { - val content = action.name + "," + values.joinToString(",") {value -> value.toXml()} - return "" -} diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index 4643c840c4..8538201cae 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -486,7 +486,7 @@ TT CANCELED FROM NS UNKNOWN Formated string - Export User Entries to xml + Export User Entries to Excel (csv) %1$d day diff --git a/database/src/main/java/info/nightscout/androidaps/database/entities/UserEntry.kt b/database/src/main/java/info/nightscout/androidaps/database/entities/UserEntry.kt index 668da47006..3929394c67 100644 --- a/database/src/main/java/info/nightscout/androidaps/database/entities/UserEntry.kt +++ b/database/src/main/java/info/nightscout/androidaps/database/entities/UserEntry.kt @@ -136,13 +136,6 @@ data class UserEntry( if (!iValue.equals(0)) return iValue return lValue } - - fun toXml() : String { - if (sValue != "") return "\""+ sValue.replace("\"", "\\\"") + "\"," + unit.text - if (!dValue.equals(0.0)) return dValue.toString() + "," + unit.text - if (!iValue.equals(0)) return return iValue.toString() + "," + unit.text - return return lValue.toString() + "," + unit.text - } } enum class Units(val text: String) { @SerializedName("None") None (""),