Restore server-scoped Settings category after relaunch

This commit is contained in:
2026-11-08 00:57:31 +00:00
parent 4fc397f0ae
commit 2aa0000382
5 changed files with 265 additions and 2 deletions
@@ -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<SettingsScreen> {
@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
+69 -1
View File
@@ -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<SettingsTopic, WidgetBuilder> pages;
/// Null keeps standalone previews independent of application persistence.
final String? navigationScope;
final SettingsTopicStore? selectionStore;
@override
State<SettingsTopicTabs> createState() => _SettingsTopicTabsState();
}
@@ -29,11 +41,55 @@ class _SettingsTopicTabsState extends State<SettingsTopicTabs>
late final TabController _tabs;
SettingsTopic _selected = SettingsTopic.appearance;
final Set<SettingsTopic> _visited = {SettingsTopic.appearance};
SettingsTopicStore? _store;
int _revision = 0;
bool _restoring = false;
Future<void> _writes = Future<void>.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<void> _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<SettingsTopicTabs>
_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
@@ -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<String?> load();
Future<void> 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<String?> load() async {
try {
return (await SharedPreferences.getInstance()).getString(key);
} catch (_) {
return null;
}
}
@override
Future<void> save(String topic) async {
try {
await (await SharedPreferences.getInstance()).setString(key, topic);
} catch (_) {
// The mounted category remains usable when local storage is unavailable.
}
}
}