import 'package:hive/hive.dart'; part 'bookmark.g.dart'; /// 书签模型 /// 用于记录和管理阅读位置标记 @HiveType(typeId: 5) class Bookmark { @HiveField(0) final String id; @HiveField(1) final String bookId; @HiveField(2) final String? chapterId; @HiveField(3) final String title; @HiveField(4) final String? description; @HiveField(5) final int pageIndex; @HiveField(6) final double position; // 0.0-1.0之间的值,表示在文档中的相对位置 @HiveField(7) final String? previewText; @HiveField(8) final DateTime createdTime; @HiveField(9) final int sortOrder; // 构造函数... const Bookmark( {required this.id, required this.bookId, this.chapterId, required this.title, this.description, required this.pageIndex, required this.position, this.previewText, required this.createdTime, required this.sortOrder}); // copyWith方法... Bookmark copyWith( {String? id, String? bookId, String? chapterId, String? title, String? description, int? pageIndex, double? position, String? previewText, DateTime? createdTime, int? sortOrder}) { return Bookmark( id: id ?? this.id, // 如果id不为null就使用新值,否则保持原值 bookId: bookId ?? this.bookId, chapterId: chapterId ?? this.chapterId, title: title ?? this.title, description: description ?? this.description, pageIndex: pageIndex ?? this.pageIndex, position: position ?? this.position, previewText: previewText ?? this.previewText, createdTime: createdTime ?? this.createdTime, sortOrder: sortOrder ?? this.sortOrder); } // toMap/fromMap方法... /// toMap方法 - 将对象转换为Map Map toMap() { return { 'id': id, 'bookId': bookId, 'chapterId': chapterId, 'title': title, 'description': description, 'pageIndex': pageIndex, 'position': position, 'previewText': previewText, 'createdTime': createdTime.toIso8601String(), 'sortOrder': sortOrder }; } /// fromMap构造函数 /// /// 这是toMap的逆操作,用于: /// 1. 从本地存储中读取数据 /// 2. 从网络请求中解析数据 /// 3. 从保存的状态中恢复对象 factory Bookmark.fromMap(Map map) { return Bookmark( id: map['id'], bookId: map['bookId'], chapterId: map['chapterId'], title: map['title'], description: map['description'], pageIndex: map['pageIndex'], position: map['position'], previewText: map['previewText'], createdTime: DateTime.parse(map['createdTime']), sortOrder: map['sortOrder']); } // 工厂方法... factory Bookmark.create({ required String bookId, required String title, required int pageIndex, required double position, String? chapterId, String? description, String? previewText, }) { return Bookmark( id: _generateId(), // 生成唯一ID bookId: bookId, chapterId: chapterId, title: title, pageIndex: pageIndex, position: position, description: description, previewText: previewText, createdTime: DateTime.now(), sortOrder: 0); } // 计算属性和实用方法... /// 生成唯一ID的私有方法 static String _generateId() { return '${DateTime.now().millisecondsSinceEpoch}_${(DateTime.now().microsecond).toString()}'; } /// 重写equals和hashCode,用于对象比较 @override bool operator ==(Object other) { if (identical(this, other)) return true; return other is Bookmark && other.id == id; } @override int get hashCode => id.hashCode; /// 重写toString方法,便于调试 @override String toString() { return 'Bookmark(id: $id, title: $title, bookId: $bookId, sortOrder: $sortOrder)'; } // 更新位置 Bookmark updatePosition(int newPageIndex, double newPosition) { return copyWith( pageIndex: newPageIndex, position: newPosition, ); } // 更新标题和描述 Bookmark updateInfo({String? title, String? description}) { return copyWith( title: title ?? this.title, description: description ?? this.description, ); } // 格式化位置显示 String get positionDisplay => '${(position * 100).toInt()}%'; // 检查位置是否有效 bool get isValidPosition => position >= 0.0 && position <= 1.0; }