fill
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import '../../domain/entities/product_entity.dart';
|
||||
import '../../domain/usecases/get_products_usecase.dart';
|
||||
|
||||
/// Products state class
|
||||
/// Holds the current state of the products feature
|
||||
class ProductsState {
|
||||
final List<ProductEntity> products;
|
||||
final String operationType;
|
||||
final int? warehouseId;
|
||||
final String? warehouseName;
|
||||
final bool isLoading;
|
||||
final String? error;
|
||||
|
||||
const ProductsState({
|
||||
this.products = const [],
|
||||
this.operationType = 'import',
|
||||
this.warehouseId,
|
||||
this.warehouseName,
|
||||
this.isLoading = false,
|
||||
this.error,
|
||||
});
|
||||
|
||||
ProductsState copyWith({
|
||||
List<ProductEntity>? products,
|
||||
String? operationType,
|
||||
int? warehouseId,
|
||||
String? warehouseName,
|
||||
bool? isLoading,
|
||||
String? error,
|
||||
}) {
|
||||
return ProductsState(
|
||||
products: products ?? this.products,
|
||||
operationType: operationType ?? this.operationType,
|
||||
warehouseId: warehouseId ?? this.warehouseId,
|
||||
warehouseName: warehouseName ?? this.warehouseName,
|
||||
isLoading: isLoading ?? this.isLoading,
|
||||
error: error,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Products notifier
|
||||
/// Manages the products state and business logic
|
||||
class ProductsNotifier extends StateNotifier<ProductsState> {
|
||||
final GetProductsUseCase getProductsUseCase;
|
||||
|
||||
ProductsNotifier(this.getProductsUseCase) : super(const ProductsState());
|
||||
|
||||
/// Load products for a specific warehouse and operation type
|
||||
///
|
||||
/// [warehouseId] - The ID of the warehouse
|
||||
/// [warehouseName] - The name of the warehouse (for display)
|
||||
/// [type] - The operation type ('import' or 'export')
|
||||
Future<void> loadProducts(
|
||||
int warehouseId,
|
||||
String warehouseName,
|
||||
String type,
|
||||
) async {
|
||||
// Set loading state
|
||||
state = state.copyWith(
|
||||
isLoading: true,
|
||||
error: null,
|
||||
warehouseId: warehouseId,
|
||||
warehouseName: warehouseName,
|
||||
operationType: type,
|
||||
);
|
||||
|
||||
// Call the use case
|
||||
final result = await getProductsUseCase(warehouseId, type);
|
||||
|
||||
// Handle the result
|
||||
result.fold(
|
||||
(failure) {
|
||||
// Handle failure
|
||||
state = state.copyWith(
|
||||
isLoading: false,
|
||||
error: failure.message,
|
||||
products: [],
|
||||
);
|
||||
},
|
||||
(products) {
|
||||
// Handle success
|
||||
state = state.copyWith(
|
||||
isLoading: false,
|
||||
error: null,
|
||||
products: products,
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/// Clear products list
|
||||
void clearProducts() {
|
||||
state = const ProductsState();
|
||||
}
|
||||
|
||||
/// Refresh products
|
||||
Future<void> refreshProducts() async {
|
||||
if (state.warehouseId != null) {
|
||||
await loadProducts(
|
||||
state.warehouseId!,
|
||||
state.warehouseName ?? '',
|
||||
state.operationType,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user