84 lines
2.7 KiB
Dart
84 lines
2.7 KiB
Dart
import 'dart:convert';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:get_storage/get_storage.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import '../models/kyc_model.dart';
|
|
import 'kyc_service.dart';
|
|
|
|
class KycController extends GetxController {
|
|
var kycList = <KycModel>[].obs; // Using an observable list to store KYC data
|
|
var isLoading = false.obs;
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
loadKycFromLocalStorage(); // Load KYC data from local storage when initialized
|
|
}
|
|
|
|
Future<void> fetchKycData() async {
|
|
try {
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
String? token = prefs.getString('token');
|
|
isLoading(true);
|
|
|
|
// Call the API to get KYC data
|
|
var data = await KycService().getKycData(token!);
|
|
|
|
if (data != null && data.isNotEmpty) {
|
|
// Parse the list of KYC objects
|
|
kycList.value = KycModel.fromJsonList(data); // Convert to List<KycModel>
|
|
saveKycToLocalStorage(); // Save the fetched KYC data to local storage
|
|
} else {
|
|
print("No KYC data found or API response is empty.");
|
|
}
|
|
|
|
print("KYC details: ${kycList}");
|
|
} finally {
|
|
isLoading(false);
|
|
}
|
|
}
|
|
|
|
// Update KYC status locally and persist the changes
|
|
Future<void> updateKycStatus(KycModel kycModel, String status, String comment) async {
|
|
final index = kycList.indexOf(kycModel);
|
|
|
|
if (index != -1) {
|
|
kycList[index].status = status; // Update status locally
|
|
saveKycToLocalStorage(); // Persist the changes locally
|
|
|
|
// Show a success message after updating
|
|
Get.snackbar(
|
|
"Success",
|
|
"KYC status updated to $status.",
|
|
snackPosition: SnackPosition.BOTTOM,
|
|
backgroundColor: Colors.green,
|
|
colorText: Colors.white,
|
|
);
|
|
}
|
|
}
|
|
|
|
// Save the current KYC list to SharedPreferences
|
|
Future<void> saveKycToLocalStorage() async {
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
List<String> kycListJson = kycList.map((kyc) => jsonEncode(kyc.toJson())).toList();
|
|
prefs.setStringList('kycList', kycListJson); // Save the updated list locally
|
|
}
|
|
|
|
// Load KYC data from SharedPreferences
|
|
Future<void> loadKycFromLocalStorage() async {
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
List<String>? kycListJson = prefs.getStringList('kycList');
|
|
|
|
if (kycListJson != null) {
|
|
kycList.value = kycListJson.map((jsonStr) => KycModel.fromJson(jsonDecode(jsonStr))).toList();
|
|
print("Loaded KYC data from local storage.");
|
|
|
|
} else {
|
|
// If no data found, fetch from API
|
|
fetchKycData();
|
|
|
|
}
|
|
}
|
|
}
|