import 'dart:async'; import 'dart:io'; import 'package:dartz/dartz.dart'; import 'package:hmg_patient_app_new/core/api/api_client.dart'; 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/features/authentication/models/request_models/check_patient_authentication_request_model.dart'; import 'package:hmg_patient_app_new/features/authentication/models/resp_models/check_activation_code_resp_model.dart'; import 'package:hmg_patient_app_new/features/authentication/models/resp_models/select_device_by_imei.dart'; import 'package:hmg_patient_app_new/services/logger_service.dart'; abstract class AuthenticationRepo { Future>> selectDeviceByImei({ required String firebaseToken, }); Future>> checkPatientAuthentication({ required CheckPatientAuthenticationReq checkPatientAuthenticationReq, }); Future>> sendActivationCodeRegister({required CheckPatientAuthenticationReq checkPatientAuthenticationReq, String? languageID}); } class AuthenticationRepoImp implements AuthenticationRepo { final ApiClient apiClient; final LoggerService loggerService; AuthenticationRepoImp({required this.loggerService, required this.apiClient}); @override Future>> selectDeviceByImei({required String firebaseToken}) async { final mapDevice = {"IMEI": firebaseToken}; try { GenericApiModel? apiResponse; Failure? failure; await apiClient.post( ApiConsts.selectDeviceImei, body: mapDevice, onFailure: (error, statusCode, {messageStatus, failureType}) { failure = failureType; }, onSuccess: (response, statusCode, {messageStatus}) { try { final list = response['Patient_SELECTDeviceIMEIbyIMEIList']; if (list == null || list.isEmpty) { throw Exception("Device list is empty"); } final model = SelectDeviceByImeiRespModelElement.fromJson(list[0] as Map); apiResponse = GenericApiModel( messageStatus: messageStatus, statusCode: statusCode, errorMessage: null, data: model, ); } 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())); } } @override Future>> checkPatientAuthentication({ required CheckPatientAuthenticationReq checkPatientAuthenticationReq, String? languageID, }) async { int isOutKsa = (checkPatientAuthenticationReq.zipCode == '966' || checkPatientAuthenticationReq.zipCode == '+966') ? 0 : 1; //TODO : We will use all these from AppState directly in the ApiClient checkPatientAuthenticationReq.versionID = VERSION_ID; checkPatientAuthenticationReq.channel = CHANNEL; checkPatientAuthenticationReq.iPAdress = IP_ADDRESS; checkPatientAuthenticationReq.generalid = GENERAL_ID; checkPatientAuthenticationReq.languageID = (languageID == 'ar' ? 1 : 2); checkPatientAuthenticationReq.patientOutSA = isOutKsa; try { GenericApiModel? apiResponse; Failure? failure; await apiClient.post( ApiConsts.selectDeviceImei, body: checkPatientAuthenticationReq.toJson(), onFailure: (error, statusCode, {messageStatus, failureType}) { failure = failureType; }, onSuccess: (response, statusCode, {messageStatus}) { try { apiResponse = GenericApiModel( messageStatus: messageStatus, statusCode: statusCode, errorMessage: null, data: response, ); } 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())); } } @override Future>> sendActivationCodeRegister({required CheckPatientAuthenticationReq checkPatientAuthenticationReq, String? languageID}) async { int isOutKsa = (checkPatientAuthenticationReq.zipCode == '966' || checkPatientAuthenticationReq.zipCode == '+966') ? 0 : 1; //TODO : We will use all these from AppState directly in the ApiClient checkPatientAuthenticationReq.versionID = VERSION_ID; checkPatientAuthenticationReq.channel = CHANNEL; checkPatientAuthenticationReq.iPAdress = IP_ADDRESS; checkPatientAuthenticationReq.generalid = GENERAL_ID; checkPatientAuthenticationReq.languageID = (languageID == 'ar' ? 1 : 2); checkPatientAuthenticationReq.deviceTypeID = Platform.isIOS ? 1 : 2; checkPatientAuthenticationReq.patientOutSA = isOutKsa; checkPatientAuthenticationReq.isDentalAllowedBackend = false; try { GenericApiModel? apiResponse; Failure? failure; await apiClient.post( SEND_ACTIVATION_CODE_REGISTER, body: checkPatientAuthenticationReq.toJson(), onFailure: (error, statusCode, {messageStatus, failureType}) { failure = failureType; }, onSuccess: (response, statusCode, {messageStatus}) { try { apiResponse = GenericApiModel( messageStatus: messageStatus, statusCode: statusCode, errorMessage: null, data: CheckActivationCode.fromJson(response), ); } 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())); } } }