import 'package:flutter/material.dart'; import 'package:hmg_patient_app_new/features/prescriptions/models/resp_models/patient_prescriptions_response_model.dart'; import 'package:hmg_patient_app_new/features/prescriptions/models/resp_models/prescription_detail_response_model.dart'; import 'package:hmg_patient_app_new/features/prescriptions/prescriptions_repo.dart'; import 'package:hmg_patient_app_new/services/error_handler_service.dart'; class PrescriptionsViewModel extends ChangeNotifier { bool isPrescriptionsOrdersLoading = false; bool isPrescriptionsDetailsLoading = false; PrescriptionsRepo prescriptionsRepo; ErrorHandlerService errorHandlerService; // Prescription Orders Lists List patientPrescriptionOrders = []; List patientPrescriptionOrdersByClinic = []; List patientPrescriptionOrdersByHospital = []; List patientPrescriptionOrdersViewList = []; // Prescription Details List List prescriptionDetailsList = []; bool isSortByClinic = true; PrescriptionsViewModel({required this.prescriptionsRepo, required this.errorHandlerService}); initPrescriptionsViewModel() { patientPrescriptionOrders.clear(); patientPrescriptionOrdersByClinic.clear(); patientPrescriptionOrdersByHospital.clear(); patientPrescriptionOrdersViewList.clear(); isPrescriptionsOrdersLoading = true; isSortByClinic = true; getPatientPrescriptionOrders(); notifyListeners(); } setPrescriptionsDetailsLoading() { isPrescriptionsDetailsLoading = true; prescriptionDetailsList.clear(); notifyListeners(); } setPrescriptionItemReminder(bool value, PrescriptionDetailResponseModel item) { int index = prescriptionDetailsList.indexOf(item); if (index != -1) { prescriptionDetailsList[index].hasReminder = value; notifyListeners(); } } notify() { notifyListeners(); } setIsSortByClinic(bool value) { isSortByClinic = value; if (isSortByClinic) { patientPrescriptionOrdersViewList = patientPrescriptionOrdersByClinic; } else { patientPrescriptionOrdersViewList = patientPrescriptionOrdersByHospital; } notifyListeners(); } Future getPatientPrescriptionOrders({Function(dynamic)? onSuccess, Function(String)? onError}) async { final result = await prescriptionsRepo.getPatientPrescriptionOrders(patientId: "1231755"); result.fold( // (failure) async => await errorHandlerService.handleError(failure: failure), (failure) async { isPrescriptionsOrdersLoading = false; notifyListeners(); }, (apiResponse) { if (apiResponse.messageStatus == 2) { // dialogService.showErrorDialog(message: apiResponse.errorMessage!, onOkPressed: () {}); } else if (apiResponse.messageStatus == 1) { patientPrescriptionOrders = apiResponse.data!; isPrescriptionsOrdersLoading = false; for (var element in patientPrescriptionOrders) { List prescriptionsByClinic = patientPrescriptionOrdersByClinic.where((elementClinic) => elementClinic.filterName == element.clinicDescription).toList(); if (prescriptionsByClinic.isNotEmpty) { patientPrescriptionOrdersByClinic[patientPrescriptionOrdersByClinic.indexOf(prescriptionsByClinic[0])].prescriptionsList!.add(element); } else { patientPrescriptionOrdersByClinic.add(PrescriptionsList(filterName: element.clinicDescription, prescriptions: element)); } List prescriptionsByHospital = patientPrescriptionOrdersByHospital.where((elementClinic) => elementClinic.filterName == element.name).toList(); if (prescriptionsByHospital.isNotEmpty) { patientPrescriptionOrdersByHospital[patientPrescriptionOrdersByHospital.indexOf(prescriptionsByHospital[0])].prescriptionsList!.add(element); } else { patientPrescriptionOrdersByHospital.add(PrescriptionsList(filterName: element.name, prescriptions: element)); } } patientPrescriptionOrdersViewList = patientPrescriptionOrdersByClinic; notifyListeners(); if (onSuccess != null) { onSuccess(apiResponse); } } }, ); } Future getPrescriptionDetails(PatientPrescriptionsResponseModel prescriptionsResponseModel, {Function(dynamic)? onSuccess, Function(String)? onError}) async { final result = await prescriptionsRepo.getPatientPrescriptionDetails(prescriptionsResponseModel: prescriptionsResponseModel); result.fold( (failure) async => await errorHandlerService.handleError(failure: failure), (apiResponse) { if (apiResponse.messageStatus == 2) { // dialogService.showErrorDialog(message: apiResponse.errorMessage!, onOkPressed: () {}); } else if (apiResponse.messageStatus == 1) { prescriptionDetailsList = apiResponse.data!; isPrescriptionsDetailsLoading = false; notifyListeners(); if (onSuccess != null) { onSuccess(apiResponse); } } }, ); } }