MineSettingComponent.ets
10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import { BottomNavi, CommonConstants, SpConstants } from 'wdConstant';
import { Logger, SPHelper, StringUtils } from 'wdKit';
import PageViewModel from '../../viewmodel/PageViewModel';
import storageStatistics from "@ohos.file.storageStatistics";
import { BusinessError } from '@ohos.base';
import router from '@ohos.router';
import { WDRouterPage, WDRouterRule } from 'wdRouter';
import { Params } from 'wdBean';
// import { common } from '@kit.AbilityKit';
import fs from '@ohos.file.fs';
import { CustomCacheDialog } from './CustomCacheDialog';
import MineSettingDatasModel from '../../model/MineSettingDatasModel';
import { MineMainSettingFunctionItem } from '../../viewmodel/MineMainSettingFunctionItem';
import common from '@ohos.app.ability.common';
import dataPreferences from '@ohos.data.preferences';
import { TitleBackComponent } from './TitleBackComponent';
@Component
export struct MineSettingComponent {
// @State listData: Array<string | Array<string>> = new Array();
@State listData: Array<MineMainSettingFunctionItem> = new Array;
@State privacySwitch: boolean = false
@State cacheSize: number = 0
@State accountState:boolean=false
preferences: dataPreferences.Preferences | null = null;
dialogController: CustomDialogController = new CustomDialogController({
builder: CustomCacheDialog({
cancel: () => {
},
confirm: () => {
this.deleteCache()
}
}),
customStyle: true,
alignment: DialogAlignment.Center
})
aboutToAppear() {
// 获取设置页面数据
this.getSettingPageData()
this.getCacheSize()
this.getAccountState()
this.addLoginStatusObserver()
}
async getAccountState(){
let userId=await SPHelper.default.get(SpConstants.USER_ID,'') as string
if(userId==''){
this.accountState=false
}else {
this.accountState=true
}
}
async getSettingPageData() {
let oldList = MineSettingDatasModel.getMineMainSettingFunctionItemData();
let userId=await SPHelper.default.get(SpConstants.USER_ID,'') as string
if(userId==''){
this.listData=oldList.slice(1,oldList.length)
}else {
this.listData = oldList;
}
}
build() {
// Navigation() {
// //滑动区域
// this.settingList()
// }.titleMode(NavigationTitleMode.Mini)
// .title('设置')
Column(){
TitleBackComponent({title:"设置"})
this.settingList()
}
}
// // 页面布局
// @Builder settingList() {
// Column() {
// List() {
// // 循环渲染ListItemGroup,contactsGroups为多个分组联系人contacts和标题title的数据集合
// ForEach(this.listData, (item: Array<string>, index: number) => {
// ListItemGroup({ header: index === 0 ? this.itemHead("") : this.itemHead("1") }) {
// // 循环渲染ListItem
// ForEach(item, (subItem: string, subIndex: number) => {
// ListItem() {
// if (subIndex == 6) {
// this.getArrowCell(subItem, subIndex, index)
// } else if (subIndex == 1 || subIndex == 3 || subIndex == 4 || subIndex == 5) {
// if (subIndex == 1 && index == 1) {
// this.getArrowCell(subItem, subIndex, index)
// } else {
// this.getSwitchCell(subItem, subIndex)
// }
//
// } else {
// this.getArrowCell(subItem, subIndex, index)
// }
//
// }.padding({ left: '27lpx' })
// .onClick(() => {
//
// // // 在Home页面中
// // let paramsInfo: object = {
// // pageId: 123,
// // pageType:'AccountAndSecurityLayout'
// // };
// console.log(subIndex + "")
// if (subIndex == 0 && index == 0) {
//
// let params: Params = {
// pageID: 'AccountAndSecurityLayout'
// }
// WDRouterRule.jumpWithPage(WDRouterPage.settingPage, params)
// }else if (subIndex == 2 && index == 0) {
// WDRouterRule.jumpWithPage(WDRouterPage.privacySettingPage)
// }else if (subIndex == 0 && index == 1) {
// this.dialogController.open()
// }
//
// })
// .height('117lpx')
// })
// }
// .divider({
// strokeWidth: 1,
// startMargin: 15,
// endMargin: 10,
// color: '#f0f0f0'
// })
// })
// }.onScrollFrameBegin((offset, state) => {
// return { offsetRemain: 0 }
// })
// }
// .backgroundColor(Color.White)
// .borderRadius(8)
// }
// 页面布局
@Builder settingList() {
Column() {
List() {
// 循环渲染ListItemGroup,contactsGroups为多个分组联系人contacts和标题title的数据集合
ForEach(this.listData, (item: MineMainSettingFunctionItem, index: number) => {
ListItem() {
if (item.type == 0) {
Column() {
this.getArrowCell(item,index)
}.padding({ left: '27lpx' }).height('117lpx').justifyContent(FlexAlign.Center)
} else if (item.type == 1) {
Column() {
this.getSwitchCell(item,index)
}.padding({ left: '27lpx' }).height('117lpx').justifyContent(FlexAlign.Center)
} else {
Column().width('100%').height('15lpx').backgroundColor(0xf0f0f0)
}
}
})
}.onScrollFrameBegin((offset, state) => {
return { offsetRemain: 0 }
}).divider({strokeWidth:1,color:"#f0f0f0",startMargin: 15,endMargin: 10})
}
.backgroundColor(Color.White)
.borderRadius(8)
}
@Builder itemHead(text: string) {
// 列表分组的头部组件,对应联系人分组A、B等位置的组件
if (text.length > 0) {
Row().width('100%').height('20lpx').backgroundColor(0xf0f0f0)
}
}
// 右侧开关cell
@Builder getSwitchCell(item:MineMainSettingFunctionItem, index:number) {
Column() {
Row() {
// 左侧logo和标题
Row() {
// 判断有没有图片
if (0) {
Image('https://pic.rmb.bdstatic.com/e182cf67c341d1128d2a6cc05886bf62.jpeg@s_0,h_2000')
.height('38lpx')
.margin({ right: '5lpx' })
Text(`${item.title}`)
.margin({ top: '8lpx' })
.height('38lpx')
.fontColor('#333333')
.fontSize('29lpx')
} else {
Text(`${item.title}`)
.margin({ top: '8lpx' })
.height('38lpx')
.fontColor('#333333')
.fontSize('29lpx')
}
}.width('60%')
// 右侧文案和右箭头
Row() {
Toggle({ type: ToggleType.Switch, isOn: item.switchState })
.height('50lpx')
.margin({ left: '81lpx', right: '29lpx' })
.selectedColor("#ED2800")
.onChange((isOn: boolean) => {
if(item.itemType=='push_switch'){
//推送
SPHelper.default.save(SpConstants.SETTING_PUSH_SWITCH,isOn)
}else if(item.itemType=='wifi_switch'){
//wifi 图片
SPHelper.default.save(SpConstants.SETTING_WIFI_IMAGE_SWITCH,isOn)
}else if(item.itemType=='video_switch'){
//wifi 视频
SPHelper.default.save(SpConstants.SETTING_WIFI_VIDEO_SWITCH,isOn)
}else if(item.itemType=='suspensionState_switch'){
//悬浮窗
SPHelper.default.save(SpConstants.SETTING_SUSPENSION_SWITCH,isOn)
}
})
}.width('40%')
.margin({ right: '29lpx' })
.justifyContent(FlexAlign.End)
}
.alignItems(VerticalAlign.Center)
.justifyContent(FlexAlign.SpaceBetween)
}.height('54lpx')
}
// 右文字+箭头cell
@Builder getArrowCell(item:MineMainSettingFunctionItem, index:number) {
Column() {
Row() {
// 左侧logo和标题
Row() {
// 判断有没有图片
// if (this.privacySwitch) {
// Image('https://pic.rmb.bdstatic.com/e182cf67c341d1128d2a6cc05886bf62.jpeg@s_0,h_2000')
// .height('38lpx')
// .margin({ right: '5lpx' })
// }
Text(`${item.title}`)
.margin({ top: '8lpx' })
.height('38lpx')
.fontColor('#333333')
.fontSize('29lpx')
}.width('60%')
// 右侧文案和右箭头
Row() {
Text((item.itemType=='clear_cache') ? this.cacheSize.toFixed(2) + 'MB' : '')
.fontColor('#999999')
.maxLines(1)
Image($r('app.media.mine_user_arrow'))
.width('27lpx')
.height('27lpx')
.objectFit(ImageFit.Auto)
Column().width('29lpx')
}.width('40%')
.margin({ right: '29lpx' })
.justifyContent(FlexAlign.End)
}
.alignItems(VerticalAlign.Center)
.justifyContent(FlexAlign.SpaceBetween)
}
.height('54lpx')
.onClick(() => {
if (item.itemType == 'account') {
let params: Params = {
pageID: 'AccountAndSecurityLayout'
}
WDRouterRule.jumpWithPage(WDRouterPage.settingPage, params)
} else if (item.itemType == 'private_setting') {
WDRouterRule.jumpWithPage(WDRouterPage.privacySettingPage)
} else if (item.itemType == 'clear_cache') {
this.dialogController.open()
}
})
}
//删除缓存
deleteCache() {
let context = getContext(this) as common.UIAbilityContext
let application = context.getApplicationContext();
let appPath = application.cacheDir
Logger.debug("MineSettingComponent", appPath)
fs.rmdir(appPath).then(() => {
this.getCacheSize()
})
let path = context.cacheDir
Logger.debug("MineSettingComponent", path)
fs.rmdir(path).then(() => {
this.getCacheSize()
})
}
getCacheSize() {
// 获取缓存数据
storageStatistics.getCurrentBundleStats((err: BusinessError, bundleStats: storageStatistics.BundleStats) => {
if (err) {
console.error(`Invoke getCurrentBundleStats failed, code is ${err.code}, message is ${err.message}`);
} else {
this.cacheSize = bundleStats.cacheSize / 1024.00 / 1024.00;
}
});
}
async addLoginStatusObserver(){
this.preferences = await SPHelper.default.getPreferences();
let observer = (key: string) => {
if(key == SpConstants.USER_ID){
this.getSettingPageData()
}
}
this.preferences.on('change', observer);
}
}