import 'package:flutter/material.dart'; import 'package:flutter_passvault/api_provider/delete_provider.dart'; import 'package:flutter_passvault/api_provider/edit_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/modelclasses/jsomdata.dart'; import 'package:flutter_passvault/hive_storage/store_credential_model.dart'; import 'package:flutter_passvault/view_pages/alertbox.dart'; import 'package:flutter_passvault/custom_widgets/new_reuse_textfield.dart'; import 'package:flutter_passvault/custom_widgets/reuse_button.dart'; import 'package:flutter_passvault/view_pages/home_screen_page.dart'; import 'package:flutter_passvault/view_pages/shared_preferance.dart'; import 'package:hive/hive.dart'; import 'package:provider/provider.dart'; import '../main.dart'; class ViewCdrentialontap extends StatelessWidget { const ViewCdrentialontap( {super.key, required this.jsondata, required this.index}); final JsonData jsondata; final int index; @override Widget build(BuildContext context) { return Scaffold( body: ViewCdrentialontap1(jsondata: jsondata, index: index), ); } } class ViewCdrentialontap1 extends StatefulWidget { const ViewCdrentialontap1( {super.key, required this.jsondata, required this.index}); final JsonData jsondata; final int index; @override State createState() => _ViewCdrentialontap1State(); } class _ViewCdrentialontap1State extends State { Color color1 = ApiConstants.backgroundcolor; var isLoading = false; final appNameController = TextEditingController(); final userNameController = TextEditingController(); final pwdController = TextEditingController(); final commentController = TextEditingController(); final Box hivebox = Hive.box('credentialListBox'); String? sendappnameupdate; String? appname; String? storeduserid; @override void initState() { super.initState(); getuserid(); appNameController.text = widget.jsondata.name; userNameController.text = widget.jsondata.username; pwdController.text = widget.jsondata.password; commentController.text = widget.jsondata.detail; print("onclick_new_data: ${widget.jsondata.id}"); } Future getuserid() async { String? storeuserid = await CustomSharedPreferences.getuserid(); setState(() { storeduserid = storeuserid; print("customKey_value_is: $storeduserid"); }); } @override Widget build(BuildContext context) { return Consumer2( builder: (context, editprovider, delprovider, child) { return Scaffold( appBar: AppBar( backgroundColor: color1, title: const Text( 'Edit Credentials', style: TextStyle(color: Colors.white), ), iconTheme: const IconThemeData(color: Colors.white), ), body: SafeArea( child: Stack( children: [ SingleChildScrollView( child: Column( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ Padding( padding: const EdgeInsets.only( left: 15.0, right: 15.0, top: 50.0, bottom: 40.0), child: Container( decoration: const BoxDecoration( border: Border( bottom: BorderSide( color: Colors.black, width: 1.5, // Set the desired border width ), ), // Set the desired border radius ), child: NewReusableTextField( controller: appNameController, labelText: "Application name", hasBorder: false, ), ), ), Padding( padding: const EdgeInsets.only( left: 15.0, right: 15.0, top: 0.8, bottom: 40.0), child: Container( decoration: const BoxDecoration( border: Border( bottom: BorderSide( color: Colors.black, width: 1.5, // Set the desired border width ), ), // Set the desired border radius ), child: NewReusableTextField( emailkeyboard: true, controller: userNameController, hasBorder: false, labelText: 'Username', ), ), ), Padding( padding: const EdgeInsets.only( left: 15.0, right: 15.0, top: 0.8, bottom: 40.0), child: Container( decoration: const BoxDecoration( border: Border( bottom: BorderSide( color: Colors.black, width: 1.5, // Set the desired border width ), ), // Set the desired border radius ), child: NewReusableTextField( secure: true, controller: pwdController, labelText: 'Password', hasBorder: false, ), ), ), Padding( padding: const EdgeInsets.only( left: 15.0, right: 15.0, top: 0.8, bottom: 40.0), child: Container( decoration: const BoxDecoration( border: Border( bottom: BorderSide( color: Colors.black, width: 1.5, // Set the desired border width ), ), ), child: NewReusableTextField( controller: commentController, hasBorder: false, labelText: 'Description', ), ), ), Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ Padding( padding: const EdgeInsets.only(top: 20.0, bottom: 0.0), child: ReusableButton( text: 'CANCEL', color: Colors.grey, isRounded: true, onPressed: () { Navigator.of(context).pop(); Route route = MaterialPageRoute( builder: (context) => const Screen(), ); Navigator.pushAndRemoveUntil( context, route, (Route route) => false, ); }, ), ), Padding( padding: const EdgeInsets.only(top: 20.0, bottom: 0.0), child: ReusableButton( text: 'UPDATE', color: const Color.fromARGB(255, 39, 121, 42), isRounded: true, onPressed: updatemethod, ), ), Padding( padding: const EdgeInsets.only(top: 20.0, bottom: 0.0), child: ReusableButton( text: 'DELETE', color: Colors.red, isRounded: true, onPressed: () async { showAlertBox( "Are you sure you want to delete the credential?"); }, ), ), ], ), ], ), ), Visibility( visible: isLoading, child: const Center( child: CircularProgressIndicator(), )) ], )), // ), ); }); } 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: [ TextButton( onPressed: () { // deleteRecord(); Navigator.of(context).pop(); }, child: ReusableTextView( text: "No", textStyle: const TextStyle(color: Colors.cyan, fontSize: 17), ), ), TextButton( onPressed: () { if (timer != null) timer?.cancel(); deleteRecord(); }, child: ReusableTextView( text: "Yes", textStyle: const TextStyle(color: Colors.cyan, fontSize: 17), ), ), ], ), barrierDismissible: false, ); } Future deleteRecord() async { delMethod(); } Future updatemethod() async { if (timer != null) timer?.cancel(); final applicationName = appNameController.text; final userName = userNameController.text; final password = pwdController.text; // final id = widget.jsondata.id; if (applicationName.isEmpty || userName.isEmpty || password.isEmpty) { alertDisplay("Please fill the all fields."); } else { updateCredential(); } } Future updateCredential() async { for (var getuid in hivebox.values) { print("get_id: ${getuid.id}"); if (getuid.id == widget.jsondata.id) { int uidForIdOne = getuid.uid; // Adjust to your field name DateTime addedAt = getuid.createdAt; bool isaddedd = getuid.isOfflinecreated; bool isdeleted = getuid.isOfflinedelete; bool isshares = getuid.isOfflineshare; List issharesid = getuid.sharedUserIds; print("uidd_is $uidForIdOne"); print("isaddedd_updatedddd $isaddedd"); print("isaddeddtimeaddedAt: $addedAt"); print("uidd_is_uniqvalueeee $uidForIdOne"); print("uidd_is_issharesuniqvalueeee $isshares"); print("uidd_is_issharesuniissharesid $issharesid"); final data = Storedcredential( id: widget.jsondata.id, name: appNameController.text, username: userNameController.text, password: pwdController.text, detail: commentController.text, createdBy: int.parse(storeduserid!), modifiedBy: int.parse(storeduserid!), createdAt: addedAt, updatedAt: DateTime.now(), isOfflinecreated: isaddedd, isOfflineupdate: true, isOfflinedelete: isdeleted, isOfflineshare: isshares, issync: false, uid: uidForIdOne, sharedUserIds: issharesid, DeletedUserIds: []); print("uppendinggg_dataaa: ${widget.jsondata.id}"); Provider.of(context, listen: false) .updateOfflineData(data); showAlertDialog1(context, "Updated Successfully"); } } } //Aletdatadisplay(String msg) { showAlertDialog1(BuildContext context, String msg) { // set up the button Widget okButton = TextButton( child: const Text("OK"), onPressed: () { Navigator.of(context).pop(); timerrfun(); Route route = MaterialPageRoute( builder: (context) => const Screen(), ); Navigator.pushAndRemoveUntil( context, route, (Route route) => false, ); }, ); // set up the AlertDialog AlertDialog alert = AlertDialog( content: Text(msg), actions: [ okButton, ], ); // show the dialog showDialog( context: context, builder: (BuildContext context) { return alert; }, barrierDismissible: false, ); } alertDisplay(String msg) { showDialog( context: context, builder: (BuildContext context) { return CustomAlertBox(message: msg); }); } void delMethod() { var matchingRecord = hivebox.values.firstWhere((record) => record.id == widget.jsondata.id); int uidForIdOne = matchingRecord.uid; // Adjust to your field name DateTime addedAt = matchingRecord.createdAt; DateTime editedAt = matchingRecord.updatedAt; bool isaddedd = matchingRecord.isOfflinecreated; bool isEdited = matchingRecord.isOfflineupdate; bool isshared = matchingRecord.isOfflineshare; print("uidd_is $uidForIdOne"); print("uidd_is $uidForIdOne"); print("uidd_is $uidForIdOne"); final dataDel = Storedcredential( id: widget.jsondata.id, name: appNameController.text, username: userNameController.text, password: pwdController.text, detail: commentController.text, createdBy: int.parse(storeduserid!), modifiedBy: int.parse(storeduserid!), createdAt: addedAt, updatedAt: editedAt, isOfflinecreated: isaddedd, isOfflineupdate: isEdited, isOfflinedelete: true, isOfflineshare: isshared, issync: false, uid: uidForIdOne, sharedUserIds: [], DeletedUserIds: []); final delcredentials = Provider.of(context, listen: false); delcredentials.delofflinecredential(dataDel); print("i_amdelmethoad"); showDialog( context: context, builder: (BuildContext context) { return AlertDialog( // title: ReusableTextView(text: 'Please wait'), content: ReusableTextView(text: 'Credential Deleted'), actions: [ TextButton( child: ReusableTextView(text: 'OK'), onPressed: () { Navigator.of(context).pop(); // Close the dialog timerrfun(); Route route = MaterialPageRoute( builder: (context) => const Screen(), ); Navigator.pushAndRemoveUntil( context, route, (Route route) => false, ); }, ), ], ); }, barrierDismissible: false, ); } @override void dispose() { super.dispose(); userNameController.dispose(); appNameController.dispose(); pwdController.dispose(); commentController.dispose(); } }