From a2310e14473810206729eb2f5e6000ec39a8df58 Mon Sep 17 00:00:00 2001 From: haroon amjad Date: Wed, 8 Oct 2025 14:01:41 +0300 Subject: [PATCH] prescription instructions PDF implemented --- lib/core/api_consts.dart | 2 + .../prescriptions/prescriptions_repo.dart | 46 +++++++++++++++++++ .../prescriptions_view_model.dart | 24 ++++++++++ .../prescription_detail_page.dart | 30 +++++++++++- 4 files changed, 101 insertions(+), 1 deletion(-) diff --git a/lib/core/api_consts.dart b/lib/core/api_consts.dart index 9591cfe..787fdad 100644 --- a/lib/core/api_consts.dart +++ b/lib/core/api_consts.dart @@ -724,6 +724,8 @@ const DEACTIVATE_ACCOUNT = 'Services/Patients.svc/REST/PatientAppleActivation_In const FAMILY_FILES= 'Services/Authentication.svc/REST/GetAllSharedRecordsByStatus'; +var GET_PRESCRIPTION_INSTRUCTIONS_PDF = 'Services/ChatBot_Service.svc/REST/Chatbot_SendMedicationInstructionByWhatsApp'; + class ApiConsts { static const maxSmallScreen = 660; diff --git a/lib/features/prescriptions/prescriptions_repo.dart b/lib/features/prescriptions/prescriptions_repo.dart index 5f2db30..0c8dc93 100644 --- a/lib/features/prescriptions/prescriptions_repo.dart +++ b/lib/features/prescriptions/prescriptions_repo.dart @@ -11,6 +11,8 @@ abstract class PrescriptionsRepo { Future>>> getPatientPrescriptionOrders({required String patientId}); Future>>> getPatientPrescriptionDetails({required PatientPrescriptionsResponseModel prescriptionsResponseModel}); + + Future>> getPrescriptionInstructionsPDF({required PatientPrescriptionsResponseModel prescriptionsResponseModel}); } class PrescriptionsRepoImp implements PrescriptionsRepo { @@ -107,4 +109,48 @@ class PrescriptionsRepoImp implements PrescriptionsRepo { return Left(UnknownFailure(e.toString())); } } + + @override + Future> getPrescriptionInstructionsPDF({required PatientPrescriptionsResponseModel prescriptionsResponseModel}) async { + Map mapDevice = { + "AppointmentNo": prescriptionsResponseModel.appointmentNo.toString(), + "SetupID": prescriptionsResponseModel.setupID, + "ClinicID": prescriptionsResponseModel.clinicID.toString(), + "ProjectID": prescriptionsResponseModel.projectID.toString(), + "LocationID": "0", + "SalesInvoiceNo": "0", + "IsTest": false, + "ChannelID": "3", + }; + + try { + GenericApiModel? apiResponse; + Failure? failure; + await apiClient.post( + GET_PRESCRIPTION_INSTRUCTIONS_PDF, + body: mapDevice, + onFailure: (error, statusCode, {messageStatus, failureType}) { + failure = failureType; + }, + onSuccess: (response, statusCode, {messageStatus, errorMessage}) { + try { + final prescriptionPDFURL = response["InvoiceUrl"]; + apiResponse = GenericApiModel( + messageStatus: messageStatus, + statusCode: statusCode, + errorMessage: null, + data: prescriptionPDFURL, + ); + } catch (e) { + failure = DataParsingFailure(e.toString()); + } + }, + ); + if (failure != null) return Left(failure!); + if (apiResponse == null) return Left(ServerFailure("Unknown error")); + return Right(apiResponse!); + } catch (e) { + return Left(UnknownFailure(e.toString())); + } + } } diff --git a/lib/features/prescriptions/prescriptions_view_model.dart b/lib/features/prescriptions/prescriptions_view_model.dart index ebc3be8..00facf1 100644 --- a/lib/features/prescriptions/prescriptions_view_model.dart +++ b/lib/features/prescriptions/prescriptions_view_model.dart @@ -23,6 +23,8 @@ class PrescriptionsViewModel extends ChangeNotifier { bool isSortByClinic = true; + String prescriptionInstructionsPDFLink = ""; + PrescriptionsViewModel({required this.prescriptionsRepo, required this.errorHandlerService}); initPrescriptionsViewModel() { @@ -126,4 +128,26 @@ class PrescriptionsViewModel extends ChangeNotifier { }, ); } + + Future getPrescriptionInstructionsPDF(PatientPrescriptionsResponseModel prescriptionsResponseModel, {Function(dynamic)? onSuccess, Function(String)? onError}) async { + final result = await prescriptionsRepo.getPrescriptionInstructionsPDF(prescriptionsResponseModel: prescriptionsResponseModel); + + result.fold( + (failure) async { + onError!(failure.message); + }, + (apiResponse) { + if (apiResponse.messageStatus == 2) { + onError!(apiResponse.errorMessage!); + // dialogService.showErrorDialog(message: apiResponse.errorMessage!, onOkPressed: () {}); + } else if (apiResponse.messageStatus == 1) { + prescriptionInstructionsPDFLink = apiResponse.data; + notifyListeners(); + if (onSuccess != null) { + onSuccess(apiResponse); + } + } + }, + ); + } } diff --git a/lib/presentation/prescriptions/prescription_detail_page.dart b/lib/presentation/prescriptions/prescription_detail_page.dart index 9328401..0d2b0ff 100644 --- a/lib/presentation/prescriptions/prescription_detail_page.dart +++ b/lib/presentation/prescriptions/prescription_detail_page.dart @@ -20,8 +20,10 @@ import 'package:hmg_patient_app_new/theme/colors.dart'; import 'package:hmg_patient_app_new/widgets/buttons/custom_button.dart'; import 'package:hmg_patient_app_new/widgets/chip/app_custom_chip_widget.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/shimmer/movies_shimmer_widget.dart'; import 'package:provider/provider.dart'; +import 'package:url_launcher/url_launcher.dart'; class PrescriptionDetailPage extends StatefulWidget { PrescriptionDetailPage({super.key, required this.prescriptionsResponseModel}); @@ -59,7 +61,33 @@ class _PrescriptionDetailPageState extends State { Expanded( child: CollapsingListView( title: LocaleKeys.prescriptions.tr(context: context), - instructions: () {}, + instructions: () async { + LoaderBottomSheet.showLoader(loadingText: "Fetching prescription PDF, Please wait...".needTranslation); + await prescriptionsViewModel.getPrescriptionInstructionsPDF(widget.prescriptionsResponseModel, onSuccess: (val) { + LoaderBottomSheet.hideLoader(); + if (prescriptionsViewModel.prescriptionInstructionsPDFLink.isNotEmpty) { + Uri uri = Uri.parse(prescriptionsViewModel.prescriptionInstructionsPDFLink); + launchUrl(uri, mode: LaunchMode.platformDefault, webOnlyWindowName: ""); + } else { + showCommonBottomSheetWithoutHeight( + context, + child: Utils.getErrorWidget(loadingText: "Unable to fetch PDF".needTranslation), + callBackFunc: () {}, + isFullScreen: false, + isCloseButtonVisible: true, + ); + } + }, onError: (err) { + LoaderBottomSheet.hideLoader(); + showCommonBottomSheetWithoutHeight( + context, + child: Utils.getErrorWidget(loadingText: err), + callBackFunc: () {}, + isFullScreen: false, + isCloseButtonVisible: true, + ); + }); + }, child: SingleChildScrollView( child: Consumer(builder: (context, prescriptionVM, child) { return Column(