This is a short one about an Undo button in the EPUB reader I'm building, and about a small change in how we decide whether Undo is still valid, which turned out to fix a whole class of bugs instead of the one I was looking at.
The reader has a settings sheet (font, margins, themes, auto-scroll and so on), and the sheet has a Reset button which puts every reading setting of the book back to the defaults, and also rewrites the format defaults that new books inherit. There was no way back from it, so one stray click and your carefully tuned settings were gone. The options were a confirmation dialog or an undo, and undo is the friendlier one, since it doesn't put a dialog in front of every click that was intentional.
The app uses The Composable Architecture (TCA), so the settings live in reducer state and every change goes through an action. The first version looked like what you'd probably write too:
case let .epubPreferencesResetTapped(defaults): state.epubPreferencesBeforeReset = state.epubPreferences state.epubPreferences = defaults return saveEPUBPreferences(defaults) case .epubPreferencesResetUndone: guard let previous = state.epubPreferencesBeforeReset else { return .none } state.epubPreferencesBeforeReset = nil state.epubPreferences = previous return saveEPUBPreferences(previous) And since an Undo that survives a later edit would throw that edit away, the action the sheet sends on every change cleared the snapshot:
case let .epubPreferencesChanged(newPrefs): if newPrefs != state.epubPreferences { state.epubPreferencesBeforeReset = nil } state.epubPreferences = newPrefs return saveEPUBPreferences(newPrefs) There was a test for exactly that case (Reset, change something in the sheet, Undo does nothing), and I had even broken the clearing line on purpose to watch the test go red, so I was pretty happy with it.
The writers nobody told about Undo
Then the review pass found what I'd missed, and most of the reviewers found it independently. The sheet's action is not the only thing that writes the settings. Night mode swaps the theme by writing the settings directly, the auto-scroll controls (speed, adaptive speed, pause at chapter end) write them directly, there's a mode correction in the auto-scroll code, and the initial load writes them too. That's six writers, and none of them knew the snapshot existed.
So this sequence worked: Reset, tap Done, change the auto-scroll speed from the reader, reopen settings, and "Undo Reset" is still there. Tap it and the old speed comes back, on screen and on disk, and if night mode was on in between, the day theme comes back with it.
The obvious patch is to clear the snapshot in each of the six writers. It would work today, but it's a rule ("every code path that writes the settings must also clear the Undo snapshot") which nothing enforces, and the seventh writer, added a year from now by someone (me) who's forgotten this exists, breaks it without any test noticing. The first version already had that rule, it just had it in one place instead of seven.
Deriving validity from the value
What we want to know is not "has anyone touched the settings since Reset", but "are the settings still what Reset put there". That second question can be answered by looking at the state itself, so instead of a snapshot to clear we store a pair, what Reset replaced and what it applied:
struct EPUBPreferencesReset: Equatable, Sendable { let previous: EPUBReaderPreferences let applied: EPUBReaderPreferences } extension ReaderFeature.State { var canUndoEPUBPreferencesReset: Bool { guard let reset = epubPreferencesReset else { return false } return epubPreferences == reset.applied } } Reset records the pair, and Undo checks the derived value before restoring:
case let .epubPreferencesResetTapped(defaults): state.epubPreferencesReset = state.epubPreferences.map { EPUBPreferencesReset(previous: $0, applied: defaults) } state.epubPreferences = defaults return saveEPUBPreferences(defaults) case .epubPreferencesResetUndone: guard state.canUndoEPUBPreferencesReset, let previous = state.epubPreferencesReset?.previous else { return .none } state.epubPreferencesReset = nil state.epubPreferences = previous return saveEPUBPreferences(previous) And the special case in .epubPreferencesChanged is deleted, the action is back to just writing the value and saving it. Any writer that moves the settings away from what Reset applied withdraws Undo, whether it's the sheet, night mode or something that doesn't exist yet, and none of them has to know Undo exists. As a small bonus, if you change something and then change it back to exactly the reset values, Undo shows up again, which is correct, since undoing at that point loses nothing.
The new test goes through a writer outside the sheet, which is the case the first version couldn't see:
await store.send(.epubPreferencesResetTapped(Self.defaults)) await store.send(.autoScrollSpeedChanged(0.8)) { $0.epubPreferences?.autoScrollNormalizedSpeed = 0.8 } #expect(!store.state.canUndoEPUBPreferencesReset) await store.send(.epubPreferencesResetUndone) { $0.epubPreferences?.autoScrollNormalizedSpeed = 0.8 } Making canUndoEPUBPreferencesReset return true always turns it red, as it should.
The cost is that
appliedis a full copy of the settings and the check is an==over the whole struct. For a settings struct that's nothing, but if the state were huge you'd compare a version number or a hash instead, and the idea stays the same.
A VoiceOver detail that came along
The toolbar showed either a "Reset" or an "Undo Reset" button, with an if/else choosing between two Buttons. In SwiftUI those are two different views with two identities, so when Reset swapped to Undo Reset, VoiceOver focus was dropped right after the user tapped it, which is about the worst moment to lose your place. One Button with a conditional title and accessibility identifier keeps the identity, and focus stays on it:
let canUndo = store.canUndoEPUBPreferencesReset Button(canUndo ? "Undo Reset" : "Reset") { if canUndo { store.send(.epubPreferencesResetUndone) return } store.send(.epubPreferencesResetTapped(defaultPreferences())) } .accessibilityIdentifier(canUndo ? undoResetButtonID : resetButtonID) Summary
When some piece of state is only valid "until something else changes", don't store a flag that every writer has to remember to clear. Store enough to check it (here, the value you applied) and derive the validity by comparing against the current state, so writers that have never heard of the feature can't break it. It also pays to test such a feature through a writer other than the one it was built around, since my test for the first version was green and mutation-checked and still missed this, because it only went through the sheet.
