chore: integrate Hive for local storage management
- Added `hive_ce` and `hive_ce_flutter` dependencies for enhanced local storage capabilities. - Refactored the main application to initialize Hive and migrate existing data. - Updated storage service implementations to utilize Hive for managing application settings and task queues. - Removed the deprecated `StorageService` class to streamline the codebase and improve maintainability.
This commit is contained in:
46
lib/core/persistence/hive_bootstrap.dart
Normal file
46
lib/core/persistence/hive_bootstrap.dart
Normal file
@@ -0,0 +1,46 @@
|
||||
import 'package:hive_ce_flutter/hive_flutter.dart';
|
||||
|
||||
import 'hive_boxes.dart';
|
||||
|
||||
/// Sets up Hive and exposes lazily opened boxes used across the app.
|
||||
class HiveBootstrap {
|
||||
HiveBootstrap._();
|
||||
|
||||
static final HiveBootstrap instance = HiveBootstrap._();
|
||||
|
||||
HiveBoxes? _boxes;
|
||||
|
||||
/// Ensures Hive is initialized and all required boxes are open.
|
||||
Future<HiveBoxes> ensureInitialized() async {
|
||||
if (_boxes != null) {
|
||||
return _boxes!;
|
||||
}
|
||||
|
||||
await Hive.initFlutter('conduit_hive');
|
||||
|
||||
final preferences = await Hive.openBox<dynamic>(HiveBoxNames.preferences);
|
||||
final caches = await Hive.openBox<dynamic>(HiveBoxNames.caches);
|
||||
final attachmentQueue = await Hive.openBox<dynamic>(
|
||||
HiveBoxNames.attachmentQueue,
|
||||
);
|
||||
final metadata = await Hive.openBox<dynamic>(HiveBoxNames.metadata);
|
||||
|
||||
_boxes = HiveBoxes(
|
||||
preferences: preferences,
|
||||
caches: caches,
|
||||
attachmentQueue: attachmentQueue,
|
||||
metadata: metadata,
|
||||
);
|
||||
|
||||
return _boxes!;
|
||||
}
|
||||
|
||||
/// Access the cached boxes after [ensureInitialized] has completed.
|
||||
HiveBoxes get boxes {
|
||||
final cached = _boxes;
|
||||
if (cached == null) {
|
||||
throw StateError('HiveBootstrap.ensureInitialized must run first.');
|
||||
}
|
||||
return cached;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user