diff --git a/lib/features/lab/lab_repo.dart b/lib/features/lab/lab_repo.dart index 5d040538..c61341ea 100644 --- a/lib/features/lab/lab_repo.dart +++ b/lib/features/lab/lab_repo.dart @@ -460,8 +460,8 @@ class LabRepoImp implements LabRepo { Map mapDevice = { "ProjectID": labResult.projectID, "SetupID": labResult.setupID, - "LabAccessionNumber": labResult.accessionNo, - "FileName": labResult.fileNameWithExtension, + "LabAccessionNumber": labResult.accessionNo ?? "", + "FileName": labResult.fileNameWithExtension ?? "", "Lang": Utils.appState.isArabic() ? "ar" : "en", "Internationalization": "false", "FileHash": Utils.isVidaPlusProject(int.parse(labResult.projectID!)) ? [labResult.uploadedDocuments?[selectedIndex]] : [], @@ -484,7 +484,7 @@ class LabRepoImp implements LabRepo { messageStatus: messageStatus, statusCode: statusCode, errorMessage: null, - data: labResultAttachmentResponse.data?.first.base64Content, + data: labResultAttachmentResponse.data!.isNotEmpty ? labResultAttachmentResponse.data?.first.base64Content : "", ); } catch (e) { failure = DataParsingFailure(e.toString()); diff --git a/lib/features/lab/lab_view_model.dart b/lib/features/lab/lab_view_model.dart index 63b64ba0..4aca8a0d 100644 --- a/lib/features/lab/lab_view_model.dart +++ b/lib/features/lab/lab_view_model.dart @@ -2,6 +2,7 @@ import 'dart:collection'; import 'dart:convert'; import 'dart:core'; +import 'package:easy_localization/easy_localization.dart'; import 'package:flutter/material.dart'; import 'package:hmg_patient_app_new/core/app_assets.dart'; import 'package:hmg_patient_app_new/core/app_state.dart'; @@ -10,12 +11,15 @@ import 'package:hmg_patient_app_new/core/dependencies.dart'; import 'package:hmg_patient_app_new/core/utils/date_util.dart'; import 'package:hmg_patient_app_new/core/utils/loading_utils.dart'; import 'package:hmg_patient_app_new/core/utils/utils.dart' show Utils; +import 'package:hmg_patient_app_new/extensions/string_extensions.dart'; +import 'package:hmg_patient_app_new/extensions/widget_extensions.dart'; import 'package:hmg_patient_app_new/features/lab/lab_repo.dart'; import 'package:hmg_patient_app_new/features/lab/models/ai_overview_labresult_model_payload.dart'; import 'package:hmg_patient_app_new/features/lab/models/resp_models/ai_overview_labresults_by_ai_model.dart'; import 'package:hmg_patient_app_new/features/lab/models/resp_models/lab_result.dart'; import 'package:hmg_patient_app_new/features/lab/models/resp_models/patient_lab_order_response_by_ai_model.dart'; import 'package:hmg_patient_app_new/features/lab/models/resp_models/patient_lab_orders_response_model.dart'; +import 'package:hmg_patient_app_new/generated/codegen_loader.g.dart'; import 'package:hmg_patient_app_new/presentation/lab/lab_result_via_clinic/LabResultByClinic.dart'; import 'package:hmg_patient_app_new/presentation/lab/lab_results/lab_result_details.dart'; import 'package:hmg_patient_app_new/presentation/lab/lab_result_via_clinic/lab_ai_analysis_detailed_page.dart'; @@ -23,9 +27,11 @@ import 'package:hmg_patient_app_new/services/cache_service.dart'; import 'package:hmg_patient_app_new/services/error_handler_service.dart'; import 'package:hmg_patient_app_new/services/navigation_service.dart'; import 'package:hmg_patient_app_new/theme/colors.dart'; +import 'package:hmg_patient_app_new/widgets/common_bottom_sheet.dart'; import 'package:hmg_patient_app_new/widgets/loader/bottomsheet_loader.dart'; import 'package:hmg_patient_app_new/widgets/routes/custom_page_route.dart'; import 'package:intl/intl.dart' show DateFormat; +import 'package:open_filex/open_filex.dart'; class LabViewModel extends ChangeNotifier { bool isLabOrdersLoading = false; @@ -238,12 +244,12 @@ class LabViewModel extends ChangeNotifier { final result = await labRepo.getLabResultAttachmentPDF(labResult: labResult, selectedIndex: selectedIndex); result.fold( - (failure) async => await errorHandlerService.handleError( - failure: failure, - onOkPressed: () { - onError!(failure.message); - }, - ), + // (failure) async => await errorHandlerService.handleError( + (failure) async { + if (onError != null) { + onError(failure.message)!; + } + }, (apiResponse) { if (apiResponse.messageStatus == 2) { onError!(apiResponse.errorMessage!); @@ -994,4 +1000,73 @@ class LabViewModel extends ChangeNotifier { LoaderBottomSheet.hideLoader(); } + + void openLabResultsAttachmentSheet({required LabResult labResult}) { + showCommonBottomSheetWithoutHeight( + getIt + .get() + .navigatorKey + .currentContext!, + title: LocaleKeys.selectAttachment.tr(), + child: (labResult.uploadedDocuments ?? []).isNotEmpty + ? Column( + children: [ + ListView.builder( + scrollDirection: Axis.vertical, + shrinkWrap: true, + itemCount: (labResult.uploadedDocuments ?? []).length, + itemBuilder: (context, index) { + return Padding( + padding: const EdgeInsets.only(right: 12), + child: "Attachment ${(index + 1)}".toText16(color: AppColors.primaryRedColor, isBold: true, isUnderLine: true), + ).onPress(() { + LoaderBottomSheet.showLoader(); + getLabResultAttachmentPDF( + labResult: labResult, + selectedIndex: index, + onError: (err) { + LoaderBottomSheet.hideLoader(); + showCommonBottomSheetWithoutHeight( + context, + child: Utils.getErrorWidget(loadingText: err), + callBackFunc: () {}, + isFullScreen: false, + isCloseButtonVisible: true, + ); + }).then((val) async { + Navigator.pop(context); + if (getIt.get().patientLabResultAttachmentPDFBase64.isNotEmpty) { + String path = await Utils.createFileFromString(getIt.get().patientLabResultAttachmentPDFBase64, "pdf"); + try { + OpenFilex.open(path); + } catch (ex) { + showCommonBottomSheetWithoutHeight( + getIt.get().navigatorKey.currentContext!, + child: Utils.getErrorWidget(loadingText: "Cannot open file"), + callBackFunc: () {}, + isFullScreen: false, + isCloseButtonVisible: true, + ); + } + } else { + showCommonBottomSheetWithoutHeight( + getIt.get().navigatorKey.currentContext!, + child: Utils.getErrorWidget(loadingText: "Attachment not available"), + callBackFunc: () {}, + isFullScreen: false, + isCloseButtonVisible: true, + ); + } + }); + }); + }, + ), + ], + ) + : Utils.getErrorWidget(loadingText: "No attachments found"), + callBackFunc: () {}, + isFullScreen: false, + isCloseButtonVisible: true, + ); + } } diff --git a/lib/features/lab/models/resp_models/lab_result.dart b/lib/features/lab/models/resp_models/lab_result.dart index f5f6f4b9..3d63ddb6 100644 --- a/lib/features/lab/models/resp_models/lab_result.dart +++ b/lib/features/lab/models/resp_models/lab_result.dart @@ -52,7 +52,7 @@ class LabResult { int? testID; String? testShortDescription; String? uOM; - List? uploadedDocuments; + List? uploadedDocuments; String? verifiedOn; dynamic verifiedOnDateTime; diff --git a/lib/presentation/lab/lab_result_via_clinic/lab_order_result_item.dart b/lib/presentation/lab/lab_result_via_clinic/lab_order_result_item.dart index 2c4a7fb9..24f407eb 100644 --- a/lib/presentation/lab/lab_result_via_clinic/lab_order_result_item.dart +++ b/lib/presentation/lab/lab_result_via_clinic/lab_order_result_item.dart @@ -174,6 +174,7 @@ class LabOrderResultItem extends StatelessWidget { text: LocaleKeys.viewAttachment.tr(context: context), onPressed: () async { if (Utils.isVidaPlusProject(int.parse(tests!.projectID!))) { + getIt.get().openLabResultsAttachmentSheet(labResult: tests!); } else { LoaderBottomSheet.showLoader(loadingText: LocaleKeys.generatingReport.tr(context: context)); await getIt @@ -206,6 +207,14 @@ class LabOrderResultItem extends StatelessWidget { isCloseButtonVisible: true, ); } + } else { + showCommonBottomSheetWithoutHeight( + getIt.get().navigatorKey.currentContext!, + child: Utils.getErrorWidget(loadingText: "No attachments available"), + callBackFunc: () {}, + isFullScreen: false, + isCloseButtonVisible: true, + ); } }); }