Add Export CSV in Maintenance Fragment

This commit is contained in:
Philoul 2021-03-08 21:42:16 +01:00
parent f7f6f4b7c1
commit f3d6fff565
10 changed files with 41 additions and 36 deletions

View file

@ -358,38 +358,21 @@ class ImportExportPrefs @Inject constructor(
}) })
} }
override fun exportUserEntriesCsv(activity: FragmentActivity, listEntries: Single<List<UserEntry>>) {
override fun exportUserEntriesXml(activity: FragmentActivity, listEntries: Single<List<UserEntry>>) {
val entries = listEntries.blockingGet() val entries = listEntries.blockingGet()
log.debug("XXXXX " + entries.size)
prefFileList.ensureExportDirExists() prefFileList.ensureExportDirExists()
val newFile = prefFileList.newExportXmlFile() val newFile = prefFileList.newExportXmlFile()
log.debug("XXXXX " + classicPrefsFormat.UserEntriesToCsv(entries)) //log.debug("XXXXX " + classicPrefsFormat.UserEntriesToCsv(entries))
askToConfirmExport(activity, newFile) { password -> try {
try { classicPrefsFormat.saveCsv(newFile, entries)
classicPrefsFormat.saveXml(newFile, entries) ToastUtils.okToast(activity, resourceHelper.gs(R.string.ue_exported))
log.debug("XXXXX " + newFile.isHidden + " " + newFile.absolutePath) } catch (e: FileNotFoundException) {
ToastUtils.okToast(activity, resourceHelper.gs(R.string.ue_exported)) ToastUtils.errorToast(activity, resourceHelper.gs(R.string.filenotfound) + " " + newFile)
} catch (e: FileNotFoundException) { log.error(LTag.CORE, "Unhandled exception", e)
ToastUtils.errorToast(activity, resourceHelper.gs(R.string.filenotfound) + " " + newFile) } catch (e: IOException) {
log.error(LTag.CORE, "Unhandled exception", e) ToastUtils.errorToast(activity, e.message)
} catch (e: IOException) { log.error(LTag.CORE, "Unhandled exception", e)
ToastUtils.errorToast(activity, e.message)
log.error(LTag.CORE, "Unhandled exception", e)
} catch (e: PrefFileNotFoundError) {
ToastUtils.Long.errorToast(activity, resourceHelper.gs(R.string.preferences_export_canceled)
+ "\n\n" + resourceHelper.gs(R.string.filenotfound)
+ ": " + e.message
+ "\n\n" + resourceHelper.gs(R.string.needstoragepermission))
log.error(LTag.CORE, "File system exception", e)
} catch (e: PrefIOError) {
ToastUtils.Long.errorToast(activity, resourceHelper.gs(R.string.preferences_export_canceled)
+ "\n\n" + resourceHelper.gs(R.string.needstoragepermission)
+ ": " + e.message)
log.error(LTag.CORE, "File system exception", e)
}
} }
} }
} }

View file

@ -99,6 +99,14 @@ class MaintenanceFragment : DaggerFragment() {
} }
} }
binding.navLogsettings.setOnClickListener { startActivity(Intent(activity, LogSettingActivity::class.java)) } binding.navLogsettings.setOnClickListener { startActivity(Intent(activity, LogSettingActivity::class.java)) }
binding.exportCsv.setOnClickListener {
activity?.let { activity ->
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.ue_export_to_csv) + "?") {
uel.log(Action.EXPORT_CSV)
importExportPrefs.exportUserEntriesCsv(activity, repository.getAllUserEntries())
}
}
}
} }
@Synchronized @Synchronized

View file

@ -61,8 +61,8 @@ class TreatmentsUserEntryFragment : DaggerFragment() {
binding.ueExportToXml.setOnClickListener { binding.ueExportToXml.setOnClickListener {
activity?.let { activity -> activity?.let { activity ->
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.ue_export_to_csv) + "?") { OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.ue_export_to_csv) + "?") {
uel.log(Action.TREATMENTS_NS_REFRESH) uel.log(Action.EXPORT_CSV)
importExportPrefs.exportUserEntriesXml(activity, repository.getAllUserEntries()) importExportPrefs.exportUserEntriesCsv(activity, repository.getAllUserEntries())
} }
} }
} }

View file

