import 'dart:async'; import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; import 'package:flutter_passvault/Network_connection/internet_access_checker.dart'; import 'package:flutter_passvault/Service/share_credentials.dart'; import 'package:flutter_passvault/constant/constantfile.dart'; import 'package:flutter_passvault/hive_storage/hive_repositary.dart'; import 'package:flutter_passvault/modelclasses/jsomdata.dart'; import 'package:flutter_passvault/modelclasses/my_credential_model.dart'; import 'package:flutter_passvault/hive_storage/store_credential_model.dart'; import 'package:flutter_passvault/view_pages/shared_preferance.dart'; import 'package:hive_flutter/adapters.dart'; class MyCredentialProvider extends ChangeNotifier { final _service = CredentialService(); bool isLoading = true; List mycredentialdata = []; List get todos => mycredentialdata; final Dio dio = Dio(); Future fetchMyCredential() async { //new final Box hivebox = Hive.box('credentialListBox'); var allRecords = hivebox.values.toList(); // var syncedRecords = // allRecords.where((record) => record.issync == true).toList(); print("Stored__syncedRecords : $allRecords"); if (allRecords.isEmpty) { NetworkConnectivity networkConnectivity = NetworkConnectivity(); bool isOnline = await networkConnectivity.isInternetAvailable(); print("providerrrr: $isOnline"); if (isOnline) { final response = await _service.getAll(); mycredentialdata = response; isLoading = false; notifyListeners(); } else { final dataRepository = HiveDataRepository(hivebox); // Initialize with your Hive box List storedData = dataRepository.getAllDataFromHive(); mycredentialdata = storedData; notifyListeners(); print("storedDatastoredData_issssss: $storedData"); } } else { String? value = await CustomSharedPreferences.getCustomValue(); print("Elseee_AuthMy_credential: $value"); dio.options.headers["Authorization"] = 'Bearer $value'; try { final response = await dio.get("${ApiConstants.newKonectarurl}/creds"); if (response.statusCode == 200) { final jsonData = response.data as Map; final messageList = List>.from(jsonData['data']); final fetchedMessages = messageList.map((json) => CredentialList.fromJson(json)).toList(); print("elseeefetchedMyCredential_is : $fetchedMessages"); print(fetchedMessages); ///////////////////////////////////////////////////////////////////////////////// /// if (fetchedMessages.isEmpty) { print("fetchedMessages_isEmptyyyy : $fetchedMessages"); List hiveData = hivebox.values.toList(); print("existingKeysCredsss_hiveData data_is : $hiveData"); List dataToDelete = hiveData .where((hiveItem) => !messageList.any((apiItem) => apiItem['id'] == hiveItem.id)) .toList(); for (var item in dataToDelete) { print("Delting_Credsdata_isss ${item.key}"); print("Delting_Crdsdata_isss1 ${item.id} ${item.username}"); //await hivebox.delete(idsNotInApi[i]); await hivebox.delete(item.key); } } //////////////////////////////////////////////////////////////// for (final item in fetchedMessages) { if (!fetchedMessages.contains(item)) { print("Del_iddd_is: ${item.id}"); } else { if (item.createdBy != item.pivot!.credId) { print("nae__is : ${item.name}"); final data = Storedcredential( id: item.id, name: item.name, username: item.username, password: item.password, detail: item.detail, createdBy: item.createdBy, modifiedBy: item.modifiedBy, createdAt: item.createdAt, updatedAt: item.updatedAt, isOfflinecreated: false, isOfflineupdate: false, isOfflinedelete: false, isOfflineshare: false, issync: true, uid: item.id, sharedUserIds: [], DeletedUserIds: []); print("data.uid_is111: ${data.uid}"); print("item.uid_is111: ${item.id}"); print("item.uid_is111: ${item.name}"); await hivebox.put(item.id, data); if (hivebox.values.every((data) => data.uid != item.id)) { print("I_am_ifffff"); print("data.uid_isupppp: ${data.uid}"); print("item.uid_is_upppp: ${item.id}"); await hivebox.put(item.id, data); } else if (hivebox.values.every((data) => data.name != item.name || data.username != item.username || data.password != item.password || data.detail != item.detail)) { print("I_am_ifffff_elseeee"); print("data.uid_isupppp: ${data.uid}"); print("item.uid_is_upppp: ${item.id}"); await hivebox.put(item.id, data); } else { print("I_am__elseeee"); final hiveData = hivebox.values.toList(); final hiveIds = hiveData.map((hiveItem) => hiveItem.id).toList(); final apiIds = messageList.map((apiItem) => apiItem['id']).toList(); print('IDs in hiveIds: $hiveIds'); print('IDs in apiIds: $apiIds'); // 5. Identify IDs not in the API response final idsNotInApi = hiveIds.where((id) => !apiIds.contains(id)).toList(); // 6. Process or display the IDs not present in the API response print('IDs in Hive but not in API: $idsNotInApi'); print('idsNotInApi API: ${idsNotInApi.length}'); for (int i = 0; i < idsNotInApi.length; i++) { print("hive_forrrr: $i"); print("hive_forrrr: ${idsNotInApi[i]}"); await hivebox.delete(idsNotInApi[i]); } notifyListeners(); } } } } } } catch (error) { print('Failed : $error'); } } } void addItem(JsonData item) { mycredentialdata.insert(0, item); fetchMyCredential(); notifyListeners(); } void delItem(int newItem) { mycredentialdata.removeAt(newItem); fetchMyCredential(); notifyListeners(); } void editItem(CredentialList item) { mycredentialdata[item.id] = item; fetchMyCredential(); notifyListeners(); } }