积分、成就系统

This commit is contained in:
ytc1012
2025-11-27 13:37:10 +08:00
parent 0195cdf54b
commit 58f6ec39b7
35 changed files with 7786 additions and 199 deletions

View File

@@ -1,17 +1,23 @@
import 'package:hive_flutter/hive_flutter.dart';
import 'package:flutter/foundation.dart';
import '../models/focus_session.dart';
import '../models/user_progress.dart';
/// Service to manage local storage using Hive
class StorageService {
static const String _focusSessionBox = 'focus_sessions';
static const String _userProgressBox = 'user_progress';
static const String _progressKey = 'user_progress_key';
// Cache for today's sessions to improve performance
List<FocusSession>? _todaySessionsCache;
DateTime? _cacheDate;
// Cache for user progress
UserProgress? _userProgressCache;
/// Initialize Hive storage service
///
///
/// This method initializes Hive, registers adapters, and opens the focus sessions box.
/// It should be called once during app initialization.
Future<void> init() async {
@@ -20,10 +26,12 @@ class StorageService {
// Register adapters
Hive.registerAdapter(FocusSessionAdapter());
Hive.registerAdapter(UserProgressAdapter());
// Open boxes
await Hive.openBox<FocusSession>(_focusSessionBox);
await Hive.openBox<UserProgress>(_userProgressBox);
if (kDebugMode) {
print('StorageService initialized successfully');
}
@@ -37,13 +45,92 @@ class StorageService {
/// Get the focus sessions box
Box<FocusSession> get _sessionsBox => Hive.box<FocusSession>(_focusSessionBox);
/// Get the user progress box
Box<UserProgress> get _progressBox => Hive.box<UserProgress>(_userProgressBox);
/// Invalidate the cache when data changes
void _invalidateCache() {
_todaySessionsCache = null;
_cacheDate = null;
}
/// Invalidate user progress cache
void _invalidateProgressCache() {
_userProgressCache = null;
}
// ==================== User Progress Methods ====================
/// Get user progress (creates new one if doesn't exist)
UserProgress getUserProgress() {
try {
// Return cached progress if available
if (_userProgressCache != null) {
return _userProgressCache!;
}
// Try to get from box
var progress = _progressBox.get(_progressKey);
// Create new progress if doesn't exist
if (progress == null) {
progress = UserProgress();
_progressBox.put(_progressKey, progress);
}
// Cache and return
_userProgressCache = progress;
return progress;
} catch (e) {
if (kDebugMode) {
print('Failed to get user progress: $e');
}
// Return new progress as fallback
return UserProgress();
}
}
/// Save user progress
Future<void> saveUserProgress(UserProgress progress) async {
try {
await _progressBox.put(_progressKey, progress);
_userProgressCache = progress; // Update cache
} catch (e) {
if (kDebugMode) {
print('Failed to save user progress: $e');
}
rethrow;
}
}
/// Update user progress with a function
Future<void> updateUserProgress(Function(UserProgress) updateFn) async {
try {
final progress = getUserProgress();
updateFn(progress);
await saveUserProgress(progress);
} catch (e) {
if (kDebugMode) {
print('Failed to update user progress: $e');
}
rethrow;
}
}
/// Clear user progress (for testing/reset)
Future<void> clearUserProgress() async {
try {
await _progressBox.delete(_progressKey);
_invalidateProgressCache();
} catch (e) {
if (kDebugMode) {
print('Failed to clear user progress: $e');
}
rethrow;
}
}
/// Save a focus session to local storage
///
/// [session] - The focus session to save