add favorite

This commit is contained in:
Phuoc Nguyen
2025-11-18 11:23:07 +07:00
parent 192c322816
commit a5eb95fa64
25 changed files with 2506 additions and 978 deletions

View File

@@ -0,0 +1,69 @@
import 'package:hive_ce_flutter/hive_flutter.dart';
import 'package:worker/core/constants/storage_constants.dart';
import 'package:worker/features/products/data/models/product_model.dart';
/// Favorite Products Local DataSource
///
/// Caches the actual product data from wishlist API.
/// This is separate from ProductsLocalDataSource to avoid conflicts.
class FavoriteProductsLocalDataSource {
/// Get the Hive box for favorite products
Box<dynamic> get _box {
return Hive.box<dynamic>(HiveBoxNames.favoriteProductsBox);
}
/// Get all favorite products from cache
Future<List<ProductModel>> getAllProducts() async {
try {
final products = _box.values
.whereType<ProductModel>()
.toList();
_debugPrint('Loaded ${products.length} favorite products from cache');
return products;
} catch (e) {
_debugPrint('Error getting favorite products: $e');
return [];
}
}
/// Save products from wishlist API to cache
Future<void> saveProducts(List<ProductModel> products) async {
try {
// Clear existing cache
await _box.clear();
// Save new products
for (final product in products) {
await _box.put(product.productId, product);
}
_debugPrint('Cached ${products.length} favorite products');
} catch (e) {
_debugPrint('Error saving favorite products: $e');
rethrow;
}
}
/// Clear all cached favorite products
Future<void> clearAll() async {
try {
await _box.clear();
_debugPrint('Cleared all favorite products cache');
} catch (e) {
_debugPrint('Error clearing favorite products: $e');
rethrow;
}
}
/// Check if the box is open
bool isBoxOpen() {
return Hive.isBoxOpen(HiveBoxNames.favoriteProductsBox);
}
}
/// Debug print helper
void _debugPrint(String message) {
// ignore: avoid_print
print('[FavoriteProductsLocalDataSource] $message');
}

View File

