This commit is contained in:
Phuoc Nguyen
2025-10-10 16:38:07 +07:00
parent e5b247d622
commit b94c158004
177 changed files with 25080 additions and 152 deletions

View File

@@ -0,0 +1,53 @@
import 'package:hive_ce/hive.dart';
import '../models/cart_item_model.dart';
/// Cart local data source using Hive
abstract class CartLocalDataSource {
Future<List<CartItemModel>> getCartItems();
Future<void> addToCart(CartItemModel item);
Future<void> updateQuantity(String productId, int quantity);
Future<void> removeFromCart(String productId);
Future<void> clearCart();
}
class CartLocalDataSourceImpl implements CartLocalDataSource {
final Box<CartItemModel> box;
CartLocalDataSourceImpl(this.box);
@override
Future<List<CartItemModel>> getCartItems() async {
return box.values.toList();
}
@override
Future<void> addToCart(CartItemModel item) async {
await box.put(item.productId, item);
}
@override
Future<void> updateQuantity(String productId, int quantity) async {
final item = box.get(productId);
if (item != null) {
final updated = CartItemModel(
productId: item.productId,
productName: item.productName,
price: item.price,
quantity: quantity,
imageUrl: item.imageUrl,
addedAt: item.addedAt,
);
await box.put(productId, updated);
}
}
@override
Future<void> removeFromCart(String productId) async {
await box.delete(productId);
}
@override
Future<void> clearCart() async {
await box.clear();
}
}

View File

@@ -0,0 +1,83 @@
import 'package:hive_ce/hive.dart';
import '../../domain/entities/cart_item.dart';
import '../../../../core/constants/storage_constants.dart';
part 'cart_item_model.g.dart';
@HiveType(typeId: StorageConstants.cartItemTypeId)
class CartItemModel extends HiveObject {
@HiveField(0)
final String productId;
@HiveField(1)
final String productName;
@HiveField(2)
final double price;
@HiveField(3)
final int quantity;
@HiveField(4)
final String? imageUrl;
@HiveField(5)
final DateTime addedAt;
CartItemModel({
required this.productId,
required this.productName,
required this.price,
required this.quantity,
this.imageUrl,
required this.addedAt,
});
/// Convert to domain entity
CartItem toEntity() {
return CartItem(
productId: productId,
productName: productName,
price: price,
quantity: quantity,
imageUrl: imageUrl,
addedAt: addedAt,
);
}
/// Create from domain entity
factory CartItemModel.fromEntity(CartItem item) {
return CartItemModel(
productId: item.productId,
productName: item.productName,
price: item.price,
quantity: item.quantity,
imageUrl: item.imageUrl,
addedAt: item.addedAt,
);
}
/// Convert to JSON
Map<String, dynamic> toJson() {
return {
'productId': productId,
'productName': productName,
'price': price,
'quantity': quantity,
'imageUrl': imageUrl,
'addedAt': addedAt.toIso8601String(),
};
}
/// Create from JSON
factory CartItemModel.fromJson(Map<String, dynamic> json) {
return CartItemModel(
productId: json['productId'] as String,
productName: json['productName'] as String,
price: (json['price'] as num).toDouble(),
quantity: json['quantity'] as int,
imageUrl: json['imageUrl'] as String?,
addedAt: DateTime.parse(json['addedAt'] as String),
);
}
}

View File

@@ -0,0 +1,56 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'cart_item_model.dart';
// **************************************************************************
// TypeAdapterGenerator
// **************************************************************************
class CartItemModelAdapter extends TypeAdapter<CartItemModel> {
@override
final typeId = 2;
@override
CartItemModel read(BinaryReader reader) {
final numOfFields = reader.readByte();
final fields = <int, dynamic>{
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
};
return CartItemModel(
productId: fields[0] as String,
productName: fields[1] as String,
price: (fields[2] as num).toDouble(),
quantity: (fields[3] as num).toInt(),
imageUrl: fields[4] as String?,
addedAt: fields[5] as DateTime,
);
}
@override
void write(BinaryWriter writer, CartItemModel obj) {
writer
..writeByte(6)
..writeByte(0)
..write(obj.productId)
..writeByte(1)
..write(obj.productName)
..writeByte(2)
..write(obj.price)
..writeByte(3)
..write(obj.quantity)
..writeByte(4)
..write(obj.imageUrl)
..writeByte(5)
..write(obj.addedAt);
}
@override
int get hashCode => typeId.hashCode;
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is CartItemModelAdapter &&
runtimeType == other.runtimeType &&
typeId == other.typeId;
}

View File

