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/lab/lab_view_model.dart

93 lines
3.1 KiB
Dart

import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/features/lab/lab_repo.dart';
import 'package:hmg_patient_app_new/features/lab/models/resp_models/patient_lab_orders_response_model.dart';
import 'package:hmg_patient_app_new/services/error_handler_service.dart';
class LabViewModel extends ChangeNotifier {
bool isLabOrdersLoading = false;
bool isLabResultsLoading = false;
LabRepo labRepo;
ErrorHandlerService errorHandlerService;
List<PatientLabOrdersResponseModel> patientLabOrders = [];
List<PatientLabOrdersResponseModel> filteredLabOrders = [];
List<PatientLabOrdersResponseModel> tempLabOrdersList = [];
2 months ago
late List<String> _labSuggestionsList = [];
2 months ago
List<String> get labSuggestions => _labSuggestionsList;
Set<TestDetails> uniqueTests = {};
LabViewModel({required this.labRepo, required this.errorHandlerService});
initLabProvider() {
patientLabOrders.clear();
filteredLabOrders.clear();
isLabOrdersLoading = true;
isLabResultsLoading = true;
getPatientLabOrders();
notifyListeners();
}
Future<void> getPatientLabOrders({Function(dynamic)? onSuccess, Function(String)? onError}) async {
2 months ago
final result = await labRepo.getPatientLabOrders();
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) {
patientLabOrders = apiResponse.data!;
filteredLabOrders = List.from(patientLabOrders);
tempLabOrdersList = apiResponse.data!;
isLabOrdersLoading = false;
isLabResultsLoading = false;
2 months ago
filterSuggestions();
getUniqueTestDescription();
notifyListeners();
if (onSuccess != null) {
onSuccess(apiResponse);
}
}
},
);
}
filterSuggestions() {
2 months ago
final List<String> labels = patientLabOrders
.expand((order) => order.testDetails!)
.map((detail) => detail.description)
.whereType<String>()
2 months ago
.toList();
_labSuggestionsList = labels.toSet().toList();
notifyListeners();
}
filterLabReports(String query) {
if (query.isEmpty) {
filteredLabOrders = List.from(patientLabOrders); // reset
} else {
filteredLabOrders = patientLabOrders.where((order) {
final descriptions = order.testDetails?.map((d) => d.description?.toLowerCase()).toList() ?? [];
return descriptions.any((desc) => desc != null && desc.contains(query.toLowerCase()));
}).toList();
patientLabOrders = filteredLabOrders;
}
2 months ago
notifyListeners();
}
getUniqueTestDescription() {
uniqueTests = {
for (var item in patientLabOrders)
if (item.testDetails != null)
...?item.testDetails?.map<TestDetails>((test) =>
TestDetails(description: test.description.toString(), testCode: test.testCode.toString(), testID: test.testID, createdOn: item.createdOn))
};
uniqueTests.forEach(print);
}
}