update cart/favorite

This commit is contained in:
Phuoc Nguyen
2025-12-03 15:53:46 +07:00
parent e1c9f818d2
commit 27798cc234
19 changed files with 370 additions and 119 deletions

View File

@@ -60,6 +60,48 @@ class FavoriteProductsLocalDataSource {
bool isBoxOpen() {
return Hive.isBoxOpen(HiveBoxNames.favoriteProductsBox);
}
/// Check if a product is in favorites (local only - no API call)
bool isFavorite(String productId) {
try {
return _box.containsKey(productId);
} catch (e) {
_debugPrint('Error checking favorite: $e');
return false;
}
}
/// Get all favorite product IDs (local only - no API call)
Set<String> getFavoriteIds() {
try {
return _box.keys.cast<String>().toSet();
} catch (e) {
_debugPrint('Error getting favorite IDs: $e');
return {};
}
}
/// Add a product to local favorites cache
Future<void> addFavorite(ProductModel product) async {
try {
await _box.put(product.productId, product);
_debugPrint('Added to local favorites: ${product.productId}');
} catch (e) {
_debugPrint('Error adding to local favorites: $e');
rethrow;
}
}
/// Remove a product from local favorites cache
Future<void> removeFavorite(String productId) async {
try {
await _box.delete(productId);
_debugPrint('Removed from local favorites: $productId');
} catch (e) {
_debugPrint('Error removing from local favorites: $e');
rethrow;
}
}
}
/// Debug print helper