189 lines
5.5 KiB
Dart
Executable File
189 lines
5.5 KiB
Dart
Executable File
import 'package:flutter/foundation.dart';
|
|
import '../postEnrollment/service/api_service.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'package:nhance_app_pwa/logger.dart';
|
|
|
|
class DataManager extends ChangeNotifier {
|
|
static final DataManager _instance = DataManager._internal();
|
|
factory DataManager() => _instance;
|
|
DataManager._internal();
|
|
|
|
late ApiService _apiService;
|
|
|
|
// === Advertisement ===
|
|
List<String> _advertisementImages = [];
|
|
bool _adsLoaded = false;
|
|
|
|
// === Policies ===
|
|
List<dynamic> _policyList = [];
|
|
bool _policyLoaded = false;
|
|
String? _empName;
|
|
int _prePolicyCount = 0;
|
|
|
|
// === Self Employee Profile ===
|
|
Map<String, dynamic>? _selfProfile;
|
|
Map<String, dynamic>? _accountManagerDetails;
|
|
bool _selfProfileLoaded = false;
|
|
|
|
// Getters
|
|
List<String> get advertisementImages => _advertisementImages;
|
|
bool get adsLoaded => _adsLoaded;
|
|
|
|
List<dynamic> get policyList => _policyList;
|
|
bool get policyLoaded => _policyLoaded;
|
|
String? get empName => _empName;
|
|
int get prePolicyCount => _prePolicyCount;
|
|
|
|
Map<String, dynamic>? get selfProfile => _selfProfile;
|
|
Map<String, dynamic>? get accountManagerDetails => _accountManagerDetails;
|
|
bool get selfProfileLoaded => _selfProfileLoaded;
|
|
|
|
/// Inject ApiService (with context) once
|
|
void init(ApiService apiService) {
|
|
_apiService = apiService;
|
|
}
|
|
|
|
/// Load Ads once
|
|
Future<void> loadAdvertisementImages() async {
|
|
if (_adsLoaded) return;
|
|
|
|
final response = await _apiService.getAdvertisementImageToApi('');
|
|
if (response['status'] == 'success') {
|
|
_advertisementImages = await List<String>.from(response['data']);
|
|
_adsLoaded = true;
|
|
notifyListeners();
|
|
logDebug("✅ Advertisement images loaded: $_advertisementImages");
|
|
}
|
|
}
|
|
|
|
/// Load Policies (first time or refresh)
|
|
Future<void> loadPolicies({
|
|
required String? clientId,
|
|
required String? empCode,
|
|
required String status,
|
|
required String? clientBranchId,
|
|
required String? mobileNo,
|
|
bool forceRefresh = false,
|
|
}) async {
|
|
if (clientId == null || empCode == null) return; // guard clause
|
|
|
|
final response = await _apiService.getActiveAndInactivePolicyDetails(
|
|
clientId,
|
|
empCode,
|
|
status,
|
|
clientBranchId!,
|
|
mobileNo ?? '',''
|
|
);
|
|
|
|
if (response['status'] == 'success') {
|
|
_policyList = response['data'];
|
|
_empName = response['emp_name'];
|
|
_prePolicyCount = response['pre_policy_count'] ?? 0;
|
|
_policyLoaded = true;
|
|
notifyListeners();
|
|
logDebug("✅ Policy list loaded: $_policyList");
|
|
} else {
|
|
logDebug("❌ Failed to load policies: ${response['status']}");
|
|
}
|
|
}
|
|
|
|
/// Load Self Employee Profile (first time or refresh)
|
|
Future<void> loadSelfEmployeeProfile({
|
|
required String clientId,
|
|
required String empCode,
|
|
required String clientBranchId,
|
|
bool forceRefresh = false,
|
|
}) async {
|
|
// if (_selfProfileLoaded && !forceRefresh) return;
|
|
|
|
final response = await _apiService.getSelfEmployeeProfileDetails(
|
|
clientId,
|
|
empCode,
|
|
clientBranchId,
|
|
);
|
|
|
|
if (response['status'] == 'success' && response.containsKey('data')) {
|
|
_selfProfile = Map<String, dynamic>.from(response['data']);
|
|
_accountManagerDetails =
|
|
Map<String, dynamic>.from(response['AccountManagerDetails']);
|
|
_selfProfileLoaded = true;
|
|
|
|
// Store selfEmpStatus in SharedPreferences
|
|
final prefs = await SharedPreferences.getInstance();
|
|
if (_selfProfile?['emp_status'] != null) {
|
|
prefs.setString('selfEmpStatus', _selfProfile!['emp_status']);
|
|
}
|
|
|
|
notifyListeners();
|
|
logDebug("✅ Self Employee Profile loaded: $_selfProfile");
|
|
} else {
|
|
logDebug("❌ Failed to load self profile: ${response['status']}");
|
|
}
|
|
}
|
|
|
|
/// Clear all cached data on logout
|
|
Future<void> clearData() async {
|
|
_advertisementImages = [];
|
|
_adsLoaded = false;
|
|
|
|
_policyList = [];
|
|
_policyLoaded = false;
|
|
_empName = null;
|
|
_prePolicyCount = 0;
|
|
|
|
_selfProfile = null;
|
|
_accountManagerDetails = null;
|
|
_selfProfileLoaded = false;
|
|
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// import 'package:flutter/foundation.dart';
|
|
// import '../postEnrollment/service/api_service.dart';
|
|
// import 'package:flutter/material.dart';
|
|
//
|
|
// class DataManager extends ChangeNotifier {
|
|
// static final DataManager _instance = DataManager._internal();
|
|
// factory DataManager() => _instance;
|
|
// DataManager._internal();
|
|
//
|
|
// late ApiService _apiService;
|
|
//
|
|
// List<String> _advertisementImages = [];
|
|
// bool _isLoaded = false;
|
|
//
|
|
// List<String> get advertisementImages => _advertisementImages;
|
|
// bool get isLoaded => _isLoaded;
|
|
//
|
|
// /// Inject ApiService (with context) once
|
|
// void init(ApiService apiService) {
|
|
// _apiService = apiService;
|
|
// }
|
|
//
|
|
// /// Load only once
|
|
// Future<void> loadAdvertisementImages() async {
|
|
// if (_isLoaded) return;
|
|
//
|
|
// final response = await _apiService.getAdvertisementImageToApi();
|
|
// if (response['status'] == 'success') {
|
|
// _advertisementImages = List<String>.from(response['data']);
|
|
// logDebug("✅ Advertisement images loaded: ${_advertisementImages}");
|
|
// _isLoaded = true;
|
|
// notifyListeners();
|
|
// } else {
|
|
// logDebug("❌ Failed to load advertisement images: ${response['message']}");
|
|
// }
|
|
// }
|
|
//
|
|
// /// Clear on logout
|
|
// Future<void> clearData() async {
|
|
// _advertisementImages = [];
|
|
// _isLoaded = false;
|
|
// notifyListeners();
|
|
// }
|
|
// }
|