diff --git a/lib/features/medical_file/medical_file_view_model.dart b/lib/features/medical_file/medical_file_view_model.dart index e0abd9d7..6fb8c8a2 100644 --- a/lib/features/medical_file/medical_file_view_model.dart +++ b/lib/features/medical_file/medical_file_view_model.dart @@ -29,7 +29,11 @@ class MedicalFileViewModel extends ChangeNotifier { bool isPatientSickLeavePDFLoading = false; bool isPatientMedicalReportsListLoading = false; bool isPatientAllergiesListLoading = false; + bool _isFamilyFilesLoading = false; + bool get isFamilyFilesLoading => _isFamilyFilesLoading; + + set setIsFamilyFilesLoading(bool value) => _isFamilyFilesLoading = value; MedicalFileRepo medicalFileRepo; ErrorHandlerService errorHandlerService; @@ -431,21 +435,42 @@ class MedicalFileViewModel extends ChangeNotifier { } Future getFamilyFiles({int? status, Function(dynamic)? onSuccess, Function(String)? onError}) async { + + setIsFamilyFilesLoading = true; + + final shouldShowLoader = patientFamilyFiles.isEmpty; + if (shouldShowLoader) { + LoaderBottomSheet.showLoader(); + } + final result = await medicalFileRepo.getPatientFamilyFiles(status, _appState.getSuperUserID != null ? _appState.getSuperUserID! : _appState.getAuthenticatedUser()!.patientId!); result.fold( - (failure) async => await errorHandlerService.handleError( - failure: failure, - onOkPressed: () { - onError!(failure.message); - }, - ), + (failure) async { + + setIsFamilyFilesLoading = false; + + await errorHandlerService.handleError( + failure: failure, + onOkPressed: () { + if (shouldShowLoader) { + LoaderBottomSheet.hideLoader(); + } + onError!(failure.message); + }, + ); + }, (apiResponse) { if (apiResponse.messageStatus == 2) { + + setIsFamilyFilesLoading = false; + // _dialogService.showErrorBottomSheet(message: apiResponse.errorMessage!, onOkPressed: () {}); + if (shouldShowLoader) { + LoaderBottomSheet.hideLoader(); + } } else if (apiResponse.messageStatus == 1) { if (apiResponse.data != null) { - // Add current user as the first active family file final currentUser = _appState.getAuthenticatedUser()!; final currentUserFamilyFile = FamilyFileResponseModelLists( patientId: currentUser.patientId, @@ -525,6 +550,12 @@ class MedicalFileViewModel extends ChangeNotifier { pendingFamilyFiles = newPendingFamilyFiles; } + // Clear loading state on success + setIsFamilyFilesLoading = false; + + if (shouldShowLoader) { + LoaderBottomSheet.hideLoader(); + } notifyListeners(); if (onSuccess != null) { onSuccess(apiResponse); @@ -662,13 +693,10 @@ class MedicalFileViewModel extends ChangeNotifier { } Future handleFamilyFileRequestOTPVerification() async { - LoaderBottomSheet.showLoader(); if (!_appState.getIsChildLoggedIn) { await getFamilyFiles(status: 0); await getAllPendingRecordsByResponseId(); } - - LoaderBottomSheet.hideLoader(); } Future removeFileFromFamilyMembers({int? id}) async { diff --git a/lib/presentation/my_family/my_family.dart b/lib/presentation/my_family/my_family.dart index 4eebc75c..4b6f820a 100644 --- a/lib/presentation/my_family/my_family.dart +++ b/lib/presentation/my_family/my_family.dart @@ -16,6 +16,7 @@ import 'package:hmg_patient_app_new/theme/colors.dart'; import 'package:hmg_patient_app_new/widgets/appbar/collapsing_list_view.dart'; import 'package:hmg_patient_app_new/widgets/buttons/custom_button.dart'; import 'package:hmg_patient_app_new/widgets/custom_tab_bar.dart'; +import 'package:hmg_patient_app_new/widgets/loader/bottomsheet_loader.dart'; import 'package:provider/provider.dart'; class FamilyMedicalScreen extends StatefulWidget { @@ -32,9 +33,15 @@ class _FamilyMedicalScreenState extends State { void initState() { super.initState(); medicalVM = context.read(); - WidgetsBinding.instance.addPostFrameCallback((_) { + WidgetsBinding.instance.addPostFrameCallback((_) async { + if(medicalVM!.isFamilyFilesLoading) { + LoaderBottomSheet.showLoader(); + } else { + LoaderBottomSheet.hideLoader(); + } medicalVM?.onFamilyFileTabChange(0); medicalVM?.getAllPendingRecordsByResponseId(); + }); } @@ -90,14 +97,22 @@ class _FamilyMedicalScreenState extends State { ), appState.isChildLoggedIn ? SizedBox() : SizedBox(height: 25.h), Selector patientFiles, List pendingFiles})>( - selector: (_, model) => (selectedIndex: model.getSelectedFamilyFileTabIndex, patientFiles: model.patientFamilyFiles, pendingFiles: model.pendingFamilyFiles), + selector: (_, model) => ( + selectedIndex: model.getSelectedFamilyFileTabIndex, + patientFiles: model.patientFamilyFiles, + pendingFiles: model.pendingFamilyFiles, + ), shouldRebuild: (previous, next) { // Only rebuild if something actually changed return previous.selectedIndex != next.selectedIndex || !identical(previous.patientFiles, next.patientFiles) || !identical(previous.pendingFiles, next.pendingFiles); }, - builder: (context, data, child) => getFamilyTabs(index: data.selectedIndex, patientFiles: data.patientFiles, pendingFiles: data.pendingFiles), + builder: (context, data, child) => getFamilyTabs( + index: data.selectedIndex, + patientFiles: data.patientFiles, + pendingFiles: data.pendingFiles, + ), ), SizedBox(height: 20.h), ], @@ -112,6 +127,7 @@ class _FamilyMedicalScreenState extends State { }) { switch (index) { case 0: + return FamilyCards( profiles: patientFiles, onSelect: (FamilyFileResponseModelLists profile) {