@@ -1,161 +0,0 @@
import 'package:hive_ce_flutter/hive_flutter.dart';
import 'package:worker/core/constants/storage_constants.dart';
import 'package:worker/features/favorites/data/models/favorite_model.dart';
/// Favorites Local DataSource
///
/// Handles all local database operations for favorites using Hive.
/// Supports multi-user functionality by filtering favorites by userId.
class FavoritesLocalDataSource {
/// Get the Hive box for favorites
Box<dynamic> get _box {
return Hive.box<dynamic>(HiveBoxNames.favoriteBox);
}
/// Get all favorites for a specific user
///
/// Returns a list of [FavoriteModel] filtered by [userId].
/// If the box is not open or an error occurs, returns an empty list.
Future<List<FavoriteModel>> getAllFavorites(String userId) async {
try {
final favorites = _box.values
.whereType<FavoriteModel>()
.where((fav) => fav.userId == userId)
.toList();
// Sort by creation date (newest first)
favorites.sort((a, b) => b.createdAt.compareTo(a.createdAt));
return favorites;
} catch (e) {
debugPrint('[FavoritesLocalDataSource] Error getting favorites: $e');
return [];
}
}
/// Add a favorite to the database
///
/// Adds a new [FavoriteModel] to the Hive box.
/// Uses the favoriteId as the key for efficient lookup.
Future<void> addFavorite(FavoriteModel favorite) async {
try {
await _box.put(favorite.favoriteId, favorite);
debugPrint(
'[FavoritesLocalDataSource] Added favorite: ${favorite.favoriteId} for user: ${favorite.userId}',
);
} catch (e) {
debugPrint('[FavoritesLocalDataSource] Error adding favorite: $e');
rethrow;
}
}
/// Remove a favorite from the database
///
/// Removes a favorite by finding it with the combination of [productId] and [userId].
/// Returns true if the favorite was found and removed, false otherwise.
Future<bool> removeFavorite(String productId, String userId) async {
try {
// Find the favorite by productId and userId
final favorites = _box.values
.whereType<FavoriteModel>()
.where((fav) => fav.productId == productId && fav.userId == userId)
.toList();
if (favorites.isEmpty) {
debugPrint(
'[FavoritesLocalDataSource] Favorite not found: productId=$productId, userId=$userId',
);
return false;
}
final favorite = favorites.first;
await _box.delete(favorite.favoriteId);
debugPrint(
'[FavoritesLocalDataSource] Removed favorite: ${favorite.favoriteId} for user: $userId',
);
return true;
} catch (e) {
debugPrint('[FavoritesLocalDataSource] Error removing favorite: $e');
rethrow;
}
}
/// Check if a product is favorited by a user
///
/// Returns true if the product is in the user's favorites, false otherwise.
bool isFavorite(String productId, String userId) {
try {
return _box.values.whereType<FavoriteModel>().any(
(fav) => fav.productId == productId && fav.userId == userId,
);
} catch (e) {
debugPrint('[FavoritesLocalDataSource] Error checking favorite: $e');
return false;
}
}
/// Clear all favorites for a specific user
///
/// Removes all favorites for the given [userId].
/// Useful for logout or data cleanup scenarios.
Future<void> clearFavorites(String userId) async {
try {
final favoriteIds = _box.values
.whereType<FavoriteModel>()
.where((fav) => fav.userId == userId)
.map((fav) => fav.favoriteId)
.toList();
await _box.deleteAll(favoriteIds);
debugPrint(
'[FavoritesLocalDataSource] Cleared ${favoriteIds.length} favorites for user: $userId',
);
} catch (e) {
debugPrint('[FavoritesLocalDataSource] Error clearing favorites: $e');
rethrow;
}
}
/// Get the count of favorites for a user
///
/// Returns the total number of favorites for the given [userId].
int getFavoriteCount(String userId) {
try {
return _box.values
.whereType<FavoriteModel>()
.where((fav) => fav.userId == userId)
.length;
} catch (e) {
debugPrint('[FavoritesLocalDataSource] Error getting favorite count: $e');
return 0;
}
}
/// Check if the favorites box is open
///
/// Returns true if the box is open and ready to use.
bool isBoxOpen() {
return Hive.isBoxOpen(HiveBoxNames.favoriteBox);
}
/// Compact the favorites box to reduce storage space
///
/// Should be called periodically to optimize database size.
Future<void> compact() async {
try {
if (isBoxOpen()) {
await _box.compact();
debugPrint('[FavoritesLocalDataSource] Favorites box compacted');
}
} catch (e) {
debugPrint(
'[FavoritesLocalDataSource] Error compacting favorites box: $e',
);
}
}
}
/// Debug print helper that works in both Flutter and Dart
void debugPrint(String message) {
print('[FavoritesLocalDataSource] $message');
}

View File

