EssenceDailyCore/lib/services/database_service.dart

70 lines
1.9 KiB
Dart

import 'package:hive_flutter/hive_flutter.dart';
import '../models/task_item.dart';
class DatabaseService {
static const String tasksBoxName = 'essence_tasks_v3'; // 更改版本号以避免旧数据冲突
static const String settingsBoxName = 'settings';
static Future<void> initialize() async {
await Hive.initFlutter();
Hive.registerAdapter(TaskItemAdapter());
}
static Future<Box<TaskItem>> openTasksBox() async {
return await Hive.openBox<TaskItem>(tasksBoxName);
}
static Future<Box> openSettingsBox() async {
return await Hive.openBox(settingsBoxName);
}
static Future<void> injectMockData(Box<TaskItem> tasksBox) async {
if (tasksBox.isEmpty) {
final now = DateTime.now();
final mockTasks = [
TaskItem(
id: 'guide_1',
title: "👋 Welcome to EssenceDailyCore",
subtitle: "Long press to drag and reorder",
notes: "EssenceDailyCore encourages you to focus on the 3 most important things daily.",
createdAt: now,
sortIndex: 0,
),
TaskItem(
id: 'guide_2',
title: "Swipe right to complete",
subtitle: "Feel the satisfying haptic feedback",
createdAt: now,
sortIndex: 1,
),
TaskItem(
id: 'guide_3',
title: "Tap the 1st task to Focus",
subtitle: "Built-in timer helps you enter flow state",
createdAt: now,
sortIndex: 2,
),
TaskItem(
id: 'guide_4',
title: "Tap here for details",
subtitle: "Add notes, tags, or edit deadlines",
createdAt: now,
sortIndex: 3,
),
];
for (var task in mockTasks) {
await tasksBox.put(task.id, task);
}
}
}
static Box<TaskItem> getTasksBox() {
return Hive.box<TaskItem>(tasksBoxName);
}
static Box getSettingsBox() {
return Hive.box(settingsBoxName);
}
}