You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			83 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			Dart
		
	
		
		
			
		
	
	
			83 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			Dart
		
	
| 
											2 months ago
										 | 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/prescriptions_repo.dart'; | ||
|  | import 'package:hmg_patient_app_new/services/error_handler_service.dart'; | ||
|  | 
 | ||
|  | class PrescriptionsViewModel extends ChangeNotifier { | ||
|  |   bool isPrescriptionsOrdersLoading = false; | ||
|  | 
 | ||
|  |   PrescriptionsRepo prescriptionsRepo; | ||
|  |   ErrorHandlerService errorHandlerService; | ||
|  | 
 | ||
|  |   List<PatientPrescriptionsResponseModel> patientPrescriptionOrders = []; | ||
|  | 
 | ||
|  |   List<PrescriptionsList> patientPrescriptionOrdersByClinic = []; | ||
|  |   List<PrescriptionsList> patientPrescriptionOrdersByHospital = []; | ||
|  | 
 | ||
|  |   List<PrescriptionsList> patientPrescriptionOrdersViewList = []; | ||
|  | 
 | ||
|  |   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(); | ||
|  |   } | ||
|  | 
 | ||
|  |   setIsSortByClinic(bool value) { | ||
|  |     isSortByClinic = value; | ||
|  |     if (isSortByClinic) { | ||
|  |       patientPrescriptionOrdersViewList = patientPrescriptionOrdersByClinic; | ||
|  |     } else { | ||
|  |       patientPrescriptionOrdersViewList = patientPrescriptionOrdersByHospital; | ||
|  |     } | ||
|  |     notifyListeners(); | ||
|  |   } | ||
|  | 
 | ||
|  |   Future<void> getPatientPrescriptionOrders({Function(dynamic)? onSuccess, Function(String)? onError}) async { | ||
|  |     final result = await prescriptionsRepo.getPatientPrescriptionOrders(patientId: "1231755"); | ||
|  | 
 | ||
|  |     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) { | ||
|  |           patientPrescriptionOrders = apiResponse.data!; | ||
|  |           isPrescriptionsOrdersLoading = false; | ||
|  | 
 | ||
|  |           for (var element in patientPrescriptionOrders) { | ||
|  |             List<PrescriptionsList> 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<PrescriptionsList> 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); | ||
|  |           } | ||
|  |         } | ||
|  |       }, | ||
|  |     ); | ||
|  |   } | ||
|  | } |