323 lines
10 KiB
Dart
Executable File
323 lines
10 KiB
Dart
Executable File
import 'dart:convert';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:adaptive_navbar/adaptive_navbar.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:nhance_app_pwa/customAppBar/responsive.dart';
|
|
import 'package:nhance_app_pwa/customAppBar/toastHelper.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
import '../config/environment.dart';
|
|
import '../models/platform_helper_mobile.dart'
|
|
if (dart.library.html) '../models/platform_helper_other.dart';
|
|
import 'package:nhance_app_pwa/logger.dart';
|
|
import '../pages/postEnrollment/service/api_service.dart';
|
|
import '../pages/service/SessionManager.dart';
|
|
import '../pages/service/TokenService.dart';
|
|
import '../pages/service/popup_helper.dart';
|
|
|
|
class CustomAppBar extends StatefulWidget implements PreferredSizeWidget {
|
|
@override
|
|
_CustomAppBarState createState() => _CustomAppBarState();
|
|
|
|
@override
|
|
Size get preferredSize => Size.fromHeight(kToolbarHeight);
|
|
}
|
|
|
|
class _CustomAppBarState extends State<CustomAppBar> {
|
|
bool showBackToHR = true;
|
|
bool hideInactiveStatus = true;
|
|
dynamic empStatus;
|
|
// dynamic _postToken;
|
|
bool isTokenAvailable = false;
|
|
late ApiService apiService;
|
|
final session = SessionManager();
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
apiService = ApiService(context);
|
|
getEmpStatus();
|
|
}
|
|
|
|
Future<void> checkEnrollToken() async {
|
|
final pre = await TokenService.getPreToken();
|
|
setState(() {
|
|
isTokenAvailable = pre != null && pre.isNotEmpty;
|
|
});
|
|
}
|
|
|
|
Future<void> getEmpStatus() async {
|
|
final postTokenValue = await TokenService.getPostToken();
|
|
|
|
setState(() {
|
|
// // empStatus: null-safe + empty-safe
|
|
// empStatus = (empStatusValue != null && empStatusValue.isNotEmpty)
|
|
// ? empStatusValue
|
|
// : null;
|
|
|
|
// token availability: null + empty check
|
|
isTokenAvailable =
|
|
postTokenValue != null && postTokenValue.trim().isNotEmpty;
|
|
|
|
logDebug('empStatus: $empStatus');
|
|
logDebug('isTokenAvailable: $isTokenAvailable');
|
|
});
|
|
}
|
|
|
|
Future<String?> checkLoginPin() async {
|
|
logDebug('checkLoginPin');
|
|
try {
|
|
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
final empMobileNo = prefs.getString('empMobileNo');
|
|
final empEmailid = prefs.getString('empEmailid');
|
|
logDebug('empMobileNo $empMobileNo');
|
|
logDebug('empEmailid $empEmailid');
|
|
|
|
var params = {};
|
|
if (empMobileNo != null && empMobileNo.isNotEmpty) {
|
|
params = {'mobile_number': empMobileNo};
|
|
} else if (empEmailid != null && empEmailid.isNotEmpty) {
|
|
params = {'email_id': empEmailid};
|
|
}
|
|
|
|
logDebug('params $params');
|
|
|
|
final response = await http.post(
|
|
Uri.parse(Environment.apiUrlEnrollment + 'checkMpin'),
|
|
body: json.encode(params),
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'APP-SIGNATURE':
|
|
'nhance-2025-signature-T8f6gV9k5fK2wf5V5c4vId3b0J8z6cAm2x8Y',
|
|
},
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
Map<String, dynamic> data = json.decode(response.body);
|
|
|
|
if (data['status'] == 'success') {
|
|
if (data['data'] != null) {
|
|
prefs.setString('mpinText', data['data']);
|
|
}
|
|
if (data['Mpin'] != null) {
|
|
prefs.setString('mpin', data['Mpin']);
|
|
}
|
|
if (data['is_biometric_enabled'] != null) {
|
|
prefs.setString(
|
|
'is_biometric_enabled', data['is_biometric_enabled']);
|
|
}
|
|
if (data['is_mpin_skipped'] != null) {
|
|
prefs.setString('is_mpin_skipped', data['is_mpin_skipped']);
|
|
return data['is_mpin_skipped'].toString();
|
|
}
|
|
}
|
|
} else {
|
|
logDebug('❌ API failed: ${response.statusCode}');
|
|
return null;
|
|
}
|
|
} catch (e) {
|
|
logDebug('❌ Error: $e');
|
|
return null;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
Future<void> logout(BuildContext context) async {
|
|
// final prefs = await SharedPreferences.getInstance();
|
|
// final String? token = prefs.getString('token');
|
|
//
|
|
// if (_postToken != null && _postToken.isNotEmpty) {
|
|
// await prefs.clear();
|
|
// Navigator.pushNamed(context, 'login');
|
|
// }
|
|
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
String? mobileNumber = prefs.getString('empMobileNo');
|
|
String? mailID = prefs.getString('empEmailid');
|
|
final isMpinSkipped = await checkLoginPin();
|
|
logDebug('isMpinSkipped $isMpinSkipped');
|
|
// bool? biometricStatus = prefs.getBool('biometricStatus') ?? false;
|
|
// int? skipStatus = prefs.getInt('skipStatus') ?? 1;
|
|
|
|
// Clear all keys
|
|
await SessionManager().clear();
|
|
// await prefs.clear();
|
|
logDebug('Local Storage Clear');
|
|
|
|
// html.window.localStorage.clear();
|
|
// Re-set the mobile_number key
|
|
if (isMobilePlatform()) {
|
|
if (mobileNumber != null) {
|
|
await prefs.setString('empMobileNo', mobileNumber);
|
|
logDebug('empMobileNo $mobileNumber');
|
|
// await prefs.setBool('biometricStatus', biometricStatus!);
|
|
// await prefs.setInt('skipStatus', skipStatus!);
|
|
}
|
|
if (mailID != null) {
|
|
await prefs.setString('empEmailid', mailID);
|
|
logDebug('empEmailid $mailID');
|
|
// await prefs.setBool('biometricStatus', biometricStatus!);
|
|
// await prefs.setInt('skipStatus', skipStatus!);
|
|
}
|
|
if (isMpinSkipped != null) {
|
|
await prefs.setString('is_mpin_skipped', isMpinSkipped!);
|
|
logDebug('isMpinSkipped $isMpinSkipped');
|
|
}
|
|
if (isMpinSkipped != null && isMpinSkipped == '0') {
|
|
logDebug('pinPage');
|
|
// return;
|
|
context.go('/pinPage');
|
|
} else {
|
|
logDebug('login');
|
|
context.go('/login');
|
|
}
|
|
} else {
|
|
await prefs.clear();
|
|
ToastHelper.showSuccessToast(context, 'logout');
|
|
// Navigate to login page
|
|
context.go('/login');
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final sw = MediaQuery.of(context).size.width;
|
|
|
|
return Scaffold(
|
|
backgroundColor: Color(0xFFFFFCE5), // Set background color for AppBar
|
|
appBar: PreferredSize(
|
|
preferredSize: widget.preferredSize,
|
|
child: SafeArea(
|
|
child: Container(
|
|
padding: EdgeInsets.symmetric(
|
|
horizontal: MediaQuery.of(context).size.width *
|
|
(Responsive.isDesktop(context) ? 0.03 : 0.02),
|
|
),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
// Logo Column
|
|
Row(
|
|
mainAxisAlignment: Responsive.isDesktop(context)
|
|
? MainAxisAlignment.spaceEvenly
|
|
: MainAxisAlignment.start,
|
|
children: [
|
|
Container(
|
|
margin: EdgeInsets.only(top: 10, bottom: 10),
|
|
width: 150,
|
|
height: 150,
|
|
child: Image.asset(
|
|
'assets/nhance_client_logo.png',
|
|
fit: BoxFit.contain,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
// AdaptiveNavBar Column
|
|
if (!isTokenAvailable)
|
|
Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
NavBarItem(
|
|
text: "Logout",
|
|
onTap: () async {
|
|
logout(context);
|
|
},
|
|
),
|
|
],
|
|
),
|
|
// if (empStatus == 'enrolled')
|
|
if (isTokenAvailable && Responsive.isDesktop(context))
|
|
Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
NavBarItem(
|
|
text: "Home",
|
|
onTap: () {
|
|
context.push('/home');
|
|
},
|
|
),
|
|
NavBarItem(
|
|
text: "Claims",
|
|
onTap: () {
|
|
context.push('/claims');
|
|
},
|
|
),
|
|
NavBarItem(
|
|
text: "FAQs",
|
|
onTap: () {
|
|
context.push('/faqs');
|
|
},
|
|
),
|
|
NavBarItem(
|
|
text: "Profile",
|
|
onTap: () {
|
|
context.push('/profile');
|
|
},
|
|
),
|
|
NavBarItem(
|
|
text: "Help",
|
|
onTap: () {
|
|
context.push('/help');
|
|
},
|
|
),
|
|
|
|
// NavBarItem(
|
|
// text: "Wellness",
|
|
// onTap: () => handleMenuTap(() {
|
|
// PopupHelper.showRedirectPopup(
|
|
// context: context,
|
|
// apiService: apiService,
|
|
// empPrimaryId: session.empPrimaryId,
|
|
// );
|
|
// }),
|
|
// ),
|
|
NavBarItem(
|
|
text: "Logout",
|
|
onTap: () {
|
|
logout(context);
|
|
},
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class NavBarItem extends StatelessWidget {
|
|
final String text;
|
|
final VoidCallback? onTap;
|
|
|
|
const NavBarItem({
|
|
Key? key,
|
|
required this.text,
|
|
this.onTap,
|
|
}) : super(key: key);
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return InkWell(
|
|
borderRadius: BorderRadius.circular(6),
|
|
onTap: onTap,
|
|
hoverColor: const Color(0xFFF4F3E7), // subtle hover background
|
|
child: Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
|
|
child: Text(
|
|
text,
|
|
style: const TextStyle(
|
|
fontSize: 14,
|
|
fontWeight: FontWeight.w500,
|
|
color: Colors.black87,
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|