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.
hmg_nurses/lib/ui/login/login_vm.dart

135 lines
3.7 KiB
Dart

import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:hmg_nurses/provider/base_vm.dart';
import 'package:hmg_nurses/classes/enums.dart';
import 'package:hmg_nurses/classes/utils.dart';
import 'package:hmg_nurses/generated/locale_keys.g.dart';
import 'package:hmg_nurses/widgets/dialogs/otp_dialog.dart';
import 'package:local_auth/local_auth.dart';
class LoginViewModel extends BaseViewModel {
bool isLogin = false;
bool unverified = false;
bool isFromLogin = false;
String localToken = "";
String nurseProfile = "";
final LocalAuthentication auth = LocalAuthentication();
late List<BiometricType> _availableBiometrics;
LoginViewModel();
/// get type name based on id.
String getType(type) {
switch (type) {
case 1:
return LocaleKeys.sms.tr();
case 3:
return LocaleKeys.fingerPrint.tr();
case 4:
return LocaleKeys.face.tr();
case 2:
return LocaleKeys.whatsapp.tr();
default:
return LocaleKeys.sms.tr();
}
}
int getLoginMethodId(AuthMethodTypes authMethodTypes) {
switch (authMethodTypes) {
case AuthMethodTypes.sms:
return 1;
case AuthMethodTypes.whatsApp:
return 2;
case AuthMethodTypes.fingerPrint:
return 3;
case AuthMethodTypes.faceID:
return 4;
default:
return 1;
}
}
/// ask user to add his biometric
showIOSAuthMessages() async {
try {
await auth.authenticate(
localizedReason: 'Scan your fingerprint to authenticate',
);
} on PlatformException catch (e) {
if (kDebugMode) {
print(e);
}
}
}
/// check specific biometric if it available or not
Future<bool> checkIfBiometricAvailable(BiometricType biometricType) async {
bool isAvailable = false;
await _getAvailableBiometrics();
if (_availableBiometrics != null) {
for (var i = 0; i < _availableBiometrics.length; i++) {
if (biometricType == _availableBiometrics[i]) isAvailable = true;
}
}
return isAvailable;
}
/// get all available biometric on the device for local Auth service
Future<void> _getAvailableBiometrics() async {
try {
_availableBiometrics = await auth.getAvailableBiometrics();
} on PlatformException catch (e) {
if (kDebugMode) {
print(e);
}
}
}
/// determine the status of the ap
setUnverified(bool unverified, {bool isFromLogin = false}) {
this.unverified = unverified;
this.isFromLogin = isFromLogin;
notifyListeners();
}
/// logout function
logout({bool isFromLogin = false}) async {
// localToken = "";
// String lang = await sharedPref.getString(APP_Language);
// await Utils.clearSharedPref();
// doctorProfile = null;
// sharedPref.setString(APP_Language, lang);
// deleteUser();
// await getDeviceInfoFromFirebase();
// this.isFromLogin = isFromLogin;
// appStatus = APPSTATUS.unAuthenticated;
// setState(ViewState.Idle);
}
deleteUser() {
// user = null;
// unverified = false;
// isLogin = false;
}
startSMSService(AuthMethodTypes type, {isSilentLogin = false, required BuildContext context}) {
OtpDialog(
type: 1,
mobileNo: "0504278212",
onSuccess: (String otpCode, TextEditingController pinPut) {
Utils.showLoading();
//TODO: API CALL
// performDirectApiCall(_title, _icon, _flag, value);
},
onFailure: () => Navigator.pop(context),
onResendCode: () {},
).displayDialog(context);
}
}