Skip to content

Commit 92bc00f

Browse files
committed
数据持久化动态更新页面数据
数据持久化动态更新页面数据
1 parent eb5e36c commit 92bc00f

File tree

9 files changed

+176
-150
lines changed

9 files changed

+176
-150
lines changed

entry/src/main/ets/common/constants/CommonConstants.ets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ export default class CommonConstants {
221221
/**
222222
* 小说AppStorage存储key
223223
*/
224+
static readonly PREFERENCES_BOOK_TYPE_NUMBER:string = 'PREFERENCES_BOOK_TYPE_NUMBER'
224225
static readonly PREFERENCES_BOOK_INIT_KEY:string = 'PREFERENCES_BOOK_INIT_KEY'
225226
static readonly PREFERENCES_BOOK_DATA_KEY:string = 'PREFERENCES_BOOK_DATA_KEY'
226227
static readonly KEY_NOVEL_BOOK_DATA:string = 'KEY_NOVEL_BOOK_DATA'
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import CommonConstants from '../constants/CommonConstants';
2+
import dataPreferences from '@ohos.data.preferences';
3+
4+
// 获取context
5+
let context = getContext(this);
6+
export default class bookDataPreferences {
7+
8+
static shared = new bookDataPreferences();
9+
preferences?: dataPreferences.Preferences;
10+
preferencesName: string = CommonConstants.PREFERENCES_BOOK_DATA_KEY;
11+
12+
// 初始化preferences实例
13+
initPreferences() {
14+
this.preferences = dataPreferences.getPreferencesSync(context, { name: this.preferencesName });
15+
}
16+
17+
// 设置数据
18+
set(key: string, value: dataPreferences.ValueType) {
19+
if (!this.preferences) {
20+
this.initPreferences();
21+
}
22+
console.log('setinitPreferences', key, value)
23+
this.preferences?.putSync(key, value);
24+
this.preferences?.flush();
25+
}
26+
27+
// 获取数据
28+
get(key: string): dataPreferences.ValueType | null | undefined {
29+
if (!this.preferences) {
30+
this.initPreferences();
31+
}
32+
let value = this.preferences?.getSync(key, null);;
33+
return value;
34+
}
35+
36+
// 删除数据
37+
delete(key: string) {
38+
if (!this.preferences) {
39+
this.initPreferences();
40+
}
41+
if (this.preferences?.hasSync(key)) {
42+
this.preferences.deleteSync(key);
43+
this.preferences.flush();
44+
}
45+
}
46+
}
47+

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import ShelfBookList from './Shelf/ShelfBookList'
1414
import picker from '@ohos.file.picker'
1515
import { FileHandler } from 'ets/common/utils/FileHandler'
1616
import RefreshComponent from '../../../componets/common/RefreshComponent'
17+
import CommonConstants from '../../../common/constants/CommonConstants'
1718

1819
@Component
1920
export default struct IndexShelf {

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import BookInfo from './BookInfo'
33
import { promptAction } from '@kit.ArkUI'
44
import BookInfoGrid from './BookInfoGrid'
55
import CommonConstants from '../../../common/constants/CommonConstants'
6-
import { delBookListAll, delBookListByBookId, getBookData } from '../../../storage/bookListData'
6+
import { delBookListAll, delBookListByBookId} from '../../../storage/bookListData'
7+
import { updateShelfBookData } from '../../../preferences/bookDataPreferences'
78

89
/**
910
* 漫画
@@ -15,11 +16,11 @@ export default struct cartoonPage{
1516
@Prop currentIndex:number
1617
@State lastIndex:string = '9999'
1718
@Prop @Watch('onIsEXHIBITChange') EXHIBIT:string
19+
@StorageLink(CommonConstants.KEY_CARTOON_BOOK_DATA)
20+
@Watch('updatePreferences')cartoonList:BookList[] = []
1821

19-
@State cartoonList:BookList[] = []
20-
21-
aboutToAppear(): void {
22-
this.cartoonList = getBookData(1) as BookList[] //初始化数据
22+
updatePreferences(){
23+
updateShelfBookData(this.cartoonList, 0)
2324
}
2425

2526
onIsClearChange() {

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

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import BookInfo from './BookInfo'
33
import { promptAction } from '@kit.ArkUI'
44
import BookInfoGrid from './BookInfoGrid'
55
import CommonConstants from '../../../common/constants/CommonConstants'
6-
import { delBookListAll, delBookListByBookId, getBookData } from '../../../storage/bookListData'
6+
import { delBookListAll, delBookListByBookId } from '../../../storage/bookListData'
7+
import { updateShelfBookData } from '../../../preferences/bookDataPreferences'
78

89

910
@Component
@@ -15,12 +16,10 @@ export default struct novelPage{
1516
@Prop bookType:string
1617
@Prop @Watch('onIsEXHIBITChange') EXHIBIT:string
1718

18-
@State bookList:BookList[] = []
19-
20-
aboutToAppear(): void {
21-
this.bookList = getBookData(0) as BookList[] //初始化数据
22-
console.log('test----------')
23-
console.log(JSON.stringify(this.bookList))
19+
@StorageLink(CommonConstants.KEY_NOVEL_BOOK_DATA)
20+
@Watch('updatePreferences')bookList:BookList[] = []
21+
updatePreferences(){
22+
updateShelfBookData(this.bookList, 0)
2423
}
2524

2625
//监听isClear变化
@@ -35,25 +34,6 @@ export default struct novelPage{
3534
}
3635
}
3736

38-
// isChangeManageDelete() {
39-
// if (this.manageDelete) {
40-
// if (this.checkBookList === null || this.checkBookList.length === 0) {
41-
// promptAction.showToast({
42-
// message: '请选择要删除的内容',
43-
// duration: 1000,
44-
// })
45-
// this.manageDelete = false
46-
// return
47-
// }
48-
// this.bookList = []
49-
// console.log('删除')
50-
// this.manageDelete = false
51-
// }
52-
// }
53-
// isChangeBookList() {
54-
// console.log(this.checkBookList + '')
55-
// }
56-
5737
//监听EXHIBIT变化
5838
onIsEXHIBITChange() {
5939
console.log(this.EXHIBIT)

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import BookInfo from './BookInfo'
33
import { promptAction } from '@kit.ArkUI'
44
import BookInfoGrid from './BookInfoGrid'
55
import CommonConstants from '../../../common/constants/CommonConstants'
6-
import { delBookListAll, delBookListByBookId, getBookData } from '../../../storage/bookListData'
6+
import { delBookListAll, delBookListByBookId } from '../../../storage/bookListData'
7+
import { updateShelfBookData } from '../../../preferences/bookDataPreferences'
78

89
/**
910
* 有声书
@@ -16,11 +17,13 @@ export default struct soundPage{
1617
@State lastIndex:string = '9999'
1718
@Prop @Watch('onIsEXHIBITChange') EXHIBIT:string
1819

19-
soundList:BookList[] = []
20+
@StorageLink(CommonConstants.KEY_SOUND_BOOK_DATA)
21+
@Watch('updatePreferences')soundList:BookList[] = []
2022

21-
aboutToAppear(): void {
22-
this.soundList = getBookData(2) as BookList[] //初始化数据
23+
updatePreferences(){
24+
updateShelfBookData(this.soundList, 0)
2325
}
26+
2427
//监听isClear变化
2528
onIsClearChange() {
2629
if (this.currentIndex === 2 && this.isClear) {
Lines changed: 65 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,73 @@
1-
import CommonConstants from '../common/constants/CommonConstants';
2-
import dataPreferences from '@ohos.data.preferences';
1+
import CommonConstants from '../common/constants/CommonConstants'
2+
import { BookList } from '../componets/dataList/bookList'
3+
import bookDataPreferences from '../common/utils/DataPreferencesUtils'
34

4-
// 获取context
5-
let context = getContext(this);
6-
export default class bookDataPreferences {
5+
/**
6+
* 初始化存放书本内容
7+
*/
8+
let KEY = ''
79

8-
static shared = new bookDataPreferences();
9-
preferences?: dataPreferences.Preferences;
10-
preferencesName: string = CommonConstants.PREFERENCES_BOOK_DATA_KEY;
10+
export const novelListData:BookList[] = [
11+
new BookList('1','仙人消失之后','会说话的肘子·未读过1','更新至·第340章 镇压',$r('app.media.cover_list'),'测试作者','玄幻')
12+
,new BookList('2','仙人消失之后','会说话的肘子·未读过2','更新至·第340章 镇压',$r('app.media.cover_list'),'测试作者','玄幻')
13+
]
1114

12-
// 初始化preferences实例
13-
initPreferences() {
14-
this.preferences = dataPreferences.getPreferencesSync(context, { name: this.preferencesName });
15-
}
15+
const soundListData:BookList[] = [
16+
new BookList('1','三体广播据','729声工厂 · 第一季 第五集 红岸基地','共101集 · 最终季 最终集 回归大宇宙',$r('app.media.cover_list2'),'测试作者','玄幻'),
17+
new BookList('2','三体广播据','729声工厂 · 第一季 第五集 红岸基地','共101集 · 最终季 最终集 回归大宇宙',$r('app.media.cover_list2'),'测试作者','玄幻'),
18+
new BookList('3','三体广播据','729声工厂 · 第一季 第五集 红岸基地','共101集 · 最终季 最终集 回归大宇宙',$r('app.media.cover_list2'),'测试作者','玄幻'),
19+
new BookList('4','三体广播据','729声工厂 · 第一季 第五集 红岸基地','共101集 · 最终季 最终集 回归大宇宙',$r('app.media.cover_list2'),'测试作者','玄幻'),
20+
new BookList('5','三体广播据','729声工厂 · 第一季 第五集 红岸基地','共101集 · 最终季 最终集 回归大宇宙',$r('app.media.cover_list2'),'测试作者','玄幻'),
21+
new BookList('6','三体广播据','729声工厂 · 第一季 第五集 红岸基地','共101集 · 最终季 最终集 回归大宇宙',$r('app.media.cover_list2'),'测试作者','玄幻'),
22+
new BookList('7','三体广播据','729声工厂 · 第一季 第五集 红岸基地','共101集 · 最终季 最终集 回归大宇宙',$r('app.media.cover_list2'),'测试作者','玄幻'),
23+
new BookList('8','三体广播据','729声工厂 · 第一季 第五集 红岸基地','共101集 · 最终季 最终集 回归大宇宙',$r('app.media.cover_list2'),'测试作者','玄幻'),
24+
new BookList('9','三体广播据','729声工厂 · 第一季 第五集 红岸基地','共101集 · 最终季 最终集 回归大宇宙',$r('app.media.cover_list2'),'测试作者','玄幻'),
25+
new BookList('10','三体广播据','729声工厂 · 第一季 第五集 红岸基地','共101集 · 最终季 最终集 回归大宇宙',$r('app.media.cover_list2'),'测试作者','玄幻')]
1626

17-
// 设置数据
18-
set(key: string, value: dataPreferences.ValueType) {
19-
if (!this.preferences) {
20-
this.initPreferences();
21-
}
22-
console.log('setinitPreferences', key, value)
23-
this.preferences?.putSync(key, value);
24-
this.preferences?.flush();
25-
}
27+
const cartoonListData:BookList[] = [
28+
new BookList('1','极主夫道','会说话的肘子·未读过1','更新至·第340章 镇压',$r('app.media.cover_list1'),'测试作者','玄幻')
29+
,new BookList('2','极主夫道','会说话的肘子·未读过2','更新至·第340章 镇压',$r('app.media.cover_list1'),'测试作者','玄幻')
30+
,new BookList('3','极主夫道','会说话的肘子·未读过3','更新至·第340章 镇压',$r('app.media.cover_list1'),'测试作者','玄幻')
31+
,new BookList('4','极主夫道','会说话的肘子·未读过4','更新至·第340章 镇压',$r('app.media.cover_list1'),'测试作者','玄幻')
32+
,new BookList('5','极主夫道','会说话的肘子·未读过5','更新至·第340章 镇压',$r('app.media.cover_list1'),'测试作者','玄幻')
33+
,new BookList('6','极主夫道','会说话的肘子·未读过6','更新至·第340章 镇压',$r('app.media.cover_list1'),'测试作者','玄幻')
34+
,new BookList('7','极主夫道','会说话的肘子·未读过7','更新至·第340章 镇压',$r('app.media.cover_list1'),'测试作者','玄幻')
35+
,new BookList('8','极主夫道','会说话的肘子·未读过8','更新至·第340章 镇压',$r('app.media.cover_list1'),'测试作者','玄幻')
36+
,new BookList('9','极主夫道','会说话的肘子·未读过9','更新至·第340章 镇压',$r('app.media.cover_list1'),'测试作者','玄幻')
37+
,new BookList('10','极主夫道','会说话的肘子·未读过10','更新至·第340章 镇压',$r('app.media.cover_list1'),'测试作者','玄幻')
38+
]
2639

27-
// 获取数据
28-
get(key: string): dataPreferences.ValueType | null | undefined {
29-
if (!this.preferences) {
30-
this.initPreferences();
31-
}
32-
let value = this.preferences?.getSync(key, null);;
33-
return value;
34-
}
3540

36-
// 删除数据
37-
delete(key: string) {
38-
if (!this.preferences) {
39-
this.initPreferences();
40-
}
41-
if (this.preferences?.hasSync(key)) {
42-
this.preferences.deleteSync(key);
43-
this.preferences.flush();
44-
}
45-
}
41+
42+
export const initBookPreferences = ()=>{
43+
//内容数据持久化
44+
bookDataPreferences.shared.set(CommonConstants.PREFERENCES_BOOK_INIT_KEY,true)
45+
bookDataPreferences.shared.set(CommonConstants.KEY_NOVEL_BOOK_DATA,novelListData)
46+
bookDataPreferences.shared.set(CommonConstants.KEY_SOUND_BOOK_DATA,soundListData)
47+
bookDataPreferences.shared.set(CommonConstants.KEY_CARTOON_BOOK_DATA,cartoonListData)
48+
}
49+
/**
50+
* 统一更新持久化
51+
* bookDate 书籍更新内容
52+
* bookType 书籍类型Key
53+
*/
54+
export const updateShelfBookData = (bookDate:BookList[], bookType:number) =>{
55+
shelfBookType(bookType)
56+
bookDataPreferences.shared.set(KEY,bookDate)
57+
}
58+
59+
//获取书架持久化数据
60+
export const getBookData = (shelfTye:number)=>{
61+
shelfBookType(shelfTye)
62+
return bookDataPreferences.shared.get(KEY) as BookList[];
4663
}
4764

65+
function shelfBookType(shelfTye:number){
66+
if (shelfTye === 0) {
67+
KEY = CommonConstants.KEY_NOVEL_BOOK_DATA
68+
} else if (shelfTye === 1) {
69+
KEY = CommonConstants.KEY_CARTOON_BOOK_DATA
70+
} else if (shelfTye === 2) {
71+
KEY = CommonConstants.KEY_SOUND_BOOK_DATA
72+
}
73+
}

entry/src/main/ets/storage/appData.ets

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import CommonConstants from '../common/constants/CommonConstants'
22
import { showMessage } from '../componets/common/promptShow'
3-
import bookDataPreferences from '../preferences/bookDataPreferences'
3+
import bookDataPreferences from '../common/utils/DataPreferencesUtils'
44
import { initBookListData } from './bookListData'
55
import { groupInitAppData } from './groupData'
66
import { shelfInitAppData } from './shelfListData'
7+
import { initBookPreferences } from '../preferences/bookDataPreferences'
78

89
let APP_INDEX_SCROLLABLE = 'APP_INDEX_SCROLLABLE'
910

@@ -13,11 +14,12 @@ export const initAppData = ()=>{
1314
AppStorage.setOrCreate(APP_INDEX_SCROLLABLE,APP_INDEX_SCROLLABLE_DATA)
1415
shelfInitAppData()
1516
if (!bookDataPreferences.shared.get(CommonConstants.PREFERENCES_BOOK_INIT_KEY) as boolean) {
16-
initBookListData()
17+
initBookPreferences()
1718
showMessage(`初始化成功${CommonConstants.PREFERENCES_BOOK_DATA_KEY} ${bookDataPreferences.shared.get(CommonConstants.PREFERENCES_BOOK_INIT_KEY) as boolean}`)
1819
} else {
1920
showMessage(`已经初始化过${CommonConstants.PREFERENCES_BOOK_DATA_KEY} ${bookDataPreferences.shared.get(CommonConstants.PREFERENCES_BOOK_INIT_KEY) as boolean}`)
2021
}
22+
initBookListData()
2123
groupInitAppData()
2224
}
2325

0 commit comments

Comments
 (0)