/// 领域实体:不依赖 Flutter、不依赖 drift/http。 /// /// 分层的关键:**领域层不知道数据从哪来**(SQLite?网络?内存?), /// 所以它可以被任何数据源复用,也可以脱离框架单测。 library; import 'package:flutter/foundation.dart' show immutable; @immutable class Bookmark { const Bookmark({ required this.id, required this.title, required this.url, this.note = '', this.favorite = false, required this.createdAt, }); final int id; final String title; final String url; final String note; final bool favorite; final DateTime createdAt; Bookmark copyWith({String? title, String? url, String? note, bool? favorite}) => Bookmark( id: id, title: title ?? this.title, url: url ?? this.url, note: note ?? this.note, favorite: favorite ?? this.favorite, createdAt: createdAt, ); @override bool operator ==(Object other) => other is Bookmark && other.id == id && other.title == title && other.url == url && other.note == note && other.favorite == favorite && other.createdAt == createdAt; @override int get hashCode => Object.hash(id, title, url, note, favorite, createdAt); @override String toString() => 'Bookmark($id, $title, favorite: $favorite)'; } /// 新建书签的输入(还没有 id / createdAt)。 @immutable class BookmarkDraft { const BookmarkDraft({required this.title, required this.url, this.note = ''}); final String title; final String url; final String note; /// 表单校验:返回 null 表示合法。 String? validate() { if (title.trim().isEmpty) return '标题不能为空'; // 注意:不能用 hasAbsolutePath 判断——`https://a.test` 的 path 是空串, // hasAbsolutePath 为 false。正确的检查是 scheme + host。 final Uri? uri = Uri.tryParse(url.trim()); if (uri == null || uri.host.isEmpty || (uri.scheme != 'http' && uri.scheme != 'https')) { return '需要一个 http(s) 链接'; } return null; } }