add firebase, add screen flow
This commit is contained in:
@@ -16,8 +16,8 @@ abstract class CartRemoteDataSource {
|
||||
/// Add items to cart
|
||||
///
|
||||
/// [items] - List of items with item_id, quantity, and amount
|
||||
/// Returns list of cart items from API
|
||||
Future<List<CartItemModel>> addToCart({
|
||||
/// Returns true if successful
|
||||
Future<bool> addToCart({
|
||||
required List<Map<String, dynamic>> items,
|
||||
});
|
||||
|
||||
@@ -47,7 +47,7 @@ class CartRemoteDataSourceImpl implements CartRemoteDataSource {
|
||||
final DioClient _dioClient;
|
||||
|
||||
@override
|
||||
Future<List<CartItemModel>> addToCart({
|
||||
Future<bool> addToCart({
|
||||
required List<Map<String, dynamic>> items,
|
||||
}) async {
|
||||
try {
|
||||
@@ -78,8 +78,7 @@ class CartRemoteDataSourceImpl implements CartRemoteDataSource {
|
||||
throw const ParseException('Invalid response format from add to cart API');
|
||||
}
|
||||
|
||||
// After adding, fetch updated cart
|
||||
return await getUserCart();
|
||||
return true;
|
||||
} on DioException catch (e) {
|
||||
throw _handleDioException(e);
|
||||
} catch (e) {
|
||||
|
||||
@@ -32,7 +32,7 @@ class CartRepositoryImpl implements CartRepository {
|
||||
final CartLocalDataSource _localDataSource;
|
||||
|
||||
@override
|
||||
Future<List<CartItem>> addToCart({
|
||||
Future<bool> addToCart({
|
||||
required List<String> itemIds,
|
||||
required List<double> quantities,
|
||||
required List<double> prices,
|
||||
@@ -57,17 +57,24 @@ class CartRepositoryImpl implements CartRepository {
|
||||
|
||||
// Try API first
|
||||
try {
|
||||
final cartItemModels = await _remoteDataSource.addToCart(items: items);
|
||||
final success = await _remoteDataSource.addToCart(items: items);
|
||||
|
||||
// Sync to local storage
|
||||
await _localDataSource.saveCartItems(cartItemModels);
|
||||
// Also save to local storage for offline access
|
||||
if (success) {
|
||||
for (int i = 0; i < itemIds.length; i++) {
|
||||
final cartItemModel = _createCartItemModel(
|
||||
productId: itemIds[i],
|
||||
quantity: quantities[i],
|
||||
unitPrice: prices[i],
|
||||
);
|
||||
await _localDataSource.addCartItem(cartItemModel);
|
||||
}
|
||||
}
|
||||
|
||||
// Convert to domain entities
|
||||
return cartItemModels.map(_modelToEntity).toList();
|
||||
return success;
|
||||
} on NetworkException catch (e) {
|
||||
// If no internet, add to local cart only
|
||||
if (e is NoInternetException || e is TimeoutException) {
|
||||
// Add items to local cart
|
||||
for (int i = 0; i < itemIds.length; i++) {
|
||||
final cartItemModel = _createCartItemModel(
|
||||
productId: itemIds[i],
|
||||
@@ -79,9 +86,7 @@ class CartRepositoryImpl implements CartRepository {
|
||||
|
||||
// TODO: Queue for sync when online
|
||||
|
||||
// Return local cart items
|
||||
final localItems = await _localDataSource.getCartItems();
|
||||
return localItems.map(_modelToEntity).toList();
|
||||
return true;
|
||||
}
|
||||
rethrow;
|
||||
}
|
||||
@@ -167,7 +172,7 @@ class CartRepositoryImpl implements CartRepository {
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<CartItem>> updateQuantity({
|
||||
Future<bool> updateQuantity({
|
||||
required String itemId,
|
||||
required double quantity,
|
||||
required double price,
|
||||
|
||||
@@ -22,14 +22,13 @@ import 'package:worker/features/cart/domain/entities/cart_item.dart';
|
||||
abstract class CartRepository {
|
||||
/// Add items to cart
|
||||
///
|
||||
/// [items] - List of cart items to add
|
||||
/// [itemIds] - Product ERPNext item codes
|
||||
/// [quantities] - Quantities for each item
|
||||
/// [prices] - Unit prices for each item
|
||||
///
|
||||
/// Returns list of cart items on success.
|
||||
/// Returns true if successful.
|
||||
/// Throws exceptions on failure.
|
||||
Future<List<CartItem>> addToCart({
|
||||
Future<bool> addToCart({
|
||||
required List<String> itemIds,
|
||||
required List<double> quantities,
|
||||
required List<double> prices,
|
||||
@@ -57,9 +56,9 @@ abstract class CartRepository {
|
||||
/// [quantity] - New quantity
|
||||
/// [price] - Unit price
|
||||
///
|
||||
/// Returns updated cart item list.
|
||||
/// Returns true if successful.
|
||||
/// Throws exceptions on failure.
|
||||
Future<List<CartItem>> updateQuantity({
|
||||
Future<bool> updateQuantity({
|
||||
required String itemId,
|
||||
required double quantity,
|
||||
required double price,
|
||||
|
||||
@@ -419,7 +419,7 @@ class _CartPageState extends ConsumerState<CartPage> {
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
ElevatedButton.icon(
|
||||
onPressed: () => context.go(RouteNames.products),
|
||||
onPressed: () => context.push(RouteNames.products),
|
||||
icon: const FaIcon(FontAwesomeIcons.bagShopping, size: 20),
|
||||
label: const Text('Xem sản phẩm'),
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user