@@ -4,27 +4,39 @@ import 'package:shared_preferences/shared_preferences.dart';
44
55class ProfileCacheService {
66 // Keys for SharedPreferences
7- static const String _profileKey = 'cached_profile_data' ;
8- static const String _lastUpdatedKey = 'last_updated_profile' ;
7+ static const String _profileKeyPrefix = 'cached_profile_data' ;
8+ static const String _lastUpdatedKeyPrefix = 'last_updated_profile' ;
9+ static const String _currentYearKey = 'cached_year_id' ;
910
10- // Save profile data to cache
11- Future <bool > cacheProfileData (Map <String , dynamic > profileData) async {
11+ // Get cache key for specific year
12+ String _getProfileKey (int yearId) => '${_profileKeyPrefix }_$yearId ' ;
13+ String _getLastUpdatedKey (int yearId) => '${_lastUpdatedKeyPrefix }_$yearId ' ;
14+
15+ // Save profile data to cache with year ID
16+ Future <bool > cacheProfileData (
17+ Map <String , dynamic > profileData,
18+ int yearId,
19+ ) async {
1220 try {
1321 final prefs = await SharedPreferences .getInstance ();
14- await prefs.setString (_profileKey, jsonEncode (profileData));
15- await prefs.setString (_lastUpdatedKey, DateTime .now ().toIso8601String ());
22+ await prefs.setString (_getProfileKey (yearId), jsonEncode (profileData));
23+ await prefs.setString (
24+ _getLastUpdatedKey (yearId),
25+ DateTime .now ().toIso8601String (),
26+ );
27+ await prefs.setInt (_currentYearKey, yearId);
1628 return true ;
1729 } catch (e) {
1830 debugPrint ('Error caching profile data: $e ' );
1931 return false ;
2032 }
2133 }
2234
23- // Retrieve profile data from cache
24- Future <Map <String , dynamic >?> getCachedProfileData () async {
35+ // Retrieve profile data from cache for specific year
36+ Future <Map <String , dynamic >?> getCachedProfileData (int yearId ) async {
2537 try {
2638 final prefs = await SharedPreferences .getInstance ();
27- final profileDataString = prefs.getString (_profileKey );
39+ final profileDataString = prefs.getString (_getProfileKey (yearId) );
2840
2941 if (profileDataString == null ) return null ;
3042
@@ -35,11 +47,11 @@ class ProfileCacheService {
3547 }
3648 }
3749
38- // Get last update timestamp for profile data
39- Future <DateTime ?> getLastUpdated () async {
50+ // Get last update timestamp for profile data of specific year
51+ Future <DateTime ?> getLastUpdated (int yearId ) async {
4052 try {
4153 final prefs = await SharedPreferences .getInstance ();
42- final timestamp = prefs.getString (_lastUpdatedKey );
54+ final timestamp = prefs.getString (_getLastUpdatedKey (yearId) );
4355 if (timestamp == null ) return null ;
4456
4557 return DateTime .parse (timestamp);
@@ -49,12 +61,26 @@ class ProfileCacheService {
4961 }
5062 }
5163
52- // Clear profile data cache
53- Future <bool > clearCache () async {
64+ // Clear profile data cache for specific year
65+ Future <bool > clearCache ([ int ? yearId] ) async {
5466 try {
5567 final prefs = await SharedPreferences .getInstance ();
56- await prefs.remove (_profileKey);
57- await prefs.remove (_lastUpdatedKey);
68+
69+ if (yearId != null ) {
70+ // Clear specific year cache
71+ await prefs.remove (_getProfileKey (yearId));
72+ await prefs.remove (_getLastUpdatedKey (yearId));
73+ } else {
74+ // Clear all profile caches
75+ final keys = prefs.getKeys ();
76+ for (final key in keys) {
77+ if (key.startsWith (_profileKeyPrefix) ||
78+ key.startsWith (_lastUpdatedKeyPrefix)) {
79+ await prefs.remove (key);
80+ }
81+ }
82+ await prefs.remove (_currentYearKey);
83+ }
5884 return true ;
5985 } catch (e) {
6086 debugPrint ('Error clearing profile cache: $e ' );
0 commit comments