Persist layout settings

This commit is contained in:
Elias Bonnici 2024-06-13 12:48:03 +02:00
parent dd7e14fb44
commit ac35861370
No known key found for this signature in database
GPG Key ID: 5EAC28EA3F980CCF
2 changed files with 23 additions and 23 deletions

View File

@ -40,20 +40,31 @@ class AccountsSearchNotifier extends StateNotifier<String> {
} }
final pinnedLayoutProvider = StateNotifierProvider<LayoutNotifier, FlexLayout>( final pinnedLayoutProvider = StateNotifierProvider<LayoutNotifier, FlexLayout>(
(ref) => LayoutNotifier(initialLayout: FlexLayout.grid), (ref) => LayoutNotifier(
'OATH_STATE_LAYOUT_PINNED', ref.watch(prefProvider), FlexLayout.grid),
); );
final layoutProvider = StateNotifierProvider<LayoutNotifier, FlexLayout>( final layoutProvider = StateNotifierProvider<LayoutNotifier, FlexLayout>(
(ref) => LayoutNotifier(), (ref) => LayoutNotifier('OATH_STATE_LAYOUT', ref.watch(prefProvider)),
); );
class LayoutNotifier extends StateNotifier<FlexLayout> { class LayoutNotifier extends StateNotifier<FlexLayout> {
final String _key;
final SharedPreferences _prefs;
final FlexLayout initialLayout; 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) { void setLayout(FlexLayout layout) {
state = 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 final oathStateProvider = AsyncNotifierProvider.autoDispose

View File

@ -385,32 +385,21 @@ class _UnlockedViewState extends ConsumerState<_UnlockedView> {
final pinnedLayout = ref.watch(pinnedLayoutProvider); final pinnedLayout = ref.watch(pinnedLayoutProvider);
final layout = ref.watch(layoutProvider); 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( final credentials = ref.watch(filteredCredentialsProvider(
ref.watch(credentialListProvider(widget.devicePath)) ?? ref.watch(credentialListProvider(widget.devicePath)) ??
[])); []));
final favorites = ref.watch(favoritesProvider); final favorites = ref.watch(favoritesProvider);
final pinnedCreds = credentials final pinnedCreds = credentials
.where((entry) => favorites.contains(entry.credential.id)); .where((entry) => favorites.contains(entry.credential.id));
ref.listen(favoritesProvider, (prev, next) {
final newPinnedCreds = credentials final mixedView = pinnedLayout == FlexLayout.grid &&
.where((entry) => next.contains(entry.credential.id)); layout == FlexLayout.list;
if (newPinnedCreds.isEmpty) { final listView =
// reset to list view (pinnedLayout == FlexLayout.list || pinnedCreds.isEmpty) &&
ref layout == FlexLayout.list;
.read(pinnedLayoutProvider.notifier) final gridView = pinnedLayout == FlexLayout.grid &&
.setLayout(FlexLayout.list); layout == FlexLayout.grid;
ref
.read(layoutProvider.notifier)
.setLayout(FlexLayout.list);
}
});
return Padding( return Padding(
padding: const EdgeInsets.symmetric( padding: const EdgeInsets.symmetric(
horizontal: 16.0, vertical: 8.0), horizontal: 16.0, vertical: 8.0),