feat: Update reportTask method to return success status and improve loading state handling in ReportTaskBottomSheet
This commit is contained in:
parent
59e6634023
commit
602d8a8dc9
@ -92,7 +92,7 @@ class ReportTaskController extends MyController {
|
||||
super.onClose();
|
||||
}
|
||||
|
||||
Future<void> reportTask({
|
||||
Future<bool> 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<Map<String, dynamic>>? 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<Map<String, dynamic>>().toList();
|
||||
}
|
||||
final results = await Future.wait(imageFutures);
|
||||
imageData = results.whereType<Map<String, dynamic>>().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;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -356,61 +356,49 @@ class _ReportTaskBottomSheetState extends State<ReportTaskBottomSheet>
|
||||
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<Color>(
|
||||
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<Color>(Colors.white),
|
||||
),
|
||||
)
|
||||
: MyText.bodySmall(
|
||||
'Report',
|
||||
color: contentTheme.onPrimary,
|
||||
),
|
||||
);
|
||||
}),
|
||||
|
||||
],
|
||||
),
|
||||
],
|
||||
|
Loading…
x
Reference in New Issue
Block a user