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.
62 lines
2.3 KiB
Dart
62 lines
2.3 KiB
Dart
import 'dart:async';
|
|
|
|
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/select_device_by_imei.dart';
|
|
import 'package:hmg_patient_app_new/services/logger_service.dart';
|
|
|
|
abstract class AuthenticationRepo {
|
|
Future<Either<Failure, GenericApiModel<SelectDeviceByImeiRespModelElement>>> selectDeviceByImei({required String firebaseToken});
|
|
}
|
|
|
|
class AuthenticationRepoImp implements AuthenticationRepo {
|
|
final ApiClient apiClient;
|
|
final LoggerService loggerService;
|
|
|
|
AuthenticationRepoImp({required this.loggerService, required this.apiClient});
|
|
|
|
@override
|
|
Future<Either<Failure, GenericApiModel<SelectDeviceByImeiRespModelElement>>> selectDeviceByImei({
|
|
required String firebaseToken,
|
|
}) async {
|
|
final mapDevice = {"IMEI": firebaseToken};
|
|
try {
|
|
GenericApiModel<SelectDeviceByImeiRespModelElement>? 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'] as List<dynamic>?;
|
|
if (list == null || list.isEmpty) {
|
|
throw Exception("Device list is empty");
|
|
}
|
|
|
|
final model = SelectDeviceByImeiRespModelElement.fromJson(list[0] as Map<String, dynamic>);
|
|
apiResponse = GenericApiModel<SelectDeviceByImeiRespModelElement>(
|
|
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()));
|
|
}
|
|
}
|
|
}
|