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.
441 lines
17 KiB
Dart
441 lines
17 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:flutter_svg/flutter_svg.dart';
|
|
import 'package:hmg_patient_app/config/config.dart';
|
|
import 'package:hmg_patient_app/config/shared_pref_kay.dart';
|
|
import 'package:hmg_patient_app/core/service/client/base_app_client.dart';
|
|
import 'package:hmg_patient_app/core/viewModels/project_view_model.dart';
|
|
import 'package:hmg_patient_app/extensions/string_extensions.dart';
|
|
import 'package:hmg_patient_app/models/Authentication/authenticated_user.dart';
|
|
import 'package:hmg_patient_app/models/Authentication/check_activation_code_response.dart';
|
|
import 'package:hmg_patient_app/models/Authentication/select_device_imei_res.dart';
|
|
import 'package:hmg_patient_app/new_ui/otp/otp_validation_bootmsheet_widget.dart';
|
|
import 'package:hmg_patient_app/pages/landing/landing_page.dart';
|
|
import 'package:hmg_patient_app/pages/login/welcome.dart';
|
|
import 'package:hmg_patient_app/services/authentication/auth_provider.dart' hide sharedPref;
|
|
import 'package:hmg_patient_app/uitl/app_toast.dart';
|
|
import 'package:hmg_patient_app/uitl/date_uitl.dart';
|
|
import 'package:hmg_patient_app/uitl/gif_loader_dialog_utils.dart';
|
|
import 'package:hmg_patient_app/uitl/translations_delegate_base.dart';
|
|
import 'package:hmg_patient_app/uitl/utils.dart' hide sharedPref;
|
|
import 'package:hmg_patient_app/widgets/text/app_texts_widget.dart';
|
|
import 'package:local_auth/local_auth.dart';
|
|
import 'package:provider/provider.dart';
|
|
|
|
|
|
class SavedLogin extends StatefulWidget {
|
|
final SelectDeviceIMEIRES savedLoginData;
|
|
const SavedLogin(this.savedLoginData, {Key? key}) : super(key: key);
|
|
|
|
@override
|
|
_SavedLogin createState() => _SavedLogin();
|
|
}
|
|
|
|
class _SavedLogin extends State<SavedLogin> {
|
|
TextEditingController get phoneController =>
|
|
TextEditingController(
|
|
text: widget.savedLoginData.mobile!.startsWith('0')
|
|
? widget.savedLoginData.mobile!.substring(1)
|
|
: widget.savedLoginData.mobile,
|
|
);
|
|
|
|
final authService = new AuthProvider();
|
|
late ProjectViewModel projectViewModel;
|
|
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
|
|
projectViewModel = Provider.of(context);
|
|
|
|
return Scaffold(
|
|
backgroundColor: const Color(0xFFF8F8FA),
|
|
body: SafeArea(
|
|
child: Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 24.0),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
children: [
|
|
const Spacer(flex: 2),
|
|
// Welcome back text
|
|
const Text(
|
|
'Welcome back',
|
|
style: TextStyle(
|
|
fontSize: 22,
|
|
color: Color(0xFF6C757D),
|
|
fontWeight: FontWeight.w400,
|
|
),
|
|
),
|
|
const SizedBox(height: 16),
|
|
// Names
|
|
Column(
|
|
children: [
|
|
Text(
|
|
widget.savedLoginData.name!.toLowerCase().capitalizeFirstofEach,
|
|
style: TextStyle(
|
|
fontSize: 28,
|
|
fontWeight: FontWeight.bold,
|
|
color: Colors.grey[800],
|
|
),
|
|
),
|
|
// Text(
|
|
// 'Abdul Ghaffar',
|
|
// style: TextStyle(
|
|
// fontSize: 28,
|
|
// fontWeight: FontWeight.bold,
|
|
// color: Colors.grey[800],
|
|
// ),
|
|
// ),
|
|
],
|
|
),
|
|
const SizedBox(height: 24),
|
|
Container(
|
|
padding: const EdgeInsets.all(16),
|
|
decoration: BoxDecoration(
|
|
color: Colors.white,
|
|
border: Border.all(color: Colors.white),
|
|
borderRadius: BorderRadius.circular(8.0),
|
|
),
|
|
child: Column(children: [
|
|
|
|
// Last login info
|
|
Text(
|
|
'Last Login by ${getType(widget.savedLoginData!.logInType!, context)}',
|
|
style: const TextStyle(
|
|
fontSize: 14,
|
|
color: Color(0xFF6C757D),
|
|
),
|
|
),
|
|
Text(
|
|
widget.savedLoginData.createdOn != null ? DateUtil.getFormattedDate(DateUtil.convertStringToDate( widget.savedLoginData!.createdOn!), "d MMMM, y 'at' HH:mm") : '--',
|
|
style: const TextStyle(
|
|
fontSize: 16,
|
|
),
|
|
),
|
|
Container(
|
|
margin: EdgeInsets.all(16),
|
|
child: SvgPicture.asset("assets/images/svg/apple-finder.svg", color: Colors.red, height: 54, width: 54,),),
|
|
// Face ID login button
|
|
CustomButton(
|
|
text: "${TranslationBase.of(context).loginBy} ${getType(widget.savedLoginData!.logInType!, context)}",
|
|
onPressed: (){
|
|
loginWithFingerPrintFace(widget.savedLoginData!.logInType!, widget.savedLoginData!.iMEI!);
|
|
|
|
},
|
|
backgroundColor:Color(0xffFEE9EA),
|
|
borderColor:Color(0xffFEE9EA),
|
|
textColor: Color(0xffED1C2B),
|
|
fontSize: 12,
|
|
padding : EdgeInsets.fromLTRB(0, 10, 0, 10),
|
|
// icon: "assets/images/svg/apple-finder.svg",
|
|
),
|
|
],),),
|
|
const SizedBox(height: 24),
|
|
Padding(
|
|
padding: EdgeInsets.symmetric(horizontal: 16.0),
|
|
|
|
child: Text(
|
|
'OR',
|
|
style: TextStyle(
|
|
fontSize: 16,
|
|
fontWeight: FontWeight.w500
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(height: 24),
|
|
// OTP login button
|
|
CustomButton(
|
|
text:TranslationBase.of(context).loginByOTP,
|
|
onPressed: (){
|
|
|
|
showModalBottomSheet(
|
|
context: context,
|
|
isScrollControlled: true,
|
|
isDismissible: false,
|
|
backgroundColor: Colors.transparent,
|
|
builder: (bottomSheetContext) => Padding(
|
|
padding: EdgeInsets.only(bottom: MediaQuery.of(bottomSheetContext).viewInsets.bottom),
|
|
child: SingleChildScrollView(
|
|
child: GenericBottomSheet(
|
|
countryCode: "966",
|
|
initialPhoneNumber: "",
|
|
textController: phoneController!, // Assumed to be a TextEditingController
|
|
buttons: [
|
|
Padding(
|
|
padding: const EdgeInsets.only(bottom: 10),
|
|
child: CustomButton(
|
|
text: "Send me OTP on SMS",
|
|
onPressed: () {
|
|
int? val = Utils.onOtpBtnPressed(OTPType.sms, phoneController);
|
|
if (val != null) {
|
|
//checkUserAuthentication(val);
|
|
}
|
|
},
|
|
backgroundColor: Colors.red,
|
|
borderColor: Colors.red,
|
|
textColor: Colors.white,
|
|
icon: "assets/images/svg/message.svg",
|
|
),
|
|
),
|
|
Row(
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 8),
|
|
child: AppText(
|
|
"OR",
|
|
fontSize: 16,
|
|
fontFamily: 'poppins',
|
|
color: Color(0xFF2E3039),
|
|
fontWeight: FontWeight.w500,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.only(bottom: 10),
|
|
child: CustomButton(
|
|
text: "Send me OTP on WhatsApp",
|
|
onPressed: () {
|
|
int? val = Utils.onOtpBtnPressed(OTPType.whatsapp, phoneController);
|
|
if (val != null) {
|
|
//checkUserAuthentication(val);
|
|
}
|
|
},
|
|
backgroundColor: Colors.white,
|
|
borderColor: Color(0xFF2E3039),
|
|
textColor: Color(0xFF2E3039),
|
|
icon: "assets/images/svg/whatsapp.svg",
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
));},
|
|
backgroundColor:Colors.white,
|
|
borderColor:Colors.black,
|
|
textColor: Colors.black,
|
|
padding : EdgeInsets.fromLTRB(0, 14, 0, 14),
|
|
// icon: "assets/images/svg/apple-finder.svg",
|
|
),
|
|
const Spacer(flex: 2),
|
|
// OR divider
|
|
|
|
const SizedBox(height: 24),
|
|
// Guest and Switch account
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
TextButton(
|
|
onPressed: () { Navigator.of(context).pushReplacement(
|
|
MaterialPageRoute(
|
|
builder: (BuildContext context) => LandingPage(),
|
|
|
|
),
|
|
);},
|
|
child: const Text(
|
|
'Guest',
|
|
style: TextStyle(
|
|
color: Color(0xFFED1C2B),
|
|
fontSize: 16,
|
|
fontWeight: FontWeight.w500,
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(width: 24),
|
|
TextButton(
|
|
onPressed: () {
|
|
Navigator.of(context).pushReplacement(
|
|
MaterialPageRoute(
|
|
builder: (BuildContext context) => WelcomeLogin(),
|
|
|
|
),
|
|
);
|
|
|
|
},
|
|
child: const Text(
|
|
'Switch account',
|
|
style: TextStyle(
|
|
color: Color(0xFFED1C2B),
|
|
fontSize: 16,
|
|
fontWeight: FontWeight.w500,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 20),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
|
|
);
|
|
}
|
|
|
|
String getType(int type, BuildContext context) {
|
|
final t = TranslationBase.of(context);
|
|
final types = {
|
|
1: t.verifySMS,
|
|
2: t.verifyFingerprint,
|
|
3: t.verifyFaceID,
|
|
4: t.verifyWhatsApp,
|
|
};
|
|
return types[type] ?? t.verifySMS;
|
|
}
|
|
|
|
//need to move this function on provider to avoid multiple repeat
|
|
|
|
loginWithFingerPrintFace(type, String deviceToken ) async {
|
|
bool authenticated = false;
|
|
final LocalAuthentication auth = LocalAuthentication();
|
|
|
|
try {
|
|
authenticated = await auth.authenticate(
|
|
localizedReason: 'Scan your fingerprint to authenticate',
|
|
options: const AuthenticationOptions(
|
|
useErrorDialogs: true,
|
|
stickyAuth: true,
|
|
),
|
|
authMessages: [
|
|
// Optional: uncomment for iOS custom messages
|
|
// IOSAuthMessages(
|
|
// cancelButton: 'Cancel',
|
|
// goToSettingsButton: 'Settings',
|
|
// goToSettingsDescription: 'Please set up your Touch ID.',
|
|
// lockOut: 'Please re-enable your Touch ID.',
|
|
// ),
|
|
],
|
|
);
|
|
} on PlatformException catch (e) {
|
|
GifLoaderDialogUtils.hideDialog(context);
|
|
AppToast.showErrorToast(message: 'Please enable your Touch or Face ID');
|
|
return;
|
|
}
|
|
|
|
|
|
if (authenticated) {
|
|
int lastLogin = 1;
|
|
var availableBiometrics = await auth.getAvailableBiometrics();
|
|
|
|
if (availableBiometrics.contains(BiometricType.strong)) {
|
|
lastLogin = 3; // Assume Face or secure fingerprint
|
|
} else if (availableBiometrics.contains(BiometricType.weak)) {
|
|
lastLogin = 2; // Likely weak fingerprint
|
|
}
|
|
|
|
var request = authService.getCommonRequest(
|
|
type: type,
|
|
registerd_data: null,
|
|
deviceToken: widget.savedLoginData.iMEI,
|
|
mobileNumber: int.parse(widget.savedLoginData.mobile!),
|
|
zipCode: widget.savedLoginData.outSA == 1 ? '971' : '966',
|
|
patientOutSA: widget.savedLoginData.outSA,
|
|
loginTokenID: null,
|
|
selectedOption: lastLogin,
|
|
user: widget.savedLoginData,
|
|
);
|
|
|
|
getMobileInfo(request, lastLogin);
|
|
}
|
|
}
|
|
|
|
|
|
getMobileInfo(request,int lastLogin) {
|
|
// GifLoaderDialogUtils.showMyDialog(context);
|
|
this.authService.getLoginInfo(request).then((result) {
|
|
GifLoaderDialogUtils.hideDialog(context);
|
|
if (result['SMSLoginRequired'] == false) {
|
|
// this.loginTokenID = result['LogInTokenID'];
|
|
// this.patientOutSA = result['PatientOutSA'];
|
|
// setDefault();
|
|
checkActivationCode(lastLogin,result['LogInTokenID']);
|
|
}
|
|
}).catchError((err) {
|
|
GifLoaderDialogUtils.hideDialog(context);
|
|
print(err);
|
|
});
|
|
}
|
|
|
|
|
|
checkActivationCode(int loginType, String loginToken) async {
|
|
|
|
AppGlobal.context = context;
|
|
GifLoaderDialogUtils.showMyDialog(context);
|
|
var request = authService.getCommonRequest(
|
|
type: loginType,
|
|
registerd_data: null,
|
|
deviceToken: widget.savedLoginData.iMEI,
|
|
mobileNumber: int.parse(widget.savedLoginData.mobile!),
|
|
zipCode: widget.savedLoginData.outSA == 1 ? '971' : '966',
|
|
patientOutSA: widget.savedLoginData.outSA,
|
|
loginTokenID: loginToken,
|
|
selectedOption: loginType,
|
|
user: widget.savedLoginData,
|
|
).toJson();
|
|
dynamic res;
|
|
|
|
authService
|
|
.checkActivationCode(request, null)
|
|
.then((result) async => {
|
|
res = result,
|
|
if (result is Map)
|
|
{
|
|
result = CheckActivationCode.fromJson(result as Map<String, dynamic>),
|
|
// if (result.errorCode == '699')
|
|
// {
|
|
// //699 block run here
|
|
// GifLoaderDialogUtils.hideDialog(context),
|
|
// Future.delayed(Duration(seconds: 2), () {
|
|
// AppToast.showErrorToast(message: result.errorEndUserMessage);
|
|
// Navigator.popUntil(context, (route) => Utils.route(route, equalsTo: Login));
|
|
// })
|
|
// }
|
|
// else if (this.registerd_data != null && this.registerd_data.isRegister == true)
|
|
// {
|
|
// widget.changePageViewIndex!(1),
|
|
// Navigator.popUntil(context, (route) => Utils.route(route, equalsTo: RegisterNew)),
|
|
// }
|
|
// else
|
|
{
|
|
sharedPref.remove(FAMILY_FILE),
|
|
result.list.isFamily = false,
|
|
// userData = result.list,
|
|
sharedPref.setString(BLOOD_TYPE, result.patientBloodType ?? ""),
|
|
//Remove o+ from here Added by Aamir
|
|
authenticatedUserObject.user = result.list,
|
|
projectViewModel.setPrivilege(privilegeList: res),
|
|
await sharedPref.setObject(MAIN_USER, result.list),
|
|
await sharedPref.setObject(USER_PROFILE, result.list),
|
|
// loginTokenID = result.logInTokenID,
|
|
await sharedPref.setObject(LOGIN_TOKEN_ID, result.logInTokenID),
|
|
await sharedPref.setString(TOKEN, result.authenticationTokenID),
|
|
// checkIfUserAgreedBefore(result),
|
|
projectViewModel.analytics.loginRegistration.login_successful(),
|
|
}
|
|
}
|
|
// else
|
|
// {
|
|
// // Navigator.of(context).pop(),
|
|
// GifLoaderDialogUtils.hideDialog(context),
|
|
// Future.delayed(Duration(seconds: 1), () {
|
|
// Navigator.of(context).pop();
|
|
// AppToast.showErrorToast(message: result, localContext: context);
|
|
// startSMSService(tempType);
|
|
// }),
|
|
//
|
|
// projectViewModel.analytics.loginRegistration.login_fail(error: result),
|
|
// projectViewModel.analytics.errorTracking.log('otp_verification_at_confirm_login', error: result)
|
|
// }
|
|
});
|
|
}
|
|
|
|
|
|
insertIMEI(lastLogin) {
|
|
|
|
authService.insertDeviceImei(lastLogin).then((value) => {}).catchError((err) {
|
|
print(err);
|
|
});
|
|
}
|
|
} |