diff --git a/assets/langs/en-US.json b/assets/langs/en-US.json index 988c8ac2..ffe888b6 100644 --- a/assets/langs/en-US.json +++ b/assets/langs/en-US.json @@ -703,7 +703,7 @@ "copyLink": "Copy Link", "done": "Done", "linkCopied": "Link copied to clipboard", - "mazayaAccessRevoked": "Mazaya access revoked", + "mazayaAccessRevoked": "Mazaya Access", "mazayaAccessRevokedDesc": "You are not authorized to access the Mazaya Program.", "contactYourSponsor": "Contact your sponsor", "contactYourSponsorDesc": "Ask your sponsor to allow Mazaya access again from Dependent Details. Only the sponsor can restore access.", diff --git a/lib/api/login_api_client.dart b/lib/api/login_api_client.dart index 9fac4e13..e87c3b6b 100644 --- a/lib/api/login_api_client.dart +++ b/lib/api/login_api_client.dart @@ -1,6 +1,7 @@ import 'dart:async'; import 'dart:io'; +import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:mohem_flutter_app/api/api_client.dart'; @@ -114,23 +115,50 @@ class LoginApiClient { } Future insertMobileLoginInfoNEW( - String email, int sessionId, String employeeName, int loginType, String mobileNumber, String userName, String deviceToken, String deviceType, + String email, int sessionId, String employeeName, int loginType, String mobileNumber, String userName, String? deviceToken, String deviceType, {int? memberType}) async { String url = "${ApiConsts.erpRest}Mohemm_InsertMobileLoginInfo"; + String devType = deviceType.isNotEmpty ? deviceType : (Platform.isAndroid ? "android" : "ios"); + String devToken = (deviceToken != null && deviceToken.isNotEmpty && deviceToken != "null") + ? deviceToken + : (AppState().getIsHuawei ? AppState().getHuaweiPushToken : (AppState().getDeviceToken ?? "")); + + if (devToken.isEmpty || devToken == "null") { + try { + String savedToken = await Utils.getStringFromPrefs("P_DEP_DEVICE_TOKEN"); + if (savedToken.isNotEmpty && savedToken != "null") { + devToken = savedToken; + } + } catch (_) {} + } + + if (devToken.isEmpty || devToken == "null") { + try { + if (!AppState().getIsHuawei) { + String? tok = await FirebaseMessaging.instance.getToken(); + if (tok != null && tok.isNotEmpty) { + devToken = tok; + AppState().setDeviceToken = tok; + Utils.saveStringFromPrefs("P_DEP_DEVICE_TOKEN", tok); + } + } + } catch (_) {} + } + Map postParams = { "MobileNumber": mobileNumber, "P_USER_NAME": userName, "UserName": userName, "CompanyID": 1, // todo 'sikander' @discuss umer for companyID - "DeviceToken": deviceToken, + "DeviceToken": devToken, "LoginType": loginType, "EmployeeName": employeeName, "P_SESSION_ID": sessionId, "P_EMAIL_ADDRESS": email, "MemberType": memberType ?? ApiConsts.selectedMemberType.value, }; - postParams["DeviceToken"] = deviceToken; - postParams["DeviceType"] = deviceType; + postParams["DeviceToken"] = devToken; + postParams["DeviceType"] = devType; postParams.addAll(AppState().postParamsJson); return await ApiClient().postJsonForObject((json) { GenericResponseModel? responseData = GenericResponseModel.fromJson(json); @@ -159,10 +187,32 @@ class LoginApiClient { }) async { String url = "${ApiConsts.erpRest}Mohemm_InsertMobileLoginInfo_Dependent"; String devType = deviceType ?? (Platform.isAndroid ? "android" : "ios"); - String devToken = (deviceToken != null && deviceToken.isNotEmpty) + String devToken = (deviceToken != null && deviceToken.isNotEmpty && deviceToken != "null") ? deviceToken : (AppState().getIsHuawei ? AppState().getHuaweiPushToken : (AppState().getDeviceToken ?? "")); + if (devToken.isEmpty || devToken == "null") { + try { + String savedToken = await Utils.getStringFromPrefs("P_DEP_DEVICE_TOKEN"); + if (savedToken.isNotEmpty && savedToken != "null") { + devToken = savedToken; + } + } catch (_) {} + } + + if (devToken.isEmpty || devToken == "null") { + try { + if (!AppState().getIsHuawei) { + String? tok = await FirebaseMessaging.instance.getToken(); + if (tok != null && tok.isNotEmpty) { + devToken = tok; + AppState().setDeviceToken = tok; + Utils.saveStringFromPrefs("P_DEP_DEVICE_TOKEN", tok); + } + } + } catch (_) {} + } + String effectiveToken = (tokenID != null && tokenID.isNotEmpty) ? tokenID : (AppState().postParamsObject?.tokenID ?? AppState().authenticationTokenID ?? ""); diff --git a/lib/ui/landing/dashboard_screen_for_dependet.dart b/lib/ui/landing/dashboard_screen_for_dependet.dart index e6bf6910..57070ad6 100644 --- a/lib/ui/landing/dashboard_screen_for_dependet.dart +++ b/lib/ui/landing/dashboard_screen_for_dependet.dart @@ -103,7 +103,17 @@ class _DashboardScreenForDependetState extends State } int sessionId = AppState().postParamsObject?.pSessionId ?? 0; String email = AppState().postParamsObject?.pEmailAddress ?? ""; - String deviceToken = AppState().getIsHuawei ? AppState().getHuaweiPushToken : (AppState().getDeviceToken ?? ""); + String deviceToken = AppState().getIsHuawei + ? AppState().getHuaweiPushToken + : ((AppState().getDeviceToken != null && AppState().getDeviceToken!.isNotEmpty && AppState().getDeviceToken != "null") + ? AppState().getDeviceToken! + : ""); + if (deviceToken.isEmpty || deviceToken == "null") { + String savedToken = await Utils.getStringFromPrefs("P_DEP_DEVICE_TOKEN"); + if (savedToken.isNotEmpty && savedToken != "null") { + deviceToken = savedToken; + } + } await LoginApiClient().insertMobileLoginInfoDependent( loginType: loginType, diff --git a/lib/ui/login/login_screen.dart b/lib/ui/login/login_screen.dart index 057420cd..4122ba47 100644 --- a/lib/ui/login/login_screen.dart +++ b/lib/ui/login/login_screen.dart @@ -404,10 +404,26 @@ class _LoginScreenState extends State { AppState().setMohemmWifiPassword = mohemmWifiPassword; } Utils.hideLoading(context); + if (firebaseToken == null || firebaseToken!.isEmpty || firebaseToken == "null") { + if (AppState().getDeviceToken != null && AppState().getDeviceToken!.isNotEmpty && AppState().getDeviceToken != "null") { + firebaseToken = AppState().getDeviceToken; + } else { + String saved = await Utils.getStringFromPrefs("P_DEP_DEVICE_TOKEN"); + if (saved.isNotEmpty && saved != "null") { + firebaseToken = saved; + } + } + } + String effectiveTokenArg = (firebaseToken != null && firebaseToken!.isNotEmpty && firebaseToken != "null") + ? firebaseToken! + : (AppState().getDeviceToken ?? ""); if (_autoLogin && loginInfo != null) { + if (loginInfo!.deviceToken == null || loginInfo!.deviceToken!.isEmpty || loginInfo!.deviceToken == "null") { + loginInfo!.deviceToken = effectiveTokenArg; + } Navigator.pushReplacementNamed(context, AppRoutes.verifyLastLogin, arguments: loginInfo); } else { - Navigator.pushNamed(context, AppRoutes.verifyLogin, arguments: "$firebaseToken"); + Navigator.pushNamed(context, AppRoutes.verifyLogin, arguments: effectiveTokenArg); } Utils.saveStringFromPrefs(SharedPrefsConsts.password, password.text); } catch (ex) { @@ -967,7 +983,14 @@ class _LoginScreenState extends State { if (checkOtpResponse?.authenticationTokenID != null && checkOtpResponse!.authenticationTokenID!.trim().isNotEmpty) { Utils.saveStringFromPrefs("P_DEP_AUTHENTICATION_TOKEN_ID", checkOtpResponse.authenticationTokenID!.trim()); } - Utils.saveStringFromPrefs("P_DEP_DEVICE_TOKEN", AppState().getIsHuawei ? AppState().getHuaweiPushToken : firebaseToken ?? ""); + String tokenToSave = AppState().getIsHuawei + ? AppState().getHuaweiPushToken + : ((firebaseToken != null && firebaseToken!.isNotEmpty && firebaseToken != "null") + ? firebaseToken! + : (AppState().getDeviceToken ?? "")); + if (tokenToSave.isNotEmpty && tokenToSave != "null") { + Utils.saveStringFromPrefs("P_DEP_DEVICE_TOKEN", tokenToSave); + } Utils.saveStringFromPrefs("P_DEP_LOGIN_TYPE", loginType.toString()); if (navigateToDashboard && ApiConsts.ISDependetLogin) { @@ -975,6 +998,30 @@ class _LoginScreenState extends State { } } + void _ensureDeviceTokenLoaded() async { + if (firebaseToken == null || firebaseToken!.isEmpty || firebaseToken == "null") { + if (AppState().getDeviceToken != null && AppState().getDeviceToken!.isNotEmpty && AppState().getDeviceToken != "null") { + firebaseToken = AppState().getDeviceToken; + } else { + String saved = await Utils.getStringFromPrefs("P_DEP_DEVICE_TOKEN"); + if (saved.isNotEmpty && saved != "null") { + firebaseToken = saved; + AppState().setDeviceToken = saved; + } else if (!AppState().getIsHuawei) { + try { + await Firebase.initializeApp(); + String? tok = await FirebaseMessaging.instance.getToken(); + if (tok != null && tok.isNotEmpty) { + firebaseToken = tok; + AppState().setDeviceToken = tok; + Utils.saveStringFromPrefs("P_DEP_DEVICE_TOKEN", tok); + } + } catch (_) {} + } + } + } + } + @override Widget build(BuildContext context) { if (isAppOpenBySystem == null) { @@ -990,7 +1037,11 @@ class _LoginScreenState extends State { // 13777 // Ab12345cd } - if (isAppOpenBySystem!) checkFirebaseToken(); + if (isAppOpenBySystem!) { + checkFirebaseToken(); + } else { + _ensureDeviceTokenLoaded(); + } } // username.text = "15444"; @@ -1011,10 +1062,12 @@ class _LoginScreenState extends State { children: [ LocaleKeys.english.tr().toText14(color: AppState().isArabic(context) ? null : MyColors.textMixColor).onPress(() { context.setLocale(const Locale("en", "US")); + _ensureDeviceTokenLoaded(); }), Container(width: 1, color: MyColors.darkWhiteColor, height: 16, margin: const EdgeInsets.only(left: 10, right: 10)), LocaleKeys.arabic.tr().toText14(color: !AppState().isArabic(context) ? null : MyColors.textMixColor).onPress(() { context.setLocale(const Locale("ar", "SA")); + _ensureDeviceTokenLoaded(); }), ], ), diff --git a/lib/ui/login/verify_last_login_screen.dart b/lib/ui/login/verify_last_login_screen.dart index feca8b34..6234a951 100644 --- a/lib/ui/login/verify_last_login_screen.dart +++ b/lib/ui/login/verify_last_login_screen.dart @@ -388,6 +388,11 @@ class _VerifyLastLoginScreenState extends State { Future performDirectApiCall(String _title, String _icon, int _flag, String value, TextEditingController? _pinPutController, {bool isDirectLogin = false}) async { try { GenericResponseModel? genericResponseModel = await LoginApiClient().checkActivationCode(true, AppState().memberLoginList?.pMOBILENUMBER, value, AppState().getUserName); + String devToken = AppState().getIsHuawei + ? AppState().getHuaweiPushToken + : ((mobileLoginInfoListModel?.deviceToken != null && mobileLoginInfoListModel!.deviceToken!.isNotEmpty && mobileLoginInfoListModel!.deviceToken != "null") + ? mobileLoginInfoListModel!.deviceToken! + : (AppState().getDeviceToken ?? "")); GenericResponseModel? genericResponseModel1 = await LoginApiClient().insertMobileLoginInfoNEW( AppState().memberLoginList?.pEMAILADDRESS ?? "", genericResponseModel?.pSESSIONID ?? 0, @@ -395,7 +400,7 @@ class _VerifyLastLoginScreenState extends State { _flag, AppState().memberLoginList?.pMOBILENUMBER ?? "", AppState().getUserName!, - AppState().getIsHuawei ? AppState().getHuaweiPushToken : (mobileLoginInfoListModel?.deviceToken ?? AppState().getDeviceToken ?? ""), + devToken, Platform.isAndroid ? "android" : "ios", memberType: MemberType.employee.value, ); diff --git a/lib/ui/login/verify_login_screen.dart b/lib/ui/login/verify_login_screen.dart index ed7564fe..9fb1c7fb 100644 --- a/lib/ui/login/verify_login_screen.dart +++ b/lib/ui/login/verify_login_screen.dart @@ -55,7 +55,18 @@ class _VerifyLoginScreenState extends State { @override Widget build(BuildContext context) { - firebaseToken ??= ModalRoute.of(context)!.settings.arguments as String; + Object? rawArg = ModalRoute.of(context)?.settings.arguments; + if (rawArg != null) { + String argStr = rawArg.toString().trim(); + if (argStr.isNotEmpty && argStr != "null") { + firebaseToken ??= argStr; + } + } + if (firebaseToken == null || firebaseToken!.isEmpty || firebaseToken == "null") { + if (AppState().getDeviceToken != null && AppState().getDeviceToken!.isNotEmpty && AppState().getDeviceToken != "null") { + firebaseToken = AppState().getDeviceToken; + } + } return Scaffold( appBar: AppBar( backgroundColor: Colors.transparent, @@ -642,6 +653,11 @@ class _VerifyLoginScreenState extends State { Utils.showLoading(context); try { GenericResponseModel? genericResponseModel = await LoginApiClient().checkActivationCode(true, AppState().memberLoginList?.pMOBILENUMBER, value, AppState().getUserName); + String devToken = AppState().getIsHuawei + ? AppState().getHuaweiPushToken + : ((firebaseToken != null && firebaseToken!.isNotEmpty && firebaseToken != "null") + ? firebaseToken! + : (AppState().getDeviceToken ?? "")); GenericResponseModel? genericResponseModel1 = await LoginApiClient().insertMobileLoginInfoNEW( AppState().memberLoginList?.pEMAILADDRESS ?? "", genericResponseModel?.pSESSIONID ?? 0, @@ -649,7 +665,7 @@ class _VerifyLoginScreenState extends State { _flag, AppState().memberLoginList?.pMOBILENUMBER ?? "", AppState().getUserName!, - AppState().getIsHuawei ? AppState().getHuaweiPushToken : firebaseToken!, + devToken, Platform.isAndroid ? "android" : "ios", memberType: MemberType.employee.value); if (genericResponseModel?.errorMessage != null) { diff --git a/lib/ui/profile/add_dependent_verify_screen.dart b/lib/ui/profile/add_dependent_verify_screen.dart index e493fb99..98608411 100644 --- a/lib/ui/profile/add_dependent_verify_screen.dart +++ b/lib/ui/profile/add_dependent_verify_screen.dart @@ -866,21 +866,21 @@ class _AddDependentVerifyScreenState extends State { } } // 14. Ticket / Education / Medical Insurance Eligibility - else if (prompt.contains('ticket') || colName.contains('TICKET') || - prompt.contains('education') || colName.contains('EDU') || - prompt.contains('insurance') || colName.contains('MED')) { - if (eServices != null && eServices.isNotEmpty) { - for (var opt in eServices) { - String valCol = (opt.vALUECOLUMNNAME ?? '').toLowerCase(); - String idCol = (opt.iDCOLUMNNAME ?? '').toUpperCase(); - if (idCol == 'Y' || valCol.contains('eligible') || valCol.contains('مؤهل') || valCol.contains('نعم') || valCol.contains('yes')) { - details.sEGMENTVALUEDSP = opt.vALUECOLUMNNAME; - details.vARCHAR2VALUE = opt.iDCOLUMNNAME; - break; - } - } - } - } + // else if (prompt.contains('ticket') || colName.contains('TICKET') || + // prompt.contains('education') || colName.contains('EDU') || + // prompt.contains('insurance') || colName.contains('MED')) { + // if (eServices != null && eServices.isNotEmpty) { + // for (var opt in eServices) { + // String valCol = (opt.vALUECOLUMNNAME ?? '').toLowerCase(); + // String idCol = (opt.iDCOLUMNNAME ?? '').toUpperCase(); + // if (idCol == '' || valCol.contains('eligible') || valCol.contains('مؤهل') || valCol.contains('نعم') || valCol.contains('yes')) { + // details.sEGMENTVALUEDSP = opt.vALUECOLUMNNAME; + // details.vARCHAR2VALUE = opt.iDCOLUMNNAME; + // break; + // } + // } + //} + // } // 15. Relationship Start Date else if (prompt.contains('start') || colName.contains('DATE_START') || colName.contains('START_DATE')) { String startVal = ymdDob.isNotEmpty ? ymdDob : DateFormat('yyyy-MM-dd').format(DateTime.now());