diff --git a/app/lib/features/settings/settings_screen.dart b/app/lib/features/settings/settings_screen.dart index 7e699f6e..dc123cf0 100644 --- a/app/lib/features/settings/settings_screen.dart +++ b/app/lib/features/settings/settings_screen.dart @@ -54,6 +54,7 @@ class SettingsScreen extends StatefulWidget { this.store, this.ping, this.embedded = false, + this.navigationScope, this.onOpenTemplates, this.onOpenSchedules, this.notifications, @@ -96,6 +97,9 @@ class SettingsScreen extends StatefulWidget { /// The application shell already owns the native navigation bar and safe area. final bool embedded; + /// The logical server scope used to restore the last Settings category. + final String? navigationScope; + /// Opens the Templates gallery. Null keeps Settings standalone/incomplete /// without a dead row that would go nowhere. final VoidCallback? onOpenTemplates; @@ -172,7 +176,10 @@ class _SettingsScreenState extends State { @override Widget build(BuildContext context) { - final content = SettingsTopicTabs(pages: _topicPages()); + final content = SettingsTopicTabs( + pages: _topicPages(), + navigationScope: widget.navigationScope, + ); if (widget.embedded) { // Under the shell's bar the Cupertino page is the grouped background // right up to the top edge: the bar's scrim takes the same colour, so diff --git a/app/lib/features/settings/settings_tabs.dart b/app/lib/features/settings/settings_tabs.dart index c8151c35..2e5fdc30 100644 --- a/app/lib/features/settings/settings_tabs.dart +++ b/app/lib/features/settings/settings_tabs.dart @@ -1,8 +1,11 @@ +import 'dart:async'; + import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'settings_layout.dart'; import 'settings_theme.dart'; import 'settings_page_body.dart'; +import 'settings_topic_store.dart'; enum SettingsTopic { appearance('Appearance'), @@ -18,8 +21,17 @@ enum SettingsTopic { /// Topic pages are created on first visit, then retained with their drafts and /// independent scroll positions. Hidden pages do not tick animations. class SettingsTopicTabs extends StatefulWidget { - const SettingsTopicTabs({super.key, required this.pages}); + const SettingsTopicTabs({ + super.key, + required this.pages, + this.navigationScope, + this.selectionStore, + }); final Map pages; + + /// Null keeps standalone previews independent of application persistence. + final String? navigationScope; + final SettingsTopicStore? selectionStore; @override State createState() => _SettingsTopicTabsState(); } @@ -29,11 +41,55 @@ class _SettingsTopicTabsState extends State late final TabController _tabs; SettingsTopic _selected = SettingsTopic.appearance; final Set _visited = {SettingsTopic.appearance}; + SettingsTopicStore? _store; + int _revision = 0; + bool _restoring = false; + Future _writes = Future.value(); @override void initState() { super.initState(); _tabs = TabController(length: SettingsTopic.values.length, vsync: this); _tabs.addListener(_changed); + _bindStore(); + } + + @override + void didUpdateWidget(SettingsTopicTabs oldWidget) { + super.didUpdateWidget(oldWidget); + if (oldWidget.navigationScope != widget.navigationScope || + oldWidget.selectionStore != widget.selectionStore) { + _bindStore(); + } + } + + void _bindStore() { + _store = + widget.selectionStore ?? + (widget.navigationScope == null + ? null + : SharedPreferencesSettingsTopicStore(widget.navigationScope!)); + final revision = ++_revision; + final store = _store; + if (store == null) return; + unawaited(_restore(store, revision)); + } + + Future _restore(SettingsTopicStore store, int revision) async { + String? saved; + try { + saved = await store.load(); + } catch (_) { + return; + } + if (!mounted || revision != _revision) return; + final topic = + SettingsTopic.values + .where((topic) => topic.name == saved) + .firstOrNull ?? + SettingsTopic.appearance; + _restoring = true; + _tabs.index = topic.index; + _restoring = false; } void _changed() { @@ -43,6 +99,18 @@ class _SettingsTopicTabsState extends State _selected = next; _visited.add(next); }); + if (_restoring) return; + ++_revision; + final store = _store; + if (store == null) return; + // Serial writes ensure a slower previous selection cannot win last. + _writes = _writes.then((_) async { + try { + await store.save(next.name); + } catch (_) { + // Navigation continues even if the local preference cannot be saved. + } + }); } @override diff --git a/app/lib/features/settings/settings_topic_store.dart b/app/lib/features/settings/settings_topic_store.dart new file mode 100644 index 00000000..3d80ccf1 --- /dev/null +++ b/app/lib/features/settings/settings_topic_store.dart @@ -0,0 +1,35 @@ +import 'package:shared_preferences/shared_preferences.dart'; + +import '../../core/servers/server_scope.dart'; + +const settingsTopicKey = 'pipelines.next.settingsTopic.v1'; + +abstract interface class SettingsTopicStore { + Future load(); + Future save(String topic); +} + +/// Stores only the selected category, never unsaved fields or popup state. +class SharedPreferencesSettingsTopicStore implements SettingsTopicStore { + const SharedPreferencesSettingsTopicStore(this.scope); + final String scope; + String get key => scopedServerKey(settingsTopicKey, scope); + + @override + Future load() async { + try { + return (await SharedPreferences.getInstance()).getString(key); + } catch (_) { + return null; + } + } + + @override + Future save(String topic) async { + try { + await (await SharedPreferences.getInstance()).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 new file mode 100644 index 00000000..25785baa --- /dev/null +++ b/app/notes/settings-topic-navigation.md @@ -0,0 +1,21 @@ +# Restored Settings category + +`SettingsScreen.navigationScope` opts the application's Settings page into +server-scoped category persistence. The app owner supplies the same logical +server storage scope used by its other navigation state. Standalone Settings +previews and fixtures keep the default null scope and do not touch preferences. + +The selected category is saved by enum name under the scoped +`pipelines.next.settingsTopic.v1` key. Missing, malformed or unreadable values +use Appearance. A delayed restore cannot replace a newer user selection, and +writes are serialized so an older slow write cannot win after a newer choice. +Existing lazy pages stay mounted with their draft and scroll state. No unsaved +fields, dialogs or popup state are stored. + +Checks: 23 focused Settings tabs/topic-restore/screen tests passed +(`/tmp/memwatch/out.EFYNIh`), including Material and Cupertino recreation, +server isolation, malformed input, opt-out behavior, delayed restoration and +write ordering. Existing retained-page draft/scroll tests also pass. Analyzer +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. diff --git a/app/test/features/settings/settings_topic_restore_test.dart b/app/test/features/settings/settings_topic_restore_test.dart new file mode 100644 index 00000000..4878fd2c --- /dev/null +++ b/app/test/features/settings/settings_topic_restore_test.dart @@ -0,0 +1,132 @@ +import 'dart:async'; + +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/core/servers/server_scope.dart'; +import 'package:pipelines_app/features/settings/settings_tabs.dart'; +import 'package:pipelines_app/features/settings/settings_topic_store.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +import 'settings_topic_test_helper.dart'; + +Widget host({ + bool cupertino = false, + String? scope, + SettingsTopicStore? store, +}) { + final tabs = SettingsTopicTabs( + navigationScope: scope, + selectionStore: store, + pages: { + for (final topic in SettingsTopic.values) + topic: (_) => Text('${topic.name} page'), + }, + ); + return cupertino + ? CupertinoApp(home: CupertinoPageScaffold(child: tabs)) + : MaterialApp(home: Scaffold(body: tabs)); +} + +class DelayedStore implements SettingsTopicStore { + final read = Completer(); + final saves = []; + final pending = >[]; + bool blockWrites = false; + @override + Future load() => read.future; + @override + Future save(String topic) async { + saves.add(topic); + if (blockWrites) { + final gate = Completer(); + pending.add(gate); + await gate.future; + } + } +} + +void main() { + for (final cupertino in [false, true]) { + testWidgets('restores selected category after recreation ($cupertino)', ( + tester, + ) async { + SharedPreferences.setMockInitialValues({}); + await tester.pumpWidget(host(cupertino: cupertino, scope: 'server-a')); + await tester.pumpAndSettle(); + await selectSettingsTopic(tester, 'Connection'); + expect(find.text('connection page'), findsOneWidget); + await tester.pumpWidget(const SizedBox.shrink()); + await tester.pumpWidget(host(cupertino: cupertino, scope: 'server-a')); + await tester.pumpAndSettle(); + expect(find.text('connection page'), findsOneWidget); + final preferences = await SharedPreferences.getInstance(); + expect( + preferences.getString(scopedServerKey(settingsTopicKey, 'server-a')), + 'connection', + ); + }); + } + + testWidgets('server scopes and malformed selections are independent', ( + tester, + ) async { + SharedPreferences.setMockInitialValues({ + scopedServerKey(settingsTopicKey, 'server-a'): 'connection', + scopedServerKey(settingsTopicKey, 'server-b'): 'not-a-topic', + }); + await tester.pumpWidget(host(scope: 'server-a')); + await tester.pumpAndSettle(); + expect(find.text('connection page'), findsOneWidget); + await tester.pumpWidget(host(scope: 'server-b')); + await tester.pumpAndSettle(); + expect(find.text('appearance page'), findsOneWidget); + await tester.pumpWidget(host(scope: 'server-a')); + await tester.pumpAndSettle(); + expect(find.text('connection page'), findsOneWidget); + }); + + testWidgets('standalone category tabs do not read or write preferences', ( + tester, + ) async { + SharedPreferences.setMockInitialValues({settingsTopicKey: 'updates'}); + await tester.pumpWidget(host()); + await tester.pumpAndSettle(); + expect(find.text('appearance page'), findsOneWidget); + await selectSettingsTopic(tester, 'Connection'); + expect( + (await SharedPreferences.getInstance()).getString(settingsTopicKey), + 'updates', + ); + }); + + testWidgets('late restore cannot overwrite a newer user selection', ( + tester, + ) async { + final store = DelayedStore(); + await tester.pumpWidget(host(store: store)); + await selectSettingsTopic(tester, 'Connection'); + store.read.complete('updates'); + await tester.pumpAndSettle(); + expect(find.text('connection page'), findsOneWidget); + expect(store.saves, ['connection']); + }); + + testWidgets('writes are serialized so the latest selection wins last', ( + tester, + ) async { + final store = DelayedStore()..blockWrites = true; + store.read.complete(null); + await tester.pumpWidget(host(store: store)); + await tester.pumpAndSettle(); + await selectSettingsTopic(tester, 'Connection'); + await selectSettingsTopic(tester, 'Notifications'); + expect(store.saves, ['connection']); + store.pending.first.complete(); + await tester.pumpAndSettle(); + expect(store.saves, ['connection', 'notifications']); + store.pending.last.complete(); + await tester.pumpAndSettle(); + expect(find.text('notifications page'), findsOneWidget); + }); +}