@@ -0,0 +1,193 @@
import 'package:dio/dio.dart';
import 'package:worker/core/constants/api_constants.dart';
import 'package:worker/core/errors/exceptions.dart';
import 'package:worker/features/products/data/models/product_model.dart';
/// Favorites Remote DataSource
///
/// Handles all API operations for favorites/wishlist using Frappe ERPNext backend.
/// Follows the API spec from docs/favorite.sh
///
/// Note: The API returns Product objects directly, not separate favorite entities.
class FavoritesRemoteDataSource {
FavoritesRemoteDataSource(this._dio);
final Dio _dio;
/// Get all favorites/wishlist items for the current user
///
/// API: POST /api/method/building_material.building_material.api.item_wishlist.get_list
/// Body: { "limit_start": 0, "limit_page_length": 0 }
///
/// Response format (from docs/favorite.sh):
/// ```json
/// {
/// "message": [
/// {
/// "name": "GIB20 G04",
/// "item_code": "GIB20 G04",
/// "item_name": "Gibellina GIB20 G04",
/// "item_group_name": "OUTDOOR [20mm]",
/// "custom_link_360": "https://...",
/// "thumbnail": "https://...",
/// "price": 0,
/// "currency": "",
/// "conversion_of_sm": 5.5556
/// }
/// ]
/// }
/// ```
///
/// Returns list of Product objects that are favorited
Future<List<ProductModel>> getFavorites({
int limitStart = 0,
int limitPageLength = 0, // 0 means get all
}) async {
try {
final response = await _dio.post<Map<String, dynamic>>(
'${ApiConstants.frappeApiMethod}${ApiConstants.getFavorites}',
data: {
'limit_start': limitStart,
'limit_page_length': limitPageLength,
},
);
if (response.data == null) {
throw const ServerException('Response data is null');
}
// Parse response according to Frappe format
final data = response.data!;
final messageList = data['message'] as List<dynamic>?;
if (messageList == null || messageList.isEmpty) {
_debugPrint('No favorites found');
return [];
}
final products = <ProductModel>[];
// Convert API response - each item is a product object from wishlist
for (final item in messageList) {
try {
final itemMap = item as Map<String, dynamic>;
final productModel = ProductModel.fromWishlistApi(itemMap);
products.add(productModel);
} catch (e) {
_debugPrint('Error parsing product: $e');
// Continue with other products even if one fails
}
}
_debugPrint('Fetched ${products.length} favorite products');
return products;
} on DioException catch (e) {
_handleDioException(e);
} catch (e) {
_debugPrint('Error getting favorites: $e');
throw ServerException('Failed to get favorites: $e');
}
}
/// Add item to wishlist
///
/// API: POST /api/method/building_material.building_material.api.item_wishlist.add_to_wishlist
/// Body: { "item_id": "GIB20 G04" }
///
/// Returns true if successful
Future<bool> addToFavorites(String itemId) async {
try {
final response = await _dio.post<Map<String, dynamic>>(
'${ApiConstants.frappeApiMethod}${ApiConstants.addToFavorites}',
data: {
'item_id': itemId,
},
);
_debugPrint('Added to favorites: $itemId');
return response.statusCode == 200;
} on DioException catch (e) {
_handleDioException(e);
} catch (e) {
_debugPrint('Error adding to favorites: $e');
throw ServerException('Failed to add to favorites: $e');
}
}
/// Remove item from wishlist
///
/// API: POST /api/method/building_material.building_material.api.item_wishlist.remove_from_wishlist
/// Body: { "item_id": "GIB20 G04" }
///
/// Returns true if successful
Future<bool> removeFromFavorites(String itemId) async {
try {
final response = await _dio.post<Map<String, dynamic>>(
'${ApiConstants.frappeApiMethod}${ApiConstants.removeFromFavorites}',
data: {
'item_id': itemId,
},
);
_debugPrint('Removed from favorites: $itemId');
return response.statusCode == 200;
} on DioException catch (e) {
_handleDioException(e);
} catch (e) {
_debugPrint('Error removing from favorites: $e');
throw ServerException('Failed to remove from favorites: $e');
}
}
// =========================================================================
// ERROR HANDLING
// =========================================================================
/// Handle Dio exceptions and convert to custom exceptions
Never _handleDioException(DioException e) {
switch (e.type) {
case DioExceptionType.connectionTimeout:
case DioExceptionType.sendTimeout:
case DioExceptionType.receiveTimeout:
throw const NetworkException('Request timeout. Please check your connection.');
case DioExceptionType.connectionError:
throw const NetworkException('No internet connection.');
case DioExceptionType.badResponse:
final statusCode = e.response?.statusCode;
final message = e.response?.data?['message'] as String? ??
e.response?.data?['error'] as String? ??
'Server error';
if (statusCode == 401) {
throw const UnauthorizedException('Unauthorized. Please login again.');
} else if (statusCode == 403) {
throw const UnauthorizedException('Access forbidden.');
} else if (statusCode == 404) {
throw const ServerException('Resource not found.');
} else if (statusCode != null && statusCode >= 500) {
throw ServerException('Server error: $message');
} else {
throw ServerException(message);
}
case DioExceptionType.cancel:
throw const NetworkException('Request was cancelled');
case DioExceptionType.unknown:
case DioExceptionType.badCertificate:
throw NetworkException('Network error: ${e.message}');
}
}
}
// ============================================================================
// DEBUG UTILITIES
// ============================================================================
/// Debug print helper
void _debugPrint(String message) {
// ignore: avoid_print
print('[FavoritesRemoteDataSource] $message');
}