Skip to content

Commit

Permalink
Merge pull request #260 from lwlizhe/main
Browse files Browse the repository at this point in the history
feat: 简单的章节缓存与章节间跳转
  • Loading branch information
MaoXiaoone authored Aug 18, 2024
2 parents 24a8461 + 3550ce7 commit 114bda9
Show file tree
Hide file tree
Showing 7 changed files with 234 additions and 525 deletions.
8 changes: 4 additions & 4 deletions entry/src/main/ets/pages/view/Reader/ReaderPage2.ets
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ struct ReaderPage2 {
}) //txt小说

if (this.chapterList.length != 0) {
this.chapterList[0].chapterList = this.chapterList[0].content.split('\n').map((value) => value + '\n');
this.chapterList[0].paragraphList = this.chapterList[0].content.split('\n').map((value) => value + '\n');
}

if (this.chapterList.length >= 2) {
for (let index = 1; index < this.chapterList.length; index++) {
this.chapterList[index].chapterList = this.chapterList[index].content.split('\n').map((value) => value + '\n');
this.chapterList[index].paragraphList = this.chapterList[index].content.split('\n').map((value) => value + '\n');
}
}
this.currentChapters = temp.clickBookChapter
Expand Down Expand Up @@ -498,8 +498,8 @@ struct ReaderPageContent {
}
if (this.turnStyle === FlipPageType.SIMULATE_FLIP_PAGE) {
SimulateCurlView({
currentChapterNum: this.currentChapterIndex,
currentPageNum: this.currentPageIndex,
currentChapterIndex: this.currentChapterIndex,
currentPageIndex: this.currentPageIndex,
})
}
if (this.turnStyle === FlipPageType.SLIDE_FLIP_PAGE) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ export class ReaderChapterItem {
content: string
chapterTitleNumber: string
title: string
chapterList: string[]
paragraphList: string[]
chapterPageList: ReaderChapterPageItem[]

constructor(number: number, title = '', chapterTitleNumber = '', content = '', chapterList = [],
constructor(number: number, title = '', chapterTitleNumber = '', content = '', paragraphList = [],
chapterPageList = []) {
this.number = number
this.content = content
this.chapterTitleNumber = chapterTitleNumber
this.title = title
this.chapterList = chapterList
this.paragraphList = paragraphList
this.chapterPageList = chapterPageList
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { ReaderChapterItem } from '../entity/ReaderChaptersItem';

export class NovelChapterOperateHelper {
static nextPageIndex(chapterList: ReaderChapterItem[], chapterIndex: number, pageIndex: number): number[] | null {
if (chapterIndex >= chapterList.length - 1) {
if (pageIndex >= chapterList[chapterIndex].chapterPageList.length - 1) {
return null;
} else {
return [chapterIndex, pageIndex + 1];
}
} else {
if (pageIndex >= chapterList[chapterIndex].chapterPageList.length - 1) {
if (chapterList[chapterIndex+1].chapterPageList.length === 0) {
return null;
}
return [chapterIndex + 1, 0];
} else {
return [chapterIndex, pageIndex + 1];
}
}
}

static prePageIndex(chapterList: ReaderChapterItem[], chapterIndex: number, pageIndex: number): number[] | null {
if (chapterIndex <= 0) {
if (pageIndex <= 0) {
return null;
} else {
return [chapterIndex, pageIndex - 1];
}
} else {
if (pageIndex <= 0) {
if (chapterList[chapterIndex-1].chapterPageList.length === 0) {
return null;
}
return [chapterIndex - 1, chapterList[chapterIndex-1].chapterPageList.length - 1];
} else {
return [chapterIndex, pageIndex - 1];
}
}
}
}
107 changes: 83 additions & 24 deletions readerLibrary/src/main/ets/provider/configProvider.ets
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BGColorType, BG_COLOR_ARRAY, Constants, FlipPageType } from '../common/
import { ReaderChapterItem, ReaderChapterPageItem } from '../common/entity/ReaderChaptersItem';
import { NovelContentParseHelper } from '../common/helper/NovelContentParseHelper';
import { display } from '@kit.ArkUI';
import taskPool from '@ohos.taskpool';

@Component
export struct ReaderProvider {
Expand Down Expand Up @@ -31,41 +32,99 @@ export struct ReaderProvider {
}
}

@Concurrent
async function calChapterContent(width: number, height: number, targetParagraphList: string[], fontSize: number,
lineHeightRatio: number): Promise<string[][]> {
try {
return await NovelContentParseHelper.calculateChapterContent(targetParagraphList,
fontSize,
fontSize * lineHeightRatio, width, height)
} catch (e) {
console.info('loadNovel 出错 ' + e)
return [];
}

}

@Component
struct ReaderSourceProvider {
@BuilderParam child: () => void // 通用组件
@Provide('chapterList') chapterList: ReaderChapterItem[] = [];
@Consume('lineHeightRatio') @Watch('onFontSizeChange') lineHeightRatio: number
@Consume('fontSize') @Watch('onFontSizeChange') fontSize: number
@Consume('displaySize') @Watch('calChapterContent') displaySize: number[]
@Consume('currentChapterIndex') currentChapterIndex: number
@Consume('paragraphSpacing') paragraphSpacing: number

onFontSizeChange() {
this.calChapterContent()
@Consume('lineHeightRatio') @Watch('onConfigSizeChange') lineHeightRatio: number
@Consume('fontSize') @Watch('onConfigSizeChange') fontSize: number
@Consume('displaySize') @Watch('onDisplaySizeChange') displaySize: number[]
@Consume('currentChapterIndex') @Watch('calChapterContent') currentChapterIndex: number
@Consume('paragraphSpacing') @Watch('onConfigSizeChange') paragraphSpacing: number

onConfigSizeChange() {
this.forceCalChapterContent()
}

onDisplaySizeChange() {
this.forceCalChapterContent()
}

private calContentOfTargetChapter(width: number, height: number, chapterIndex: number, fontSize: number,
lineHeightRatio: number, forceUpdate: boolean) {
if (!forceUpdate) {
if (this.chapterList[chapterIndex].chapterPageList.length !== 0) {
return
}
}
taskPool.execute(calChapterContent, width, height, this.chapterList[chapterIndex].paragraphList, fontSize,
lineHeightRatio).then((chapterList) => {
if (Array.isArray(chapterList)) {
let startIndex = 0
let result: string[][] = chapterList as string[][]
if (result.length !== 0) {
this.chapterList[chapterIndex].chapterPageList = result.map((item) => {
let contentLength = item.reduce((accumulator, currentString) => {
return accumulator + currentString.length;
}, 0)
let result = new ReaderChapterPageItem(startIndex, startIndex + contentLength, item);
startIndex += contentLength;
return result
})
this.chapterList = Array.from(this.chapterList)
}
}
})
}

calChapterContent() {
let width = this.displaySize[0];
let height = this.displaySize[1];

if (width !== 0 && height !== 0 && this.chapterList.length !== 0) {
NovelContentParseHelper.calculateChapterContent(this.chapterList[this.currentChapterIndex].chapterList,
this.fontSize,
this.fontSize * this.lineHeightRatio, width, height).then((chapterList) => {
let startIndex = 0
this.chapterList[this.currentChapterIndex].chapterPageList = chapterList.map((item) => {
let contentLength = item.reduce((accumulator, currentString) => {
return accumulator + currentString.length;
}, 0)
let result = new ReaderChapterPageItem(startIndex, startIndex + contentLength, item);
startIndex += contentLength;
return result
})
this.chapterList = Array.from(this.chapterList)
}).catch((reason: string) => {
console.log(reason);
});
this.calContentOfTargetChapter(width, height, this.currentChapterIndex,
this.fontSize, this.lineHeightRatio, false);
if (this.currentChapterIndex > 0) {
this.calContentOfTargetChapter(width, height, this.currentChapterIndex - 1,
this.fontSize, this.lineHeightRatio, false);
}
if (this.currentChapterIndex < this.chapterList.length - 1) {
this.calContentOfTargetChapter(width, height, this.currentChapterIndex + 1,
this.fontSize, this.lineHeightRatio, false);
}
}

}

forceCalChapterContent() {
let width = this.displaySize[0];
let height = this.displaySize[1];

if (width !== 0 && height !== 0 && this.chapterList.length !== 0) {
this.calContentOfTargetChapter(width, height, this.currentChapterIndex,
this.fontSize, this.lineHeightRatio, true);
if (this.currentChapterIndex > 0) {
this.calContentOfTargetChapter(width, height, this.currentChapterIndex - 1,
this.fontSize, this.lineHeightRatio, true);
}
if (this.currentChapterIndex < this.chapterList.length - 1) {
this.calContentOfTargetChapter(width, height, this.currentChapterIndex + 1,
this.fontSize, this.lineHeightRatio, true);
}
}

}
Expand Down
49 changes: 37 additions & 12 deletions readerLibrary/src/main/ets/view/CoverFlipView.ets
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,49 @@ import { Constants } from '../common/constants/Constants';
import { Reader } from './Reader';
import { promptAction } from '@kit.ArkUI';
import { ReaderChapterItem } from '../common/entity/ReaderChaptersItem';
import { NovelChapterOperateHelper } from '../common/helper/NovelChapterOperateHelper';

@Component
export struct CoverFlipView {
@State offsetX: number = 0
@Consume('screenW') screenW: number;
@Link currentPageIndex: number;
@Consume('chapterList') chapterList: ReaderChapterItem[];
@Consume('chapterList') @Watch('calPageIndex') chapterList: ReaderChapterItem[];
@Link currentChapterIndex: number;
private isFirst: boolean = false;

@State nextPageIndex: number[] | null = null;
@State prePageIndex: number[] | null = null;

aboutToAppear(): void {
this.calPageIndex();
}

calPageIndex() {
this.nextPageIndex =
NovelChapterOperateHelper.nextPageIndex(this.chapterList, this.currentChapterIndex, this.currentPageIndex);
this.prePageIndex =
NovelChapterOperateHelper.prePageIndex(this.chapterList, this.currentChapterIndex, this.currentPageIndex);
}

build() {
Stack() {
if (this.chapterList.length === 0 || this.chapterList[this.currentChapterIndex].chapterPageList.length === 0) {
Reader({ content: '' })
} else {
if (this.currentPageIndex + 1 < this.chapterList[this.currentChapterIndex].chapterPageList.length) {
if (this.nextPageIndex !== null) {
Reader({
content: this.chapterList[this.currentChapterIndex].chapterPageList[this.currentPageIndex+1].content
content: this.chapterList[this.nextPageIndex[0]].chapterPageList[this.nextPageIndex[1]].content
})
}

Reader({ content: this.chapterList[this.currentChapterIndex].chapterPageList[this.currentPageIndex].content })
.translate({ x: this.offsetX >= 0 ? 0 : this.offsetX, y: 0, z: 0 })
.width(this.screenW)

if (this.currentPageIndex >= 1) {
if (this.prePageIndex != null) {
Reader({
content: this.chapterList[this.currentChapterIndex].chapterPageList[this.currentPageIndex-1].content
content: this.chapterList[this.prePageIndex[0]].chapterPageList[this.prePageIndex[1]].content
})
.translate({
x: -this.screenW + this.offsetX
Expand All @@ -40,29 +55,37 @@ export struct CoverFlipView {
.gesture(
PanGesture()
.onActionUpdate((event?: GestureEvent) => {
let prePageIndex =
NovelChapterOperateHelper.prePageIndex(this.chapterList, this.currentChapterIndex, this.currentPageIndex);
let nextPageIndex =
NovelChapterOperateHelper.nextPageIndex(this.chapterList, this.currentChapterIndex, this.currentPageIndex);

if (!event) {
return;
}
if (this.currentPageIndex == 0 && event.offsetX > 0) {
if (prePageIndex === null && event.offsetX > 0) {
this.isFirst = true;
return;
}

if (nextPageIndex === null && event.offsetX < 0) {
return;
}
this.offsetX = event.offsetX;
})
.onActionEnd(() => {
animateTo({
duration: Constants.FLIP_DURATION,
curve: Curve.EaseOut,
onFinish: () => {
this.calPageIndex();
if (this.offsetX > 0) {
this.currentPageIndex -= 1;
// if (this.currentStartIndex !== 0) {
// this.currentStartIndex -= this.sumRow * this.rowWord;
// }
this.currentChapterIndex = (this.prePageIndex as number[])[0]
this.currentPageIndex = (this.prePageIndex as number[])[1]
}
if (this.offsetX < 0) {
this.currentPageIndex += 1;
// this.currentStartIndex += this.sumRow * this.rowWord;
this.currentChapterIndex = (this.nextPageIndex as number[])[0]
this.currentPageIndex = (this.nextPageIndex as number[])[1]
}
if (this.isFirst) {
promptAction.showToast({
Expand All @@ -72,6 +95,7 @@ export struct CoverFlipView {
this.isFirst = false;
}
this.offsetX = 0;
this.calPageIndex();
}
}, () => {
if (this.offsetX > 0) {
Expand All @@ -80,6 +104,7 @@ export struct CoverFlipView {
if (this.offsetX < 0) {
this.offsetX = -this.screenW;
}
this.calPageIndex();
})
})
)
Expand Down
Loading

0 comments on commit 114bda9

Please sign in to comment.