import 'package:flutter/material.dart'; import 'package:hmg_patient_app_new/features/insurance/insurance_repo.dart'; import 'package:hmg_patient_app_new/features/insurance/models/resp_models/patient_insurance_card_history.dart'; import 'package:hmg_patient_app_new/features/insurance/models/resp_models/patient_insurance_details_response_model.dart'; import 'package:hmg_patient_app_new/features/insurance/models/resp_models/patient_insurance_update_response_model.dart'; import 'package:hmg_patient_app_new/features/lab/lab_repo.dart'; import 'package:hmg_patient_app_new/services/error_handler_service.dart'; class InsuranceViewModel extends ChangeNotifier { bool isInsuranceLoading = false; bool isInsuranceHistoryLoading = false; bool isInsuranceDetailsLoading = false; bool isInsuranceUpdateDetailsLoading = false; InsuranceRepo insuranceRepo; ErrorHandlerService errorHandlerService; List patientInsuranceList = []; List patientInsuranceCardHistoryList = []; PatientInsuranceUpdateResponseModel? patientInsuranceUpdateResponseModel; InsuranceViewModel({required this.insuranceRepo, required this.errorHandlerService}); initInsuranceProvider() { patientInsuranceList.clear(); patientInsuranceCardHistoryList.clear(); isInsuranceLoading = true; isInsuranceHistoryLoading = true; isInsuranceDetailsLoading = true; isInsuranceUpdateDetailsLoading = true; getPatientInsuranceDetails(); notifyListeners(); } setIsInsuranceHistoryLoading(bool val) { isInsuranceHistoryLoading = val; notifyListeners(); } setIsInsuranceDetailsLoading(bool val) { isInsuranceDetailsLoading = val; notifyListeners(); } setIsInsuranceUpdateDetailsLoading(bool val) { isInsuranceUpdateDetailsLoading = val; notifyListeners(); } Future getPatientInsuranceDetails({Function(dynamic)? onSuccess, Function(String)? onError}) async { final result = await insuranceRepo.getPatientInsuranceDetails(); result.fold( // (failure) async => await errorHandlerService.handleError(failure: failure), (failure) async { isInsuranceLoading = false; }, (apiResponse) { if (apiResponse.messageStatus == 2) { // dialogService.showErrorDialog(message: apiResponse.errorMessage!, onOkPressed: () {}); } else if (apiResponse.messageStatus == 1) { patientInsuranceList = apiResponse.data!; isInsuranceLoading = false; notifyListeners(); if (onSuccess != null) { onSuccess(apiResponse); } } }, ); } Future getPatientInsuranceCardHistory({Function(dynamic)? onSuccess, Function(String)? onError}) async { final result = await insuranceRepo.getPatientInsuranceCardHistory(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) { patientInsuranceCardHistoryList = apiResponse.data!; patientInsuranceCardHistoryList = patientInsuranceCardHistoryList.take(3).toList(); isInsuranceHistoryLoading = false; notifyListeners(); if (onSuccess != null) { onSuccess(apiResponse); } } }, ); } Future getPatientInsuranceDetailsForUpdate(String patientID, String identificationNo, {Function(dynamic)? onSuccess, Function(String)? onError}) async { final result = await insuranceRepo.getPatientInsuranceDetailsForUpdate(patientId: patientID, identificationNo: identificationNo); 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) { patientInsuranceUpdateResponseModel = apiResponse.data!; isInsuranceUpdateDetailsLoading = false; notifyListeners(); if (onSuccess != null) { onSuccess(apiResponse); } } }, ); } }