refactor persist bool provider

This commit is contained in:
Fabian Freund
2026-02-28 18:51:15 +01:00
parent 71974935de
commit 67c6222245
13 changed files with 149 additions and 280 deletions
@@ -1,46 +0,0 @@
/*
* Copyright (c) 2024-2026 Fabian Freund.
*
* This file is part of WebLibre
* (see https://weblibre.eu).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import 'dart:convert';
import 'package:riverpod/experimental/persist.dart';
import 'package:riverpod_annotation/experimental/persist.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:weblibre/features/user/data/providers.dart';
part 'search_suggestions_view.g.dart';
@Riverpod(keepAlive: true)
class SearchSuggestionsExpanded extends _$SearchSuggestionsExpanded {
void toggle() {
state = !state;
}
@override
bool build() {
persist(
ref.watch(riverpodDatabaseStorageProvider),
key: 'SearchSuggestionsExpanded',
encode: (state) => jsonEncode([state]),
decode: (encoded) => (jsonDecode(encoded) as List<dynamic>).first as bool,
);
return stateOrNull ?? true;
}
}
@@ -1,63 +0,0 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'search_suggestions_view.dart';
// **************************************************************************
// RiverpodGenerator
// **************************************************************************
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint, type=warning
@ProviderFor(SearchSuggestionsExpanded)
final searchSuggestionsExpandedProvider = SearchSuggestionsExpandedProvider._();
final class SearchSuggestionsExpandedProvider
extends $NotifierProvider<SearchSuggestionsExpanded, bool> {
SearchSuggestionsExpandedProvider._()
: super(
from: null,
argument: null,
retry: null,
name: r'searchSuggestionsExpandedProvider',
isAutoDispose: false,
dependencies: null,
$allTransitiveDependencies: null,
);
@override
String debugGetCreateSourceHash() => _$searchSuggestionsExpandedHash();
@$internal
@override
SearchSuggestionsExpanded create() => SearchSuggestionsExpanded();
/// {@macro riverpod.override_with_value}
Override overrideWithValue(bool value) {
return $ProviderOverride(
origin: this,
providerOverride: $SyncValueProvider<bool>(value),
);
}
}
String _$searchSuggestionsExpandedHash() =>
r'520bcdb9ef624c2ebe5a570d4dab034de5b4f475';
abstract class _$SearchSuggestionsExpanded extends $Notifier<bool> {
bool build();
@$mustCallSuper
@override
void runBuild() {
final ref = this.ref as $Ref<bool, bool>;
final element =
ref.element
as $ClassProviderElement<
AnyNotifier<bool, bool>,
bool,
Object?,
Object?
>;
element.handleCreate(ref, build);
}
}
@@ -24,11 +24,11 @@ import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:nullability/nullability.dart';
import 'package:sliver_tools/sliver_tools.dart';
import 'package:weblibre/core/providers/persisted_bool.dart';
import 'package:weblibre/features/bangs/data/models/bang_data.dart';
import 'package:weblibre/features/bangs/domain/providers/bangs.dart';
import 'package:weblibre/features/bangs/domain/repositories/data.dart';
import 'package:weblibre/features/geckoview/features/search/domain/providers/search_suggestions.dart';
import 'package:weblibre/features/geckoview/features/search/domain/providers/search_suggestions_view.dart';
import 'package:weblibre/features/geckoview/features/search/presentation/widgets/smart_bang_selector.dart';
class FullSearchTermSuggestions extends HookConsumerWidget {
@@ -58,7 +58,7 @@ class FullSearchTermSuggestions extends HookConsumerWidget {
final searchSuggestions = ref.watch(searchSuggestionsProvider());
final searchHistory = ref.watch(searchHistoryProvider);
final expanded = ref.watch(searchSuggestionsExpandedProvider);
final expanded = ref.watch(persistedBoolProvider(PersistedBoolKey.searchSuggestionsExpanded));
useOnListenableChange(searchTextController, () {
ref
@@ -111,7 +111,7 @@ class FullSearchTermSuggestions extends HookConsumerWidget {
padding: const EdgeInsets.only(top: 6.0),
child: IconButton(
onPressed: ref
.read(searchSuggestionsExpandedProvider.notifier)
.read(persistedBoolProvider(PersistedBoolKey.searchSuggestionsExpanded).notifier)
.toggle,
icon: Icon(expanded ? Icons.unfold_less : Icons.unfold_more),
),