Skip to content

Commit 0381319

Browse files
authored
Merge pull request #256 from mgz0227/dev_2008
书架书籍改为懒加载,增加页面书籍分页查询,批量管理界面书籍操作页面操作数据100条,新增通用懒加载类,
2 parents 310a76e + 4452d09 commit 0381319

File tree

17 files changed

+418
-187
lines changed

17 files changed

+418
-187
lines changed

entry/src/main/ets/common/LazyIDataSource/BasicDataSource.ets

Lines changed: 132 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,131 @@
33
* @date 2024/5/22 20:10
44
*/
55

6-
export default class BasicDataSource implements IDataSource {
6+
export default class BasicDataSource<T> implements IDataSource {
77
private listeners: DataChangeListener[] = [];
8-
private originDataArray: Array<ESObject> = [];
8+
private originDataArray: T[] = [];
99

10+
//获取数据源总长度
1011
public totalCount(): number {
11-
return 0;
12+
return this.originDataArray.length??0;
1213
}
13-
14-
public getData(index: number): ESObject {
14+
//根据下标获取数据
15+
public getData(index: number): T {
1516
return this.originDataArray[index];
1617
}
1718

19+
// 获取数据源
20+
public getOriginDataArray(): T[] {
21+
return this.originDataArray;
22+
}
23+
24+
//清空数据
25+
public clear(){
26+
this.originDataArray = [];
27+
this.notifyDataReload();
28+
}
29+
30+
//插入指定位置数据
31+
public addSpecifyIndexData(index: number, data: T): void {
32+
if (index < 0 || index > this.originDataArray.length) {
33+
throw new Error("Index out of bounds");
34+
}
35+
this.originDataArray.splice(index, 0, data);
36+
this.notifyDataAdd(index);
37+
}
38+
39+
// 全量数据添加
40+
public pushAllData(data: T[]): void {
41+
for (const d of data) {
42+
this.originDataArray.push(d);
43+
this.notifyDataAdd(this.originDataArray.length - 1);
44+
}
45+
}
46+
// 追加数据
47+
public appendData(data: T): void {
48+
this.originDataArray.push(data);
49+
this.notifyDataAdd(this.originDataArray.length - 1);
50+
}
51+
52+
// 删除指定index的数据
53+
public deleteData(index: number): void {
54+
if (index < 0 || index >= this.originDataArray.length) {
55+
throw new Error("Index out of bounds");
56+
}
57+
this.originDataArray.splice(index, 1);
58+
this.notifyDataDelete(index);
59+
}
60+
61+
// 移动数据到指定index
62+
public moveData(from: number, to: number): void {
63+
if (from < 0 || from >= this.originDataArray.length || to < 0 || to >= this.originDataArray.length) {
64+
throw new Error("Index out of bounds");
65+
}
66+
const data = this.originDataArray[from];
67+
this.originDataArray.splice(from, 1);
68+
this.originDataArray.splice(to, 0, data);
69+
this.notifyDataMoveOperation(from, to);
70+
}
71+
72+
public moveDataUpdate(from: number, to: number,newData:T): void {
73+
if (from < 0 || from >= this.originDataArray.length || to < 0 || to >= this.originDataArray.length) {
74+
throw new Error("Index out of bounds");
75+
}
76+
const data = this.originDataArray[from];
77+
this.originDataArray.splice(from, 1);
78+
this.originDataArray.splice(to, 0, newData);
79+
this.notifyDataMoveOperation(from, to);
80+
}
81+
82+
// 向数组第一个位置插入数据
83+
public unshiftData(data: T): void {
84+
this.originDataArray.unshift(data);
85+
this.notifyDataAdd(0);
86+
}
87+
88+
// 翻转数组
89+
public reverseData(): void {
90+
this.originDataArray.reverse();
91+
this.notifyDataReload();
92+
}
93+
94+
// 数组按照指定的规则排序
95+
public sortData(compareFn: (a: T, b: T) => number): void {
96+
this.originDataArray.sort(compareFn);
97+
this.notifyDataReload();
98+
}
99+
100+
// 追加多条数据
101+
public appendDataList(dataList: T[]): void {
102+
dataList.forEach(data => {
103+
this.appendData(data)
104+
})
105+
this.notifyDataReload();
106+
}
107+
108+
// 插入多条数据
109+
public insertDataList(index: number, dataList: T[]): void {
110+
if (index < 0 || index > this.originDataArray.length) {
111+
throw new Error("Index out of bounds");
112+
}
113+
dataList.forEach(data => {
114+
this.addSpecifyIndexData(index, data)
115+
})
116+
}
117+
118+
// 第 index的元素发生了改变
119+
public changeData(index: number, data: T): void {
120+
this.originDataArray[index] = data;
121+
this.notifyDataChange(index);
122+
}
123+
124+
//获取数据下标
125+
public getIndex(data: T): number {
126+
return this.originDataArray.findIndex((item) => {
127+
return JSON.stringify(item) === JSON.stringify(data);
128+
});
129+
}
130+
18131
// 该方法为框架侧调用,为LazyForEach组件向其数据源处添加listener监听
19132
registerDataChangeListener(listener: DataChangeListener): void {
20133
if (this.listeners.indexOf(listener) < 0) {
@@ -35,28 +148,38 @@ export default class BasicDataSource implements IDataSource {
35148
// 通知LazyForEach组件需要重载所有子组件
36149
notifyDataReload(): void {
37150
this.listeners.forEach(listener => {
38-
listener.onDataReloaded();
151+
listener.onDatasetChange([{type: DataOperationType.RELOAD}]);
39152
})
40153
}
41154

42155
// 通知LazyForEach组件需要在index对应索引处添加子组件
43156
notifyDataAdd(index: number): void {
44157
this.listeners.forEach(listener => {
45-
listener.onDataAdd(index);
158+
listener.onDatasetChange([{type: DataOperationType.ADD, index: index}]);
46159
})
47160
}
48161

49162
// 通知LazyForEach组件在index对应索引处数据有变化,需要重建该子组件
50163
notifyDataChange(index: number): void {
51164
this.listeners.forEach(listener => {
52-
listener.onDataChange(index);
165+
listener.onDatasetChange([{type: DataOperationType.CHANGE,index:index}])
166+
// listener.onDataChange(index);
53167
})
54168
}
55169

170+
56171
// 通知LazyForEach组件需要在index对应索引处删除该子组件
57172
notifyDataDelete(index: number): void {
58173
this.listeners.forEach(listener => {
59-
listener.onDataDelete(index);
174+
listener.onDatasetChange([{type: DataOperationType.DELETE,index:index}])
175+
// listener.onDataDelete(index);
176+
})
177+
}
178+
179+
//移动位置
180+
notifyDataMoveOperation(oldIndex: number, newIndex:number): void {
181+
this.listeners.forEach(listener => {
182+
listener.onDatasetChange([{type: DataOperationType.MOVE,index:{from:oldIndex,to:newIndex}}])
60183
})
61184
}
62185
}

entry/src/main/ets/common/LazyIDataSource/ChaptersDataSource.ets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import { chaptersItem } from '../../componets/dataList/ReaderChaptersItem';
77
import BasicDataSource from './BasicDataSource';
88

9-
export default class ChaptersDataSource extends BasicDataSource{
9+
export default class ChaptersDataSource extends BasicDataSource<chaptersItem>{
1010
private dataArray: chaptersItem[] =[]
1111

1212
public totalCount(): number {

entry/src/main/ets/common/LazyIDataSource/FileListDataSource.ets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import FolderAndFileItem from '../../componets/dataList/FolderAndFileItem';
88
/**
99
* 文件夹 文件列表 数据懒加载对象
1010
* */
11-
export default class FolderFileDataSource extends BasicDataSource {
11+
export default class FolderFileDataSource extends BasicDataSource<FolderAndFileItem> {
1212
private dataArray: FolderAndFileItem[] = [];
1313

1414
public totalCount(): number {

entry/src/main/ets/componets/group/GroupTypeSearch.ets

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export default struct groupTypeSearch {
1010
@State groupList:BookGroups[] = []
1111
@Link bookTypeNumber:number
1212
@Link searchBookList:number[]
13+
@Prop isShowMore:boolean = false
1314
aboutToAppear(): void {
1415
this.getGroupList()
1516
}
@@ -73,6 +74,7 @@ export default struct groupTypeSearch {
7374
).onClick(()=>{
7475
this.groupCoverShow = true
7576
})
77+
.visibility(!this.isShowMore?Visibility.Visible:Visibility.Hidden)
7678
}.margin({ left: 15,bottom:10})
7779
}
7880

entry/src/main/ets/database/dao/BooksDao.ets

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,87 @@ class BooksDao {
8282
}
8383
}
8484

85+
//获取书籍总数量
86+
async getBookCount(param?: BooksTypeSearchParams) {
87+
let type = param?.type;
88+
let bookGroup = param?.bookGroup;
89+
try {
90+
const column: ColumnInfo[] = AppDatabaseUtil.getColumn(this.TABLE_NAME);
91+
const predicates = DbUtil.getPredicates(this.TABLE_NAME);
92+
predicates.equalTo('bookType', type)
93+
if (bookGroup !== undefined && bookGroup !== 2) {
94+
predicates.equalTo('bookGroup', bookGroup)
95+
}
96+
const count = await DbUtil.queryForCount(predicates, column)
97+
return count
98+
} catch (e){
99+
console.log("TagInfo", JSON.stringify(e))
100+
return 0
101+
}
102+
}
103+
104+
async searchPage(param?: BooksTypeSearchParams, pageNumber: number = 1, pageSize: number = 21) {
105+
let searchKey = param?.searchKey ?? '';
106+
let type = param?.type;
107+
let bookGroup = param?.bookGroup;
108+
let order = param?.order;
109+
let sort = 0;
110+
if (order !== undefined) {
111+
sort = booksUtils.getOrder(order);
112+
}
113+
114+
try {
115+
const column: ColumnInfo[] = AppDatabaseUtil.getColumn(this.TABLE_NAME);
116+
let sql = `SELECT * FROM ${this.TABLE_NAME}`;
117+
let whereClause: string[] = [];
118+
119+
if (type !== undefined) {
120+
whereClause.push(`bookType = ${type}`);
121+
}
122+
if (bookGroup !== undefined && bookGroup !== 2) {
123+
whereClause.push(`bookGroup = ${bookGroup}`);
124+
}
125+
if (searchKey) {
126+
whereClause.push(`(
127+
bookName LIKE '%${searchKey}%'
128+
OR author LIKE '%${searchKey}%'
129+
OR originName LIKE '%${searchKey}%'
130+
)`);
131+
}
132+
133+
if (whereClause.length > 0) {
134+
sql += ` WHERE ${whereClause.join(' AND ')}`;
135+
}
136+
137+
sql += ` ORDER BY`;
138+
if (order !== undefined) {
139+
switch (sort) {
140+
case 0:
141+
sql += ` isTop DESC, durChapterTime DESC`;
142+
break;
143+
case 1:
144+
sql += ` isTop DESC, latestChapterTime DESC`;
145+
break;
146+
case 2:
147+
sql += ` isTop DESC, sort ASC`;
148+
break;
149+
}
150+
} else {
151+
sql += ` isTop DESC, createTime DESC`;
152+
}
153+
154+
const offset = (pageNumber - 1) * pageSize;
155+
sql += ` LIMIT ${pageSize} OFFSET ${offset}`;
156+
157+
const bookDbList = await DbUtil.querySqlForList<Books>(sql, column);
158+
console.log("TagInfo", '书籍:' + bookDbList.length)
159+
return bookDbList;
160+
} catch (err) {
161+
console.log("TagInfo", JSON.stringify(err))
162+
return [];
163+
}
164+
}
165+
85166
async batchInsert(books: Books[]) {
86167
try {
87168
for (let index = 0; index < books.length; index++) {

entry/src/main/ets/pages/view/BookDetailPage.ets

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ struct BookDetailPage {
175175
})
176176

177177
}
178+
.chainAnimation(true)
179+
.edgeEffect(EdgeEffect.Fade)
178180
.divider({
179181
strokeWidth: 0.8,
180182
color: '#cccccc',

entry/src/main/ets/pages/view/bookShelf/IndexShelf.ets

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export default struct IndexShelf {
3737
builder: Dialog({
3838
title: '网址导入',
3939
child: () => {
40-
this.DialogInput("请输入书籍目录或详情页", 10)
40+
this.DialogInput("请输入书籍目录或详情页")
4141
}
4242
}),
4343
cornerRadius: 8,
@@ -268,6 +268,9 @@ export default struct IndexShelf {
268268
showMessage(this.bookImport.length > 0?`相关书单书籍内容导入${this.bookImport.length}本`:'未找到相关书单书籍内容')
269269
}
270270
})
271+
setTimeout(()=>{
272+
this.isBookRefreshing++
273+
},500)
271274
break
272275
}
273276
})
@@ -308,11 +311,11 @@ export default struct IndexShelf {
308311
}
309312

310313
@Builder
311-
DialogInput(placeholder: string, maxlength: number) {
314+
DialogInput(placeholder: string, maxlength?: number) {
312315
Row() {
313316
TextInput({ placeholder: placeholder })
314-
.maxLength(maxlength)
315-
.showCounter(true)
317+
// .maxLength(maxlength)
318+
// .showCounter(true)
316319
.cancelButton({
317320
icon: {
318321
src: $r('app.media.close')

0 commit comments

Comments
 (0)