import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; import 'package:flutter_passvault/constant/constantfile.dart'; import 'package:flutter_passvault/hive_storage/add_creds_offline.dart'; import 'package:flutter_passvault/hive_storage/store_credential_model.dart'; import 'package:flutter_passvault/modelclasses/usr_list_model.dart'; import 'package:flutter_passvault/modelclasses/view_credential_model.dart'; import 'package:flutter_passvault/view_pages/shared_preferance.dart'; import 'package:hive/hive.dart'; class APIProvider with ChangeNotifier { final Dio dio = Dio(); final Box hivestoredbox = Hive.box('offlinedata'); final Box hivestoredboxstoreshare = Hive.box('credentialListBox'); List _messages = []; List _viewmessages = []; List viewcheckedid = []; final List _comparedata = []; bool isLoading = true; List get messages => _messages; List get viewmessages => _viewmessages; List get comparedata => _comparedata; set messages(List messages) { _messages = messages; notifyListeners(); } void selectAllMessages(bool isSelected) { for (var message in _messages) { if (message.isEnabled == false) { message.isChecked = isSelected; } } notifyListeners(); } List getSelectedMessages() { return _messages.where((message) => message.isChecked).toList(); } void updateMessageCheckedStatus(int index, bool isChecked) { _messages[index].isChecked = isChecked; notifyListeners(); } Future fetchMessages() async { String? keyToken = await CustomSharedPreferences.getCustomValue(); List newdata = []; print("userList: $keyToken"); dio.options.headers["Authorization"] = 'Bearer $keyToken'; try { final response = await dio.post("${ApiConstants.newKonectarurl}/users"); final jsonData = response.data as Map; print("Api_Data : ${response.data}"); final messageList = List>.from(jsonData['data']); print("messageList_is : $messageList"); final List data1 = messageList .map((json) => Addofflinecredential(json['name'], json['id'], false)) .toList(); print("Storing_hove_data: $messageList"); var existingData = hivestoredbox.values.toList(); print("existingKeys data_is : $existingData"); // Filter out the new data that doesn't already exist in Hive var newDataToAdd = messageList.where((newUserData) { var isNew = !existingData.any( (existingUserData) => existingUserData.usrid == newUserData['id']); print("isNew_is : $isNew"); return isNew; }).toList(); for (var data in newDataToAdd) { print("new_hive data_is : $data"); print("new_hive data_is1 : ${data['key']}"); final List data11 = newDataToAdd .map( (json) => Addofflinecredential(json['name'], json['id'], false)) .toList(); print("new_hive data_is_data1 : $data11"); newdata = data11; } print("new_inserted_data_is: $newdata"); await hivestoredbox.addAll(newdata); if (hivestoredbox.isEmpty) { await hivestoredbox.addAll(data1); } else { List hiveData = hivestoredbox.values.toList(); List dataToDelete = hiveData .where((hiveItem) => !messageList.any((apiItem) => apiItem['id'] == hiveItem.usrid)) .toList(); for (var item in dataToDelete) { print("Delting_data_isss ${item.key}"); print("Delting_data_isss1 ${item.usrid} ${item.username}"); hivestoredbox.delete(item.key); } } notifyListeners(); } catch (error) { print('Error: $error'); } notifyListeners(); } Future getcredential1() async { print("getting _user_info_credential"); var allRecords = hivestoredboxstoreshare.values.toList(); print("hive_Stored_data_forshare : $allRecords"); var unsyncedsharesRecords = allRecords.where((record) => record.issync == true).toList(); print("Stored_unsyncedsharesRecords : $unsyncedsharesRecords"); for (var record in unsyncedsharesRecords) { print("unsyncedShareRecords: ${record.id}"); print("unsyncedShareRecords: ${record.name}"); print("unsyncedShareRecords: ${record.uid}"); String? token = await CustomSharedPreferences.getCustomValue(); dio.options.headers['Authorization'] = 'Bearer $token'; try { dio.options.receiveTimeout = const Duration(seconds: 5); // 10 seconds timeout final response = await dio.get( "${ApiConstants.newKonectarurl}/creds/${record.uid}", ); if (response.statusCode == 200) { print('I_am_calling from_getcredential1Neww added successful'); print(response.data); print(response.data['message']); print(response.data['data']); print("response_dataaa_users : ${response.data['data']['users']}"); final jsonData = response.data as Map; final messageList1 = List>.from(jsonData['data']['users']); print( "messageList_isNew I_am_calling from_getcredential1 : $messageList1"); final fetchedMessages1 = messageList1.map((json) => User.fromJson(json)).toList(); print("fetchedMessages1_isss: $fetchedMessages1"); _viewmessages = fetchedMessages1; List userIds = _viewmessages.map((user) => user.id).toList(); print("userIds_isss: $userIds"); viewcheckedid = userIds; print("View_resposeeeeeeeeeeeeee_apiprobider: $_viewmessages"); print("viewcheckedid: $viewcheckedid" "${record.uid}"); storeuserid(viewcheckedid, record.uid); } else { print('Failed : ${response.statusCode}'); } } catch (error) { print('Failed111: $error'); } } } void getCommonItems() { print("Common_listttL $messages"); print("Common_listtt_viewdata $viewmessages"); for (final item1 in _messages) { item1.isChecked = viewmessages.any((item2) => item1.id == item2.id); item1.isEnabled = !item1.isChecked; } notifyListeners(); } void updateMessageCheckedStatus1(Addofflinecredential data) { data.isChecked = !data.isChecked; // Toggle the checked state notifyListeners(); } List getofflineselecteduser(Storedcredential data) { hivestoredboxstoreshare.put(data.id, data); final checkedUserIds = []; for (final data in hivestoredbox.values) { if (data.isChecked) { print("user_id_is: ${data.usrid}"); checkedUserIds.add(data.usrid); } } return checkedUserIds; } prefiilcheckbox(List selectedid) async { String? getstoreduserid = await CustomSharedPreferences.getuserid(); for (int i = 0; i < hivestoredbox.length; i++) { final data = hivestoredbox.getAt(i); data!.isChecked = false; hivestoredbox.putAt(i, data); } final List dataForIds = []; final usernameIds = []; print("fecth_selected_id: $selectedid"); var allRecords = hivestoredbox.values.toList(); print("fecth_selected_allRecords_id: $allRecords"); for (int i = 0; i < hivestoredbox.length; i++) { final data = hivestoredbox.getAt(i); print("Pooja_data: ${data!.username}"); print("Pooja_id: ${data.usrid}"); dataForIds.add(data.usrid); } print("fecth_selected_aonly_id: $selectedid"); List commonValues = dataForIds.where((element) => selectedid.contains(element)).toList(); print("commonValues_iss: $commonValues"); // [4, 21] commonValues.remove(int.parse(getstoreduserid!)); print("after removeee: $commonValues"); // [4, 21] for (final id in commonValues) { print("value_in_common_is :$id"); final data = hivestoredbox.values.firstWhere((element) => element.usrid == id); usernameIds.add(data); } print("after_usernameIds: $usernameIds"); // [4, 21] for (var record in usernameIds) { print("unsyncedRecords: ${record.username}"); print("unsyncedRecords: ${record.isChecked}"); print("unsyncedRecords: ${record.usrid}"); record.isChecked = true; hivestoredbox.putAt(record.usrid, record); } notifyListeners(); } void storeuserid(List getofflineselecteduser, int? credentialid) async { print("getofflineselecteduser_isss: $getofflineselecteduser"); print("getofflineselecteduser_isss: $credentialid"); var getall = hivestoredboxstoreshare.values.toList(); print("getall_storeuserid : $getall"); try { Storedcredential storedDatacid = hivestoredboxstoreshare.values .firstWhere((element) => element.uid == credentialid); print( 'Element with ID $credentialid: ${storedDatacid.name}, ${storedDatacid.sharedUserIds}'); storedDatacid.sharedUserIds = getofflineselecteduser; print("sharedUserIdsonlinee_isss : ${storedDatacid.sharedUserIds}"); print("storedDatacid_api : $storedDatacid,$credentialid"); await hivestoredboxstoreshare.put(credentialid!, storedDatacid); // getAllDataFromHive(); } catch (e) { print('Element with ID $credentialid not found'); } Storedcredential storedDatacid11 = hivestoredboxstoreshare.values .firstWhere((element) => element.uid == credentialid); print('485 with ID ${storedDatacid11.sharedUserIds}'); } prefiilcheckbox11(List selectedid) async { print("prefiilcheckbox11_selectedddd_id:isss: $selectedid"); for (int i = 0; i < hivestoredbox.length; i++) { final data = hivestoredbox.getAt(i); print( "data_is_delectinggg: ${data!.usrid} ${data.username} ${data.isChecked}"); if (data.isChecked) { data.isChecked = false; hivestoredbox.putAt(i, data); } } final List storingSelecteid = []; for (final id in selectedid) { print("prefilled_id: $id"); var allRecords = hivestoredbox.values.toList(); print("allRecordsStored_added_unsyncedRecords : $allRecords"); var unsyncedRecords = allRecords.where((record) => record.usrid == id).toList(); print("Shareddddcheckbox_unsyncedRecords : $unsyncedRecords"); for (var record in unsyncedRecords) { print( "record_description : ${record.username}, ${record.usrid}, ${record.isChecked}"); storingSelecteid.add(record.usrid); } } print("Storedd_test_id_is: $storingSelecteid"); for (int id in storingSelecteid) { final index = hivestoredbox.values .cast() .toList() .indexWhere((element) => element.usrid == id); if (index != -1) { // Update the checkbox value for the specified ID final yourData = hivestoredbox.getAt(index)!; yourData.isChecked = true; hivestoredbox.putAt(index, yourData); } } } List getselecteduserid11(int? credentialid) { print("cred_iddgetselecteduserid11: $credentialid"); var getall111 = hivestoredboxstoreshare.values.toList(); print("getall_storeuserid : $getall111"); for (var id in getall111) { print("cjeck_idddd: ${id.name},${id.id},${id.sharedUserIds}"); } Storedcredential getstoredData = hivestoredboxstoreshare.values .toList() .firstWhere((element) => element.id == credentialid!); print('485 with ID11 ${getstoredData.sharedUserIds}'); ///Storedcredential? getstoredData = hivestoredboxstoreshare.get(credentialid); if (getstoredData != null) { print("get_checked_list11: ${getstoredData.sharedUserIds}"); print("get_checked_list11: ${getstoredData.name}"); print("get_checked_list11: ${getstoredData.uid}"); return getstoredData.sharedUserIds; } return []; } List getAlluserlist() { print("Stored_ALL_valuesssss : ${hivestoredbox.values.toList()}"); print( "Stored_ALL_valuesssss_leangthhh : ${hivestoredbox.values.toList().length}"); return hivestoredbox.values.toList(); } void searchInHive(String value) {} }