mobileapplicationPassvault/lib/view_pages/my_drawer.dart

455 lines
14 KiB
Dart

import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:flutter/material.dart';
import 'package:flutter_passvault/Network_connection/internet_access_checker.dart';
import 'package:flutter_passvault/api_provider/add_credentail_provider.dart';
import 'package:flutter_passvault/api_provider/api_provider_class.dart';
import 'package:flutter_passvault/api_provider/delete_provider.dart';
import 'package:flutter_passvault/api_provider/edit_credential_provider.dart';
import 'package:flutter_passvault/api_provider/my_credential_provider.dart';
import 'package:flutter_passvault/api_provider/share_credential_provider.dart';
import 'package:flutter_passvault/constant/constantfile.dart';
import 'package:flutter_passvault/custom_widgets/reuse_textview.dart';
import 'package:flutter_passvault/hive_storage/hive_repositary.dart';
import 'package:flutter_passvault/seesionManager/session_manager.dart';
import 'package:flutter_passvault/view_pages/alertbox.dart';
import 'package:flutter_passvault/view_pages/home_screen_page.dart';
import 'package:flutter_passvault/view_pages/shared_preferance.dart';
import 'package:intl/intl.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:provider/provider.dart';
import 'package:webview_cookie_manager/webview_cookie_manager.dart';
// import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
// import 'dart:html' as html;
import 'package:universal_html/html.dart' as html;
import '../main.dart';
// ignore: use_key_in_widget_constructors
class MyDrawer extends StatefulWidget {
@override
State<MyDrawer> createState() => _MyDrawerState();
}
class _MyDrawerState extends State<MyDrawer> {
String useremailname = "";
String username = "";
String synctime = "";
String? versionno;
// DateTime? now = DateTime.now();
DateTime? now;
String? formattedDateTime;
@override
void initState() {
super.initState();
getAppVersion();
getuserdetail();
}
@override
Widget build(BuildContext context) {
// double widthDrawer = MediaQuery.of(context).size.width * 0.70;
double widthDrawer = ApiConstants().isTablet
? MediaQuery.of(context).size.width * 0.30
: MediaQuery.of(context).size.width * 0.75;
String fontFamily = ApiConstants.fontFamily;
return Container(
width: widthDrawer,
child: Drawer(
surfaceTintColor: Colors.white,
backgroundColor: Colors.white,
// child: Column(
// children: [
child: ListView(
padding: EdgeInsets.zero,
children: [
UserAccountsDrawerHeader(
decoration: const BoxDecoration(
color: Colors
.transparent, // Change this to the desired background color
),
currentAccountPictureSize:
Size(widthDrawer, 80), //set custom height
currentAccountPicture: Center(
child: GestureDetector(
onTap: () {},
child: CircleAvatar(
radius: 30.0,
backgroundColor: const Color.fromARGB(200, 42, 71, 165),
child: Center(
child: Text(
username.isNotEmpty
? username[0].toUpperCase()
: "", // Get the first character or an empty string if the string is empty
style: const TextStyle(
fontSize: 30.0,
color: Colors.white,
fontWeight: FontWeight.bold),
),
), //Text
),
),
),
accountName: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(username,
style:
const TextStyle(fontSize: 18.0, color: Colors.black)),
],
),
accountEmail: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(useremailname,
style:
const TextStyle(fontSize: 14.0, color: Colors.black)),
],
),
),
// Add other ListTile items or content here
Container(
color: Colors.white,
child: ListTile(
title: ReusableTextView(
text: 'Logout',
textStyle: TextStyle(
fontFamily: fontFamily,
color: Colors.black,
fontSize: 16,
),
),
onTap: () async {
// logout1();
showAlertBox("Are you sure to logout?");
},
),
),
// const SizedBox(
// height: 4.0,
// ),
const Divider(color: Colors.black38, thickness: 1.0),
Container(
color: Colors.white,
child: ListTile(
title: ReusableTextView(
text: 'Sync now',
textStyle: TextStyle(
fontFamily: fontFamily,
color: Colors.black,
fontSize: 16,
),
),
subtitle: ReusableTextView(
text: synctime == "" ? " " : "Last Synced: " + synctime,
textStyle: TextStyle(
fontFamily: fontFamily,
color: Colors.black,
fontSize: 14,
),
),
onTap: () async {
print("Timerr_is: $timer");
if (timer != null) timer?.cancel();
NetworkConnectivity networkConnectivity =
NetworkConnectivity();
bool isOnline =
await networkConnectivity.isInternetAvailable();
print("providerrrr: $isOnline");
if (isOnline) {
// ignore: use_build_context_synchronously
showSyncDialog(context);
simulateSync();
} else {
showAlertBox1("Internet is required");
}
},
),
),
const Divider(color: Colors.black38, thickness: 1.0),
// Container(
// padding: const EdgeInsets.all(0),
// height: 20,
// child: ReusableTextView(
// text: "Version Number : $versionno",
// textStyle: const TextStyle(
// fontWeight: FontWeight.bold, color: Colors.grey),
// )),
],
),
),
);
}
void simulateSync() async {
// Simulate a delay for synchronization (replace with your actual synchronization code)
setState(() {
final now = DateTime.now();
final truncatedDateTime = DateTime(
now.year,
now.month,
now.day,
now.hour,
now.minute,
now.second,
);
final formatter = DateFormat('dd-MM-yyyy HH:mm');
formattedDateTime = formatter.format(truncatedDateTime);
});
final result1 = Provider.of<AddCredentialProvider>(context, listen: false);
final result2 = Provider.of<DelCredentialProvider>(context, listen: false);
final result3 = Provider.of<EditCredentialProvider>(context, listen: false);
final result4 =
Provider.of<ShareCredentialProvider>(context, listen: false);
final result5 = Provider.of<MyCredentialProvider>(context, listen: false);
final result6 = Provider.of<APIProvider>(context, listen: false);
final result7 = Provider.of<HiveDataRepository>(context, listen: false);
//try {
await result1.postcredential();
print("result_addedProvider : ${result1.success}");
await result3.posteditcredential();
print("result_editProvider11 : $result3");
await result2.postdelcredential();
print("result_DelProvider11 : ${result1.success}");
await result4.postsharecredential();
print("result_shareProvider11 : $result4");
await result5.fetchMyCredential();
print("result_FeccthProvider11 : $result5");
await result6.fetchMessages();
print("result_getcredential1 : $result6");
await result6.getcredential1();
print("result_getcredential1 : $result6");
// await result5.fetchMyCredential();
print("result_shareProvider11 : $result5");
result7.getAllDataFromHive();
await CustomSharedPreferences.setsynctime(formattedDateTime!);
Future.delayed(const Duration(seconds: 3), () {
// Close the "Please wait" dialog
Navigator.of(context).pop();
// Show "Synced successfully" dialog
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('Synced Successfully'),
content: const Text('Your data has been synced successfully.'),
actions: [
TextButton(
onPressed: () async {
Navigator.of(context).pop();
timerrfun();
await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const Screen(),
),
);
},
child: const Text('OK'),
),
],
);
},
barrierDismissible: false);
});
}
void showSyncDialog(BuildContext context) {
showDialog(
context: context,
builder: (context) {
return const AlertDialog(
title: Text('Syncing'),
content: Text('Please wait...'),
);
},
barrierDismissible: false);
}
showAlertBox(String msg) {
showDialog(
context: context,
builder: (ctx) => AlertDialog(
title: const Text("Alert!"),
content: Text(
msg,
style: const TextStyle(fontSize: 18, fontWeight: FontWeight.w400),
),
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.of(ctx).pop();
Provider.of<HiveDataRepository>(context, listen: false)
.getAllDataFromHive();
},
// ignore: avoid_unnecessary_containers
child: Container(
child: const Text(
"No",
style: TextStyle(color: Colors.cyan, fontSize: 17),
),
),
),
TextButton(
onPressed: () {
Navigator.of(ctx).pop();
logout1();
},
// ignore: avoid_unnecessary_containers
child: Container(
child: const Text(
"Yes",
style: TextStyle(color: Colors.cyan, fontSize: 17),
),
),
),
],
),
);
}
Future<void> getuserdetail() async {
String useremailname1 = await CustomSharedPreferences.getname() ?? "";
String username1 = await CustomSharedPreferences.getusername() ?? "";
String datetime = await CustomSharedPreferences.getsynctime() ?? "";
print("useremailname1 : $useremailname1");
print("username1 : $username1");
setState(() {
username = username1;
useremailname = useremailname1;
synctime = datetime;
});
}
void logout1() async {
await clearTokens();
if (kIsWeb) {
await clearSession();
} else {
await clearCookies();
}
}
clearTokens() async {
await CustomSharedPreferences.clearAllValues();
await SessionManager().clearSession();
}
clearCookies() async {
// const keycloakBaseUrl = 'https://sso.konectar.io/auth';
// const realm = 'konectar';
var ssourl = ApiConstants.ssoUrl;
var logoutUrl = '$ssourl/protocol/openid-connect/logout';
// const logoutUrl =
// '$keycloakBaseUrl/realms/$realm/protocol/openid-connect/logout';
// Make a GET request to the Keycloak logout endpoint
final response = await Dio().get(logoutUrl);
print("response_logout: ${response.statusCode}");
if (response.statusCode == 200) {
final cookieManager = WebviewCookieManager();
await cookieManager.clearCookies();
// ignore: use_build_context_synchronously
Navigator.of(context, rootNavigator: true).pushNamedAndRemoveUntil(
'/ssologin',
(Route<dynamic> route) =>
false, // This will remove all the routes from the stack
);
} else {
// Handle logout error
print('Logout error: ${response.statusCode}');
}
}
getAppVersion() async {
PackageInfo packageInfo = await PackageInfo.fromPlatform();
String appName = packageInfo.appName;
String packageName = packageInfo.packageName;
String version = packageInfo.version;
String buildNumber = packageInfo.buildNumber;
print("App_version_appName: $appName");
print("App_version_packageName: $packageName");
print("App_version_version: $version");
print("App_version_buildNumber: $buildNumber");
setState(() {
versionno = version;
});
}
showAlertBox1(String msg) {
showDialog(
context: context,
builder: (BuildContext context) => CustomAlertBox(message: msg));
}
@pragma('vm:entry-point')
Future<void> clearSession() async {
// const keycloakBaseUrl = 'https://sso.konectar.io/auth';
// const realm = 'konectar';
var ssourl = ApiConstants.ssoUrl;
var logoutUrl =
'$ssourl/protocol/openid-connect/logout?redirect_uri=https://passvault.aissel.com/';
// const logoutUrl =
// '$keycloakBaseUrl/realms/$realm/protocol/openid-connect/logout?redirect_uri=http://localhost:8080/';
html.window.location.href = logoutUrl;
}
@override
void dispose() {
// TODO: implement dispose
super.dispose();
}
}