mobileapplicationPassvault/lib/api_provider/share_credential_provider.dart

115 lines
3.3 KiB
Dart
Raw Normal View History

2024-04-12 05:23:32 +00:00
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_passvault/constant/constantfile.dart';
import 'package:flutter_passvault/modelclasses/share_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/hive.dart';
class ShareCredentialProvider extends ChangeNotifier {
final dio = Dio();
ShareCredentialData? _serverResponse;
ShareCredentialData? get message => _serverResponse;
Future<void> postsharecredential() async {
final Box<Storedcredential> hivebox =
Hive.box<Storedcredential>('credentialListBox');
var allRecords = hivebox.values.toList();
var unsyncedRecords =
allRecords.where((record) => record.isOfflineshare == true).toList();
print("Stored_shared_unsyncedRecords : $unsyncedRecords");
for (var record in unsyncedRecords) {
print("shareddd_data_value: ${record.name}");
print("shareddd_data_value: ${record.sharedUserIds}");
print("shareddd_data_value: ${record.uid}");
print("shareddd_data_value_: ${record.isOfflineshare}");
print("user_id: ${record.sharedUserIds.length}");
String key1 = 'users[]';
List<MapEntry<String, int>> keyValuePairs = [];
for (int i = 0; i < record.sharedUserIds.length; i++) {
int value = record.sharedUserIds[i];
print("keyShared_value_is: $value");
keyValuePairs.add(MapEntry(key1, value));
}
print("My_map_listdata_is $keyValuePairs");
String? token = await CustomSharedPreferences.getCustomValue();
final queryParameters = <String, dynamic>{};
for (var entry in keyValuePairs) {
queryParameters[entry.key] = entry.value;
print("entryyy_valueee: ${entry.value}");
}
dio.options.headers['Authorization'] = 'Bearer $token';
print("key_values: $keyValuePairs");
print("record.sharedUserIds: ${record.sharedUserIds}");
List<int> userlistid = [];
userlistid.addAll(record.sharedUserIds);
userlistid.add(record.createdBy);
print("record.userlist: $userlistid");
print(
"Shareeee_is: ${keyValuePairs.map((users) => 'users[]=$users').join('&')}");
final url =
'${ApiConstants.newKonectarurl}/creds/share/${record.uid}?${userlistid.map((users) => 'users[]=$users').join('&')}';
print("urllll_is: $url");
print("url_is: $url");
try {
final response = await dio.post(url);
if (response.statusCode == 200) {
record.issync = true;
record.isOfflineshare = false;
await hivebox.put(record.id, record);
print('share data successful');
print(response.data);
print(response.data['message']);
print(response.data['data']);
_serverResponse = ShareCredentialData.fromJson(response.data);
notifyListeners();
} else {
record.issync = false;
record.isOfflineshare = true;
await hivebox.put(record.id, record);
print('Failed : ${response.statusCode}');
}
} catch (error) {
print('Failed : $error');
}
}
}
//}
String listToString(List<int> inputList) {
return inputList.join(', ');
}
}