Skip to content

Commit

Permalink
Merge pull request #268 from mgz0227/dev_2008
Browse files Browse the repository at this point in the history
解决搜索结果过多导致堆栈闪退
  • Loading branch information
MaoXiaoone authored Aug 25, 2024
2 parents e49c48c + a8411fb commit c3e1647
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 22 deletions.
22 changes: 22 additions & 0 deletions entry/src/main/ets/common/model/BookChapter.ets
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @author 2008
* @datetime 2024/8/25 0:11
* @className: BookChapter
*/
export class BookChapter {
url:string = '' // 章节地址
title:string = '' // 章节标题
isVolume:boolean = false // 是否是卷名
baseUrl:string = '' // 用来拼接相对url
bookUrl:string = '' // 书籍地址
index:number = 0 // 章节序号
isVip:boolean = false // 是否VIP
isPay:boolean = false // 是否已购买
resourceUrl?:string // 音频真实URL
tag?:string // 更新时间或其他章节附加信息
start:number = 0 // 章节起始位置
end:number = 0 // 章节终止位置
startFragmentId:string = '' //EPUB书籍当前章节的fragmentId
endFragmentId:string = '' //EPUB书籍下一章节的fragmentId
variable:string = '' //变量
}
3 changes: 3 additions & 0 deletions entry/src/main/ets/common/model/XmlAnalysis.ets
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ export class XmlAnalysis {
bookList = (response.data as SearchBook[]).map(item => {
item.bookType = this.xmlDate.bookSource?.bookSourceType ?? 0
item.source = this.xmlDate.bookSource
if (item.bookUrl && !isNetworkUrl(item.bookUrl)){
item.tocUrl = this.xmlDate.bookSource?.bookSourceUrl + item.bookUrl
}
if (item.coverUrl && !isNetworkUrl(item.coverUrl)) {
item.coverUrl = this.xmlDate.bookSource?.bookSourceUrl + item.coverUrl
}
Expand Down
23 changes: 15 additions & 8 deletions entry/src/main/ets/common/model/taskSearchBook.ets
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,23 @@ class taskSearchBook{
private bookSourceParts:BookSource[] = []
MAX_THREADS:number = 5;
tasks:taskPool.Task[] = [];
taskSearchBook:SearchBook[] = []
taskSearchBookNumber:number = 0
dataStart:number = 0;
async search(searchKey: string, returnBook: (newUrls: SearchBook[]) => void, cancel: () => void): Promise<void> {
async search(searchKey: string, returnBook: (newUrls: SearchBook[]) => void, cancel: () => void,source?:BookSource[]): Promise<void> {
this.dataStart = Date.now();
this.tasks = [];
this.taskSearchBook = []
this.taskSearchBookNumber = 0
this.bookSourceParts = []
if (!searchKey) {
return;
}

this.bookSourceParts = await bookSourceDao.getEnabledPartByGroup();
if (!source) {
this.bookSourceParts = await bookSourceDao.getEnabledPartByGroup();
} else {
this.bookSourceParts.push(...source)
}

if (!this.bookSourceParts || this.bookSourceParts.length === 0) {
showMessage('启用书源为空');
cancel()
Expand All @@ -37,10 +42,12 @@ class taskSearchBook{
const numThreads = Math.min(Math.ceil(numBookSources / 20), this.MAX_THREADS);
// 创建一个回调函数
const callback = (newUrls: SearchBook[]) => {
this.taskSearchBook.push(...newUrls)
this.taskSearchBookNumber += newUrls.length
returnBook(newUrls)
};

console.log(`${this.bookSourceParts}`)

for (let i = 0; i < numThreads; i++) {
const sourcesSlice = this.bookSourceParts.slice(
(i * numBookSources) / numThreads,
Expand Down Expand Up @@ -70,7 +77,7 @@ class taskSearchBook{
const searchBooks = results.flat();
let dataEnd = Date.now();
if(this.tasks.length > 0){
showMessage(`搜索中止,共搜索到${this.taskSearchBook.length}个结果` + `用时${(dataEnd - this.dataStart) / 1000}秒`);
showMessage(`搜索中止,共搜索到${this.taskSearchBookNumber}个结果` + `用时${(dataEnd - this.dataStart) / 1000}秒`);
}
cancel()
}
Expand All @@ -93,9 +100,9 @@ class taskSearchBook{
})
}
let dataEnd = Date.now();
showMessage(`搜索中止,共搜索到${this.taskSearchBook.length}个结果` + `用时${(dataEnd - this.dataStart) / 1000}秒`);
showMessage(`搜索中止,共搜索到${this.taskSearchBookNumber}个结果` + `用时${(dataEnd - this.dataStart) / 1000}秒`);
this.tasks = [];
this.taskSearchBook = []
this.taskSearchBookNumber = 0
this.bookSourceParts = []
}

Expand Down
2 changes: 1 addition & 1 deletion entry/src/main/ets/common/utils/searchUtils.ets
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class searchUtils{

async searchBook(bookSource:BookSource): Promise<SearchBook[]>{
if (!bookSource.searchUrl || bookSource.searchUrl === undefined) {
return []
throw new Error('搜索url为空');
}
let books:SearchBook[] = []
let urls = ''
Expand Down
6 changes: 5 additions & 1 deletion entry/src/main/ets/componets/search/SearchBookItem.ets
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export struct SearchBookItem {
//书名
Row(){
Text(this.book.name)
.fontColor(this.index<=3?"rgba(255, 102, 0, 1)":"#000000")
.fontColor(this.SearchValue=== this.book.name?"rgba(255, 102, 0, 1)":"#000000")
}
//作者及书源数量
Row({space:2}){
Expand Down Expand Up @@ -67,9 +67,13 @@ export struct SearchBookItem {
coverUrl: this.book.coverUrl,
author: this.book.author,
intro: this.book.intro,
kind:this.book.kind,
bookUrl:this.book.bookUrl,
wordCount:this.book.wordCount,
bookType:this.book.bookType,
toUrl:this.book.tocUrl,
source:this.book.source,
sourceList:this.book.sourceList,
origin:this.book.source?.bookSourceUrl,
originName:this.book.source?.bookSourceName
}
Expand Down
2 changes: 0 additions & 2 deletions entry/src/main/ets/componets/search/SearchBookList.ets
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
import { common } from '@kit.AbilityKit'
import { util } from '@kit.ArkTS'
import CommonConstants from '../../common/constants/CommonConstants'
import PaddingConstants from '../../common/constants/PaddingConstants'
import taskSearchBooks from '../../common/model/taskSearchBook'
import { SearchBook } from '../../database/entities/SearchBook'
import { showMessage } from '../common/promptShow'
import { SearchBookItem } from './SearchBookItem'

@Component
Expand Down
22 changes: 14 additions & 8 deletions entry/src/main/ets/componets/search/SearchHead.ets
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ export struct SearchHead{
@Consume('searchBookList') books:SearchBook[]
@State lastSearch:string = ''
@Consume('isSearch') isSearch:boolean
testBook(){
console.log(this.books.length.toString())
console.log(this.books.toString())
}
// @State bookIndex:number = 0
build() {
Flex({
alignItems:ItemAlign.End,
Expand Down Expand Up @@ -77,14 +74,19 @@ export struct SearchHead{
}

setBookList(books: SearchBook[]) {
if (books.length === 0) return
books.forEach((book: SearchBook) => {
let found = false;

if(!book.name) return
for (let item of this.books) {
if (item.name === book.name && item.author === book.author) {
item.belongCount++;
//源来的books的source源 id和 books的source源 id相同不添加
if (item.source?.bookSourceUrl !== book.source?.bookSourceUrl && book.source) {
// 确保 sourceList 已经存在并且是一个数组
if (!item.sourceList) {
item.sourceList = [];
}
item.sourceList.push(book.source)
}
found = true;
Expand All @@ -101,10 +103,14 @@ export struct SearchHead{
this.sortBooksBySearchValue()
}

sortBooksBySearchValue(){
sortBooksBySearchValue() {
this.books.sort((a: SearchBook, b: SearchBook) => {
const aNameSimilarity = this.getSimilarity(a.name.toLowerCase(), this.SearchValue.toLowerCase());
const bNameSimilarity = this.getSimilarity(b.name.toLowerCase(), this.SearchValue.toLowerCase());
const aName = a.name || '';
const bName = b.name || '';
const searchValue = this.SearchValue || '';

const aNameSimilarity = this.getSimilarity(aName.toLowerCase(), searchValue.toLowerCase());
const bNameSimilarity = this.getSimilarity(bName.toLowerCase(), searchValue.toLowerCase());

if (aNameSimilarity !== bNameSimilarity) {
return bNameSimilarity - aNameSimilarity;
Expand Down
3 changes: 3 additions & 0 deletions entry/src/main/ets/database/entities/Books.ets
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* @className: Books
* 书籍
*/
import { BookSource } from './BookSource';

@Observed
export class Books {
Expand Down Expand Up @@ -77,6 +78,8 @@ export class Books {
isJoin:boolean = false
//是否本地导入
isLocalBook:boolean = false
source?:BookSource
sourceList?:BookSource[] = []
}

export class bookShelfImport{
Expand Down
7 changes: 7 additions & 0 deletions entry/src/main/ets/pages/view/BookDetailPage.ets
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,15 @@ struct BookDetailPage {
// FileHandler.readerEpubFile(this.bookData?.bookUrl||'')
this.txtFile.pushAllData(temp)
this.txtFiles = this.txtFile.getListData()
this.initUrlBook()
}
}
initUrlBook(){
let book = this.bookData.bookUrl
console.log(book)
}


@StorageLink('BOOK_IS_BOOK_REFRESHING') isBookRefreshing: number = 0
// 加入书架函数
addBookShelf() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export default struct BookContentRefresh {
this.counter++
this.isRefreshing = false
}, 1000)
showMessage('刷新成功!')
showMessage('刷新成功')
})
}
@Builder
Expand Down
1 change: 0 additions & 1 deletion entry/src/main/ets/pages/view/search/SearchBook.ets
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { SearchDetails } from '../../../componets/search/SearchDetails'
import { SearchRefresh } from '../../../componets/search/SearchRefresh'
import taskSearchBooks from '../../../common/model/taskSearchBook'
import { router } from '@kit.ArkUI'
import { showMessage } from '../../../componets/common/promptShow'

interface routeParams {
searchValue: string,
Expand Down

0 comments on commit c3e1647

Please sign in to comment.