Vaibhav Surve 91184b48bb Refactor employee model imports and restructure employee-related files
- Updated import paths for employee model files to reflect new directory structure.
- Deleted obsolete models: JobRecentApplicationModel, LeadReportModel, Product, ProductOrderModal, ProjectSummaryModel, RecentOrderModel, TaskListModel, TimeLineModel, User, VisitorByChannelsModel.
- Introduced new AttendanceLogModel, AttendanceLogViewModel, AttendanceModel, TaskModel, TaskListModel, EmployeeInfo, and EmployeeModel with comprehensive fields and JSON serialization methods.
- Enhanced data handling in attendance and task management features.
2025-08-26 11:53:53 +05:30

174 lines
6.1 KiB
Dart

import 'dart:convert';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:marco/controller/project_controller.dart';
import 'package:marco/helpers/services/auth_service.dart';
import 'package:marco/helpers/services/localizations/language.dart';
import 'package:marco/helpers/theme/theme_customizer.dart';
import 'package:marco/model/employees/employee_info.dart';
import 'package:marco/model/user_permission.dart';
class LocalStorage {
static const String _loggedInUserKey = "user";
static const String _themeCustomizerKey = "theme_customizer";
static const String _languageKey = "lang_code";
static const String _jwtTokenKey = "jwt_token";
static const String _refreshTokenKey = "refresh_token";
static const String _userPermissionsKey = "user_permissions";
static const String _employeeInfoKey = "employee_info";
static const String _mpinTokenKey = "mpinToken";
static const String _isMpinKey = "isMpin";
static const String _fcmTokenKey = 'fcm_token';
static SharedPreferences? _preferencesInstance;
static SharedPreferences get preferences {
if (_preferencesInstance == null) {
throw ("Call LocalStorage.init() before using it");
}
return _preferencesInstance!;
}
/// Initialization
static Future<void> init() async {
_preferencesInstance = await SharedPreferences.getInstance();
await initData();
}
static Future<void> initData() async {
AuthService.isLoggedIn = preferences.getBool(_loggedInUserKey) ?? false;
ThemeCustomizer.fromJSON(preferences.getString(_themeCustomizerKey));
}
/// ================== User Permissions ==================
static Future<bool> setUserPermissions(
List<UserPermission> permissions) async {
final jsonList = permissions.map((e) => e.toJson()).toList();
return preferences.setString(_userPermissionsKey, jsonEncode(jsonList));
}
static List<UserPermission> getUserPermissions() {
final storedJson = preferences.getString(_userPermissionsKey);
if (storedJson == null) return [];
return (jsonDecode(storedJson) as List)
.map((e) => UserPermission.fromJson(e as Map<String, dynamic>))
.toList();
}
static Future<bool> removeUserPermissions() =>
preferences.remove(_userPermissionsKey);
/// ================== Employee Info ==================
static Future<bool> setEmployeeInfo(EmployeeInfo employeeInfo) => preferences
.setString(_employeeInfoKey, jsonEncode(employeeInfo.toJson()));
static EmployeeInfo? getEmployeeInfo() {
final storedJson = preferences.getString(_employeeInfoKey);
return storedJson == null
? null
: EmployeeInfo.fromJson(jsonDecode(storedJson));
}
static Future<bool> removeEmployeeInfo() =>
preferences.remove(_employeeInfoKey);
/// ================== Login / Logout ==================
static Future<bool> setLoggedInUser(bool loggedIn) =>
preferences.setBool(_loggedInUserKey, loggedIn);
static Future<bool> removeLoggedInUser() =>
preferences.remove(_loggedInUserKey);
static Future<void> logout() async {
try {
final refreshToken = getRefreshToken();
final fcmToken = getFcmToken();
// Call API only if both tokens exist
if (refreshToken != null && fcmToken != null) {
await AuthService.logoutApi(refreshToken, fcmToken);
}
} catch (e) {
print("Logout API error: $e");
}
// ===== Local Cleanup =====
await removeLoggedInUser();
await removeToken(_jwtTokenKey);
await removeToken(_refreshTokenKey);
await removeUserPermissions();
await removeEmployeeInfo();
await removeMpinToken();
await removeIsMpin();
await preferences.remove("mpin_verified");
await preferences.remove(_languageKey);
await preferences.remove(_themeCustomizerKey);
await preferences.remove('selectedProjectId');
if (Get.isRegistered<ProjectController>()) {
Get.find<ProjectController>().clearProjects();
}
Get.offAllNamed('/auth/login-option');
}
/// ================== Theme & Language ==================
static Future<bool> setCustomizer(ThemeCustomizer themeCustomizer) =>
preferences.setString(_themeCustomizerKey, themeCustomizer.toJSON());
static Future<bool> setLanguage(Language language) =>
preferences.setString(_languageKey, language.locale.languageCode);
static String? getLanguage() => preferences.getString(_languageKey);
/// ================== Tokens ==================
static Future<bool> setToken(String key, String token) =>
preferences.setString(key, token);
static String? getToken(String key) => preferences.getString(key);
static Future<bool> removeToken(String key) => preferences.remove(key);
static Future<bool> setJwtToken(String jwtToken) =>
setToken(_jwtTokenKey, jwtToken);
static Future<bool> setRefreshToken(String refreshToken) =>
setToken(_refreshTokenKey, refreshToken);
static String? getJwtToken() => getToken(_jwtTokenKey);
static String? getRefreshToken() => getToken(_refreshTokenKey);
/// ================== FCM Token ==================
static Future<void> setFcmToken(String token) =>
preferences.setString(_fcmTokenKey, token);
static String? getFcmToken() => preferences.getString(_fcmTokenKey);
/// ================== MPIN ==================
static Future<bool> setMpinToken(String token) =>
preferences.setString(_mpinTokenKey, token);
static String? getMpinToken() => preferences.getString(_mpinTokenKey);
static Future<bool> removeMpinToken() => preferences.remove(_mpinTokenKey);
static Future<bool> setIsMpin(bool value) =>
preferences.setBool(_isMpinKey, value);
static bool getIsMpin() => preferences.getBool(_isMpinKey) ?? false;
static Future<bool> removeIsMpin() => preferences.remove(_isMpinKey);
/// ================== Generic Set/Get ==================
static Future<bool> setBool(String key, bool value) =>
preferences.setBool(key, value);
static bool? getBool(String key) => preferences.getBool(key);
static String? getString(String key) => preferences.getString(key);
static Future<bool> saveString(String key, String value) =>
preferences.setString(key, value);
}