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.
HMG_Patient_App_New/lib/features/prescriptions/prescriptions_view_model.dart

177 lines
6.9 KiB
Dart

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<PatientPrescriptionsResponseModel> patientPrescriptionOrders = [];
List<PrescriptionsList> patientPrescriptionOrdersByClinic = [];
List<PrescriptionsList> patientPrescriptionOrdersByHospital = [];
List<PrescriptionsList> patientPrescriptionOrdersViewList = [];
// Prescription Details List
List<PrescriptionDetailResponseModel> prescriptionDetailsList = [];
bool isSortByClinic = true;
String prescriptionInstructionsPDFLink = "";
String prescriptionPDFBase64Data = "";
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<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),
(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<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);
}
}
},
);
}
Future<void> 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);
}
}
},
);
}
Future<void> 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);
}
}
},
);
}
Future<void> getPrescriptionPDFBase64(PatientPrescriptionsResponseModel prescriptionsResponseModel, {Function(dynamic)? onSuccess, Function(String)? onError}) async {
final result = await prescriptionsRepo.getPrescriptionPDF(prescriptionsResponseModel: prescriptionsResponseModel, prescriptionDetailsList: prescriptionDetailsList);
result.fold(
(failure) async {
onError!(failure.message);
},
(apiResponse) {
if (apiResponse.messageStatus == 2) {
onError!(apiResponse.errorMessage!);
} else if (apiResponse.messageStatus == 1) {
prescriptionPDFBase64Data = apiResponse.data;
notifyListeners();
if (onSuccess != null) {
onSuccess(apiResponse);
}
}
},
);
}
}