import { promptAction } from '@kit.ArkUI'; import { HdsNavDestination, HdsNavDestinationTitleMode, ScrollEffectType } from '@kit.UIDesignKit'; import { AppHub } from '../common/AppHub'; import { Dim, FontSize, HDS_MATERIAL, InsetKey, Insets } from '../common/Theme'; import { ConfigStore } from '../model/Config'; import { TaskManager } from '../model/TaskManager'; import { LkCoverCandidate } from '../model/TaskModel'; import { DownloadDraft } from '../model/Draft'; import { SymIcon, FloatingToolbar, ToolbarAction } from './Widgets'; /** * LK 封面候选选择页(等价脚本单卷封面面板): * 按章节顺序扫描正文插图,列出前 N 张不同图片(N 为设置中的候选数量), * 从中选一张作为本次下载的封面,也可不设置。仅对选章下载生效。 */ @Component export struct LkCoverSelectPage { @Consume pathStack: NavPathStack; @StorageProp(InsetKey.TOP) topInset: number = 0; @StorageProp(InsetKey.BOTTOM) bottomInset: number = 0; private scroller: Scroller = new Scroller(); @State targetUrl: string = ''; @State loading: boolean = false; @State loadError: string = ''; @State scanText: string = ''; @State candidates: LkCoverCandidate[] = []; @State selected: number = -1; // -1 = 不设置封面 aboutToAppear(): void { // 从导航参数兜底获取目标 URL try { const params = this.pathStack.getParamByName('lkCoverSelect') as Array; if (params !== undefined && params.length > 0) { this.targetUrl = params[params.length - 1] as string; } } catch (e) { // 忽略 } if (this.targetUrl.length > 0) { this.scan(); } } async scan(): Promise { if (this.targetUrl.length === 0 || this.loading) { return; } // 等价脚本:封面面板仅在单卷下载出现 if (!DownloadDraft.lkVolumeSingle(this.targetUrl)) { this.loadError = '自定义封面仅用于单卷下载:请先在「选择卷」中只勾选一卷'; return; } this.loading = true; this.loadError = ''; this.candidates = []; this.selected = -1; const count = ConfigStore.get().lk.coverCandidateCount; const volumeId = DownloadDraft.lkVolumeIds.length > 0 ? DownloadDraft.lkVolumeIds[0] : ''; const res = await TaskManager.instance.fetchLkCoverCandidates(this.targetUrl, count, volumeId, (text: string): void => { this.scanText = text; }); this.loading = false; if (!res.ok) { this.loadError = res.error; return; } this.candidates = res.candidates; let selectable = 0; for (const c of this.candidates) { if (c.previewPath.length > 0) { selectable++; } } if (this.candidates.length === 0) { this.scanText = '正文没有找到可用的图片地址,可无封面下载。'; } else { this.scanText = `已列出 ${this.candidates.length} 张(${selectable} 张可选)` + `${this.candidates.length < count ? ',已遍历全部章节' : ''}。点击图片选择封面。`; } } confirm(): void { if (this.selected >= 0 && this.selected < this.candidates.length) { const c = this.candidates[this.selected]; if (c.previewPath.length === 0) { promptAction.showToast({ message: '该图片加载失败,请选择其他图片' }); return; } DownloadDraft.lkCoverUrl = c.url; DownloadDraft.lkCoverVolumeId = DownloadDraft.lkVolumeIds.length > 0 ? DownloadDraft.lkVolumeIds[0] : ''; } else { DownloadDraft.lkCoverUrl = ''; DownloadDraft.lkCoverVolumeId = ''; } AppHub.emit('draft', ''); promptAction.showToast({ message: DownloadDraft.lkCoverUrl.length > 0 ? '已选择封面' : '未设置封面' }); this.pathStack.pop(); } subTitle(): string | undefined { const name = DownloadDraft.bookName.length > 0 ? `《${DownloadDraft.bookName}》 · ` : ''; return this.loading ? `${name}正在扫描…` : `${name}${this.selected >= 0 ? '已选择第 ' + (this.selected + 1) + ' 张' : '未选择封面'}`; } @Builder candidateCard(c: LkCoverCandidate, idx: number) { Column({ space: 6 }) { if (c.previewPath.length > 0) { Image(c.previewPath) .width('100%') .height(110) .objectFit(ImageFit.Contain) .borderRadius(8) .backgroundColor($r('app.color.chip_bg')) } else { Column({ space: 6 }) { SymIcon({ sym: $r('sys.symbol.exclamationmark_triangle'), iconSize: 20, colorRes: $r('app.color.font_ter') }) Text('加载失败') .fontSize(FontSize.cap) .fontColor($r('app.color.font_ter')) } .width('100%') .height(110) .justifyContent(FlexAlign.Center) .borderRadius(8) .backgroundColor($r('app.color.chip_bg')) } Text(`第 ${idx + 1} 张`) .fontSize(FontSize.cap) .fontWeight(this.selected === idx ? FontWeight.Medium : FontWeight.Normal) .fontColor(this.selected === idx ? $r('app.color.brand') : $r('app.color.font_sec')) .maxLines(1) Text(c.chapterTitle) .fontSize(FontSize.cap) .fontColor($r('app.color.font_ter')) .maxLines(1) .textOverflow({ overflow: TextOverflow.Ellipsis }) } .width('100%') .padding(6) .borderRadius(12) .border({ width: 2, color: this.selected === idx ? $r('app.color.brand') : Color.Transparent }) .backgroundColor($r('app.color.card_bg')) .opacity(c.previewPath.length > 0 ? 1 : 0.55) .onClick(() => { if (c.previewPath.length > 0) { this.selected = this.selected === idx ? -1 : idx; } }) } build() { HdsNavDestination() { Stack({ alignContent: Alignment.Bottom }) { if (this.loading) { Column({ space: 12 }) { LoadingProgress() .width(44) .height(44) .color($r('app.color.brand')) Text(this.scanText.length > 0 ? this.scanText : '正在扫描候选封面…') .fontSize(FontSize.sub) .fontColor($r('app.color.font_sec')) .textAlign(TextAlign.Center) .maxLines(2) } .width('100%') .height('100%') .justifyContent(FlexAlign.Center) .padding(Dim.pagePad) } else if (this.loadError.length > 0) { Column({ space: 12 }) { Text(this.loadError) .fontSize(FontSize.sub) .fontColor($r('app.color.err')) .textAlign(TextAlign.Center) Button('重新扫描', { type: ButtonType.Capsule }) .fontSize(FontSize.sub) .height(40) .padding({ left: 32, right: 32 }) .backgroundColor($r('app.color.brand')) .fontColor($r('app.color.on_brand')) .onClick(() => { this.scan(); }) } .width('100%') .height('100%') .justifyContent(FlexAlign.Center) .padding(Dim.pagePad) } else { Scroll(this.scroller) { Column({ space: 12 }) { Text(this.scanText) .fontSize(FontSize.cap) .fontColor($r('app.color.font_sec')) .width('100%') // 不设置封面 Row({ space: 10 }) { SymIcon({ sym: this.selected === -1 ? $r('sys.symbol.checkmark_circle_fill') : $r('sys.symbol.circle'), iconSize: 20, colorRes: this.selected === -1 ? $r('app.color.brand') : $r('app.color.font_ter') }) Column({ space: 2 }) { Text('不设置封面') .fontSize(FontSize.sub) .fontColor($r('app.color.font_pri')) Text('本次下载不嵌入封面图') .fontSize(FontSize.cap) .fontColor($r('app.color.font_ter')) } .alignItems(HorizontalAlign.Start) .layoutWeight(1) } .width('100%') .padding(12) .borderRadius(Dim.innerRadius) .border({ width: 2, color: this.selected === -1 ? $r('app.color.brand') : Color.Transparent }) .backgroundColor($r('app.color.card_bg')) .onClick(() => { this.selected = -1; }) Grid() { ForEach(this.candidates, (c: LkCoverCandidate, idx: number) => { GridItem() { this.candidateCard(c, idx) } }, (c: LkCoverCandidate, idx: number) => `${idx}_${c.previewPath.length > 0 ? 1 : 0}_${this.selected === idx ? 1 : 0}`) } .columnsTemplate('1fr 1fr 1fr') .columnsGap(10) .rowsGap(10) .width('100%') Text('按本卷章节与正文顺序取图;封面独立于「嵌入正文插图」开关。整本下载默认使用书籍封面,此处选择仅对单卷下载生效。') .fontSize(FontSize.cap) .fontColor($r('app.color.font_ter')) .width('100%') } .width('100%') .padding({ top: Insets.contentTop(this.topInset), left: Dim.pagePad, right: Dim.pagePad, bottom: Insets.contentBottom(this.bottomInset) }) } .scrollBar(BarState.Off) .align(Alignment.Top) .clipContent(ContentClipMode.SAFE_AREA) .width('100%') .height('100%') FloatingToolbar() { ToolbarAction({ label: '重新扫描', onTap: (): void => { this.scan(); } }) ToolbarAction({ label: this.selected >= 0 ? `确定(第 ${this.selected + 1} 张)` : '确定(不设置)', primary: true, onTap: (): void => { this.confirm(); } }) } } } .width('100%') .height('100%') .backgroundColor($r('app.color.page_bg')) } .titleMode(HdsNavDestinationTitleMode.MINI) .titleBar({ content: { title: { mainTitle: '选择封面' } }, style: { scrollEffectOpts: { enableScrollEffect: true, scrollEffectType: ScrollEffectType.GRADIENT_BLUR }, systemMaterialEffect: HDS_MATERIAL } }) .bindToScrollable([this.scroller]) .ignoreLayoutSafeArea([LayoutSafeAreaType.SYSTEM], [LayoutSafeAreaEdge.TOP, LayoutSafeAreaEdge.BOTTOM]) .onReady((ctx: NavDestinationContext) => { const p = ctx.pathInfo.param as Object; if (typeof p === 'string' && p.length > 0 && this.targetUrl === '') { this.targetUrl = p; this.scan(); } }) } }