import 'package:hive_ce/hive.dart'; import 'package:retail/core/database/hive_database.dart'; import 'package:retail/features/categories/data/models/category_model.dart'; import 'package:retail/features/categories/data/datasources/category_local_datasource.dart'; import 'package:retail/features/categories/domain/entities/category.dart'; /// Hive implementation of CategoryLocalDataSource class CategoryLocalDataSourceHive implements CategoryLocalDataSource { final HiveDatabase _database; CategoryLocalDataSourceHive(this._database); Box get _box => _database.categoriesBox; /// Convert CategoryModel to Category entity Category _toEntity(CategoryModel model) { return Category( id: model.id, name: model.name, description: model.description, iconPath: model.iconPath, color: model.color, createdAt: model.createdAt, ); } /// Convert Category entity to CategoryModel CategoryModel _toModel(Category entity) { return CategoryModel( id: entity.id, name: entity.name, description: entity.description, iconPath: entity.iconPath, color: entity.color, productCount: 0, // Will be calculated from products createdAt: entity.createdAt, ); } @override Future> getAllCategories() async { try { return _box.values.map(_toEntity).toList(); } catch (e) { throw Exception('Failed to get categories: $e'); } } @override Future getCategoryById(String id) async { try { final model = _box.get(id); return model != null ? _toEntity(model) : null; } catch (e) { throw Exception('Failed to get category by ID: $e'); } } @override Future saveCategories(List categories) async { try { final models = categories.map(_toModel).toList(); final Map categoriesMap = { for (var model in models) model.id: model }; await _box.putAll(categoriesMap); } catch (e) { throw Exception('Failed to save categories: $e'); } } @override Future deleteAllCategories() async { try { await _box.clear(); } catch (e) { throw Exception('Failed to delete all categories: $e'); } } /// Additional Hive-specific methods /// Save a single category Future saveCategory(Category category) async { try { final model = _toModel(category); await _box.put(model.id, model); } catch (e) { throw Exception('Failed to save category: $e'); } } /// Update an existing category Future updateCategory(Category category) async { try { if (!_box.containsKey(category.id)) { throw Exception('Category not found: ${category.id}'); } final model = _toModel(category); await _box.put(model.id, model); } catch (e) { throw Exception('Failed to update category: $e'); } } /// Delete a specific category Future deleteCategory(String id) async { try { await _box.delete(id); } catch (e) { throw Exception('Failed to delete category: $e'); } } /// Check if category exists Future categoryExists(String id) async { try { return _box.containsKey(id); } catch (e) { throw Exception('Failed to check category existence: $e'); } } /// Update product count for a category Future updateProductCount(String categoryId, int count) async { try { final model = _box.get(categoryId); if (model != null) { final updated = model.copyWith(productCount: count); await _box.put(categoryId, updated); } } catch (e) { throw Exception('Failed to update product count: $e'); } } }