Files
1356/lifetimer/lib/data/services/offline_cache_service.dart
2026-04-10 12:05:40 +02:00

122 lines
3.2 KiB
Dart

import 'package:hive_flutter/hive_flutter.dart';
import '../models/cached_goal_model.dart';
class OfflineCacheService {
static const String _goalsBoxName = 'cached_goals';
static const String _userBoxName = 'cached_user';
static const String _countdownBoxName = 'cached_countdown';
Box<CachedGoal>? _goalsBox;
Box? _userBox;
Box? _countdownBox;
Future<void> init() async {
try {
await Hive.initFlutter();
if (!Hive.isAdapterRegistered(0)) {
Hive.registerAdapter(CachedGoalAdapter());
}
_goalsBox = await Hive.openBox<CachedGoal>(_goalsBoxName);
_userBox = await Hive.openBox(_userBoxName);
_countdownBox = await Hive.openBox(_countdownBoxName);
} catch (e) {
print('Error initializing offline cache: $e');
}
}
Future<void> cacheGoals(List<CachedGoal> goals) async {
if (_goalsBox == null) return;
await _goalsBox!.clear();
for (var goal in goals) {
await _goalsBox!.put(goal.id, goal);
}
}
Future<List<CachedGoal>> getCachedGoals() async {
if (_goalsBox == null) return [];
return _goalsBox!.values.toList();
}
Future<CachedGoal?> getCachedGoal(String goalId) async {
if (_goalsBox == null) return null;
return _goalsBox!.get(goalId);
}
Future<void> cacheGoal(CachedGoal goal) async {
if (_goalsBox == null) return;
await _goalsBox!.put(goal.id, goal);
}
Future<void> deleteCachedGoal(String goalId) async {
if (_goalsBox == null) return;
await _goalsBox!.delete(goalId);
}
Future<void> markGoalAsDirty(String goalId) async {
if (_goalsBox == null) return;
final goal = _goalsBox!.get(goalId);
if (goal != null) {
await _goalsBox!.put(goalId, goal.copyWith(isDirty: true));
}
}
Future<List<CachedGoal>> getDirtyGoals() async {
if (_goalsBox == null) return [];
return _goalsBox!.values.where((goal) => goal.isDirty).toList();
}
Future<void> clearDirtyFlag(String goalId) async {
if (_goalsBox == null) return;
final goal = _goalsBox!.get(goalId);
if (goal != null) {
await _goalsBox!.put(goalId, goal.copyWith(isDirty: false));
}
}
Future<void> cacheUserData(Map<String, dynamic> userData) async {
if (_userBox == null) return;
await _userBox!.putAll(userData);
}
Future<Map<String, dynamic>> getCachedUserData() async {
if (_userBox == null) return {};
return Map<String, dynamic>.from(_userBox!.toMap());
}
Future<void> cacheCountdownData(Map<String, dynamic> countdownData) async {
if (_countdownBox == null) return;
await _countdownBox!.putAll(countdownData);
}
Future<Map<String, dynamic>> getCachedCountdownData() async {
if (_countdownBox == null) return {};
return Map<String, dynamic>.from(_countdownBox!.toMap());
}
Future<void> clearAllCache() async {
if (_goalsBox != null) await _goalsBox!.clear();
if (_userBox != null) await _userBox!.clear();
if (_countdownBox != null) await _countdownBox!.clear();
}
Future<void> close() async {
if (_goalsBox != null) await _goalsBox!.close();
if (_userBox != null) await _userBox!.close();
if (_countdownBox != null) await _countdownBox!.close();
}
}