aaa
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import '../../domain/entities/product_stage_entity.dart';
|
||||
import '../../domain/usecases/get_product_detail_usecase.dart';
|
||||
|
||||
/// Product detail state class
|
||||
/// Holds the current state of product stages and selection
|
||||
class ProductDetailState {
|
||||
final List<ProductStageEntity> stages;
|
||||
final int selectedStageIndex;
|
||||
final bool isLoading;
|
||||
final String? error;
|
||||
|
||||
const ProductDetailState({
|
||||
this.stages = const [],
|
||||
this.selectedStageIndex = 0,
|
||||
this.isLoading = false,
|
||||
this.error,
|
||||
});
|
||||
|
||||
/// Get the currently selected stage
|
||||
ProductStageEntity? get selectedStage {
|
||||
if (stages.isEmpty || selectedStageIndex >= stages.length) {
|
||||
return null;
|
||||
}
|
||||
return stages[selectedStageIndex];
|
||||
}
|
||||
|
||||
/// Check if there are any stages loaded
|
||||
bool get hasStages => stages.isNotEmpty;
|
||||
|
||||
ProductDetailState copyWith({
|
||||
List<ProductStageEntity>? stages,
|
||||
int? selectedStageIndex,
|
||||
bool? isLoading,
|
||||
String? error,
|
||||
}) {
|
||||
return ProductDetailState(
|
||||
stages: stages ?? this.stages,
|
||||
selectedStageIndex: selectedStageIndex ?? this.selectedStageIndex,
|
||||
isLoading: isLoading ?? this.isLoading,
|
||||
error: error,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Product detail notifier
|
||||
/// Manages the product detail state and business logic
|
||||
class ProductDetailNotifier extends StateNotifier<ProductDetailState> {
|
||||
final GetProductDetailUseCase getProductDetailUseCase;
|
||||
|
||||
ProductDetailNotifier(this.getProductDetailUseCase)
|
||||
: super(const ProductDetailState());
|
||||
|
||||
/// Load product stages for a specific warehouse and product
|
||||
///
|
||||
/// [warehouseId] - The ID of the warehouse
|
||||
/// [productId] - The ID of the product
|
||||
Future<void> loadProductDetail(int warehouseId, int productId) async {
|
||||
// Set loading state
|
||||
state = state.copyWith(
|
||||
isLoading: true,
|
||||
error: null,
|
||||
);
|
||||
|
||||
// Call the use case
|
||||
final result = await getProductDetailUseCase(warehouseId, productId);
|
||||
|
||||
// Handle the result
|
||||
result.fold(
|
||||
(failure) {
|
||||
// Handle failure
|
||||
state = state.copyWith(
|
||||
isLoading: false,
|
||||
error: failure.message,
|
||||
stages: [],
|
||||
);
|
||||
},
|
||||
(stages) {
|
||||
// Handle success - load all stages
|
||||
state = state.copyWith(
|
||||
isLoading: false,
|
||||
error: null,
|
||||
stages: stages,
|
||||
selectedStageIndex: 0, // Select first stage by default
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/// Select a stage by index
|
||||
void selectStage(int index) {
|
||||
if (index >= 0 && index < state.stages.length) {
|
||||
state = state.copyWith(selectedStageIndex: index);
|
||||
}
|
||||
}
|
||||
|
||||
/// Refresh product stages
|
||||
Future<void> refreshProductDetail(int warehouseId, int productId) async {
|
||||
await loadProductDetail(warehouseId, productId);
|
||||
}
|
||||
|
||||
/// Clear product detail
|
||||
void clearProductDetail() {
|
||||
state = const ProductDetailState();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user