From 7476c8e55a164e755401624b13bc458992378290 Mon Sep 17 00:00:00 2001 From: haroon amjad Date: Mon, 15 Sep 2025 09:26:48 +0300 Subject: [PATCH] book appo implementation contd. --- .../book_appointments_repo.dart | 55 +++++++++++++++++-- .../book_appointments_view_model.dart | 31 ++++++++++- .../book_appointments/models/free_slot.dart | 12 ++++ .../doctor_profile_response_model.dart | 0 4 files changed, 93 insertions(+), 5 deletions(-) create mode 100644 lib/features/book_appointments/models/free_slot.dart rename lib/features/book_appointments/models/{ => resp_models}/doctor_profile_response_model.dart (100%) diff --git a/lib/features/book_appointments/book_appointments_repo.dart b/lib/features/book_appointments/book_appointments_repo.dart index baa7a09..71023b5 100644 --- a/lib/features/book_appointments/book_appointments_repo.dart +++ b/lib/features/book_appointments/book_appointments_repo.dart @@ -4,7 +4,7 @@ import 'package:hmg_patient_app_new/core/api_consts.dart'; import 'package:hmg_patient_app_new/core/common_models/generic_api_model.dart'; import 'package:hmg_patient_app_new/core/exceptions/api_failure.dart'; import 'package:hmg_patient_app_new/core/utils/date_util.dart'; -import 'package:hmg_patient_app_new/features/book_appointments/models/doctor_profile_response_model.dart'; +import 'package:hmg_patient_app_new/features/book_appointments/models/resp_models/doctor_profile_response_model.dart'; import 'package:hmg_patient_app_new/features/book_appointments/models/resp_models/doctors_list_response_model.dart'; import 'package:hmg_patient_app_new/features/book_appointments/models/resp_models/get_clinic_list_response_model.dart'; import 'package:hmg_patient_app_new/services/logger_service.dart'; @@ -18,7 +18,8 @@ abstract class BookAppointmentsRepo { Future>> getDoctorProfile(int clinicID, int projectID, int doctorId, {Function(dynamic)? onSuccess, Function(String)? onError}); - Future>> getDoctorFreeSlots(int clinicID, int projectID, int doctorId, {Function(dynamic)? onSuccess, Function(String)? onError}); + Future>> getDoctorFreeSlots(int clinicID, int projectID, int doctorId, bool isBookingForLiveCare, + {Function(dynamic)? onSuccess, Function(String)? onError}); } class BookAppointmentsRepoImp implements BookAppointmentsRepo { @@ -175,8 +176,54 @@ class BookAppointmentsRepoImp implements BookAppointmentsRepo { } } + //TODO: Implement the logic for Dental & laser clinics @override - Future>> getDoctorFreeSlots(int clinicID, int projectID, int doctorId, {Function(dynamic)? onSuccess, Function(String)? onError}) { - throw UnimplementedError(); + Future>> getDoctorFreeSlots(int clinicID, int projectID, int doctorId, bool isBookingForLiveCare, + {Function(dynamic)? onSuccess, Function(String)? onError}) async { + Map mapDevice = { + "DoctorID": doctorId, + "IsBookingForLiveCare": isBookingForLiveCare, + "ClinicID": clinicID, + "ProjectID": projectID, + "OriginalClinicID": clinicID, + "days": 0, + "isReschadual": false, + }; + + try { + GenericApiModel? apiResponse; + Failure? failure; + await apiClient.post( + GET_DOCTOR_FREE_SLOTS, + body: mapDevice, + onFailure: (error, statusCode, {messageStatus, failureType}) { + failure = failureType; + }, + onSuccess: (response, statusCode, {messageStatus, errorMessage}) { + try { + final list = response['FreeTimeSlots']; + // if (list == null || list.isEmpty) { + // throw Exception("lab list is empty"); + // } + + // final freeSlotsList = list.map((item) => DoctorsListResponseModel.fromJson(item as Map)).toList().cast(); + + apiResponse = GenericApiModel>( + messageStatus: messageStatus, + statusCode: statusCode, + errorMessage: null, + data: response['FreeTimeSlots'], + ); + } catch (e) { + failure = DataParsingFailure(e.toString()); + } + }, + ); + if (failure != null) return Left(failure!); + if (apiResponse == null) return Left(ServerFailure("Unknown error")); + return Right(apiResponse!); + } catch (e) { + return Left(UnknownFailure(e.toString())); + } } } diff --git a/lib/features/book_appointments/book_appointments_view_model.dart b/lib/features/book_appointments/book_appointments_view_model.dart index dbe1439..c4258cd 100644 --- a/lib/features/book_appointments/book_appointments_view_model.dart +++ b/lib/features/book_appointments/book_appointments_view_model.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import 'package:hmg_patient_app_new/features/book_appointments/book_appointments_repo.dart'; -import 'package:hmg_patient_app_new/features/book_appointments/models/doctor_profile_response_model.dart'; +import 'package:hmg_patient_app_new/features/book_appointments/models/free_slot.dart'; +import 'package:hmg_patient_app_new/features/book_appointments/models/resp_models/doctor_profile_response_model.dart'; import 'package:hmg_patient_app_new/features/book_appointments/models/resp_models/doctors_list_response_model.dart'; import 'package:hmg_patient_app_new/features/book_appointments/models/resp_models/get_clinic_list_response_model.dart'; import 'package:hmg_patient_app_new/services/error_handler_service.dart'; @@ -24,6 +25,8 @@ class BookAppointmentsViewModel extends ChangeNotifier { late DoctorsProfileResponseModel doctorsProfileResponseModel; + List slotsList = []; + BookAppointmentsRepo bookAppointmentsRepo; ErrorHandlerService errorHandlerService; @@ -49,6 +52,7 @@ class BookAppointmentsViewModel extends ChangeNotifier { isDoctorProfileLoading = true; clinicsList.clear(); doctorsList.clear(); + slotsList.clear(); notifyListeners(); } @@ -156,4 +160,29 @@ class BookAppointmentsViewModel extends ChangeNotifier { }, ); } + + Future getDoctorFreeSlots({bool isBookingForLiveCare = false, Function(dynamic)? onSuccess, Function(String)? onError}) async { + slotsList.clear(); + final result = await bookAppointmentsRepo.getDoctorFreeSlots(selectedDoctor.clinicID ?? 0, selectedDoctor.projectID ?? 0, selectedDoctor.doctorID ?? 0, isBookingForLiveCare, onError: onError); + + result.fold( + (failure) async {}, + (apiResponse) { + if (apiResponse.messageStatus == 2) { + onError!(apiResponse.errorMessage ?? "Unknown error occurred"); + // dialogService.showErrorDialog(message: apiResponse.errorMessage!, onOkPressed: () {}); + } else if (apiResponse.messageStatus == 1) { + + // apiResponse.data.forEach((element) { + // slotsList.add(FreeSlot.fromJson(element)); + // }); + + notifyListeners(); + if (onSuccess != null) { + onSuccess(apiResponse); + } + } + }, + ); + } } diff --git a/lib/features/book_appointments/models/free_slot.dart b/lib/features/book_appointments/models/free_slot.dart new file mode 100644 index 0000000..4669298 --- /dev/null +++ b/lib/features/book_appointments/models/free_slot.dart @@ -0,0 +1,12 @@ +class FreeSlot { + List event; + DateTime slot; + + + FreeSlot(this.slot, this.event); + + @override + String toString() { + return '{ ${this.slot}, ${this.event} }'; + } +} \ No newline at end of file diff --git a/lib/features/book_appointments/models/doctor_profile_response_model.dart b/lib/features/book_appointments/models/resp_models/doctor_profile_response_model.dart similarity index 100% rename from lib/features/book_appointments/models/doctor_profile_response_model.dart rename to lib/features/book_appointments/models/resp_models/doctor_profile_response_model.dart