@ -75,6 +75,20 @@
android:text="@string/nav_import" android:text="@string/nav_import"
android:textColor="@color/colorTreatmentButton" /> android:textColor="@color/colorTreatmentButton" />
<Button
android:id="@+id/export_csv"
style="?android:attr/buttonStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="3dp"
android:layout_weight="0.5"
android:text="@string/ue_export_to_csv"
android:textColor="@color/colorTreatmentButton" />
<Button <Button
android:id="@+id/nav_resetdb" android:id="@+id/nav_resetdb"
style="?android:attr/buttonStyle" style="?android:attr/buttonStyle"

View file

@ -14,5 +14,5 @@ interface ImportExportPrefsInterface {
fun prefsFileExists(): Boolean fun prefsFileExists(): Boolean
fun verifyStoragePermissions(fragment: Fragment, onGranted: Runnable) fun verifyStoragePermissions(fragment: Fragment, onGranted: Runnable)
fun exportSharedPreferences(f: Fragment) fun exportSharedPreferences(f: Fragment)
fun exportUserEntriesXml(activity: FragmentActivity, entries: Single<List<UserEntry>>) fun exportUserEntriesCsv(activity: FragmentActivity, entries: Single<List<UserEntry>>)
} }

View file

@ -77,7 +77,7 @@ class ClassicPrefsFormat @Inject constructor(
return metadata return metadata
} }
fun saveXml(file: File, userEntries: List<UserEntry>) { fun saveCsv(file: File, userEntries: List<UserEntry>) {
try { try {
val contents = UserEntriesToCsv(userEntries) val contents = UserEntriesToCsv(userEntries)
storage.putFileContents(file, contents) storage.putFileContents(file, contents)

View file

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

View file

@ -1,7 +1,6 @@
package info.nightscout.androidaps.utils.extensions package info.nightscout.androidaps.utils.extensions
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.*
fun Action.stringId(): Int { fun Action.stringId(): Int {
@ -102,7 +101,8 @@ fun Action.stringId(): Int {
Action.TT_DELETED_FROM_NS -> R.string.uel_tt_deleted_from_ns Action.TT_DELETED_FROM_NS -> R.string.uel_tt_deleted_from_ns
Action.TT_FROM_NS -> R.string.uel_tt_from_ns Action.TT_FROM_NS -> R.string.uel_tt_from_ns
Action.TT_CANCELED_FROM_NS -> R.string.uel_tt_canceleted_from_ns Action.TT_CANCELED_FROM_NS -> R.string.uel_tt_canceleted_from_ns
else -> R.string.uel_unknown Action.EXPORT_CSV -> R.string.uel_export_csv
else -> R.string.uel_unknown
} }
} }

View file

@ -485,6 +485,7 @@
<string name="uel_tt_deleted_from_ns">TT DELETED FROM NS</string> <string name="uel_tt_deleted_from_ns">TT DELETED FROM NS</string>
<string name="uel_tt_from_ns">TT FROM NS</string> <string name="uel_tt_from_ns">TT FROM NS</string>
<string name="uel_tt_canceleted_from_ns">TT CANCELED FROM NS</string> <string name="uel_tt_canceleted_from_ns">TT CANCELED FROM NS</string>
<string name="uel_export_csv">EXPORT USER ENTRIES</string>
<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>

View file

@ -115,6 +115,7 @@ data class UserEntry(
@SerializedName("TT_DELETED_FROM_NS") TT_DELETED_FROM_NS (ColorGroup.TT), @SerializedName("TT_DELETED_FROM_NS") TT_DELETED_FROM_NS (ColorGroup.TT),
@SerializedName("TT_FROM_NS") TT_FROM_NS (ColorGroup.TT), @SerializedName("TT_FROM_NS") TT_FROM_NS (ColorGroup.TT),
@SerializedName("TT_CANCELED_FROM_NS") TT_CANCELED_FROM_NS (ColorGroup.TT), @SerializedName("TT_CANCELED_FROM_NS") TT_CANCELED_FROM_NS (ColorGroup.TT),
@SerializedName("EXPORT_CSV") EXPORT_CSV (ColorGroup.Aaps),
@SerializedName("UNKNOWN") UNKNOWN (ColorGroup.Aaps) @SerializedName("UNKNOWN") UNKNOWN (ColorGroup.Aaps)
; ;
@ -169,5 +170,4 @@ data class UserEntry(
Pump, Pump,
Aaps Aaps
} }
} }