Skip to content

Commit

Permalink
Export to file: add a pretty CSV mode
Browse files Browse the repository at this point in the history
End-users sometimes want to show their sleep log to other people, but
the data exported to a file, but that is not human-readable.

The default export format is a CSV that is meant to be easy to import
back, i.e. a machine-readable file format.

Fix the problem by adding a setting (defaults to false): if enabled, the
export result will have formatted timestamps and also the sleep length
will be written there, if if that's redundant for a machine reader.

Fixes <#515>.
  • Loading branch information
vmiklos committed Dec 31, 2024
1 parent 1af2358 commit eb06539
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
28 changes: 26 additions & 2 deletions app/src/main/java/hu/vmiklos/plees_tracker/DataModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ object DataModel {
uri: Uri,
showToast: Boolean
) {
val prettyBackup = preferences.getBoolean("pretty_backup", false)
val sleeps = database.sleepDao().getAll()

try {
Expand All @@ -321,9 +322,32 @@ object DataModel {
return
}
val writer = CSVPrinter(OutputStreamWriter(os, "UTF-8"), CSVFormat.DEFAULT)
writer.printRecord("sid", "start", "stop", "rating", "comment")
if (prettyBackup) {
writer.printRecord("sid", "start", "stop", "length", "rating", "comment")
} else {
writer.printRecord("sid", "start", "stop", "rating", "comment")
}
for (sleep in sleeps) {
writer.printRecord(sleep.sid, sleep.start, sleep.stop, sleep.rating, sleep.comment)
if (prettyBackup) {
val durationMS = sleep.stop - sleep.start

writer.printRecord(
sleep.sid,
DataModel.formatTimestamp(Date(sleep.start), DataModel.getCompactView()),
DataModel.formatTimestamp(Date(sleep.stop), DataModel.getCompactView()),
DataModel.formatDuration(durationMS / 1000, DataModel.getCompactView()),
sleep.rating,
sleep.comment
)
} else {
writer.printRecord(
sleep.sid,
sleep.start,
sleep.stop,
sleep.rating,
sleep.comment
)
}
}
writer.close()
} catch (e: Exception) {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
</plurals>
<string name="settings_category_backup">Backup</string>
<string name="settings_automatic_backup">Automatic backup</string>
<string name="settings_pretty_backup">Pretty backup: human-readable, but can\'t be imported</string>
<string name="settings_compact_view">Compact view: no seconds and timezone</string>
<string name="settings_backup_path">Backup path</string>
<string name="widget_start_stop">Toggle tracking</string>
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
<Preference
android:key="auto_backup_path"
android:title="@string/settings_backup_path" />
<SwitchPreference
android:defaultValue="false"
android:key="pretty_backup"
android:title="@string/settings_pretty_backup" />
</PreferenceCategory>

<PreferenceCategory android:title="@string/settings_category_dashboard">
Expand Down
3 changes: 3 additions & 0 deletions guide/src/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ Backup settings allow you to automatically back up your sleeps after a tracking
useful in case you selected a path which is then implicitly synchronized to some external server,
e.g. Nextcloud.

Pretty backup allows you to create a CSV file which has human-readable start, stop and length values
during exporting to a file. This pretty output can't be imported back, though.

### Dashboard

You can also customize the dashboard duration, which limits the sleeps and sleep statistics on the
Expand Down

0 comments on commit eb06539

Please sign in to comment.