Formatage xml 1ère version

This commit is contained in:
Philoul 2021-03-07 03:09:30 +01:00
parent cf83d20e13
commit 6096825f4c
7 changed files with 94 additions and 17 deletions

View file

@ -361,25 +361,16 @@ class ImportExportPrefs @Inject constructor(
override fun exportUserEntriesXml(activity: FragmentActivity, listEntries: Single<List<UserEntry>>) {
val entries = listEntries.blockingGet()
log.debug("XXXXX " + entries.size)
prefFileList.ensureExportDirExists()
val legacyFile = prefFileList.legacyFile()
val newFile = prefFileList.newExportXmlFile()
/*
log.debug("XXXXX " + classicPrefsFormat.returnXml(entries))
askToConfirmExport(activity, newFile) { password ->
try {
val entries: MutableMap<String, String> = mutableMapOf()
for ((key, value) in sp.getAll()) {
entries[key] = value.toString()
}
val prefs = Prefs(entries, prepareMetadata(activity))
if (BuildConfig.DEBUG && buildHelper.isEngineeringMode()) {
classicPrefsFormat.savePreferences(legacyFile, prefs)
}
encryptedPrefsFormat.savePreferences(newFile, prefs, password)
ToastUtils.okToast(activity, resourceHelper.gs(R.string.exported))
classicPrefsFormat.saveXml(newFile, entries)
log.debug("XXXXX " + newFile.isHidden + " " + newFile.absolutePath)
ToastUtils.okToast(activity, resourceHelper.gs(R.string.ue_exported))
} catch (e: FileNotFoundException) {
ToastUtils.errorToast(activity, resourceHelper.gs(R.string.filenotfound) + " " + newFile)
log.error(LTag.CORE, "Unhandled exception", e)
@ -400,6 +391,5 @@ class ImportExportPrefs @Inject constructor(
}
}
*/
}
}

View file

@ -179,6 +179,7 @@
<string name="configbuilder_nightscoutversion_label">Nightscout version:</string>
<string name="missing_carbs">Missing %1$d g</string>
<string name="exported">Preferences exported</string>
<string name="ue_exported">User Entries exported</string>
<string name="export_to">Export settings to</string>
<string name="import_from">Import settings from</string>
<string name="setting_imported">Settings imported</string>

View file

@ -31,6 +31,7 @@ class PrefFileListProvider @Inject constructor(
private val path = File(Environment.getExternalStorageDirectory().toString())
private val aapsPath = File(path, "AAPS" + File.separator + "preferences")
private val xmlPath = File(path, "AAPS" + File.separator + "xml")
private const val IMPORT_AGE_NOT_YET_OLD_DAYS = 60
}
@ -93,6 +94,9 @@ class PrefFileListProvider @Inject constructor(
if (!aapsPath.exists()) {
aapsPath.mkdirs()
}
if (!xmlPath.exists()) {
xmlPath.mkdirs()
}
}
fun newExportFile(): File {
@ -102,7 +106,7 @@ class PrefFileListProvider @Inject constructor(
fun newExportXmlFile(): File {
val timeLocal = LocalDateTime.now().toString(DateTimeFormat.forPattern("yyyy-MM-dd'_'HHmmss"))
return File(aapsPath, timeLocal + "_UserEntry.xml")
return File(xmlPath, timeLocal + "_UserEntry.xml")
}
// check metadata for known issues, change their status and add info with explanations

View file

@ -2,6 +2,13 @@ package info.nightscout.androidaps.plugins.general.maintenance.formats
import info.nightscout.androidaps.Constants
import info.nightscout.androidaps.core.R
import info.nightscout.androidaps.database.entities.UserEntry
import info.nightscout.androidaps.database.entities.UserEntry.*
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.extensions.stringId
import info.nightscout.androidaps.utils.resources.ResourceHelper
import info.nightscout.androidaps.utils.storage.Storage
import java.io.File
@ -13,6 +20,9 @@ import javax.inject.Singleton
@Singleton
class ClassicPrefsFormat @Inject constructor(
private var resourceHelper: ResourceHelper,
private var dateUtil: DateUtil,
private var translator: Translator,
private var profileFunction: ProfileFunction,
private var storage: Storage
) : PrefsFormat {
@ -67,4 +77,61 @@ class ClassicPrefsFormat @Inject constructor(
return metadata
}
fun saveXml(file: File, userEntries: List<UserEntry>) {
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"
}
}
storage.putFileContents(file, contents)
} catch (e: FileNotFoundException) {
throw PrefFileNotFoundError(file.absolutePath)
} catch (e: IOException) {
throw PrefIOError(file.absolutePath)
}
}
fun returnXml(userEntries: List<UserEntry>): String {
return 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) }
} 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 {
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 ","
}
}
}

View file

@ -5,6 +5,7 @@ import android.os.Parcelable
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import info.nightscout.androidaps.core.R
import info.nightscout.androidaps.database.entities.UserEntry
import kotlinx.parcelize.Parcelize
import java.io.File

View file

@ -1,6 +1,7 @@
package info.nightscout.androidaps.utils.extensions
import info.nightscout.androidaps.core.R
import info.nightscout.androidaps.database.entities.UserEntry
import info.nightscout.androidaps.database.entities.UserEntry.*
fun Action.stringId(): Int {
@ -228,3 +229,8 @@ fun Units.stringId(): Int {
else -> 0
}
}
fun UserEntry.toXml(): String {
val content = action.name + "," + values.joinToString(",") {value -> value.toXml()}
return ""
}

View file

@ -136,6 +136,13 @@ 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 (""),
@ -169,4 +176,5 @@ data class UserEntry(
Pump,
Aaps
}
}