persist bookmarks ui state

This commit is contained in:
Fabian Freund
2026-07-02 11:01:21 +02:00
parent 7e3a957c3c
commit 867fd47a8f
4 changed files with 95 additions and 3 deletions
@@ -19,15 +19,21 @@
*/
import 'package:copy_with_extension/copy_with_extension.dart';
import 'package:fast_equatable/fast_equatable.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:weblibre/features/geckoview/features/bookmarks/domain/entities/bookmark_sort_type.dart';
part 'bookmark_list_ui_state.g.dart';
@JsonSerializable()
@CopyWith()
class BookmarkListUiState with FastEquatable {
@JsonKey(includeFromJson: false, includeToJson: false)
final bool selectionMode;
@JsonKey(includeFromJson: false, includeToJson: false)
final Set<String> selectedGuids;
@JsonKey(defaultValue: BookmarkSortType.manual)
final BookmarkSortType sortType;
@JsonKey(defaultValue: false)
final bool foldersOnly;
BookmarkListUiState({
@@ -37,6 +43,11 @@ class BookmarkListUiState with FastEquatable {
this.foldersOnly = false,
});
factory BookmarkListUiState.fromJson(Map<String, dynamic> json) =>
_$BookmarkListUiStateFromJson(json);
Map<String, dynamic> toJson() => _$BookmarkListUiStateToJson(this);
@override
List<Object?> get hashParameters => [
selectionMode,
@@ -98,3 +98,32 @@ extension $BookmarkListUiStateCopyWith on BookmarkListUiState {
_$BookmarkListUiStateCWProxy get copyWith =>
_$BookmarkListUiStateCWProxyImpl(this);
}
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
BookmarkListUiState _$BookmarkListUiStateFromJson(Map<String, dynamic> json) =>
BookmarkListUiState(
sortType:
$enumDecodeNullable(_$BookmarkSortTypeEnumMap, json['sortType']) ??
BookmarkSortType.manual,
foldersOnly: json['foldersOnly'] as bool? ?? false,
);
Map<String, dynamic> _$BookmarkListUiStateToJson(
BookmarkListUiState instance,
) => <String, dynamic>{
'sortType': _$BookmarkSortTypeEnumMap[instance.sortType]!,
'foldersOnly': instance.foldersOnly,
};
const _$BookmarkSortTypeEnumMap = {
BookmarkSortType.manual: 'manual',
BookmarkSortType.titleAsc: 'titleAsc',
BookmarkSortType.titleDesc: 'titleDesc',
BookmarkSortType.urlAsc: 'urlAsc',
BookmarkSortType.urlDesc: 'urlDesc',
BookmarkSortType.dateAddedDesc: 'dateAddedDesc',
BookmarkSortType.dateAddedAsc: 'dateAddedAsc',
};
@@ -17,16 +17,27 @@
* 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 'package:riverpod/experimental/persist.dart';
import 'package:riverpod_annotation/experimental/json_persist.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:weblibre/features/geckoview/features/bookmarks/domain/entities/bookmark_list_ui_state.dart';
import 'package:weblibre/features/geckoview/features/bookmarks/domain/entities/bookmark_sort_type.dart';
import 'package:weblibre/features/user/data/providers.dart';
part 'bookmark_list_ui_state.g.dart';
@Riverpod()
@JsonPersist()
class BookmarkListUiStateNotifier extends _$BookmarkListUiStateNotifier {
@override
BookmarkListUiState build() => BookmarkListUiState();
BookmarkListUiState build() {
persist(
ref.watch(riverpodDatabaseStorageProvider),
key: 'BookmarkListUiState',
);
return stateOrNull ?? BookmarkListUiState();
}
void enterSelectionMode({String? initialGuid}) {
state = state.copyWith(
@@ -10,8 +10,10 @@ part of 'bookmark_list_ui_state.dart';
// ignore_for_file: type=lint, type=warning
@ProviderFor(BookmarkListUiStateNotifier)
@JsonPersist()
final bookmarkListUiStateProvider = BookmarkListUiStateNotifierProvider._();
@JsonPersist()
final class BookmarkListUiStateNotifierProvider
extends
$NotifierProvider<BookmarkListUiStateNotifier, BookmarkListUiState> {
@@ -43,9 +45,10 @@ final class BookmarkListUiStateNotifierProvider
}
String _$bookmarkListUiStateNotifierHash() =>
r'7c764fc2f1deb178063ca95764775ca237471f9f';
r'13f01daba9d1591c700e956428f0e0e8cb2bfe92';
abstract class _$BookmarkListUiStateNotifier
@JsonPersist()
abstract class _$BookmarkListUiStateNotifierBase
extends $Notifier<BookmarkListUiState> {
BookmarkListUiState build();
@$mustCallSuper
@@ -63,3 +66,41 @@ abstract class _$BookmarkListUiStateNotifier
return element.handleCreate(ref, build);
}
}
// **************************************************************************
// JsonGenerator
// **************************************************************************
// GENERATED CODE - DO NOT MODIFY BY HAND
abstract class _$BookmarkListUiStateNotifier
extends _$BookmarkListUiStateNotifierBase {
/// The default key used by [persist].
String get key {
const resolvedKey = "BookmarkListUiStateNotifier";
return resolvedKey;
}
/// A variant of [persist], for JSON-specific encoding.
///
/// You can override [key] to customize the key used for storage.
PersistResult persist(
FutureOr<Storage<String, String>> storage, {
String? key,
String Function(BookmarkListUiState state)? encode,
BookmarkListUiState Function(String encoded)? decode,
StorageOptions options = const StorageOptions(),
}) {
return NotifierPersistX(this).persist<String, String>(
storage,
key: key ?? this.key,
encode: encode ?? $jsonCodex.encode,
decode:
decode ??
(encoded) {
final e = $jsonCodex.decode(encoded);
return BookmarkListUiState.fromJson(e as Map<String, Object?>);
},
options: options,
);
}
}