diff --git a/.pi/orksorksorks/alanvardy-var-1177-no-speech-was-detected-needs-to-expire/done.md b/.pi/orksorksorks/alanvardy-var-1177-no-speech-was-detected-needs-to-expire/done.md new file mode 100644 index 00000000..4ade3947 --- /dev/null +++ b/.pi/orksorksorks/alanvardy-var-1177-no-speech-was-detected-needs-to-expire/done.md @@ -0,0 +1,30 @@ +# Done + +- **What was built**: `DictationViewModel.dictationError` now auto-expires after + `dictationErrorDisplayDuration` (2.0s). All error mutations route through + `showDictationError(_:)` / `clearDictationError()`, which schedule a + generation-guarded `Task.sleep`-then-clear. A newer error cancels the pending + timer and bumps the generation, so a stale timer cannot clear a newer error. + `creationFeedback` and the `ContentView.bottomBar` render path are untouched. +- **Commit SHA(s)**: `e5e85222` — "Expire dictation errors after a short delay" + (pushed to `alanvardy-var-1177-no-speech-was-detected-needs-to-expire`, PR #253). +- **Verification**: + - `make format` — clean (261 files) + - `make lint` — 0 violations, 0 serious (248 files) + - `./scripts/test-one.sh SingleThreadTests/ReminderDictationTests` — 16 cases passed + (includes new `dictationErrorClearsAfterDelay` happy path and + `staleTimerDoesNotClearNewerDictationError` sad path) + - `./scripts/test-one.sh SingleThreadTests/MicrophoneToggleTests` — 19 cases passed + (confirms the defaulted `errorDisplayDuration` init param preserves the other + `DictationViewModel` call site) +- **Reviewer findings**: No blockers. Two optional nits deferred: + 1. The sad-path test proves the stale-timer *outcome* but exercises the + cancellation path more than the generation guard (the guard is + defense-in-depth for the cancel-after-sleep race, hard to hit deterministically). + 2. The completed `dictationErrorClearTask` reference is not nil'd after it fires + (harmless; `clearDictationError()` nils it). +- **Remaining manual items**: The unrelated `DELETEME` scaffold deletion is + still uncommitted in the working tree — remove/commit it before merge per the + ticket-teardown rules. Full CI gate (`./scripts/test.sh`) still needs to run + once via the `run-gate` skill before merge. On-device/manual check: trigger a + dictation error (e.g. no speech) and confirm the red caption clears after ~2s. diff --git a/.pi/orksorksorks/alanvardy-var-1177-no-speech-was-detected-needs-to-expire/small.md b/.pi/orksorksorks/alanvardy-var-1177-no-speech-was-detected-needs-to-expire/small.md new file mode 100644 index 00000000..0f18e542 --- /dev/null +++ b/.pi/orksorksorks/alanvardy-var-1177-no-speech-was-detected-needs-to-expire/small.md @@ -0,0 +1,37 @@ +# Task + +The dictation error message "No speech was detected." (and the other dictation +errors shown in the same place) currently stays visible forever once it +appears. It must disappear automatically after a couple of seconds, matching +the existing transient-feedback behaviour used for reminder creation. + +Scope: `DictationViewModel.dictationError` is set in `startDictation()` and +rendered in `ContentView.bottomBar`. Add an auto-expiry (e.g. a +`Task.sleep`-based clear, mirroring how `creationFeedback` is cleared with +`CreationFeedback.displayDuration`) so the error text clears itself after a +short delay. Ensure a new error restarts/extends the timer and that a stale +timer cannot clear a newer error. Add unit-test coverage in +`SingleThreadTests/ReminderDictationTests.swift` (happy path: error clears +after the delay; sad path: a newer error is not cleared by an older timer). + +## Why SMALL + +A–F hold: one module (`SingleThread` app target), ~2–3 files, an existing +pattern (`creationFeedback` + `CreationFeedback.displayDuration` sleep-then-nil) +to copy; no schema, no new subsystem, no shared/convention code, no design +decision, and only local unit tests. + +## Key files (if the recon found any) + +- `SingleThread/DictationViewModel.swift` — owns `dictationError`; set in + `startDictation()` (auth-denied branches and the `catch`), cleared at the + start of a new dictation. +- `SingleThread/ContentView.swift:743` — `bottomBar` renders + `viewModel.dictation.dictationError` as red caption text. +- `SingleThread/CreationFeedback.swift:17` — `displayDuration` (1.0s) and the + existing sleep-then-clear pattern in `DictationViewModel.startDictation()`. +- `SingleThread/ReminderDictation.swift:216` — `DictationError` enum + (`noSpeechDetected` etc.) producing the localized strings. +- `SingleThreadTests/ReminderDictationTests.swift` — existing + `DictationViewModel` tests (e.g. `startDictationAddsReminderAndFlowsText`, + `dictationError == nil` assertions) to extend. diff --git a/SingleThread/DictationViewModel.swift b/SingleThread/DictationViewModel.swift index 67021a21..2142256d 100644 --- a/SingleThread/DictationViewModel.swift +++ b/SingleThread/DictationViewModel.swift @@ -12,13 +12,18 @@ final class DictationViewModel { init( speechTranscriber: any SpeechTranscribing, - store: ReminderStore) { + store: ReminderStore, + errorDisplayDuration: TimeInterval = DictationViewModel.dictationErrorDisplayDuration) { self.speechTranscriber = speechTranscriber self.store = store + self.errorDisplayDuration = errorDisplayDuration } // MARK: Internal + /// Seconds a dictation error stays visible before it clears itself. + static let dictationErrorDisplayDuration: TimeInterval = 2.0 + private(set) var isDictating = false var isProcessing = false var dictationText = "" @@ -47,23 +52,23 @@ final class DictationViewModel { if speechTranscriber.authorizationStatus == .notDetermined { let status = await speechTranscriber.requestAuthorization() guard status == .authorized else { - dictationError = String( + showDictationError(String( localized: "Speech recognition access is required.", table: "Localizable", - bundle: .main) + bundle: .main)) return } } guard speechTranscriber.authorizationStatus == .authorized else { - dictationError = String( + showDictationError(String( localized: "Speech recognition access was denied.", table: "Localizable", - bundle: .main) + bundle: .main)) return } isDictating = true dictationText = "" - dictationError = nil + clearDictationError() do { let result = try await speechTranscriber.transcribe { [weak self] text in self?.dictationText = text @@ -87,7 +92,7 @@ final class DictationViewModel { creationFeedback = nil } } catch { - dictationError = error.localizedDescription + showDictationError(error.localizedDescription) isDictating = false isProcessing = true } @@ -98,4 +103,31 @@ final class DictationViewModel { private let speechTranscriber: any SpeechTranscribing private let store: ReminderStore + private let errorDisplayDuration: TimeInterval + @ObservationIgnored private var dictationErrorClearTask: Task? + @ObservationIgnored private var dictationErrorGeneration = 0 + + /// Shows `message` and schedules it to clear after ``errorDisplayDuration``. + /// A newer error cancels any pending timer; a timer whose generation is stale + /// is ignored so it cannot clear a newer error. + private func showDictationError(_ message: String) { + dictationErrorGeneration &+= 1 + let generation = dictationErrorGeneration + let duration = errorDisplayDuration + dictationErrorClearTask?.cancel() + dictationError = message + dictationErrorClearTask = Task { [weak self] in + try? await Task.sleep(nanoseconds: UInt64(duration * 1_000_000_000)) + guard !Task.isCancelled else { return } + guard let self, dictationErrorGeneration == generation else { return } + dictationError = nil + } + } + + private func clearDictationError() { + dictationErrorGeneration &+= 1 + dictationErrorClearTask?.cancel() + dictationErrorClearTask = nil + dictationError = nil + } } diff --git a/SingleThreadTests/ReminderDictationTests.swift b/SingleThreadTests/ReminderDictationTests.swift index 1577c296..35bb3774 100644 --- a/SingleThreadTests/ReminderDictationTests.swift +++ b/SingleThreadTests/ReminderDictationTests.swift @@ -274,6 +274,56 @@ struct ReminderDictationTests { #expect(!viewModel.isProcessing) } + @Test + func dictationErrorClearsAfterDelay() async { + let fake = FakeSpeechTranscriber(transcriptionError: DictationError.noSpeechDetected) + let store = ReminderStore(eventStore: InMemoryEventStore(), loadsReminders: false) + let viewModel = DictationViewModel( + speechTranscriber: fake, store: store, errorDisplayDuration: 0.2) + + await viewModel.startDictation() + #expect(viewModel.dictationError != nil) + + for _ in 0 ..< 50 { + if viewModel.dictationError == nil { + break + } + try? await Task.sleep(nanoseconds: 50_000_000) + } + #expect(viewModel.dictationError == nil) + } + + @Test + func staleTimerDoesNotClearNewerDictationError() async { + let fake = FakeSpeechTranscriber(authorizationStatus: .denied) + let store = ReminderStore(eventStore: InMemoryEventStore(), loadsReminders: false) + let viewModel = DictationViewModel( + speechTranscriber: fake, store: store, errorDisplayDuration: 0.5) + + await viewModel.startDictation() + #expect(viewModel.dictationError != nil) + + // A newer error arrives while the first timer is pending, restarting it. + try? await Task.sleep(nanoseconds: 400_000_000) + await viewModel.startDictation() + let newerError = viewModel.dictationError + #expect(newerError != nil) + + // Past the first timer's fire time (~0.5s), before the newer one (~0.9s): + // the newer error must still be present. + try? await Task.sleep(nanoseconds: 300_000_000) + #expect(viewModel.dictationError == newerError) + + // The newer error then expires on its own timer. + for _ in 0 ..< 60 { + if viewModel.dictationError == nil { + break + } + try? await Task.sleep(nanoseconds: 50_000_000) + } + #expect(viewModel.dictationError == nil) + } + @Test func reentryGuardBlocksConcurrentCalls() async { let fake = FakeSpeechTranscriber(