feat: Update reportTask method to return success status and improve loading state handling in ReportTaskBottomSheet

This commit is contained in:
Vaibhav Surve 2025-06-12 23:27:50 +05:30
parent 59e6634023
commit 602d8a8dc9
2 changed files with 74 additions and 77 deletions

View File

@ -92,7 +92,7 @@ class ReportTaskController extends MyController {
super.onClose(); super.onClose();
} }
Future<void> reportTask({ Future<bool> reportTask({
required String projectId, required String projectId,
required String comment, required String comment,
required int completedTask, required int completedTask,
@ -110,7 +110,7 @@ class ReportTaskController extends MyController {
message: "Completed work is required.", message: "Completed work is required.",
type: SnackbarType.error, type: SnackbarType.error,
); );
return; return false;
} }
final completedWorkInt = int.tryParse(completedWork); final completedWorkInt = int.tryParse(completedWork);
@ -120,7 +120,7 @@ class ReportTaskController extends MyController {
message: "Completed work must be a positive integer.", message: "Completed work must be a positive integer.",
type: SnackbarType.error, type: SnackbarType.error,
); );
return; return false;
} }
final commentField = commentController.text.trim(); final commentField = commentController.text.trim();
@ -130,13 +130,14 @@ class ReportTaskController extends MyController {
message: "Comment is required.", message: "Comment is required.",
type: SnackbarType.error, type: SnackbarType.error,
); );
return; return false;
} }
try { try {
reportStatus.value = ApiStatus.loading;
isLoading.value = true; isLoading.value = true;
List<Map<String, dynamic>>? imageData; List<Map<String, dynamic>>? imageData;
if (images != null && images.isNotEmpty) {
if (images != null && images.isNotEmpty) { if (images != null && images.isNotEmpty) {
final imageFutures = images.map((file) async { final imageFutures = images.map((file) async {
final compressedBytes = await compressImageToUnder100KB(file); final compressedBytes = await compressImageToUnder100KB(file);
@ -158,7 +159,6 @@ class ReportTaskController extends MyController {
final results = await Future.wait(imageFutures); final results = await Future.wait(imageFutures);
imageData = results.whereType<Map<String, dynamic>>().toList(); imageData = results.whereType<Map<String, dynamic>>().toList();
} }
}
final success = await ApiService.reportTask( final success = await ApiService.reportTask(
id: projectId, id: projectId,
@ -169,28 +169,37 @@ class ReportTaskController extends MyController {
); );
if (success) { if (success) {
reportStatus.value = ApiStatus.success;
showAppSnackbar( showAppSnackbar(
title: "Success", title: "Success",
message: "Task reported successfully!", message: "Task reported successfully!",
type: SnackbarType.success, type: SnackbarType.success,
); );
await taskController.fetchTaskData(projectId); await taskController.fetchTaskData(projectId);
return true;
} else { } else {
reportStatus.value = ApiStatus.failure;
showAppSnackbar( showAppSnackbar(
title: "Error", title: "Error",
message: "Failed to report task.", message: "Failed to report task.",
type: SnackbarType.error, type: SnackbarType.error,
); );
return false;
} }
} catch (e) { } catch (e) {
logger.e("Error reporting task: $e"); logger.e("Error reporting task: $e");
reportStatus.value = ApiStatus.failure;
showAppSnackbar( showAppSnackbar(
title: "Error", title: "Error",
message: "An error occurred while reporting the task.", message: "An error occurred while reporting the task.",
type: SnackbarType.error, type: SnackbarType.error,
); );
return false;
} finally { } finally {
isLoading.value = false; isLoading.value = false;
Future.delayed(const Duration(milliseconds: 500), () {
reportStatus.value = ApiStatus.idle;
});
} }
} }

View File

@ -357,34 +357,24 @@ class _ReportTaskBottomSheetState extends State<ReportTaskBottomSheet>
), ),
MySpacing.width(12), MySpacing.width(12),
Obx(() { Obx(() {
final isLoading = controller.reportStatus.value == ApiStatus.loading;
return MyButton( return MyButton(
onPressed: controller.reportStatus.value == onPressed: isLoading
ApiStatus.loading
? null ? null
: () async { : () async {
if (controller.basicValidator if (controller.basicValidator.validateForm()) {
.validateForm()) { final success = await controller.reportTask(
await controller.reportTask( projectId: controller.basicValidator.getController('task_id')?.text ?? '',
projectId: controller.basicValidator comment: controller.basicValidator.getController('comment')?.text ?? '',
.getController('task_id')
?.text ??
'',
comment: controller.basicValidator
.getController('comment')
?.text ??
'',
completedTask: int.tryParse( completedTask: int.tryParse(
controller.basicValidator controller.basicValidator.getController('completed_work')?.text ?? '') ??
.getController(
'completed_work')
?.text ??
'') ??
0, 0,
checklist: [], checklist: [],
reportedDate: DateTime.now(), reportedDate: DateTime.now(),
images: controller.selectedImages, images: controller.selectedImages,
); );
if (widget.onReportSuccess != null) { if (success && widget.onReportSuccess != null) {
widget.onReportSuccess!(); widget.onReportSuccess!();
} }
} }
@ -393,16 +383,13 @@ class _ReportTaskBottomSheetState extends State<ReportTaskBottomSheet>
padding: MySpacing.xy(20, 16), padding: MySpacing.xy(20, 16),
backgroundColor: Colors.blueAccent, backgroundColor: Colors.blueAccent,
borderRadiusAll: AppStyle.buttonRadius.medium, borderRadiusAll: AppStyle.buttonRadius.medium,
child: controller.reportStatus.value == child: isLoading
ApiStatus.loading ? const SizedBox(
? SizedBox(
height: 16, height: 16,
width: 16, width: 16,
child: CircularProgressIndicator( child: CircularProgressIndicator(
strokeWidth: 2, strokeWidth: 2,
valueColor: valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
AlwaysStoppedAnimation<Color>(
contentTheme.onPrimary),
), ),
) )
: MyText.bodySmall( : MyText.bodySmall(
@ -410,7 +397,8 @@ class _ReportTaskBottomSheetState extends State<ReportTaskBottomSheet>
color: contentTheme.onPrimary, color: contentTheme.onPrimary,
), ),
); );
}), }),
], ],
), ),
], ],