import 'dart:convert'; import 'package:diplomaticquarterapp/config/config.dart'; import 'package:diplomaticquarterapp/config/shared_pref_kay.dart'; import 'package:diplomaticquarterapp/models/Authentication/check_activation_code_request.dart'; import 'package:diplomaticquarterapp/models/Authentication/authenticated_user.dart'; import 'package:diplomaticquarterapp/models/Authentication/check_activation_code_response.dart'; import 'package:diplomaticquarterapp/models/Authentication/check_paitent_authentication_req.dart'; import 'package:diplomaticquarterapp/models/Authentication/get_mobile_info_request.dart'; import 'package:diplomaticquarterapp/models/Authentication/get_mobile_info_response.dart'; import 'package:diplomaticquarterapp/models/Authentication/insert_device_imei_request.dart'; import 'package:diplomaticquarterapp/models/Authentication/select_device_imei_res.dart'; import 'package:diplomaticquarterapp/core/service/client/base_app_client.dart'; import 'package:diplomaticquarterapp/models/Request.dart'; import 'package:diplomaticquarterapp/uitl/app_shared_preferences.dart'; import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart'; import 'package:flutter/cupertino.dart'; // SharedPreferences sharedPref = new SharedPreferences(); enum APP_STATUS { LOADING, UNAUTHENTICATED, AUTHENTICATED } AppSharedPreferences sharedPref = new AppSharedPreferences(); const String INSERT_DEVICE_IMEI = '/Patients.svc/REST/Patient_INSERTDeviceIMEI'; const String SELECT_DEVICE_IMEI = '/Patients.svc/REST/Patient_SELECTDeviceIMEIbyIMEI'; const String CHECK_PATIENT_AUTH = '/Authentication.svc/REST/CheckPatientAuthentication'; const GET_MOBILE_INFO = '/Authentication.svc/REST/GetMobileLoginInfo'; const SEND_ACTIVATION_CODE = '/Authentication.svc/REST/SendActivationCodebyOTPNotificationType'; const CHECK_ACTIVATION_CODE = '/Authentication.svc/REST/CheckActivationCode'; class AuthProvider with ChangeNotifier { bool isLogin = false; bool isLoading = true; var authenticatedUser; AuthProvider() { getUserAuthentication(); } void getUserAuthentication() async { Map profile = await sharedPref.getObject(USER_PROFILE); if (profile != null) { isLoading = false; isLogin = true; } else { isLoading = false; isLogin = false; } notifyListeners(); } APP_STATUS get stutas { if (isLoading) { return APP_STATUS.LOADING; } else { if (this.isLogin) { return APP_STATUS.AUTHENTICATED; } else { return APP_STATUS.UNAUTHENTICATED; } } } // Future login(UserModel userInfo) async { // try { // dynamic localRes; // await BaseAppClient.post(LOGIN_URL, // onSuccess: (dynamic response, int statusCode) { // localRes = response; // }, onFailure: (String error, int statusCode) { // throw error; // }, body: userInfo.toJson()); // return Future.value(localRes); // } catch (error) { // print(error); // throw error; // } // } Future insertDeviceImei() async { try { dynamic localRes; var lastLogin = await sharedPref.getInt( LAST_LOGIN); //this.cs.sharedService.getStorage(AuthenticationService.LAST_LOGIN); //this.cs.sharedService.getSharedData(AuthenticationService.LAST_LOGIN, false); var request = AuthenticatedUser.fromJson(await sharedPref.getObject(USER_PROFILE)); var newRequest = INSERTDeviceIMEIRequest(); var imei = await sharedPref.getString(PUSH_TOKEN); // if (!request.) { newRequest.iMEI = imei; newRequest.firstName = request.firstName + " " + request.lastName; newRequest.firstNameN = request.firstNameN + " " + request.lastNameN; newRequest.lastNameN = request.lastNameN ?? ""; newRequest.outSA = request.outSA == 1 ? true : false; newRequest.biometricEnabled = false; newRequest.preferredLanguage = int.parse(request.preferredLanguage) ?? 1; newRequest.logInTypeID = lastLogin ?? 1; // } await new BaseAppClient().post(INSERT_DEVICE_IMEI, onSuccess: (dynamic response, int statusCode) { localRes = response; }, onFailure: (String error, int statusCode) { throw error; }, body: newRequest.toJson()); return Future.value(localRes); } catch (error) { print(error); throw error; } } Future selectDeviceImei(imei) async { try { dynamic localRes; Map request = {}; request['IMEI'] = imei; await new BaseAppClient().post(SELECT_DEVICE_IMEI, onSuccess: (dynamic response, int statusCode) { localRes = response; }, onFailure: (String error, int statusCode) { throw error; }, body: request); return Future.value(localRes); } catch (error) { print(error); throw error; } } Future checkPatientAuthentication( CheckPatientAuthenticationReq request) async { request.versionID = VERSION_ID; request.channel = CHANNEL; request.iPAdress = IP_ADDRESS; request.generalid = GENERAL_ID; request.languageID = 2; request.patientOutSA = request.zipCode == '966' ? 0 : 1; try { dynamic localRes; await new BaseAppClient().post(CHECK_PATIENT_AUTH, onSuccess: (dynamic response, int statusCode) { localRes = response; }, onFailure: (String error, int statusCode) { throw error; }, body: request.toJson()); return Future.value(localRes); } catch (error) { print(error); throw error; } } Future getLoginInfo(request) async { request.versionID = VERSION_ID; request.channel = CHANNEL; request.iPAdress = IP_ADDRESS; request.generalid = GENERAL_ID; request.languageID = 2; request.deviceTypeID = DeviceTypeID; request.patientOutSA = request.zipCode == '966' ? 0 : 1; request.isDentalAllowedBackend = false; try { dynamic localRes; await new BaseAppClient().post(GET_MOBILE_INFO, onSuccess: (dynamic response, int statusCode) { localRes = response; }, onFailure: (String error, int statusCode) { throw error; }, body: request.toJson()); return Future.value(localRes); } catch (error) { print(error); throw error; } } Future sendActivationCode(request) async { request.versionID = VERSION_ID; request.channel = CHANNEL; request.iPAdress = IP_ADDRESS; request.generalid = GENERAL_ID; request.languageID = 2; request.deviceTypeID = DeviceTypeID; request.patientOutSA = request.zipCode == '966' ? 0 : 1; request.isDentalAllowedBackend = false; try { var localRes; await new BaseAppClient().post(SEND_ACTIVATION_CODE, onSuccess: (dynamic response, int statusCode) { localRes = response; authenticatedUser = localRes; }, onFailure: (String error, int statusCode) { throw error; }, body: request.toJson()); return Future.value(localRes); } catch (error) { print(error); throw error; } } Future checkActivationCode(request, value) async { var neRequest = CheckActivationCodeReq.fromJson(request); neRequest.activationCode = value ?? "0000"; neRequest.isSilentLogin = value != null ? false : true; neRequest.isRegister = false; neRequest.versionID = VERSION_ID; neRequest.channel = CHANNEL; neRequest.iPAdress = IP_ADDRESS; neRequest.generalid = GENERAL_ID; neRequest.languageID = 2; neRequest.deviceTypeID = DeviceTypeID; neRequest.patientOutSA = neRequest.zipCode == '966' ? 0 : 1; neRequest.isDentalAllowedBackend = false; try { dynamic localRes; await new BaseAppClient().post(CHECK_ACTIVATION_CODE, onSuccess: (dynamic response, int statusCode) { localRes = CheckActivationCode.fromJson(response); }, onFailure: (String error, int statusCode) { throw error; }, body: neRequest.toJson()); return Future.value(localRes); } catch (error) { print(error); throw error; } } AuthenticatedUser getAuthenticatedUser() { this.isLogin = true; return authenticatedUser; } }