fix: prefs persistence

This commit is contained in:
problematicconsumer
2023-09-17 14:24:25 +03:30
parent 493be47c8f
commit e6eab038ce
4 changed files with 73 additions and 145 deletions

View File

@@ -26,12 +26,24 @@ enum ProxiesSort {
};
}
final proxiesSortProvider = AlwaysAlivePrefNotifier.provider(
"proxies_sort_mode",
ProxiesSort.unsorted,
mapFrom: ProxiesSort.values.byName,
mapTo: (value) => value.name,
);
@Riverpod(keepAlive: true)
class ProxiesSortNotifier extends _$ProxiesSortNotifier {
late final _pref = Pref(
ref.watch(sharedPreferencesProvider),
"proxies_sort_mode",
ProxiesSort.unsorted,
mapFrom: ProxiesSort.values.byName,
mapTo: (value) => value.name,
);
@override
ProxiesSort build() => _pref.getValue();
Future<void> update(ProxiesSort value) {
state = value;
return _pref.update(value);
}
}
@riverpod
class ProxiesNotifier extends _$ProxiesNotifier with AppLogger {
@@ -42,7 +54,7 @@ class ProxiesNotifier extends _$ProxiesNotifier with AppLogger {
if (!serviceRunning) {
throw const CoreServiceNotRunning();
}
final sortBy = ref.watch(proxiesSortProvider);
final sortBy = ref.watch(proxiesSortNotifierProvider);
yield* ref
.watch(coreFacadeProvider)
.watchOutbounds()

View File

@@ -16,7 +16,7 @@ class ProxiesPage extends HookConsumerWidget with PresLogger {
final asyncProxies = ref.watch(proxiesNotifierProvider);
final notifier = ref.watch(proxiesNotifierProvider.notifier);
final sortBy = ref.watch(proxiesSortProvider);
final sortBy = ref.watch(proxiesSortNotifierProvider);
final selectActiveProxyMutation = useMutation(
initialOnFailure: (error) =>
@@ -55,7 +55,8 @@ class ProxiesPage extends HookConsumerWidget with PresLogger {
actions: [
PopupMenuButton<ProxiesSort>(
initialValue: sortBy,
onSelected: ref.read(proxiesSortProvider.notifier).update,
onSelected:
ref.read(proxiesSortNotifierProvider.notifier).update,
icon: const Icon(Icons.sort),
tooltip: t.proxies.sortTooltip,
itemBuilder: (context) {