Skip to content

Keep the user's layout when part of it can't be read - #35

Open
edward-mcnair wants to merge 1 commit into
kemalandic:mainfrom
edward-mcnair:layout-protection
Open

edward-mcnair wants to merge 1 commit into
kemalandic:mainfrom
edward-mcnair:layout-protection

Conversation

@edward-mcnair

Copy link
Copy Markdown

What does this PR do?

layout.json holds the only copy of the dashboard, and one bad part used to cost all of it:

  • An unknown enum value (a theme name from a newer version), one malformed widget, or a wrong-typed setting failed the whole document.
  • A second failure then replaced the newer file with defaults, because quarantine keeps only one .corrupt copy.
  • Import accepted any JSON object, {} included, and wrote it straight over the layout.

Loading

  • Each field falls back to its default, and each unreadable page or widget is skipped, instead of failing the document (LenientDecoding.swift).
  • A widget whose settings can't be read keeps its place with default settings.
  • The decoder counts every fallback, so a repaired read is never mistaken for a clean one.
  • layout.json.bak keeps the last file that read cleanly.
  • A file that can't be decoded is moved aside under its own timestamped name (every one is kept), and the backup is restored. A partly read file is kept as a copy.
  • A file that can't be opened (a transient I/O error), couldn't be moved aside, or was written by a newer version is never saved over that session.
  • Widgets with impossible geometry or repeated ids are dropped before use (LayoutValidation.swift).

Importing

  • An imported file must decode cleanly, have pages, lay out validly, and be from a supported version. Otherwise it's refused with a reason, and the layout is unchanged.
  • The layout on screen, including unsaved changes, is kept as layout-before-import.json.
  • LayoutEngine.importLayout(_:) bumps layoutVersion, so services follow the new widgets, and clamps the current page.

Settings shows the import result, and says when a save failed.

Tests:

  • New LayoutRecoveryTests (Swift Testing, 5 tests): lenient decoding, restoring the last good copy across repeated failures, protecting a newer file, refusing bad imports ({}, no pages, a page without an id, a damaged widget list, negative sizes, a newer version), and keeping the on-screen layout on import.
  • LayoutStoreTests' two quarantine tests now expect one file per failure, which is stronger than the single .corrupt copy they checked. They're XCTest, so CI runs them.

Screenshots

No visual change, apart from a line of text under Import Layout in Settings → General.

Checklist

  • Tested on macOS 14+ (macOS 27)
  • No third-party dependencies added
  • Follows existing code style (swift-format lint clean)

Note: overlaps #32 in GlobalSettings.init(from:); I'll rebase whichever lands second.

🤖 Generated with Claude Code

layout.json holds the only copy of the dashboard, and one bad part used to
cost all of it: an unknown enum value (a theme name from a newer version),
one malformed widget, or a wrong-typed setting failed the whole document,
and a second failure replaced the newer file with defaults.

Loading
- Each field falls back to its default and each unreadable page or widget
  is skipped, instead of failing the document. A widget whose settings
  can't be read keeps its place with default settings. The decoder counts
  every fallback, so a repaired read is never mistaken for a clean one.
- layout.json.bak keeps the last file that read cleanly. A file that can't
  be decoded is moved aside under its own name (every one is kept) and the
  backup is restored; a partly read file is kept as a copy.
- A file that can't be opened (a transient I/O error), couldn't be moved
  aside, or was written by a newer version is never saved over.
- Widgets with impossible geometry or repeated ids are dropped before use.

Importing
- An imported file must decode cleanly, have pages and lay out validly, and
  be from a supported version; otherwise it's refused with a reason.
- The layout on screen, including unsaved changes, is kept as
  layout-before-import.json, and services refresh after an import.

Settings shows the import result and says when a save failed.

LayoutStoreTests' two quarantine tests now expect one file per failure
(stronger than the single .corrupt copy they checked). New Swift Testing
tests cover lenient decoding, restoring the last good copy, protecting a
newer file, and refusing or keeping layouts on import.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant