readful/lib/services/database_service.dart
ddshi 02010ff972 feat: 完成数据持久化阶段并规划UI开发
📊 数据持久化阶段完成:
- 完成4个数据模型的Hive集成(Book, Bookshelf, Bookmark, Highlight)
- 实现4个Repository数据访问层
- 生成5个TypeAdapter自动序列化文件
- 完成所有模型的CRUD操作和测试验证

📚 项目文档更新:
- 新增数据持久化阶段完成总结文档
- 更新CLAUDE.md项目主文档
- 完善项目结构说明和开发进度

🚀 UI开发阶段规划:
- 定义产品定位:类似微信读书的Material Design电子书阅读器
- 制定4阶段开发计划:UI基础架构→顶部导航→首页内容→数据集成
- 明确页面结构:底部Tab导航(首页/书库/统计/我的)
- 规划核心功能:搜索、导入、最近阅读、摘录列表

🎯 下一里程碑:
- 开始UI基础架构搭建
- 实现底部Tab导航和Material Design主题系统

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-01 17:34:50 +08:00

96 lines
2.9 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'package:hive_flutter/hive_flutter.dart';
import 'package:path_provider/path_provider.dart';
import '../models/book.dart';
import '../models/bookshelf.dart';
import '../models/bookmark.dart';
import '../models/highlight.dart';
///单例模式,让整个应用只有一个数据库
class DatabaseService {
static DatabaseService? _instance;
static DatabaseService get instance => _instance ??= DatabaseService._();
DatabaseService._();
late Box<Book> _booksBox;
late Box<Bookshelf> _bookshelvesBox;
late Box<Bookmark> _bookmarksBox;
late Box<Highlight> _highlightsBox;
Future<void> init() async {
try {
// 1. 获取应用文档目录
final appDocumentDir = await getApplicationDocumentsDirectory();
// 2. 初始化Hive
await Hive.initFlutter(appDocumentDir.path);
// 3. 注册枚举TypeAdapter使用内置的枚举适配器
Hive.registerAdapter(BookFormatAdapter());
Hive.registerAdapter(ReadingStatusAdapter());
Hive.registerAdapter(BookshelfTypeAdapter());
Hive.registerAdapter(HighlightColorAdapter());
Hive.registerAdapter(AnnotationTypeAdapter());
// 4. 注册模型TypeAdapter
Hive.registerAdapter(BookAdapter());
Hive.registerAdapter(BookshelfAdapter());
Hive.registerAdapter(BookmarkAdapter());
Hive.registerAdapter(HighlightAdapter());
// 5. 打开Box
_booksBox = await Hive.openBox<Book>('books');
_bookshelvesBox = await Hive.openBox<Bookshelf>('bookshelves');
_bookmarksBox = await Hive.openBox<Bookmark>('bookmarks');
_highlightsBox = await Hive.openBox<Highlight>('highlights');
print('✅ Hive数据库初始化成功');
} catch (e) {
print('❌ Hive数据库初始化失败: $e');
rethrow; // 重新抛出异常,让调用者知道初始化失败
}
}
// 获取Book Box的方法
Box<Book> getBooksBox() {
if (!Hive.isBoxOpen('books')) {
throw Exception('Book Box未打开请先调用init()');
}
return _booksBox;
}
// 获取Bookshelf Box的方法
Box<Bookshelf> getBookshelvesBox() {
if (!Hive.isBoxOpen('bookshelves')) {
throw Exception('Bookshelf Box未打开请先调用init()');
}
return _bookshelvesBox;
}
/// 获取Bookmark Box的方法
Box<Bookmark> getBookmarksBox() {
if (!Hive.isBoxOpen('bookmarks')) {
throw Exception('Bookmark Box未打开请先调用init()');
}
return _bookmarksBox;
}
/// 获取Highlight Box的方法
Box<Highlight> getHighlightsBox() {
if (!Hive.isBoxOpen('highlights')) {
throw Exception('Highlight Box未打开请先调用init()');
}
return _highlightsBox;
}
/// 关闭数据库
Future<void> close() async {
await _booksBox.close();
await _bookshelvesBox.close();
await _bookmarksBox.close();
await _highlightsBox.close();
await Hive.close();
}
}