|
| 1 | +import 'dart:convert'; |
| 2 | +import 'package:flutter/foundation.dart'; |
| 3 | +import 'package:shared_preferences/shared_preferences.dart'; |
| 4 | +import 'package:progres/features/debts/data/models/academic_year_debt.dart'; |
| 5 | + |
| 6 | +class DebtsCacheService { |
| 7 | + // Keys for SharedPreferences |
| 8 | + static const String _debtsKey = 'cached_debts'; |
| 9 | + static const String _lastUpdatedKey = 'last_updated_debts'; |
| 10 | + |
| 11 | + // Save debts |
| 12 | + Future<bool> cacheDebts(List<AcademicYearDebt> debts) async { |
| 13 | + try { |
| 14 | + final prefs = await SharedPreferences.getInstance(); |
| 15 | + final debtsJson = debts.map((d) => d.toJson()).toList(); |
| 16 | + await prefs.setString(_debtsKey, jsonEncode(debtsJson)); |
| 17 | + await prefs.setString(_lastUpdatedKey, DateTime.now().toIso8601String()); |
| 18 | + return true; |
| 19 | + } catch (e) { |
| 20 | + debugPrint('Error caching debts: $e'); |
| 21 | + return false; |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + // Retrieve debts |
| 26 | + Future<List<AcademicYearDebt>?> getCachedDebts() async { |
| 27 | + try { |
| 28 | + final prefs = await SharedPreferences.getInstance(); |
| 29 | + final debtsString = prefs.getString(_debtsKey); |
| 30 | + |
| 31 | + if (debtsString == null) return null; |
| 32 | + |
| 33 | + final List<dynamic> decodedJson = jsonDecode(debtsString); |
| 34 | + return decodedJson |
| 35 | + .map((json) => AcademicYearDebt.fromJson(json)) |
| 36 | + .toList(); |
| 37 | + } catch (e) { |
| 38 | + debugPrint('Error retrieving cached debts: $e'); |
| 39 | + return null; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + // Get last update timestamp |
| 44 | + Future<DateTime?> getLastUpdated() async { |
| 45 | + try { |
| 46 | + final prefs = await SharedPreferences.getInstance(); |
| 47 | + final timestamp = prefs.getString(_lastUpdatedKey); |
| 48 | + if (timestamp == null) return null; |
| 49 | + |
| 50 | + return DateTime.parse(timestamp); |
| 51 | + } catch (e) { |
| 52 | + debugPrint('Error getting last updated time: $e'); |
| 53 | + return null; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + // Clear all debts cached data |
| 58 | + Future<bool> clearCache() async { |
| 59 | + try { |
| 60 | + final prefs = await SharedPreferences.getInstance(); |
| 61 | + await prefs.remove(_debtsKey); |
| 62 | + await prefs.remove(_lastUpdatedKey); |
| 63 | + return true; |
| 64 | + } catch (e) { |
| 65 | + debugPrint('Error clearing cache: $e'); |
| 66 | + return false; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // Check if data is stale (older than specified duration) |
| 71 | + Future<bool> isDataStale({ |
| 72 | + Duration staleDuration = const Duration(hours: 12), |
| 73 | + }) async { |
| 74 | + final lastUpdated = await getLastUpdated(); |
| 75 | + if (lastUpdated == null) return true; |
| 76 | + |
| 77 | + final now = DateTime.now(); |
| 78 | + return now.difference(lastUpdated) > staleDuration; |
| 79 | + } |
| 80 | +} |
0 commit comments