mirror of
https://github.com/Yubico/yubioath-flutter.git
synced 2024-11-22 08:22:16 +03:00
Persist layout settings
This commit is contained in:
parent
dd7e14fb44
commit
ac35861370
@ -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
|
||||
|
@ -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),
|
||||
|
Loading…
Reference in New Issue
Block a user