83 lines
2.4 KiB
Dart
83 lines
2.4 KiB
Dart
|
import 'package:dio/dio.dart';
|
||
|
import 'package:flutter/material.dart';
|
||
|
import 'package:flutter_passvault/constant/constantfile.dart';
|
||
|
import 'package:flutter_passvault/modelclasses/add_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 AddCredentialProvider extends ChangeNotifier {
|
||
|
final dio = Dio();
|
||
|
|
||
|
AddCredentialsModel? _serverResponse;
|
||
|
|
||
|
AddCredentialsModel? get success => _serverResponse;
|
||
|
|
||
|
Future<void> postcredential() async {
|
||
|
final Box<Storedcredential> hivebox =
|
||
|
Hive.box<Storedcredential>('credentialListBox');
|
||
|
var allRecords = hivebox.values.toList();
|
||
|
|
||
|
var unsyncedRecords = allRecords
|
||
|
.where((record) =>
|
||
|
record.issync == false && record.isOfflinecreated == true)
|
||
|
.toList();
|
||
|
|
||
|
print("Stored_added_unsyncedRecords : $unsyncedRecords");
|
||
|
|
||
|
for (var record in unsyncedRecords) {
|
||
|
final Map<String, dynamic> senddata = {
|
||
|
'username': record.username,
|
||
|
'password': record.password,
|
||
|
'name': record.name,
|
||
|
'detail': record.detail
|
||
|
};
|
||
|
|
||
|
print("senddd_offline_data_iss: $senddata");
|
||
|
|
||
|
String? token = await CustomSharedPreferences.getCustomValue();
|
||
|
|
||
|
print("senddd_offline_token: $token");
|
||
|
|
||
|
dio.options.headers['Authorization'] = 'Bearer $token';
|
||
|
|
||
|
try {
|
||
|
final response = await dio.post("${ApiConstants.newKonectarurl}/creds",
|
||
|
data: senddata);
|
||
|
|
||
|
if (response.statusCode == 200) {
|
||
|
print('Neww added successful');
|
||
|
print(response.data);
|
||
|
print(response.data['message']);
|
||
|
|
||
|
print(response.data['data']);
|
||
|
print(response.data['data']['id']);
|
||
|
|
||
|
record.issync = true;
|
||
|
record.isOfflinecreated = false;
|
||
|
|
||
|
record.uid = response.data['data']['id'];
|
||
|
|
||
|
await hivebox.put(record.id, record);
|
||
|
|
||
|
print("update: ${record.uid}");
|
||
|
|
||
|
final addedList = AddCredentialsModel.fromJson(response.data);
|
||
|
|
||
|
_serverResponse = addedList;
|
||
|
|
||
|
// notifyListeners();
|
||
|
} else {
|
||
|
record.issync = false;
|
||
|
record.isOfflinecreated = true;
|
||
|
|
||
|
await hivebox.put(record.id, record);
|
||
|
}
|
||
|
} catch (error) {
|
||
|
// ignore: use_rethrow_when_possible
|
||
|
throw error;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|