update cart/favorite
This commit is contained in:
@@ -190,15 +190,21 @@ class CartRemoteDataSourceImpl implements CartRemoteDataSource {
|
||||
try {
|
||||
// Map API response to CartItemModel
|
||||
// API fields: name, item, quantity, amount, item_code, item_name, image, conversion_of_sm
|
||||
final quantity = (item['quantity'] as num?)?.toDouble() ?? 0.0;
|
||||
final unitPrice = (item['amount'] as num?)?.toDouble() ?? 0.0;
|
||||
|
||||
final cartItem = CartItemModel(
|
||||
cartItemId: item['name'] as String? ?? '',
|
||||
cartId: 'user_cart', // Fixed cart ID for user's cart
|
||||
productId: item['item_code'] as String? ?? item['item'] as String? ?? '',
|
||||
quantity: (item['quantity'] as num?)?.toDouble() ?? 0.0,
|
||||
unitPrice: (item['amount'] as num?)?.toDouble() ?? 0.0,
|
||||
subtotal: ((item['quantity'] as num?)?.toDouble() ?? 0.0) *
|
||||
((item['amount'] as num?)?.toDouble() ?? 0.0),
|
||||
quantity: quantity,
|
||||
unitPrice: unitPrice,
|
||||
subtotal: quantity * unitPrice,
|
||||
addedAt: DateTime.now(), // API doesn't provide timestamp
|
||||
// Product details from cart API - no need to fetch separately
|
||||
itemName: item['item_name'] as String?,
|
||||
image: item['image'] as String?,
|
||||
conversionOfSm: (item['conversion_of_sm'] as num?)?.toDouble(),
|
||||
);
|
||||
|
||||
cartItems.add(cartItem);
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
import 'package:hive_ce/hive.dart';
|
||||
import 'package:worker/core/constants/storage_constants.dart';
|
||||
import 'package:worker/features/cart/domain/entities/cart_item.dart';
|
||||
|
||||
part 'cart_item_model.g.dart';
|
||||
|
||||
/// Cart Item Model - Type ID: 5
|
||||
///
|
||||
/// Includes product details from cart API to avoid fetching each product.
|
||||
@HiveType(typeId: HiveTypeIds.cartItemModel)
|
||||
class CartItemModel extends HiveObject {
|
||||
CartItemModel({
|
||||
@@ -14,6 +17,9 @@ class CartItemModel extends HiveObject {
|
||||
required this.unitPrice,
|
||||
required this.subtotal,
|
||||
required this.addedAt,
|
||||
this.itemName,
|
||||
this.image,
|
||||
this.conversionOfSm,
|
||||
});
|
||||
|
||||
@HiveField(0)
|
||||
@@ -37,6 +43,18 @@ class CartItemModel extends HiveObject {
|
||||
@HiveField(6)
|
||||
final DateTime addedAt;
|
||||
|
||||
/// Product name from cart API
|
||||
@HiveField(7)
|
||||
final String? itemName;
|
||||
|
||||
/// Product image URL from cart API
|
||||
@HiveField(8)
|
||||
final String? image;
|
||||
|
||||
/// Conversion factor (m² to tiles) from cart API
|
||||
@HiveField(9)
|
||||
final double? conversionOfSm;
|
||||
|
||||
factory CartItemModel.fromJson(Map<String, dynamic> json) {
|
||||
return CartItemModel(
|
||||
cartItemId: json['cart_item_id'] as String,
|
||||
@@ -67,6 +85,9 @@ class CartItemModel extends HiveObject {
|
||||
double? unitPrice,
|
||||
double? subtotal,
|
||||
DateTime? addedAt,
|
||||
String? itemName,
|
||||
String? image,
|
||||
double? conversionOfSm,
|
||||
}) => CartItemModel(
|
||||
cartItemId: cartItemId ?? this.cartItemId,
|
||||
cartId: cartId ?? this.cartId,
|
||||
@@ -75,5 +96,22 @@ class CartItemModel extends HiveObject {
|
||||
unitPrice: unitPrice ?? this.unitPrice,
|
||||
subtotal: subtotal ?? this.subtotal,
|
||||
addedAt: addedAt ?? this.addedAt,
|
||||
itemName: itemName ?? this.itemName,
|
||||
image: image ?? this.image,
|
||||
conversionOfSm: conversionOfSm ?? this.conversionOfSm,
|
||||
);
|
||||
|
||||
/// Convert to domain entity
|
||||
CartItem toEntity() => CartItem(
|
||||
cartItemId: cartItemId,
|
||||
cartId: cartId,
|
||||
productId: productId,
|
||||
quantity: quantity,
|
||||
unitPrice: unitPrice,
|
||||
subtotal: subtotal,
|
||||
addedAt: addedAt,
|
||||
itemName: itemName,
|
||||
image: image,
|
||||
conversionOfSm: conversionOfSm,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -24,13 +24,16 @@ class CartItemModelAdapter extends TypeAdapter<CartItemModel> {
|
||||
unitPrice: (fields[4] as num).toDouble(),
|
||||
subtotal: (fields[5] as num).toDouble(),
|
||||
addedAt: fields[6] as DateTime,
|
||||
itemName: fields[7] as String?,
|
||||
image: fields[8] as String?,
|
||||
conversionOfSm: (fields[9] as num?)?.toDouble(),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void write(BinaryWriter writer, CartItemModel obj) {
|
||||
writer
|
||||
..writeByte(7)
|
||||
..writeByte(10)
|
||||
..writeByte(0)
|
||||
..write(obj.cartItemId)
|
||||
..writeByte(1)
|
||||
@@ -44,7 +47,13 @@ class CartItemModelAdapter extends TypeAdapter<CartItemModel> {
|
||||
..writeByte(5)
|
||||
..write(obj.subtotal)
|
||||
..writeByte(6)
|
||||
..write(obj.addedAt);
|
||||
..write(obj.addedAt)
|
||||
..writeByte(7)
|
||||
..write(obj.itemName)
|
||||
..writeByte(8)
|
||||
..write(obj.image)
|
||||
..writeByte(9)
|
||||
..write(obj.conversionOfSm);
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -36,6 +36,7 @@ class CartRepositoryImpl implements CartRepository {
|
||||
required List<String> itemIds,
|
||||
required List<double> quantities,
|
||||
required List<double> prices,
|
||||
List<double?>? conversionFactors,
|
||||
}) async {
|
||||
try {
|
||||
// Validate input
|
||||
@@ -48,11 +49,16 @@ class CartRepositoryImpl implements CartRepository {
|
||||
// Build API request items
|
||||
final items = <Map<String, dynamic>>[];
|
||||
for (int i = 0; i < itemIds.length; i++) {
|
||||
items.add({
|
||||
final item = <String, dynamic>{
|
||||
'item_id': itemIds[i],
|
||||
'quantity': quantities[i],
|
||||
'amount': prices[i],
|
||||
});
|
||||
};
|
||||
// Add conversion_of_sm if provided
|
||||
if (conversionFactors != null && i < conversionFactors.length) {
|
||||
item['conversion_of_sm'] = conversionFactors[i] ?? 0.0;
|
||||
}
|
||||
items.add(item);
|
||||
}
|
||||
|
||||
// Try API first
|
||||
@@ -66,6 +72,7 @@ class CartRepositoryImpl implements CartRepository {
|
||||
productId: itemIds[i],
|
||||
quantity: quantities[i],
|
||||
unitPrice: prices[i],
|
||||
conversionOfSm: conversionFactors?[i],
|
||||
);
|
||||
await _localDataSource.addCartItem(cartItemModel);
|
||||
}
|
||||
@@ -80,6 +87,7 @@ class CartRepositoryImpl implements CartRepository {
|
||||
productId: itemIds[i],
|
||||
quantity: quantities[i],
|
||||
unitPrice: prices[i],
|
||||
conversionOfSm: conversionFactors?[i],
|
||||
);
|
||||
await _localDataSource.addCartItem(cartItemModel);
|
||||
}
|
||||
@@ -176,6 +184,7 @@ class CartRepositoryImpl implements CartRepository {
|
||||
required String itemId,
|
||||
required double quantity,
|
||||
required double price,
|
||||
double? conversionFactor,
|
||||
}) async {
|
||||
try {
|
||||
// API doesn't have update endpoint, use add with new quantity
|
||||
@@ -184,6 +193,7 @@ class CartRepositoryImpl implements CartRepository {
|
||||
itemIds: [itemId],
|
||||
quantities: [quantity],
|
||||
prices: [price],
|
||||
conversionFactors: conversionFactor != null ? [conversionFactor] : null,
|
||||
);
|
||||
} catch (e) {
|
||||
throw UnknownException('Failed to update cart item quantity', e);
|
||||
@@ -268,6 +278,9 @@ class CartRepositoryImpl implements CartRepository {
|
||||
unitPrice: model.unitPrice,
|
||||
subtotal: model.subtotal,
|
||||
addedAt: model.addedAt,
|
||||
itemName: model.itemName,
|
||||
image: model.image,
|
||||
conversionOfSm: model.conversionOfSm,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -276,6 +289,7 @@ class CartRepositoryImpl implements CartRepository {
|
||||
required String productId,
|
||||
required double quantity,
|
||||
required double unitPrice,
|
||||
double? conversionOfSm,
|
||||
}) {
|
||||
return CartItemModel(
|
||||
cartItemId: DateTime.now().millisecondsSinceEpoch.toString(),
|
||||
@@ -285,6 +299,7 @@ class CartRepositoryImpl implements CartRepository {
|
||||
unitPrice: unitPrice,
|
||||
subtotal: quantity * unitPrice,
|
||||
addedAt: DateTime.now(),
|
||||
conversionOfSm: conversionOfSm,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ library;
|
||||
/// Cart Item Entity
|
||||
///
|
||||
/// Contains item-level information:
|
||||
/// - Product reference
|
||||
/// - Product reference and basic info
|
||||
/// - Quantity
|
||||
/// - Pricing
|
||||
class CartItem {
|
||||
@@ -31,6 +31,15 @@ class CartItem {
|
||||
/// Timestamp when item was added
|
||||
final DateTime addedAt;
|
||||
|
||||
/// Product name from cart API
|
||||
final String? itemName;
|
||||
|
||||
/// Product image URL from cart API
|
||||
final String? image;
|
||||
|
||||
/// Conversion factor (m² to tiles) from cart API
|
||||
final double? conversionOfSm;
|
||||
|
||||
const CartItem({
|
||||
required this.cartItemId,
|
||||
required this.cartId,
|
||||
@@ -39,6 +48,9 @@ class CartItem {
|
||||
required this.unitPrice,
|
||||
required this.subtotal,
|
||||
required this.addedAt,
|
||||
this.itemName,
|
||||
this.image,
|
||||
this.conversionOfSm,
|
||||
});
|
||||
|
||||
/// Calculate subtotal (for verification)
|
||||
@@ -53,6 +65,9 @@ class CartItem {
|
||||
double? unitPrice,
|
||||
double? subtotal,
|
||||
DateTime? addedAt,
|
||||
String? itemName,
|
||||
String? image,
|
||||
double? conversionOfSm,
|
||||
}) {
|
||||
return CartItem(
|
||||
cartItemId: cartItemId ?? this.cartItemId,
|
||||
@@ -62,6 +77,9 @@ class CartItem {
|
||||
unitPrice: unitPrice ?? this.unitPrice,
|
||||
subtotal: subtotal ?? this.subtotal,
|
||||
addedAt: addedAt ?? this.addedAt,
|
||||
itemName: itemName ?? this.itemName,
|
||||
image: image ?? this.image,
|
||||
conversionOfSm: conversionOfSm ?? this.conversionOfSm,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@ abstract class CartRepository {
|
||||
/// [itemIds] - Product ERPNext item codes
|
||||
/// [quantities] - Quantities for each item
|
||||
/// [prices] - Unit prices for each item
|
||||
/// [conversionFactors] - Conversion factors (m² to tiles) for each item
|
||||
///
|
||||
/// Returns true if successful.
|
||||
/// Throws exceptions on failure.
|
||||
@@ -32,6 +33,7 @@ abstract class CartRepository {
|
||||
required List<String> itemIds,
|
||||
required List<double> quantities,
|
||||
required List<double> prices,
|
||||
List<double?>? conversionFactors,
|
||||
});
|
||||
|
||||
/// Remove items from cart
|
||||
@@ -55,6 +57,7 @@ abstract class CartRepository {
|
||||
/// [itemId] - Product ERPNext item code
|
||||
/// [quantity] - New quantity
|
||||
/// [price] - Unit price
|
||||
/// [conversionFactor] - Conversion factor (m² to tiles)
|
||||
///
|
||||
/// Returns true if successful.
|
||||
/// Throws exceptions on failure.
|
||||
@@ -62,6 +65,7 @@ abstract class CartRepository {
|
||||
required String itemId,
|
||||
required double quantity,
|
||||
required double price,
|
||||
double? conversionFactor,
|
||||
});
|
||||
|
||||
/// Clear all items from cart
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
/// Shopping cart screen with selection and checkout.
|
||||
/// Features expanded item list with total price at bottom.
|
||||
library;
|
||||
|
||||
import 'package:worker/core/utils/extensions.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
|
||||
@@ -35,14 +35,8 @@ class CartPage extends ConsumerStatefulWidget {
|
||||
class _CartPageState extends ConsumerState<CartPage> {
|
||||
bool _isSyncing = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
// Initialize cart from API on mount
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
ref.read(cartProvider.notifier).initialize();
|
||||
});
|
||||
}
|
||||
// Cart is initialized once in home_page.dart at app startup
|
||||
// Provider has keepAlive: true, so no need to reload here
|
||||
|
||||
// Note: Sync is handled in PopScope.onPopInvokedWithResult for back navigation
|
||||
// and in checkout button handler for checkout flow.
|
||||
@@ -53,11 +47,7 @@ class _CartPageState extends ConsumerState<CartPage> {
|
||||
final colorScheme = Theme.of(context).colorScheme;
|
||||
final cartState = ref.watch(cartProvider);
|
||||
|
||||
final currencyFormatter = NumberFormat.currency(
|
||||
locale: 'vi_VN',
|
||||
symbol: 'đ',
|
||||
decimalDigits: 0,
|
||||
);
|
||||
|
||||
|
||||
final itemCount = cartState.itemCount;
|
||||
final hasSelection = cartState.selectedCount > 0;
|
||||
@@ -144,7 +134,11 @@ class _CartPageState extends ConsumerState<CartPage> {
|
||||
context,
|
||||
cartState,
|
||||
ref,
|
||||
currencyFormatter,
|
||||
NumberFormat.currency(
|
||||
locale: 'vi_VN',
|
||||
symbol: 'đ',
|
||||
decimalDigits: 0,
|
||||
),
|
||||
hasSelection,
|
||||
),
|
||||
],
|
||||
|
||||
@@ -9,7 +9,6 @@ import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:worker/features/cart/presentation/providers/cart_data_providers.dart';
|
||||
import 'package:worker/features/cart/presentation/providers/cart_state.dart';
|
||||
import 'package:worker/features/products/domain/entities/product.dart';
|
||||
import 'package:worker/features/products/presentation/providers/products_provider.dart';
|
||||
|
||||
part 'cart_provider.g.dart';
|
||||
|
||||
@@ -46,8 +45,12 @@ class Cart extends _$Cart {
|
||||
|
||||
/// Initialize cart by loading from API
|
||||
///
|
||||
/// Call this from UI on mount to load cart items from backend.
|
||||
/// Call this ONCE from HomePage on app startup.
|
||||
/// Cart API returns product details, no need to fetch each product separately.
|
||||
Future<void> initialize() async {
|
||||
// Skip if already loaded
|
||||
if (state.items.isNotEmpty) return;
|
||||
|
||||
final repository = await ref.read(cartRepositoryProvider.future);
|
||||
|
||||
// Set loading state
|
||||
@@ -55,6 +58,7 @@ class Cart extends _$Cart {
|
||||
|
||||
try {
|
||||
// Load cart items from API (with Hive fallback)
|
||||
// Cart API returns: item_code, item_name, image, conversion_of_sm, quantity, amount
|
||||
final cartItems = await repository.getCartItems();
|
||||
|
||||
// Get member tier from user profile
|
||||
@@ -63,41 +67,47 @@ class Cart extends _$Cart {
|
||||
const memberDiscountPercent = 15.0;
|
||||
|
||||
// Convert CartItem entities to CartItemData for UI
|
||||
// Use product data from cart API directly - no need to fetch each product
|
||||
final items = <CartItemData>[];
|
||||
final selectedItems = <String, bool>{};
|
||||
|
||||
// Fetch product details for each cart item
|
||||
final productsRepository = await ref.read(productsRepositoryProvider.future);
|
||||
|
||||
for (final cartItem in cartItems) {
|
||||
try {
|
||||
// Fetch full product entity from products repository
|
||||
final product = await productsRepository.getProductById(cartItem.productId);
|
||||
// Create minimal Product from cart item data (no need to fetch from API)
|
||||
final now = DateTime.now();
|
||||
final product = Product(
|
||||
productId: cartItem.productId,
|
||||
name: cartItem.itemName ?? cartItem.productId,
|
||||
basePrice: cartItem.unitPrice,
|
||||
images: cartItem.image != null ? [cartItem.image!] : [],
|
||||
thumbnail: cartItem.image ?? '',
|
||||
imageCaptions: const {},
|
||||
specifications: const {},
|
||||
conversionOfSm: cartItem.conversionOfSm,
|
||||
erpnextItemCode: cartItem.productId,
|
||||
isActive: true,
|
||||
isFeatured: false,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
);
|
||||
|
||||
// Calculate conversion for this item
|
||||
final converted = _calculateConversion(
|
||||
cartItem.quantity,
|
||||
product.conversionOfSm,
|
||||
);
|
||||
// Calculate conversion for this item
|
||||
final converted = _calculateConversion(
|
||||
cartItem.quantity,
|
||||
product.conversionOfSm,
|
||||
);
|
||||
|
||||
// Create CartItemData with full product info
|
||||
items.add(
|
||||
CartItemData(
|
||||
product: product,
|
||||
quantity: cartItem.quantity,
|
||||
quantityConverted: converted.convertedQuantity,
|
||||
boxes: converted.boxes,
|
||||
),
|
||||
);
|
||||
// Create CartItemData with product info from cart API
|
||||
items.add(
|
||||
CartItemData(
|
||||
product: product,
|
||||
quantity: cartItem.quantity,
|
||||
quantityConverted: converted.convertedQuantity,
|
||||
boxes: converted.boxes,
|
||||
),
|
||||
);
|
||||
|
||||
// Initialize as not selected by default
|
||||
selectedItems[product.productId] = false;
|
||||
} catch (productError) {
|
||||
// Skip this item if product can't be fetched
|
||||
// In production, use a proper logging framework
|
||||
// ignore: avoid_print
|
||||
print('[CartProvider] Failed to load product ${cartItem.productId}: $productError');
|
||||
}
|
||||
// Initialize as not selected by default
|
||||
selectedItems[product.productId] = false;
|
||||
}
|
||||
|
||||
final newState = CartState(
|
||||
@@ -150,6 +160,7 @@ class Cart extends _$Cart {
|
||||
itemIds: [product.erpnextItemCode ?? product.productId],
|
||||
quantities: [quantity],
|
||||
prices: [product.basePrice],
|
||||
conversionFactors: [product.conversionOfSm],
|
||||
);
|
||||
|
||||
// Calculate conversion
|
||||
@@ -332,6 +343,7 @@ class Cart extends _$Cart {
|
||||
itemId: item.product.erpnextItemCode ?? productId,
|
||||
quantity: quantity,
|
||||
price: item.product.basePrice,
|
||||
conversionFactor: item.product.conversionOfSm,
|
||||
);
|
||||
} catch (e) {
|
||||
// Silent fail - keep local state, user can retry later
|
||||
@@ -370,6 +382,7 @@ class Cart extends _$Cart {
|
||||
itemId: item.product.erpnextItemCode ?? productId,
|
||||
quantity: newQuantity,
|
||||
price: item.product.basePrice,
|
||||
conversionFactor: item.product.conversionOfSm,
|
||||
);
|
||||
|
||||
// Update local state
|
||||
|
||||
@@ -64,7 +64,7 @@ final class CartProvider extends $NotifierProvider<Cart, CartState> {
|
||||
}
|
||||
}
|
||||
|
||||
String _$cartHash() => r'706de28734e7059b2e9484f3b1d94226a0e90bb9';
|
||||
String _$cartHash() => r'a12712db833127ef66b75f52cba8376409806afa';
|
||||
|
||||
/// Cart Notifier
|
||||
///
|
||||
|
||||
@@ -8,6 +8,7 @@ import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:worker/core/utils/extensions.dart';
|
||||
import 'package:worker/core/widgets/loading_indicator.dart';
|
||||
import 'package:worker/core/theme/typography.dart';
|
||||
import 'package:worker/features/cart/presentation/providers/cart_provider.dart';
|
||||
@@ -79,11 +80,6 @@ class _CartItemWidgetState extends ConsumerState<CartItemWidget> {
|
||||
final isSelected =
|
||||
cartState.selectedItems[widget.item.product.productId] ?? false;
|
||||
|
||||
final currencyFormatter = NumberFormat.currency(
|
||||
locale: 'vi_VN',
|
||||
symbol: 'đ',
|
||||
decimalDigits: 0,
|
||||
);
|
||||
|
||||
return Container(
|
||||
margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 6),
|
||||
@@ -121,7 +117,11 @@ class _CartItemWidgetState extends ConsumerState<CartItemWidget> {
|
||||
ClipRRect(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
child: CachedNetworkImage(
|
||||
imageUrl: widget.item.product.thumbnail,
|
||||
imageUrl: widget.item.product.thumbnail.isNotEmpty
|
||||
? widget.item.product.thumbnail
|
||||
: (widget.item.product.images.isNotEmpty
|
||||
? widget.item.product.images.first
|
||||
: ''),
|
||||
width: 100,
|
||||
height: 100,
|
||||
fit: BoxFit.cover,
|
||||
@@ -168,7 +168,7 @@ class _CartItemWidgetState extends ConsumerState<CartItemWidget> {
|
||||
|
||||
// Price
|
||||
Text(
|
||||
'${currencyFormatter.format(widget.item.product.basePrice)}/m²',
|
||||
'${widget.item.product.basePrice.toVNCurrency}/m²',
|
||||
style: AppTypography.titleMedium.copyWith(
|
||||
color: colorScheme.primary,
|
||||
fontWeight: FontWeight.bold,
|
||||
|
||||
Reference in New Issue
Block a user