..."]
│ └─ 遍历 fragments:
│ ├─ select(fragment, searchNameRule) → "海贼王"
│ ├─ select(fragment, searchAuthorRule) → "尾田荣一郎"
│ ├─ select(fragment, searchDetailRule) → "/book/123"
│ └─ UnifiedSearchResult(title:"海贼王", detailUrl:"https://.../book/123")
│
├─ search(源2, "海贼王") → 同上...
└─ ...
↓
Future.wait → List
:
[SearchSection(id:'cloud', items: [...]),
SearchSection(id:'online', items: [UnifiedSearchResult...])]
↓
ExplorePage FutureBuilder → 渲染结果列表
```
### 11.2 详情链路
```
用户点击搜索结果中的 "海贼王"
↓
ExplorePage._openItem(item)
↓ item.online.detailUrl = "https://example.com/book/123"
Navigator.push(OnlineBookDetailPage(
source: 源1,
detailUrl: "https://example.com/book/123",
initialTitle: "海贼王",
initialAuthor: "尾田荣一郎",
))
↓
OnlineBookDetailPage._load()
↓
LegadoEngine.fetchBookInfo(源1, "https://example.com/book/123")
├─ cache.getBookInfo(源1.id, url) → 未命中
├─ _fetchBody(源1, url) → HTML 字符串
├─ RuleParser(html) → 提取:
│ ├─ bookInfoNameRule → "海贼王"
│ ├─ bookInfoAuthorRule → "尾田荣一郎"
│ ├─ bookInfoCoverRule → "https://example.com/img/cover.jpg"
│ ├─ bookInfoIntroRule → "拥有财富、名声、权力..."
│ └─ bookInfoTocRule → "/book/123/catalog"
├─ tocUrl = "https://example.com/book/123/catalog"
├─ cache.putBookInfo(...)
└─ return OnlineBookInfo(title:"海贼王", tocUrl:"https://.../catalog", ...)
↓
setState → loadingInfo=false, info=OnlineBookInfo, loadingToc=true
↓ 上半部分立即渲染(封面+书名+作者+简介+按钮)
↓
_loadToc("https://example.com/book/123/catalog")
↓
LegadoEngine.fetchToc(源1, "https://example.com/book/123/catalog")
├─ cache.getToc(源1.id, url) → 未命中
├─ _fetchPaginatedToc(源1, url)
│ ├─ page=1: _fetchBody → HTML
│ │ RuleParser → tocListRule → fragments
│ │ 每 fragment: tocNameRule="第1话" + tocUrlRule="/book/123/ch1"
│ │ → chapters.add(OnlineChapter("第1话", "https://.../ch1", index:1))
│ │ ...
│ ├─ tocNextPageRule → 有 → nextUrl = "?page=2"
│ ├─ page=2: 同上...
│ └─ tocNextPageRule → 空 → 停止
├─ cache.putToc(...) → List(共 1130 条)
└─ return chapters
↓
setState → loadingToc=false, chapters=[1130条]
↓ 下半部分渲染章节列表
↓
OnlineReadingHistory.find(源1.id, detailUrl) → readingRecord 或 null
```
### 11.3 阅读链路
```
用户在详情页点击 "第1话"
↓
OnlineBookDetailPage._openChapter(0) // index 从 0 开始
↓
Navigator.push(OnlineMangaReaderPage(
source: 源1,
title: "海贼王",
chapters: [1130条],
currentChapterIndex: 0,
detailUrl: "https://example.com/book/123",
))
↓
OnlineMangaReaderPage._load()
↓
chapter = chapters[0] → {title:"第1话", url:"https://.../ch1"}
↓
LegadoEngine.fetchChapterContent(源1, chapter)
├─ cache.getChapter(源1.id, url) → 未命中
├─ _fetchBody(源1, url) → HTML
├─ RuleParser(html) → contentRule → "#chapter-content" 的 innerHtml
├─ _extractImageUrls(contentBody, absoluteUrl)
│ ├─ RegExp:
/ data-src / data-original
│ └─ JSON 图片扫描
├─ contentNextRule → "/book/123/ch2" → nextChapterUrl
├─ contentPrevRule → 空 → prevChapterUrl=null
├─ cache.putChapter(...)
└─ return OnlineChapterContent(
title: "第1话",
imageUrls: ["https://.../img01.jpg", "https://.../img02.jpg", ...],
nextChapterUrl: "https://.../ch2",
)
↓
OnlineReadingHistory.save(OnlineReadingRecord(
sourceId, sourceName, bookTitle, detailUrl,
chapterUrl, chapterTitle, chapterIndex:0, totalChapters:1130,
lastReadTime: now,
))
↓
预加载下一章:
engine.fetchChapterContent(源1, chapters[1])
.catchError(...) // 后台静默,失败不影响当前阅读
↓
渲染 PageView:
Image.network("https://.../img01.jpg") ← 第1页
Image.network("https://.../img02.jpg") ← 第2页
...
用户点击 "下一章"
↓
_move(1) → chapterIndex = 1
↓
setState → contentFuture = _load()
↓
engine.fetchChapterContent(源1, chapters[1])
├─ cache 命中!(已预加载) → 秒切
└─ _load() 内部再次 save 阅读记录 + 预加载第3章
```
---
## 附录:关键设计要点总结
### 数据流方向
```
LegadoSource (静态规则,SharedPreferences 持久化)
↓
LegadoEngine (规则驱动解析,HTTP + 缓存)
↓
OnlineBookInfo / OnlineChapter / OnlineChapterContent (运行时数据,内存缓存)
↓
UI Pages (ExplorePage / OnlineBookDetailPage / ReaderPages)
```
### 编码处理链
```
搜索关键词:
_encodeKeyword(source, keyword)
→ gbk.encode / Uri.encodeQueryComponent / 平台通道
响应体:
_fetchBody → ResponseType.bytes
→ _decodeBody(bytes, source.charset, content-type charset)
→ gbk.decode / utf8.decode / latin1.decode
```
### 缓存策略
```
BookInfo → 进入详情页时缓存,同一书源 + 同一 detailUrl 只请求一次
TOC → 进入详情页时缓存,同一书源 + 同一 tocUrl 只请求一次
Chapter → 阅读时缓存当前章 + 预加载下一章
阅读记录 → SharedPreferences 持久化,跨会话保留
```
### 回退策略优先级
```
规则解析优先:
1. 用书源声明的 rule 解析(CSS/JSON/Regex)
2. 规则为空或无结果 → 回退
- 搜索: _fallbackSearchResults (全 扫描 + 关键词过滤)
- 目录: _fallbackChapters (章节正则过滤的 扫描)
- 内容: _htmlToText / _extractImageUrls (从 HTML 暴力提取)
```