import 'package:flutter/material.dart'; import 'package:flutter_passvault/Network_connection/internet_access_checker.dart'; import 'package:flutter_passvault/api_provider/connectivty_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_button.dart'; import 'package:flutter_passvault/custom_widgets/reuse_textview.dart'; import 'package:flutter_passvault/hive_storage/add_creds_offline.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/modelclasses/usr_list_model.dart'; import 'package:flutter_passvault/view_pages/alertbox.dart'; import 'package:flutter_passvault/view_pages/home_screen_page.dart'; import 'package:hive/hive.dart'; import 'package:provider/provider.dart'; import '../api_provider/api_provider_class.dart'; class ShareDataToUsr extends StatelessWidget { const ShareDataToUsr( {super.key, required this.credentialjsondata, required this.userid}); final JsonData credentialjsondata; final int userid; @override Widget build(BuildContext context) { return Scaffold( body: ShareDataToUsr1( credentialjsondata: credentialjsondata, userid: userid)); } } class ShareDataToUsr1 extends StatefulWidget { const ShareDataToUsr1( {super.key, required this.credentialjsondata, required this.userid}); final JsonData credentialjsondata; final int userid; @override State createState() => _ShareDataToUsr1State(); } class _ShareDataToUsr1State extends State { Color color1 = ApiConstants.backgroundcolor; String fontFamily = ApiConstants.fontFamily; bool isLoading = false; bool isshow = false; int? credentialid, userid; late List selectedMessages; final TextEditingController _searchController = TextEditingController(); @override void initState() { super.initState(); refresh(); credentialid = widget.credentialjsondata.id; userid = widget.userid; offlinelistid(); } @override void didChangeDependencies() { // TODO: implement didChangeDependencies super.didChangeDependencies(); print("didChangeDependencies"); refresh(); } @override Widget build(BuildContext context) { Provider.of(context, listen: false); return GestureDetector( onTap: () { print('Clicked outside'); FocusScope.of(context).unfocus(); }, child: Scaffold( appBar: AppBar( backgroundColor: color1, title: const Text( 'Users', style: TextStyle(color: Colors.white), ), iconTheme: const IconThemeData(color: Colors.white), ), body: Consumer(builder: (context, apiProvider1, child) { print("apiProvider1: ${apiProvider1.hivestoredbox.values.toList()}"); final apiDataList = apiProvider1.hivestoredbox.values.toList(); print("apillist_isss: $apiDataList"); //apiProvider1.hivestoredbox.clear(); print("Afterapillist_isss: $apiDataList"); List storedcredential = apiProvider1.hivestoredbox.values.toList(); List filteredCredentials = storedcredential .where((credential) => credential.username .toLowerCase() .contains(_searchController.text.toLowerCase())) .toList(); if (apiDataList.isEmpty) { shareuserdata(); } return Stack( children: [ Container( padding: const EdgeInsets.only(top: 15, bottom: 20.0), child: Column( children: [ Padding( padding: const EdgeInsets.only( top: 8.0, bottom: 8.0, left: 20.0, right: 20.0), child: Center( child: TextField( controller: _searchController, onChanged: (value) { setState(() {}); }, // ignore: prefer_const_constructors decoration: InputDecoration( contentPadding: const EdgeInsets.symmetric( vertical: 9.0), // Adjust the vertical padding border: const OutlineInputBorder(), labelText: ' Search', prefixIcon: const Icon(Icons.search), )), ), ), Expanded( child: ListView.builder( itemCount: filteredCredentials.length, // ignore: body_might_complete_normally_nullable itemBuilder: (context, index) { final user = filteredCredentials[index]; print("User.id == ${user.usrid}"); print("Useriddd = $userid"); if (user.usrid == userid) { return Container(); } if (user.username.isNotEmpty) { return Card( surfaceTintColor: Colors.white, shape: const RoundedRectangleBorder( borderRadius: BorderRadius.all( Radius.circular(0.0), ), ), elevation: 2, child: ListTile( title: Padding( padding: const EdgeInsets.only( top: 18.0, bottom: 18.0, left: 20.0), child: ReusableTextView( text: user.username, // ignore: prefer_const_constructors textStyle: TextStyle( fontFamily: fontFamily, color: Colors.black, fontSize: 20, ), ), ), trailing: Checkbox( value: user.isChecked, onChanged: (value) { //setState(() { apiProvider1 .updateMessageCheckedStatus1(user); // }); }), ), ); } else { const CircularProgressIndicator(); } //return null; }, ), ), ReusableButton( text: "SHARE CREDENTIAL", color: color1, onPressed: () async { final Box hivebox = Hive.box('credentialListBox'); var matchingRecord = hivebox.values.firstWhere( (record) => record.id == widget.credentialjsondata.id); int uidForIdOne = matchingRecord.uid; // Adjust to your field name // DateTime addedAt = matchingRecord.createdAt; bool isaddedd = matchingRecord.isOfflinecreated; bool isdeleted = matchingRecord.isOfflinedelete; bool isedited = matchingRecord.isOfflineupdate; print("share_uidd_is $uidForIdOne"); print("share_isaddedd $isaddedd"); print("share_isdeleted $isdeleted"); print("share_isedited $isedited"); print("credentialid_isss $credentialid"); final data = Storedcredential( id: credentialid!, name: widget.credentialjsondata.name, username: widget.credentialjsondata.username, password: widget.credentialjsondata.password, detail: widget.credentialjsondata.detail, createdBy: userid!, modifiedBy: userid!, createdAt: DateTime.now(), updatedAt: DateTime.now(), isOfflinecreated: isaddedd, isOfflineupdate: isedited, isOfflinedelete: isdeleted, isOfflineshare: true, issync: false, uid: uidForIdOne, sharedUserIds: [], DeletedUserIds: []); final storedUserid = apiProvider1.getofflineselecteduser(data); print("Selected_offline: $storedUserid"); final adduseridcredentials = Provider.of(context, listen: false); adduseridcredentials.adduserid( storedUserid, credentialid); showAlertBox("Your credential has been shared"); }) ], ), ), Visibility( visible: isLoading, child: const Center( child: CircularProgressIndicator(), )) ], ); } // ], ), ), ); } void shareuserdata() async { final internetProvider = Provider.of(context, listen: false); if (internetProvider.isConnected) { final dataProvider = Provider.of(context, listen: false); print("credential_id: $credentialid"); await dataProvider.fetchMessages(); } else { showAlertBox("No Internet"); } } 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: () { Navigator.of(context).pop(); Route route = MaterialPageRoute(builder: (context) => const Screen()); // Navigator.pushReplacement(context, route); Navigator.pushAndRemoveUntil( context, route, (Route route) => false, ); }, child: const Text( "Ok", style: TextStyle(color: Colors.cyan, fontSize: 17), ), ), ], ), ); } void alertBox(String msg) { showDialog( context: context, builder: (BuildContext context) { return CustomAlertBox(message: msg); }); } void offlinelistid() async { final apiProvider1 = Provider.of(context, listen: false); //selectedid; NetworkConnectivity networkConnectivity = NetworkConnectivity(); bool isOnline = await networkConnectivity.isInternetAvailable(); print("providerrrr: $isOnline"); print("providerrrrcredentialid: $credentialid"); final selectedid = apiProvider1.getselecteduserid11(credentialid); print("checkoffline_listedddd_id: $selectedid"); await apiProvider1.prefiilcheckbox11(selectedid); } void refresh() async { await Future.delayed(const Duration(seconds: 0)); setState(() { fecthDetails(); }); } void fecthDetails() async { Provider.of(context, listen: false).getAlluserlist(); } }