Skip to content

Fix import/export bugs and keep the UI responsive - #121

Merged
danielchalmers merged 5 commits into
mainfrom
c/import-export-audit-bbd230
Nov 23, 2026
Merged

danielchalmers merged 5 commits into
mainfrom
c/import-export-audit-bbd230

Conversation

@danielchalmers

@danielchalmers danielchalmers commented Nov 23, 2026 •

Copy link
Copy Markdown
Owner

Closes #118. Works through the import/export audit there. Old app versions can still import files written by this one: a new backup imports into a build of main on the Android 17 emulator.

These are the bugs the audit reproduced:

  • Import only overwrites the preferences the backup carries, so an import no longer resets theme, device colors and "hide notes".
  • Preferences are restored after the data is committed and outside the database try block. Duplicate or unnamed entries are skipped, and if that step still fails the user is told the data was imported but some settings weren't, instead of "Import failed" with the database already replaced.
  • A backup with missing or null sections reads as empty sections instead of throwing a NullReferenceException.
  • Cancelling the save dialog is silent. FileSaverResult.IsSuccessful is just Exception is null, so the old cancelled branch could never run. On Android, main shows "Export failed: TaskCanceledException_ctor_DefaultMessage".
  • The backup is opened with File.OpenRead, so read-only files and files open in another program can be imported.
  • The export file name is formatted with the invariant culture, so it's backup-2026-11-21 on a Thai, Arabic or Persian device too.
  • Built-in categories are seeded again right after an import, the same way the next launch would. Before, a backup without the Notes category broke adding notes until restart.

Reading a backup is also more forgiving and says more when it fails:

  • Read failures go through one BackupReadException with a message for the user ("This file isn't a Journal App backup, or it's damaged.", "This backup is damaged and can't be read.") and the original exception goes to the log. That includes the CRC32 check .NET 11 added to zip reads, which has a test that flips one byte in a stored entry. File system errors show "The file couldn't be opened." and a failed database replace says the current data was not changed.
  • The data file is matched by name, ignoring case and folders, so a backup that was unzipped and zipped again still imports.
  • Backups record FormatVersion and AppVersion. A backup with a higher format version than the app knows gets a warning in the confirmation. Files without them read as before.

What goes into a backup changed a little:

  • theme, device_colors and hide_notes are exported when the user has set them, and restored through their typed setters so the stored types stay right.
  • Unset preferences are left out instead of being written as empty strings, and mood_palette is no longer written. One consequence: importing a backup that has no safety plan now leaves the device's current safety plan alone instead of clearing it.
  • The JSON is no longer indented.

And the UI no longer freezes while it works:

  • Reading, replacing and creating the backup run in Task.Run, so the spinner paints. In the Windows Release build with 2348 days and 17010 points the spinner entered the DOM 19 ms after clicking Export (1253 ms before), and 6 ms after confirming an import, with no frame gap over 9 ms during the 2.7 s replace.
  • Settings ignores Back while an export or import is running, since Back can now arrive mid-import.
  • CreateBackup loads each table once and lets the change tracker fix up the collections. Same build and data: 554 ms, down from 1066 ms.
  • The archive stream goes straight to the file saver instead of being copied to an array and a second stream.
  • The copy Android's picker makes in the app cache is deleted after the import.

On the Android 17 emulator (Release, CoreCLR, about 17000 points, single runs, so treat as rough) the steps cost about the same as on main: read around 360 ms, replace around 3.1 s, create backup 730 to 790 ms. The archive went from 991 KB to 905 KB. So on Android the gain is that the UI isn't frozen for those seconds, not that they got shorter.

The real apps were run too: on Windows, an export through the Save dialog and an import of that file through the Open dialog, and on the emulator export, import, cancel, a file that isn't a backup, and Back pressed twice during an import. In both, theme and "hide notes" (and device colors on Android) were changed after the export and came back from the import. Not run on a physical phone.

Not done from #118: dropping points that have no day or category (only a hand-edited backup has them), the bulk add change (no difference on the emulator), saving the import in chunks, password protected backups and opening .journalapp files from Windows.

Import no longer wipes preferences that aren't in the backup, and no longer reports a failure after the data has been replaced. Backups with missing sections read as empty, cancelling the save dialog is silent, read-only files can be imported, and the export file name always uses the Gregorian date.
Reading a backup now drops points that have no day or category, finds the data file by name in any folder or casing, and reports damaged or foreign files with a readable message. Built-in categories are added back right after an import. Backups record a format and app version, carry the theme, device colors and hide notes settings, and skip unset preferences and the unused mood_palette.

Export and import run off the UI thread so the spinner shows straight away. The backup snapshot loads each table once, the bulk add skips change detection, the JSON is compact and the archive is no longer copied before saving. The copy Android makes of a picked file is deleted after the import.
The picker reports the copy under /data/data while FileSystem.CacheDirectory is /data/user/0, so the prefix check never matched on a device.
@danielchalmers danielchalmers changed the title Fix import/export correctness bugs Fix import/export bugs and keep the UI responsive Nov 23, 2026
@danielchalmers
danielchalmers requested a balanced review from Copilot November 23, 2026 03:39

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Only a hand-edited backup has points without a day or category, and the change detection tweak made no difference to import time on the Android emulator.
@danielchalmers
danielchalmers merged commit 04b6d3e into main Nov 23, 2026
1 check passed
@danielchalmers
danielchalmers deleted the c/import-export-audit-bbd230 branch November 23, 2026 03:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Import/export audit: bugs and improvements

2 participants