initial import
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
import 'dart:convert';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import '../models/event.dart';
|
||||
|
||||
class ScheduleCacheService {
|
||||
String _eventsKey(String account) => 'cached_events_$account';
|
||||
String _agegroupKey(String account) => 'filters_agegroup_$account';
|
||||
String _subgroupKey(String account) => 'filters_subgroup_$account';
|
||||
String _timestampKey(String account) => 'cached_events_timestamp_$account';
|
||||
|
||||
Future<void> saveEvents(String account, List<Event> events) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final jsonList = events.map((e) => e.toJson()).toList();
|
||||
await prefs.setString(_eventsKey(account), jsonEncode(jsonList));
|
||||
await prefs.setInt(
|
||||
_timestampKey(account),
|
||||
DateTime.now().millisecondsSinceEpoch,
|
||||
);
|
||||
}
|
||||
|
||||
Future<List<Event>?> loadEvents(String account) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final jsonString = prefs.getString(_eventsKey(account));
|
||||
if (jsonString == null) return null;
|
||||
try {
|
||||
final jsonList = jsonDecode(jsonString) as List<dynamic>;
|
||||
return jsonList
|
||||
.map((json) => Event.fromJson(json as Map<String, dynamic>))
|
||||
.toList();
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Future<DateTime?> getLastUpdated(String account) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final timestamp = prefs.getInt(_timestampKey(account));
|
||||
if (timestamp == null) return null;
|
||||
return DateTime.fromMillisecondsSinceEpoch(timestamp);
|
||||
}
|
||||
|
||||
Future<void> saveFilters(
|
||||
String account,
|
||||
String? agegroup,
|
||||
String? subgroup,
|
||||
) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
if (agegroup != null) {
|
||||
await prefs.setString(_agegroupKey(account), agegroup);
|
||||
} else {
|
||||
await prefs.remove(_agegroupKey(account));
|
||||
}
|
||||
if (subgroup != null) {
|
||||
await prefs.setString(_subgroupKey(account), subgroup);
|
||||
} else {
|
||||
await prefs.remove(_subgroupKey(account));
|
||||
}
|
||||
}
|
||||
|
||||
Future<(String?, String?)> loadFilters(String account) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
return (
|
||||
prefs.getString(_agegroupKey(account)),
|
||||
prefs.getString(_subgroupKey(account)),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> clearCache(String account) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.remove(_eventsKey(account));
|
||||
await prefs.remove(_timestampKey(account));
|
||||
await prefs.remove(_agegroupKey(account));
|
||||
await prefs.remove(_subgroupKey(account));
|
||||
}
|
||||
|
||||
Future<void> clearAllCache() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final keys = prefs.getKeys().where((k) => k.startsWith('cached_events_') ||
|
||||
k.startsWith('filters_') ||
|
||||
k.startsWith('cached_events_timestamp_'));
|
||||
for (final key in keys) {
|
||||
await prefs.remove(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user