From eee3b70684dcacbf211f1860b337753e6f9f11f6 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Sun, 8 Nov 2026 01:13:27 +0000 Subject: [PATCH] Queue accepted Settings choices before replacement reads --- app/lib/features/settings/settings_tabs.dart | 41 +++--- .../settings/settings_topic_store.dart | 5 +- app/notes/settings-topic-navigation.md | 25 ++-- .../settings/settings_topic_restore_test.dart | 121 +++++++++--------- 4 files changed, 98 insertions(+), 94 deletions(-) diff --git a/app/lib/features/settings/settings_tabs.dart b/app/lib/features/settings/settings_tabs.dart index 38771889..5c5b148b 100644 --- a/app/lib/features/settings/settings_tabs.dart +++ b/app/lib/features/settings/settings_tabs.dart @@ -6,6 +6,7 @@ import 'settings_layout.dart'; import 'settings_theme.dart'; import 'settings_page_body.dart'; import 'settings_topic_store.dart'; +import '../../core/storage/ordered_local_storage.dart'; enum SettingsTopic { appearance('Appearance'), @@ -43,9 +44,7 @@ class _SettingsTopicTabsState extends State final Set _visited = {SettingsTopic.appearance}; SettingsTopicStore? _store; int _revision = 0; - int _storeGeneration = 0; bool _restoring = false; - Future _writes = Future.value(); @override void initState() { super.initState(); @@ -64,15 +63,11 @@ class _SettingsTopicTabsState extends State } void _bindStore() { - final generation = ++_storeGeneration; _store = widget.selectionStore ?? (widget.navigationScope == null ? null - : SharedPreferencesSettingsTopicStore( - widget.navigationScope!, - canSave: () => mounted && generation == _storeGeneration, - )); + : SharedPreferencesSettingsTopicStore(widget.navigationScope!)); final revision = ++_revision; final store = _store; if (store == null) return; @@ -82,7 +77,7 @@ class _SettingsTopicTabsState extends State Future _restore(SettingsTopicStore store, int revision) async { String? saved; try { - saved = await store.load(); + saved = await _ordered(store, store.load); } catch (_) { return; } @@ -97,7 +92,25 @@ class _SettingsTopicTabsState extends State _restoring = false; } + Future _ordered( + SettingsTopicStore store, + Future Function() operation, + ) => store is SharedPreferencesSettingsTopicStore + ? operation() + : orderedLocalStorage(store, operation); + + void _persist(SettingsTopicStore store, String topic) { + // Register accepted choices synchronously, before a replacement can load. + // The production store coordinates all instances by their preference key. + unawaited( + _ordered(store, () => store.save(topic)).catchError((Object _) { + // Navigation continues when the local preference cannot be saved. + }), + ); + } + void _changed() { + if (!mounted) return; final next = SettingsTopic.values[_tabs.index]; if (next == _selected) return; setState(() { @@ -107,17 +120,7 @@ class _SettingsTopicTabsState extends State if (_restoring) return; ++_revision; final store = _store; - final generation = _storeGeneration; - if (store == null) return; - // Serial writes ensure a slower previous selection cannot win last. - _writes = _writes.then((_) async { - if (!mounted || generation != _storeGeneration) return; - try { - await store.save(next.name); - } catch (_) { - // Navigation continues even if the local preference cannot be saved. - } - }); + if (store != null) _persist(store, next.name); } @override diff --git a/app/lib/features/settings/settings_topic_store.dart b/app/lib/features/settings/settings_topic_store.dart index 2f35218a..504613c7 100644 --- a/app/lib/features/settings/settings_topic_store.dart +++ b/app/lib/features/settings/settings_topic_store.dart @@ -12,9 +12,8 @@ abstract interface class SettingsTopicStore { /// Stores only the selected category, never unsaved fields or popup state. class SharedPreferencesSettingsTopicStore implements SettingsTopicStore { - const SharedPreferencesSettingsTopicStore(this.scope, {this.canSave}); + const SharedPreferencesSettingsTopicStore(this.scope); final String scope; - final bool Function()? canSave; String get key => scopedServerKey(settingsTopicKey, scope); @override @@ -28,10 +27,8 @@ class SharedPreferencesSettingsTopicStore implements SettingsTopicStore { @override Future save(String topic) => orderedLocalStorage(key, () async { - if (canSave?.call() == false) return; try { final preferences = await SharedPreferences.getInstance(); - if (canSave?.call() == false) return; await preferences.setString(key, topic); } catch (_) { // The mounted category remains usable when local storage is unavailable. diff --git a/app/notes/settings-topic-navigation.md b/app/notes/settings-topic-navigation.md index 80d0e513..8a7de9fa 100644 --- a/app/notes/settings-topic-navigation.md +++ b/app/notes/settings-topic-navigation.md @@ -20,16 +20,19 @@ clean (`flutter-analyze.q5b4VU`). Manager owns the full-app integration wiring, rendered tab-switch/relaunch capture and delivery. No deployment, server restart or device testing was performed in this lane. -Replacement ownership is also guarded: a disposed widget or changed scope skips -queued saves before they start, and the production store rechecks its owner -inside the shared per-key storage queue. A replacement Settings page reads after -any already-started write from the previous owner, then saves its newer choice -last. The queue is shared with tab-location persistence through + +Accepted category choices register their writes synchronously in the shared +per-key queue. They are not canceled on disposal: old A, accepted old B, +replacement load, and newer C execute in that order. Thus a normal recreation +restores B, while a newer replacement choice C still wins. Production stores +coordinate all instances by actual preference key; injected stores are +coordinated by store identity. Restoration still checks mounted/revision before +changing the UI. No widget-local deferred write chain can enqueue old B late. +The queue is shared with tab-location persistence through `core/storage/ordered_local_storage.dart` (companion prerequisite `f6e1ede6`). -The follow-up passed 27 tests including the shared queue tests -(`/tmp/memwatch/out.a86IS6`) and clean analysis (`2CMlDH`). The new recreation -regression blocks old choice A, queues old choice B, disposes the widget, -chooses C in its replacement, then releases A: B never writes and C remains -selected/persisted. A production-store test also verifies reads wait for pending -writes and an invalidated queued save is skipped inside the operation. +Recreation regressions cover both no-new-choice (B restored) and a new choice +(C wins), plus a production two-instance store read after accepted queued +writes. All existing topic retention, scope and delayed-load checks remain. +Final durability follow-up: 28 focused tests passed (`/tmp/memwatch/out.13xC9z`), +including the shared queue tests; analyzer clean (`flutter-analyze.NUqNw2`). diff --git a/app/test/features/settings/settings_topic_restore_test.dart b/app/test/features/settings/settings_topic_restore_test.dart index 9f127714..22361382 100644 --- a/app/test/features/settings/settings_topic_restore_test.dart +++ b/app/test/features/settings/settings_topic_restore_test.dart @@ -48,7 +48,6 @@ class DelayedStore implements SettingsTopicStore { } class SharedTopicBackend { - final key = Object(); final firstWrite = Completer(); final writes = []; String? value; @@ -58,14 +57,13 @@ class SharedQueuedTopicStore implements SettingsTopicStore { SharedQueuedTopicStore(this.backend); final SharedTopicBackend backend; @override - Future load() => - orderedLocalStorage(backend.key, () async => backend.value); + Future load() async => backend.value; @override - Future save(String topic) => orderedLocalStorage(backend.key, () async { + Future save(String topic) async { backend.writes.add(topic); if (backend.writes.length == 1) await backend.firstWrite.future; backend.value = topic; - }); + } } void main() { @@ -151,63 +149,66 @@ void main() { await tester.pumpAndSettle(); expect(find.text('notifications page'), findsOneWidget); }); - testWidgets('recreated owner wins over disposed owner queued selections', ( + for (final newerChoice in [false, true]) { + testWidgets('recreation preserves accepted B; newer choice: $newerChoice', ( + tester, + ) async { + final backend = SharedTopicBackend(); + final store = SharedQueuedTopicStore(backend); + await tester.pumpWidget(host(store: store)); + await tester.pumpAndSettle(); + await selectSettingsTopic(tester, 'Connection'); + expect(backend.writes, ['connection']); + await selectSettingsTopic(tester, 'Notifications'); + expect(backend.writes, ['connection']); + await tester.pumpWidget(const SizedBox.shrink()); + await tester.pumpWidget(host(store: store)); + if (newerChoice) await selectSettingsTopic(tester, 'Tools'); + backend.firstWrite.complete(); + await tester.pumpAndSettle(); + expect( + find.text(newerChoice ? 'tools page' : 'notifications page'), + findsOneWidget, + ); + expect(backend.value, newerChoice ? 'tools' : 'notifications'); + expect(backend.writes, [ + 'connection', + 'notifications', + if (newerChoice) 'tools', + ]); + }); + } + + testWidgets('production replacement reads after all accepted old writes', ( tester, ) async { - final backend = SharedTopicBackend(); - await tester.pumpWidget(host(store: SharedQueuedTopicStore(backend))); - await tester.pumpAndSettle(); - await selectSettingsTopic(tester, 'Connection'); - expect(backend.writes, ['connection']); - await selectSettingsTopic(tester, 'Notifications'); - expect(backend.writes, [ - 'connection', - ], reason: 'old second choice waits behind first write'); - await tester.pumpWidget(const SizedBox.shrink()); - await tester.pumpWidget(host(store: SharedQueuedTopicStore(backend))); - // Restoration waits for the old write, while this new owner remains usable. - await selectSettingsTopic(tester, 'Tools'); - backend.firstWrite.complete(); - await tester.pumpAndSettle(); - expect(find.text('tools page'), findsOneWidget); - expect(backend.value, 'tools'); - expect(backend.writes, ['connection', 'tools']); - }); - - testWidgets( - 'production reads await pending writes and disposed saves are skipped', - (tester) async { - SharedPreferences.setMockInitialValues({}); - var current = true; - final store = SharedPreferencesSettingsTopicStore( - 'ownership-check', - canSave: () => current, - ); - final gate = Completer(); - final oldWrite = orderedLocalStorage(store.key, () async { - await gate.future; - await (await SharedPreferences.getInstance()).setString( - store.key, - 'connection', - ); - }); - var readFinished = false; - final read = store.load().then((value) { - readFinished = true; - return value; - }); - final staleQueuedSave = store.save('notifications'); - await tester.pump(); - expect(readFinished, isFalse); - current = false; - gate.complete(); - await oldWrite; - expect(await read, 'connection'); - await staleQueuedSave; - expect( - (await SharedPreferences.getInstance()).getString(store.key), + SharedPreferences.setMockInitialValues({}); + const oldStore = SharedPreferencesSettingsTopicStore('ownership-check'); + final gate = Completer(); + final oldA = orderedLocalStorage(oldStore.key, () async { + await gate.future; + await (await SharedPreferences.getInstance()).setString( + oldStore.key, 'connection', ); - }, - ); + }); + final oldB = oldStore.save('notifications'); + final replacement = SharedPreferencesSettingsTopicStore('ownership-check'); + var readFinished = false; + final read = replacement.load().then((value) { + readFinished = true; + return value; + }); + await tester.pump(); + expect(readFinished, isFalse); + gate.complete(); + await oldA; + await oldB; + expect(await read, 'notifications'); + await replacement.save('tools'); + expect( + (await SharedPreferences.getInstance()).getString(oldStore.key), + 'tools', + ); + }); }