import 'package:hive_flutter/hive_flutter.dart'; import '../../features/inventory/models/food_item.dart'; import '../../features/settings/models/app_settings.dart'; /// Singleton class to manage Hive database class HiveDatabase { static bool _initialized = false; /// Initialize Hive static Future init() async { if (_initialized) return; await Hive.initFlutter(); // Register adapters Hive.registerAdapter(FoodItemAdapter()); Hive.registerAdapter(LocationAdapter()); Hive.registerAdapter(ExpirationStatusAdapter()); Hive.registerAdapter(AppSettingsAdapter()); _initialized = true; } /// Get the food items box static Future> getFoodBox() async { if (!Hive.isBoxOpen('foodItems')) { return await Hive.openBox('foodItems'); } return Hive.box('foodItems'); } /// Get the settings box static Future> getSettingsBox() async { if (!Hive.isBoxOpen('appSettings')) { return await Hive.openBox('appSettings'); } return Hive.box('appSettings'); } /// Get or create app settings static Future getSettings() async { final box = await getSettingsBox(); if (box.isEmpty) { final settings = AppSettings(); await box.add(settings); return settings; } return box.getAt(0)!; } /// Clear all data static Future clearAll() async { final box = await getFoodBox(); await box.clear(); } /// Close all boxes static Future closeAll() async { await Hive.close(); } }