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>(
(ref) => LayoutNotifier(initialLayout: FlexLayout.grid),
(ref) => LayoutNotifier(
'OATH_STATE_LAYOUT_PINNED', ref.watch(prefProvider), FlexLayout.grid),
);
final layoutProvider = StateNotifierProvider<LayoutNotifier, FlexLayout>(
(ref) => LayoutNotifier(),
(ref) => LayoutNotifier('OATH_STATE_LAYOUT', ref.watch(prefProvider)),
);
class LayoutNotifier extends StateNotifier<FlexLayout> {
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

View File

@ -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),