mobileapplicationPassvault/lib/Service/share_credentials.dart

96 lines
2.8 KiB
Dart
Raw Permalink Normal View History

2024-04-12 05:23:32 +00:00
import 'package:dio/dio.dart';
import 'package:flutter_passvault/constant/constantfile.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/hive.dart';
class CredentialService {
final Dio dio = Dio();
final Box<Storedcredential> hivebox =
Hive.box<Storedcredential>('credentialListBox');
int? autoIncrementKey;
int i = 0;
Future<List<CredentialList>> getAll() async {
String? value = await CustomSharedPreferences.getCustomValue();
print("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<String, dynamic>;
print("AuthMy_credential_jsonData: $jsonData");
final messageList = List<Map<String, dynamic>>.from(jsonData['data']);
print("AuthMy_credential_jsonDatamessageList: $messageList");
final fetchedMessages =
messageList.map((json) => CredentialList.fromJson(json)).toList();
print("fetchedMyCredential_is : $fetchedMessages");
print(fetchedMessages);
// await hivebox.clear();
int currentIndex = hivebox.length;
for (final item in fetchedMessages) {
print("currentIndex_is : $currentIndex");
autoIncrementKey = await getNextAutoIncrementValue();
final data = Storedcredential(
id: currentIndex,
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: []);
//.toList();
await hivebox.put(currentIndex, data);
currentIndex++;
}
return fetchedMessages.reversed.toList();
}
} catch (error) {
print('Failed : $error');
}
return [];
}
Future<int> getNextAutoIncrementValue() async {
var counterBox = await Hive.openBox<int>('counterBox');
if (!counterBox.containsKey('counter')) {
counterBox.put('counter', 0);
}
int? counter = counterBox.get('counter');
counterBox.put('counter', counter! + 1);
await counterBox.close();
return counter;
}
}