diff --git a/lib/controller/task_planing/report_task_controller.dart b/lib/controller/task_planing/report_task_controller.dart index 6b22049..ea54937 100644 --- a/lib/controller/task_planing/report_task_controller.dart +++ b/lib/controller/task_planing/report_task_controller.dart @@ -92,7 +92,7 @@ class ReportTaskController extends MyController { super.onClose(); } - Future reportTask({ + Future reportTask({ required String projectId, required String comment, required int completedTask, @@ -110,7 +110,7 @@ class ReportTaskController extends MyController { message: "Completed work is required.", type: SnackbarType.error, ); - return; + return false; } final completedWorkInt = int.tryParse(completedWork); @@ -120,7 +120,7 @@ class ReportTaskController extends MyController { message: "Completed work must be a positive integer.", type: SnackbarType.error, ); - return; + return false; } final commentField = commentController.text.trim(); @@ -130,34 +130,34 @@ class ReportTaskController extends MyController { message: "Comment is required.", type: SnackbarType.error, ); - return; + return false; } try { + reportStatus.value = ApiStatus.loading; isLoading.value = true; + List>? imageData; if (images != null && images.isNotEmpty) { - if (images != null && images.isNotEmpty) { - final imageFutures = images.map((file) async { - final compressedBytes = await compressImageToUnder100KB(file); - if (compressedBytes == null) return null; + final imageFutures = images.map((file) async { + final compressedBytes = await compressImageToUnder100KB(file); + if (compressedBytes == null) return null; - final base64Image = base64Encode(compressedBytes); - final fileName = file.path.split('/').last; - final contentType = _getContentTypeFromFileName(fileName); + final base64Image = base64Encode(compressedBytes); + final fileName = file.path.split('/').last; + final contentType = _getContentTypeFromFileName(fileName); - return { - "fileName": fileName, - "base64Data": base64Image, - "contentType": contentType, - "fileSize": compressedBytes.lengthInBytes, - "description": "Image uploaded for task report", - }; - }).toList(); + return { + "fileName": fileName, + "base64Data": base64Image, + "contentType": contentType, + "fileSize": compressedBytes.lengthInBytes, + "description": "Image uploaded for task report", + }; + }).toList(); - final results = await Future.wait(imageFutures); - imageData = results.whereType>().toList(); - } + final results = await Future.wait(imageFutures); + imageData = results.whereType>().toList(); } final success = await ApiService.reportTask( @@ -169,28 +169,37 @@ class ReportTaskController extends MyController { ); if (success) { + reportStatus.value = ApiStatus.success; showAppSnackbar( title: "Success", message: "Task reported successfully!", type: SnackbarType.success, ); await taskController.fetchTaskData(projectId); + return true; } else { + reportStatus.value = ApiStatus.failure; showAppSnackbar( title: "Error", message: "Failed to report task.", type: SnackbarType.error, ); + return false; } } catch (e) { logger.e("Error reporting task: $e"); + reportStatus.value = ApiStatus.failure; showAppSnackbar( title: "Error", message: "An error occurred while reporting the task.", type: SnackbarType.error, ); + return false; } finally { isLoading.value = false; + Future.delayed(const Duration(milliseconds: 500), () { + reportStatus.value = ApiStatus.idle; + }); } } diff --git a/lib/model/dailyTaskPlaning/report_task_bottom_sheet.dart b/lib/model/dailyTaskPlaning/report_task_bottom_sheet.dart index 100119d..9b86441 100644 --- a/lib/model/dailyTaskPlaning/report_task_bottom_sheet.dart +++ b/lib/model/dailyTaskPlaning/report_task_bottom_sheet.dart @@ -356,61 +356,49 @@ class _ReportTaskBottomSheetState extends State child: MyText.bodySmall('Cancel'), ), MySpacing.width(12), - Obx(() { - return MyButton( - onPressed: controller.reportStatus.value == - ApiStatus.loading - ? null - : () async { - if (controller.basicValidator - .validateForm()) { - await controller.reportTask( - projectId: controller.basicValidator - .getController('task_id') - ?.text ?? - '', - comment: controller.basicValidator - .getController('comment') - ?.text ?? - '', - completedTask: int.tryParse( - controller.basicValidator - .getController( - 'completed_work') - ?.text ?? - '') ?? - 0, - checklist: [], - reportedDate: DateTime.now(), - images: controller.selectedImages, - ); - if (widget.onReportSuccess != null) { - widget.onReportSuccess!(); - } - } - }, - elevation: 0, - padding: MySpacing.xy(20, 16), - backgroundColor: Colors.blueAccent, - borderRadiusAll: AppStyle.buttonRadius.medium, - child: controller.reportStatus.value == - ApiStatus.loading - ? SizedBox( - height: 16, - width: 16, - child: CircularProgressIndicator( - strokeWidth: 2, - valueColor: - AlwaysStoppedAnimation( - contentTheme.onPrimary), - ), - ) - : MyText.bodySmall( - 'Report', - color: contentTheme.onPrimary, - ), - ); - }), + Obx(() { + final isLoading = controller.reportStatus.value == ApiStatus.loading; + + return MyButton( + onPressed: isLoading + ? null + : () async { + if (controller.basicValidator.validateForm()) { + final success = await controller.reportTask( + projectId: controller.basicValidator.getController('task_id')?.text ?? '', + comment: controller.basicValidator.getController('comment')?.text ?? '', + completedTask: int.tryParse( + controller.basicValidator.getController('completed_work')?.text ?? '') ?? + 0, + checklist: [], + reportedDate: DateTime.now(), + images: controller.selectedImages, + ); + if (success && widget.onReportSuccess != null) { + widget.onReportSuccess!(); + } + } + }, + elevation: 0, + padding: MySpacing.xy(20, 16), + backgroundColor: Colors.blueAccent, + borderRadiusAll: AppStyle.buttonRadius.medium, + child: isLoading + ? const SizedBox( + height: 16, + width: 16, + child: CircularProgressIndicator( + strokeWidth: 2, + valueColor: AlwaysStoppedAnimation(Colors.white), + ), + ) + : MyText.bodySmall( + 'Report', + color: contentTheme.onPrimary, + ), + ); +}), + ], ), ],