86 lines
2.2 KiB
Dart
86 lines
2.2 KiB
Dart
import 'package:hive_flutter/hive_flutter.dart';
|
|
import '../models/focus_session.dart';
|
|
|
|
/// Service to manage local storage using Hive
|
|
class StorageService {
|
|
static const String _focusSessionBox = 'focus_sessions';
|
|
|
|
/// Initialize Hive
|
|
static Future<void> init() async {
|
|
await Hive.initFlutter();
|
|
|
|
// Register adapters
|
|
Hive.registerAdapter(FocusSessionAdapter());
|
|
|
|
// Open boxes
|
|
await Hive.openBox<FocusSession>(_focusSessionBox);
|
|
}
|
|
|
|
/// Get the focus sessions box
|
|
Box<FocusSession> get _sessionsBox => Hive.box<FocusSession>(_focusSessionBox);
|
|
|
|
/// Save a focus session
|
|
Future<void> saveFocusSession(FocusSession session) async {
|
|
await _sessionsBox.add(session);
|
|
}
|
|
|
|
/// Get all focus sessions
|
|
List<FocusSession> getAllSessions() {
|
|
return _sessionsBox.values.toList();
|
|
}
|
|
|
|
/// Get today's focus sessions
|
|
List<FocusSession> getTodaySessions() {
|
|
final now = DateTime.now();
|
|
final today = DateTime(now.year, now.month, now.day);
|
|
|
|
return _sessionsBox.values.where((session) {
|
|
final sessionDate = DateTime(
|
|
session.startTime.year,
|
|
session.startTime.month,
|
|
session.startTime.day,
|
|
);
|
|
return sessionDate == today;
|
|
}).toList();
|
|
}
|
|
|
|
/// Get total focus minutes for today
|
|
int getTodayTotalMinutes() {
|
|
return getTodaySessions()
|
|
.fold<int>(0, (sum, session) => sum + session.actualMinutes);
|
|
}
|
|
|
|
/// Get total distractions for today
|
|
int getTodayDistractionCount() {
|
|
return getTodaySessions()
|
|
.fold<int>(0, (sum, session) => sum + session.distractionCount);
|
|
}
|
|
|
|
/// Get total completed sessions for today
|
|
int getTodayCompletedCount() {
|
|
return getTodaySessions()
|
|
.where((session) => session.completed)
|
|
.length;
|
|
}
|
|
|
|
/// Get total sessions count for today (including stopped early)
|
|
int getTodaySessionsCount() {
|
|
return getTodaySessions().length;
|
|
}
|
|
|
|
/// Delete a focus session
|
|
Future<void> deleteSession(FocusSession session) async {
|
|
await session.delete();
|
|
}
|
|
|
|
/// Clear all sessions (for testing/debugging)
|
|
Future<void> clearAllSessions() async {
|
|
await _sessionsBox.clear();
|
|
}
|
|
|
|
/// Close all boxes
|
|
static Future<void> close() async {
|
|
await Hive.close();
|
|
}
|
|
}
|