所有一级页面和不分二级页面
This commit is contained in:
137
lib/features/v2/report/presentation/cubit/report_cubit.dart
Normal file
137
lib/features/v2/report/presentation/cubit/report_cubit.dart
Normal file
@@ -0,0 +1,137 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import '../../domain/entities/report_entity.dart';
|
||||
import '../../domain/usecases/submit_report_usecase.dart';
|
||||
import '../constants/report_constants.dart';
|
||||
import '../states/report_state.dart';
|
||||
|
||||
/// 上报页面 Cubit
|
||||
class ReportCubit extends Cubit<ReportState> {
|
||||
final SubmitReportUseCase submitReportUseCase;
|
||||
|
||||
ReportCubit({required this.submitReportUseCase})
|
||||
: super(ReportFormState(report: ReportEntity()));
|
||||
|
||||
/// 选择上报类型
|
||||
void selectReportType(ReportType type) {
|
||||
if (state is ReportFormState) {
|
||||
final currentState = state as ReportFormState;
|
||||
emit(currentState.copyWith(
|
||||
selectedReportType: type,
|
||||
report: currentState.report.copyWith(
|
||||
reportType: type.label,
|
||||
),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/// 选择设备
|
||||
void selectDevice(String device) {
|
||||
if (state is ReportFormState) {
|
||||
final currentState = state as ReportFormState;
|
||||
emit(currentState.copyWith(
|
||||
selectedDevice: device,
|
||||
report: currentState.report.copyWith(deviceId: device),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/// 更新位置
|
||||
void updateLocation(String location) {
|
||||
if (state is ReportFormState) {
|
||||
final currentState = state as ReportFormState;
|
||||
emit(currentState.copyWith(
|
||||
location: location,
|
||||
report: currentState.report.copyWith(location: location),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/// 更新问题描述
|
||||
void updateDescription(String description) {
|
||||
if (state is ReportFormState) {
|
||||
final currentState = state as ReportFormState;
|
||||
emit(currentState.copyWith(
|
||||
report: currentState.report.copyWith(description: description),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/// 选择问题等级
|
||||
void selectProblemLevel(ProblemLevel level) {
|
||||
if (state is ReportFormState) {
|
||||
final currentState = state as ReportFormState;
|
||||
emit(currentState.copyWith(
|
||||
selectedProblemLevel: level,
|
||||
report: currentState.report.copyWith(
|
||||
problemLevel: level.label,
|
||||
),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/// 添加媒体文件
|
||||
void addMediaFile(String filePath) {
|
||||
if (state is ReportFormState) {
|
||||
final currentState = state as ReportFormState;
|
||||
final updatedFiles = List<String>.from(currentState.mediaFiles)
|
||||
..add(filePath);
|
||||
emit(currentState.copyWith(
|
||||
mediaFiles: updatedFiles,
|
||||
report: currentState.report.copyWith(mediaUrls: updatedFiles),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/// 删除媒体文件
|
||||
void removeMediaFile(int index) {
|
||||
if (state is ReportFormState) {
|
||||
final currentState = state as ReportFormState;
|
||||
final updatedFiles = List<String>.from(currentState.mediaFiles)
|
||||
..removeAt(index);
|
||||
emit(currentState.copyWith(
|
||||
mediaFiles: updatedFiles,
|
||||
report: currentState.report.copyWith(mediaUrls: updatedFiles),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/// 提交上报
|
||||
Future<void> submitReport() async {
|
||||
if (state is! ReportFormState) return;
|
||||
|
||||
final currentState = state as ReportFormState;
|
||||
|
||||
// 验证必填项
|
||||
if (currentState.selectedDevice == null ||
|
||||
currentState.selectedDevice!.isEmpty) {
|
||||
emit(const ReportFailure('请选择设备'));
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentState.report.description == null ||
|
||||
currentState.report.description!.isEmpty) {
|
||||
emit(const ReportFailure('请填写问题描述'));
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentState.selectedProblemLevel == null) {
|
||||
emit(const ReportFailure('请选择问题等级'));
|
||||
return;
|
||||
}
|
||||
|
||||
// 开始提交
|
||||
emit(ReportSubmitting());
|
||||
|
||||
final result = await submitReportUseCase.execute(currentState.report);
|
||||
|
||||
result.fold(
|
||||
(failure) => emit(ReportFailure(failure.message)),
|
||||
(success) => emit(ReportSuccess()),
|
||||
);
|
||||
}
|
||||
|
||||
/// 重置表单
|
||||
void resetForm() {
|
||||
emit(ReportFormState(report: ReportEntity()));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user