added support for multiple accounts with an account switcher in settings
This commit is contained in:
@@ -9,19 +9,43 @@ import 'package:hydrated_bloc/hydrated_bloc.dart';
|
||||
Future<Storage> buildHydratedStorageWithFallback(String directoryPath) async {
|
||||
final directory = HydratedStorageDirectory(directoryPath);
|
||||
try {
|
||||
return await HydratedStorage.build(storageDirectory: directory);
|
||||
return await _build(directory);
|
||||
} catch (e, s) {
|
||||
log('HydratedStorage open failed, rebuilding: $e', stackTrace: s);
|
||||
}
|
||||
try {
|
||||
await _deleteHydratedBox(directoryPath);
|
||||
return await HydratedStorage.build(storageDirectory: directory);
|
||||
return await _build(directory);
|
||||
} catch (e, s) {
|
||||
log('HydratedStorage rebuild failed, using memory: $e', stackTrace: s);
|
||||
return InMemoryStorage();
|
||||
}
|
||||
}
|
||||
|
||||
final Expando<List<String> Function()> _keyReaders = Expando();
|
||||
|
||||
Future<Storage> _build(HydratedStorageDirectory directory) async {
|
||||
final storage = await HydratedStorage.build(storageDirectory: directory);
|
||||
// hydrated_bloc exposes no key listing; the Hive instance is the only way
|
||||
// in. Every build replaces the static one, so it is captured per storage.
|
||||
// ignore: invalid_use_of_visible_for_testing_member
|
||||
final hive = HydratedStorage.hive;
|
||||
_keyReaders[storage] = () =>
|
||||
hive.box<dynamic>('hydrated_box').keys.map((k) => '$k').toList();
|
||||
return storage;
|
||||
}
|
||||
|
||||
/// All keys persisted in [storage], sorted; `null` when they can't be
|
||||
/// enumerated.
|
||||
List<String>? hydratedStorageKeys(Storage storage) {
|
||||
if (storage is InMemoryStorage) return storage.keys.toList()..sort();
|
||||
try {
|
||||
return _keyReaders[storage]?.call()?..sort();
|
||||
} on Object {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _deleteHydratedBox(String directoryPath) async {
|
||||
final directory = Directory(directoryPath);
|
||||
if (!directory.existsSync()) return;
|
||||
|
||||
Reference in New Issue
Block a user