@@ -0,0 +1,123 @@
import 'package:hive_ce/hive.dart';
import 'package:retail/core/constants/storage_constants.dart';
import 'package:retail/features/home/data/models/cart_item_model.dart';
part 'transaction_model.g.dart';
/// Transaction model with Hive CE type adapter
@HiveType(typeId: StorageConstants.transactionTypeId)
class TransactionModel extends HiveObject {
/// Unique transaction identifier
@HiveField(0)
final String id;
/// List of cart items in this transaction
@HiveField(1)
final List<CartItemModel> items;
/// Subtotal amount (before tax and discount)
@HiveField(2)
final double subtotal;
/// Tax amount
@HiveField(3)
final double tax;
/// Discount amount
@HiveField(4)
final double discount;
/// Total amount (subtotal + tax - discount)
@HiveField(5)
final double total;
/// Transaction completion timestamp
@HiveField(6)
final DateTime completedAt;
/// Payment method used (e.g., 'cash', 'card', 'digital')
@HiveField(7)
final String paymentMethod;
TransactionModel({
required this.id,
required this.items,
required this.subtotal,
required this.tax,
required this.discount,
required this.total,
required this.completedAt,
required this.paymentMethod,
});
/// Get total number of items in transaction
int get totalItems => items.fold(0, (sum, item) => sum + item.quantity);
/// Create a copy with updated fields
TransactionModel copyWith({
String? id,
List<CartItemModel>? items,
double? subtotal,
double? tax,
double? discount,
double? total,
DateTime? completedAt,
String? paymentMethod,
}) {
return TransactionModel(
id: id ?? this.id,
items: items ?? this.items,
subtotal: subtotal ?? this.subtotal,
tax: tax ?? this.tax,
discount: discount ?? this.discount,
total: total ?? this.total,
completedAt: completedAt ?? this.completedAt,
paymentMethod: paymentMethod ?? this.paymentMethod,
);
}
/// Convert to JSON
Map<String, dynamic> toJson() {
return {
'id': id,
'items': items.map((item) => item.toJson()).toList(),
'subtotal': subtotal,
'tax': tax,
'discount': discount,
'total': total,
'completedAt': completedAt.toIso8601String(),
'paymentMethod': paymentMethod,
};
}
/// Create from JSON
factory TransactionModel.fromJson(Map<String, dynamic> json) {
return TransactionModel(
id: json['id'] as String,
items: (json['items'] as List)
.map((item) => CartItemModel.fromJson(item as Map<String, dynamic>))
.toList(),
subtotal: (json['subtotal'] as num).toDouble(),
tax: (json['tax'] as num).toDouble(),
discount: (json['discount'] as num).toDouble(),
total: (json['total'] as num).toDouble(),
completedAt: DateTime.parse(json['completedAt'] as String),
paymentMethod: json['paymentMethod'] as String,
);
}
@override
String toString() {
return 'TransactionModel(id: $id, total: $total, items: ${items.length}, method: $paymentMethod)';
}
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
return other is TransactionModel && other.id == id;
}
@override
int get hashCode => id.hashCode;
}

View File

@@ -0,0 +1,62 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'transaction_model.dart';
// **************************************************************************
// TypeAdapterGenerator
// **************************************************************************
class TransactionModelAdapter extends TypeAdapter<TransactionModel> {
@override
final typeId = 3;
@override
TransactionModel read(BinaryReader reader) {
final numOfFields = reader.readByte();
final fields = <int, dynamic>{
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
};
return TransactionModel(
id: fields[0] as String,
items: (fields[1] as List).cast<CartItemModel>(),
subtotal: (fields[2] as num).toDouble(),
tax: (fields[3] as num).toDouble(),
discount: (fields[4] as num).toDouble(),
total: (fields[5] as num).toDouble(),
completedAt: fields[6] as DateTime,
paymentMethod: fields[7] as String,
);
}
@override
void write(BinaryWriter writer, TransactionModel obj) {
writer
..writeByte(8)
..writeByte(0)
..write(obj.id)
..writeByte(1)
..write(obj.items)
..writeByte(2)
..write(obj.subtotal)
..writeByte(3)
..write(obj.tax)
..writeByte(4)
..write(obj.discount)
..writeByte(5)
..write(obj.total)
..writeByte(6)
..write(obj.completedAt)
..writeByte(7)
..write(obj.paymentMethod);
}
@override
int get hashCode => typeId.hashCode;
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is TransactionModelAdapter &&
runtimeType == other.runtimeType &&
typeId == other.typeId;
}

View File

@@ -0,0 +1,66 @@
import 'package:dartz/dartz.dart';
import '../../domain/entities/cart_item.dart';
import '../../domain/repositories/cart_repository.dart';
import '../datasources/cart_local_datasource.dart';
import '../models/cart_item_model.dart';
import '../../../../core/errors/failures.dart';
import '../../../../core/errors/exceptions.dart';
class CartRepositoryImpl implements CartRepository {
final CartLocalDataSource localDataSource;
CartRepositoryImpl({
required this.localDataSource,
});
@override
Future<Either<Failure, List<CartItem>>> getCartItems() async {
try {
final items = await localDataSource.getCartItems();
return Right(items.map((model) => model.toEntity()).toList());
} on CacheException catch (e) {
return Left(CacheFailure(e.message));
}
}
@override
Future<Either<Failure, void>> addToCart(CartItem item) async {
try {
final model = CartItemModel.fromEntity(item);
await localDataSource.addToCart(model);
return const Right(null);
} on CacheException catch (e) {
return Left(CacheFailure(e.message));
}
}
@override
Future<Either<Failure, void>> updateQuantity(String productId, int quantity) async {
try {
await localDataSource.updateQuantity(productId, quantity);
return const Right(null);
} on CacheException catch (e) {
return Left(CacheFailure(e.message));
}
}
@override
Future<Either<Failure, void>> removeFromCart(String productId) async {
try {
await localDataSource.removeFromCart(productId);
return const Right(null);
} on CacheException catch (e) {
return Left(CacheFailure(e.message));
}
}
@override
Future<Either<Failure, void>> clearCart() async {
try {
await localDataSource.clearCart();
return const Right(null);
} on CacheException catch (e) {
return Left(CacheFailure(e.message));
}
}
}