import 'package:hive_ce/hive.dart'; import 'package:hive_ce_flutter/hive_flutter.dart'; import 'package:retail/core/constants/storage_constants.dart'; import 'package:retail/features/products/data/models/product_model.dart'; import 'package:retail/features/categories/data/models/category_model.dart'; import 'package:retail/features/home/data/models/cart_item_model.dart'; import 'package:retail/features/home/data/models/transaction_model.dart'; import 'package:retail/features/settings/data/models/app_settings_model.dart'; /// Hive database initialization and management class HiveDatabase { static HiveDatabase? _instance; static HiveDatabase get instance => _instance ??= HiveDatabase._(); HiveDatabase._(); bool _isInitialized = false; /// Initialize Hive database Future init() async { if (_isInitialized) { return; } try { // Initialize Hive for Flutter await Hive.initFlutter(); // Register all type adapters _registerAdapters(); // Open all boxes await _openBoxes(); // Initialize default settings if needed await _initializeDefaults(); _isInitialized = true; } catch (e) { throw Exception('Failed to initialize Hive database: $e'); } } /// Register all Hive type adapters void _registerAdapters() { // Register only if not already registered if (!Hive.isAdapterRegistered(StorageConstants.productTypeId)) { Hive.registerAdapter(ProductModelAdapter()); } if (!Hive.isAdapterRegistered(StorageConstants.categoryTypeId)) { Hive.registerAdapter(CategoryModelAdapter()); } if (!Hive.isAdapterRegistered(StorageConstants.cartItemTypeId)) { Hive.registerAdapter(CartItemModelAdapter()); } if (!Hive.isAdapterRegistered(StorageConstants.transactionTypeId)) { Hive.registerAdapter(TransactionModelAdapter()); } if (!Hive.isAdapterRegistered(StorageConstants.appSettingsTypeId)) { Hive.registerAdapter(AppSettingsModelAdapter()); } } /// Open all required boxes Future _openBoxes() async { await Future.wait([ Hive.openBox(StorageConstants.productsBox), Hive.openBox(StorageConstants.categoriesBox), Hive.openBox(StorageConstants.cartBox), Hive.openBox(StorageConstants.transactionsBox), Hive.openBox(StorageConstants.settingsBox), ]); } /// Initialize default settings and seed data if first launch Future _initializeDefaults() async { final settingsBox = Hive.box(StorageConstants.settingsBox); // Initialize default settings if not exists if (settingsBox.isEmpty) { await settingsBox.put( 'app_settings', AppSettingsModel.defaultSettings(), ); } } /// Get a specific box by name Box getBox(String boxName) { return Hive.box(boxName); } /// Get products box Box get productsBox => Hive.box(StorageConstants.productsBox); /// Get categories box Box get categoriesBox => Hive.box(StorageConstants.categoriesBox); /// Get cart box Box get cartBox => Hive.box(StorageConstants.cartBox); /// Get transactions box Box get transactionsBox => Hive.box(StorageConstants.transactionsBox); /// Get settings box Box get settingsBox => Hive.box(StorageConstants.settingsBox); /// Clear all data from all boxes (useful for logout or reset) Future clearAllData() async { await Future.wait([ productsBox.clear(), categoriesBox.clear(), cartBox.clear(), transactionsBox.clear(), // Don't clear settings ]); } /// Clear cart only Future clearCart() async { await cartBox.clear(); } /// Compact all boxes (optimize storage) Future compactAll() async { await Future.wait([ productsBox.compact(), categoriesBox.compact(), cartBox.compact(), transactionsBox.compact(), settingsBox.compact(), ]); } /// Close all boxes Future closeAll() async { await Hive.close(); _isInitialized = false; } /// Delete all boxes (complete database reset) Future deleteAll() async { await Future.wait([ Hive.deleteBoxFromDisk(StorageConstants.productsBox), Hive.deleteBoxFromDisk(StorageConstants.categoriesBox), Hive.deleteBoxFromDisk(StorageConstants.cartBox), Hive.deleteBoxFromDisk(StorageConstants.transactionsBox), Hive.deleteBoxFromDisk(StorageConstants.settingsBox), ]); _isInitialized = false; } /// Get database statistics Map getStatistics() { return { 'products': productsBox.length, 'categories': categoriesBox.length, 'cartItems': cartBox.length, 'transactions': transactionsBox.length, 'isInitialized': _isInitialized, }; } /// Check if database is initialized bool get isInitialized => _isInitialized; }