mobileapplicationPassvault/lib/api_provider/api_provider_class.dart

417 lines
12 KiB
Dart

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<Addofflinecredential> hivestoredbox =
Hive.box<Addofflinecredential>('offlinedata');
final Box<Storedcredential> hivestoredboxstoreshare =
Hive.box<Storedcredential>('credentialListBox');
List<Message> _messages = [];
List<User> _viewmessages = [];
List<int> viewcheckedid = [];
final List<Message> _comparedata = [];
bool isLoading = true;
List<Message> get messages => _messages;
List<User> get viewmessages => _viewmessages;
List<Message> get comparedata => _comparedata;
set messages(List<Message> messages) {
_messages = messages;
notifyListeners();
}
void selectAllMessages(bool isSelected) {
for (var message in _messages) {
if (message.isEnabled == false) {
message.isChecked = isSelected;
}
}
notifyListeners();
}
List<Message> getSelectedMessages() {
return _messages.where((message) => message.isChecked).toList();
}
void updateMessageCheckedStatus(int index, bool isChecked) {
_messages[index].isChecked = isChecked;
notifyListeners();
}
Future<void> fetchMessages() async {
String? keyToken = await CustomSharedPreferences.getCustomValue();
List<Addofflinecredential> 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<String, dynamic>;
print("Api_Data : ${response.data}");
final messageList = List<Map<String, dynamic>>.from(jsonData['data']);
print("messageList_is : $messageList");
final List<Addofflinecredential> 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<Addofflinecredential> 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<Addofflinecredential> hiveData = hivestoredbox.values.toList();
List<Addofflinecredential> 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<void> 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<String, dynamic>;
final messageList1 =
List<Map<String, dynamic>>.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<int> 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<int> getofflineselecteduser(Storedcredential data) {
hivestoredboxstoreshare.put(data.id, data);
final checkedUserIds = <int>[];
for (final data in hivestoredbox.values) {
if (data.isChecked) {
print("user_id_is: ${data.usrid}");
checkedUserIds.add(data.usrid);
}
}
return checkedUserIds;
}
prefiilcheckbox(List<int> 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<int> dataForIds = [];
final usernameIds = <Addofflinecredential>[];
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<int> 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<int> 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<int> 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<int> 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<Addofflinecredential>()
.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<int> 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<Addofflinecredential> 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) {}
}