Queue accepted Settings choices before replacement reads
This commit is contained in:
@@ -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<SettingsTopicTabs>
|
||||
final Set<SettingsTopic> _visited = {SettingsTopic.appearance};
|
||||
SettingsTopicStore? _store;
|
||||
int _revision = 0;
|
||||
int _storeGeneration = 0;
|
||||
bool _restoring = false;
|
||||
Future<void> _writes = Future<void>.value();
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
@@ -64,15 +63,11 @@ class _SettingsTopicTabsState extends State<SettingsTopicTabs>
|
||||
}
|
||||
|
||||
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<SettingsTopicTabs>
|
||||
Future<void> _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<SettingsTopicTabs>
|
||||
_restoring = false;
|
||||
}
|
||||
|
||||
Future<T> _ordered<T>(
|
||||
SettingsTopicStore store,
|
||||
Future<T> 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<SettingsTopicTabs>
|
||||
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
|
||||
|
||||
@@ -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<void> 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.
|
||||
|
||||
Reference in New Issue
Block a user