| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- import promptAction from '@ohos.promptAction';
- import { router } from '@kit.ArkUI';
- import { contextConstant } from '@kit.AbilityKit';
- import { fileIo, storageStatistics } from '@kit.CoreFileKit';
- import { BusinessError } from '@kit.BasicServicesKit';
- import { yTToast } from '../../utils/YTToast';
- @Component
- export struct MineTab {
- @State cacheSize: string = '0.00';
- aboutToAppear() {
- // Simulate getting cache size
- this.getCache()
- }
- // 获取应用数据空间大小
- getCache(): string {
- storageStatistics.getCurrentBundleStats((error: BusinessError, bundleStats: storageStatistics.BundleStats) => {
- if (error) {
- } else {
- this.cacheSize = `${(bundleStats.cacheSize / 1024 / 1024).toFixed(2)}MB`
- }
- });
- return this.cacheSize
- }
- clearCacheTask(dir: string): Promise<boolean> {
- return new Promise((resolve) => {
- fileIo.access(dir).then((exist: boolean) => {
- if (exist) {
- fileIo.rmdir(dir)
- }
- resolve(true)
- })
- })
- }
- clearCache() {
- yTToast.alertToast({
- text: '清理缓存',
- message: '确定清理当前应用的所有缓存数据?',
- confirmMessage: '确定',
- cancelMessage: '取消',
- isShowClose: false,
- onConfirm: async () => {
- this.cacheSize = '0.00';
- let context: Context = getContext(this)
- context.getApplicationContext().area = contextConstant.AreaMode.EL1;
- context.area = contextConstant.AreaMode.EL1;
- const el1AppCacheDir = context.getApplicationContext().cacheDir
- const el1HapCacheDir = context.cacheDir
- context.getApplicationContext().area = contextConstant.AreaMode.EL2;
- context.area = contextConstant.AreaMode.EL2;
- const el2AppCacheDir = context.getApplicationContext().cacheDir
- const el2HapCacheDir = context.cacheDir
- const task = [
- this.clearCacheTask(el1AppCacheDir),
- this.clearCacheTask(el1HapCacheDir),
- this.clearCacheTask(el2AppCacheDir),
- this.clearCacheTask(el2HapCacheDir)
- ]
- await Promise.all(task)
- let cache = this.getCache()
- // if (cache == '0.00MB') {
- // promptAction.showToast({ message: '清除完毕' })
- // } else {
- // //未清理完毕,递归
- // await this.clearCache()
- // }
- promptAction.showToast({ message: '清理成功' });
- yTToast.hide()
- },
- onCancel: () => {
- yTToast.hide()
- }
- })
- }
- build() {
- Column() {
- // Top BG (Simulated)
- Stack() {
- Image($r('app.media.logo')) // Assuming logo exists
- .width(118)
- .height(118)
- .borderRadius(12)
- .shadow({ radius: 10, color: '#00000010' })
- }
- .width('100%')
- .height('30%')
- .alignContent(Alignment.Center)
- .backgroundColor('#FBE3EB') // Simple background color
- Column() {
- Text('小易旅记').fontSize(16).fontWeight(FontWeight.Medium).margin({ top: 20, bottom: 40 })
- // Menu Items
- this.MenuItem('用户协议', () => {
- router.pushUrl({
- url: '/pages/UserAgreement'.slice(1)
- })
- })
- this.MenuItem('隐私协议', () => {
- router.pushUrl({
- url: '/pages/Privacy'.slice(1)
- })
- })
- this.MenuItem('意见反馈', () => {
- router.pushUrl({
- url: '/pages/SuggestionPage'.slice(1)
- })
- })
- this.MenuItem('数据缓存', () => {
- this.clearCache()
- }, this.cacheSize + ' MB')
- }
- .width('100%')
- .layoutWeight(1)
- .backgroundColor(Color.White)
- .borderRadius({ topLeft: 30, topRight: 30 })
- .margin({ top: -30 })
- .padding({ top: 30, left: 24, right: 24 })
- }
- .width('100%')
- .height('100%')
- .backgroundColor('#F5F7FA')
- }
- @Builder
- MenuItem(title: string, onClick: () => void, rightText?: string) {
- Row() {
- Text(title).fontSize(14).fontColor('#303030')
- Row() {
- if (rightText) {
- Text(rightText).fontSize(14).fontColor('#070202').margin({ right: 8 })
- }
- Image($r('app.media.right_arrow_icon'))
- .width(24).height(24)
- }
- }
- .width('100%')
- .height(48)
- .onClick(onClick)
- .border({ width: { bottom: 1 }, color: '#F2F2F7' })
- .margin({ bottom: 10 })
- .justifyContent(FlexAlign.SpaceBetween)
- }
- }
|