runable
This commit is contained in:
171
lib/core/database/hive_database.dart
Normal file
171
lib/core/database/hive_database.dart
Normal file
@@ -0,0 +1,171 @@
|
||||
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<void> 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<void> _openBoxes() async {
|
||||
await Future.wait([
|
||||
Hive.openBox<ProductModel>(StorageConstants.productsBox),
|
||||
Hive.openBox<CategoryModel>(StorageConstants.categoriesBox),
|
||||
Hive.openBox<CartItemModel>(StorageConstants.cartBox),
|
||||
Hive.openBox<TransactionModel>(StorageConstants.transactionsBox),
|
||||
Hive.openBox<AppSettingsModel>(StorageConstants.settingsBox),
|
||||
]);
|
||||
}
|
||||
|
||||
/// Initialize default settings and seed data if first launch
|
||||
Future<void> _initializeDefaults() async {
|
||||
final settingsBox = Hive.box<AppSettingsModel>(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<T> getBox<T>(String boxName) {
|
||||
return Hive.box<T>(boxName);
|
||||
}
|
||||
|
||||
/// Get products box
|
||||
Box<ProductModel> get productsBox =>
|
||||
Hive.box<ProductModel>(StorageConstants.productsBox);
|
||||
|
||||
/// Get categories box
|
||||
Box<CategoryModel> get categoriesBox =>
|
||||
Hive.box<CategoryModel>(StorageConstants.categoriesBox);
|
||||
|
||||
/// Get cart box
|
||||
Box<CartItemModel> get cartBox =>
|
||||
Hive.box<CartItemModel>(StorageConstants.cartBox);
|
||||
|
||||
/// Get transactions box
|
||||
Box<TransactionModel> get transactionsBox =>
|
||||
Hive.box<TransactionModel>(StorageConstants.transactionsBox);
|
||||
|
||||
/// Get settings box
|
||||
Box<AppSettingsModel> get settingsBox =>
|
||||
Hive.box<AppSettingsModel>(StorageConstants.settingsBox);
|
||||
|
||||
/// Clear all data from all boxes (useful for logout or reset)
|
||||
Future<void> clearAllData() async {
|
||||
await Future.wait([
|
||||
productsBox.clear(),
|
||||
categoriesBox.clear(),
|
||||
cartBox.clear(),
|
||||
transactionsBox.clear(),
|
||||
// Don't clear settings
|
||||
]);
|
||||
}
|
||||
|
||||
/// Clear cart only
|
||||
Future<void> clearCart() async {
|
||||
await cartBox.clear();
|
||||
}
|
||||
|
||||
/// Compact all boxes (optimize storage)
|
||||
Future<void> compactAll() async {
|
||||
await Future.wait([
|
||||
productsBox.compact(),
|
||||
categoriesBox.compact(),
|
||||
cartBox.compact(),
|
||||
transactionsBox.compact(),
|
||||
settingsBox.compact(),
|
||||
]);
|
||||
}
|
||||
|
||||
/// Close all boxes
|
||||
Future<void> closeAll() async {
|
||||
await Hive.close();
|
||||
_isInitialized = false;
|
||||
}
|
||||
|
||||
/// Delete all boxes (complete database reset)
|
||||
Future<void> 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<String, dynamic> 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;
|
||||
}
|
||||
Reference in New Issue
Block a user