Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

阅读器txt小说文件加载和显示,目前为scorll上下拖动显示小说内容 #136

Merged
merged 6 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions entry/src/main/ets/common/utils/readFile.ets
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BusinessError } from '@ohos.base';
import fs, { Options } from '@ohos.file.fs';
import { common } from '@kit.AbilityKit';
import { chaptersItem } from 'ets/storage/ReaderFile';

let context = getContext(this) as common.UIAbilityContext;
let filePath = context.filesDir + '/desTest.txt';
Expand All @@ -9,13 +10,30 @@ let options: Options = {
encoding: 'utf-8'
};

export function readFile(readFileUrl?: string): void {
console.info('filePath:' + filePath)
fs.readLines(filePath, options).then((readerIterator: fs.ReaderIterator) => {
for (let it = readerIterator.next();!it.done; it = readerIterator.next()) {
console.info("content: " + it.value);
}
}).catch((err: BusinessError) => {
console.error("readLines failed with error message: " + err.message + ", error code: " + err.code);
});
let chapterNumber = 0
const chapters: chaptersItem[] = [];

export class TxtFileHandler {
static async readFile(readFileUrl?: string) {
console.info('filePath:' + filePath)
const regex = /===第(.*?)章 (.*?)===/g;
await fs.readLines(filePath, options).then((readerIterator: fs.ReaderIterator) => {
for (let it = readerIterator.next();!it.done; it = readerIterator.next()) {
const match = regex.exec(it.value);
if (match) {
const chapterTitleNumber = match[1]; // 书源内部章节
const chapterTitle = match[2];
chapterNumber++
chapters.push(new chaptersItem(chapterNumber, chapterTitle, chapterTitleNumber, ''))
} else {
if (chapters.length > 0) {
chapters[chapters.length - 1].content += it.value
}
}
}
}).catch((err: BusinessError) => {
console.error("readLines failed with error message: " + err.message + ", error code: " + err.code);
});
return chapters
}
}
70 changes: 47 additions & 23 deletions entry/src/main/ets/pages/view/Reader/ReaderPage.ets
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import battery from 'ets/componets/common/battery';
import Dialog from 'ets/componets/common/Dialog';
import media from '@ohos.multimedia.media';
import { router } from '@kit.ArkUI';
import sourceView from './sourceView';
import { readFile } from 'ets/common/utils/readFile';
import { TxtFileHandler } from 'ets/common/utils/readFile';
import { AddDownloadDialog } from 'ets/componets/Reader/AddDownloadDialog';
import { NetworkInput } from 'ets/componets/Reader/NetworkInput';

import { MusicPlayer } from 'ets/componets/MusicPlayer';
import purificationView from './purificationView';
import { chaptersItem } from 'ets/storage/ReaderFile';

@Entry
@Component
Expand Down Expand Up @@ -41,13 +41,15 @@ struct ReaderPage {
cornerRadius: 8,
offset: { dx: 0, dy: -100 }
})
scroller: Scroller = new Scroller()
@State isShowSource: boolean = false
@State isShowPurification:boolean = false
@State isShowPurification: boolean = false
@State txtFile: chaptersItem[] = [];

onPageShow(): void {
async onPageShow() {
let temp = router.getParams()
console.log('router' + JSON.stringify(temp))
readFile()
// console.log('router' + JSON.stringify(temp))
this.txtFile = await TxtFileHandler.readFile() //txt小说
}

@Builder
Expand All @@ -69,6 +71,7 @@ struct ReaderPage {

build() {
Stack() {
// 阅读器-上方弹框
if (this.isShowSetting) {
Column() {
Row({ space: 8 }) {
Expand All @@ -77,7 +80,7 @@ struct ReaderPage {
.onClick(() => {
router.back()
})
Text("第一章 牢狱之灾")
Text(`第${this.txtFile[0]?.chapterTitleNumber}章 ${this.txtFile[0]?.title}`)
Blank(1)
Row({ space: 16 }) {
Image($r("app.media.refresh"))
Expand Down Expand Up @@ -131,28 +134,48 @@ struct ReaderPage {
.backgroundColor("white")
.transition(TransitionEffect.move(TransitionEdge.TOP).animation({ duration: 500 }))
}

// 阅读器-小说内容
Column() {
Row() {
}
.width("100%")
.height(40)

Row() {
Text("第一章 牢狱之灾")
.lineHeight(20)
.font({
size: 12,
weight: 400
Scroll(this.scroller) {
Column() {
Row() {
Text(this.txtFile[0]?.title)
.lineHeight(20)
.font({
size: 12,
weight: 400
})
.fontColor("rgba(0, 0, 0, 0.88)")
}
.padding({
top: 16,
bottom: 16,
left: 24,
right: 24
})
.fontColor("rgba(0, 0, 0, 0.88)")
.width("100%")

Text(this.txtFile[0]?.content)
.lineHeight(20)
.font({
size: 12,
weight: 400
})
.fontColor("rgba(0, 0, 0, 0.88)")
}
.padding({
left:24,
right:24
})
}
.padding({
top: 16,
bottom: 16,
left: 24,
right: 24
})
.width("100%")
// .scrollable(ScrollDirection.Horizontal)
.layoutWeight(1)


Blank(1)
Expand All @@ -174,6 +197,7 @@ struct ReaderPage {
})
.width("100%")

// 阅读器-下方弹框
if (this.isShowSetting) {
Column({ space: 20 }) {
Divider()
Expand Down Expand Up @@ -303,7 +327,7 @@ struct ReaderPage {
}
.backgroundColor("white")
.transition(TransitionEffect.move(TransitionEdge.BOTTOM).animation({ duration: 500 }))
.zIndex(2)
// .zIndex(2)
.onClick(() => {
// 这里必须有onclick,不然点击弹窗会消失,走下面的onclick逻辑
})
Expand Down Expand Up @@ -341,7 +365,7 @@ struct ReaderPage {
purificationView() {
Column() {
purificationView({
isShowPurification:this.isShowPurification
isShowPurification: this.isShowPurification
})
}
}
Expand Down
13 changes: 13 additions & 0 deletions entry/src/main/ets/storage/ReaderFile.ets
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export class chaptersItem {
number: number
content: string
chapterTitleNumber: string
title: string

constructor(number: number, title = '', chapterTitleNumber = '', content = '') {
this.number = number
this.content = content
this.chapterTitleNumber = chapterTitleNumber
this.title = title
}
}
Loading