From ac35861370cb721b97493adac72eac37b5bf7fd9 Mon Sep 17 00:00:00 2001 From: Elias Bonnici Date: Thu, 13 Jun 2024 12:48:03 +0200 Subject: [PATCH] Persist layout settings --- lib/oath/state.dart | 17 ++++++++++++++--- lib/oath/views/oath_screen.dart | 29 +++++++++-------------------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/lib/oath/state.dart b/lib/oath/state.dart index c1c13857..859f197b 100755 --- a/lib/oath/state.dart +++ b/lib/oath/state.dart @@ -40,20 +40,31 @@ class AccountsSearchNotifier extends StateNotifier { } final pinnedLayoutProvider = StateNotifierProvider( - (ref) => LayoutNotifier(initialLayout: FlexLayout.grid), + (ref) => LayoutNotifier( + 'OATH_STATE_LAYOUT_PINNED', ref.watch(prefProvider), FlexLayout.grid), ); final layoutProvider = StateNotifierProvider( - (ref) => LayoutNotifier(), + (ref) => LayoutNotifier('OATH_STATE_LAYOUT', ref.watch(prefProvider)), ); class LayoutNotifier extends StateNotifier { + final String _key; + final SharedPreferences _prefs; final FlexLayout initialLayout; - LayoutNotifier({this.initialLayout = FlexLayout.list}) : super(initialLayout); + LayoutNotifier(this._key, this._prefs, [this.initialLayout = FlexLayout.list]) + : super(_fromName(_prefs.getString(_key), initialLayout)); void setLayout(FlexLayout layout) { state = layout; + _prefs.setString(_key, layout.name); } + + static FlexLayout _fromName(String? name, FlexLayout initialLayout) => + FlexLayout.values.firstWhere( + (element) => element.name == name, + orElse: () => initialLayout, + ); } final oathStateProvider = AsyncNotifierProvider.autoDispose diff --git a/lib/oath/views/oath_screen.dart b/lib/oath/views/oath_screen.dart index 44c61734..f91608fd 100755 --- a/lib/oath/views/oath_screen.dart +++ b/lib/oath/views/oath_screen.dart @@ -385,32 +385,21 @@ class _UnlockedViewState extends ConsumerState<_UnlockedView> { final pinnedLayout = ref.watch(pinnedLayoutProvider); final layout = ref.watch(layoutProvider); - final mixedView = pinnedLayout == FlexLayout.grid && - layout == FlexLayout.list; - final listView = pinnedLayout == FlexLayout.list && - layout == FlexLayout.list; - final gridView = pinnedLayout == FlexLayout.grid && - layout == FlexLayout.grid; - final credentials = ref.watch(filteredCredentialsProvider( ref.watch(credentialListProvider(widget.devicePath)) ?? [])); final favorites = ref.watch(favoritesProvider); final pinnedCreds = credentials .where((entry) => favorites.contains(entry.credential.id)); - ref.listen(favoritesProvider, (prev, next) { - final newPinnedCreds = credentials - .where((entry) => next.contains(entry.credential.id)); - if (newPinnedCreds.isEmpty) { - // reset to list view - ref - .read(pinnedLayoutProvider.notifier) - .setLayout(FlexLayout.list); - ref - .read(layoutProvider.notifier) - .setLayout(FlexLayout.list); - } - }); + + final mixedView = pinnedLayout == FlexLayout.grid && + layout == FlexLayout.list; + final listView = + (pinnedLayout == FlexLayout.list || pinnedCreds.isEmpty) && + layout == FlexLayout.list; + final gridView = pinnedLayout == FlexLayout.grid && + layout == FlexLayout.grid; + return Padding( padding: const EdgeInsets.symmetric( horizontal: 16.0, vertical: 8.0),