MineTab.ets 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import promptAction from '@ohos.promptAction';
  2. import { router } from '@kit.ArkUI';
  3. import { contextConstant } from '@kit.AbilityKit';
  4. import { fileIo, storageStatistics } from '@kit.CoreFileKit';
  5. import { BusinessError } from '@kit.BasicServicesKit';
  6. import { yTToast } from '../../utils/YTToast';
  7. @Component
  8. export struct MineTab {
  9. @State cacheSize: string = '0.00';
  10. aboutToAppear() {
  11. // Simulate getting cache size
  12. this.getCache()
  13. }
  14. // 获取应用数据空间大小
  15. getCache(): string {
  16. storageStatistics.getCurrentBundleStats((error: BusinessError, bundleStats: storageStatistics.BundleStats) => {
  17. if (error) {
  18. } else {
  19. this.cacheSize = `${(bundleStats.cacheSize / 1024 / 1024).toFixed(2)}MB`
  20. }
  21. });
  22. return this.cacheSize
  23. }
  24. clearCacheTask(dir: string): Promise<boolean> {
  25. return new Promise((resolve) => {
  26. fileIo.access(dir).then((exist: boolean) => {
  27. if (exist) {
  28. fileIo.rmdir(dir)
  29. }
  30. resolve(true)
  31. })
  32. })
  33. }
  34. clearCache() {
  35. yTToast.alertToast({
  36. text: '清理缓存',
  37. message: '确定清理当前应用的所有缓存数据?',
  38. confirmMessage: '确定',
  39. cancelMessage: '取消',
  40. isShowClose: false,
  41. onConfirm: async () => {
  42. this.cacheSize = '0.00';
  43. let context: Context = getContext(this)
  44. context.getApplicationContext().area = contextConstant.AreaMode.EL1;
  45. context.area = contextConstant.AreaMode.EL1;
  46. const el1AppCacheDir = context.getApplicationContext().cacheDir
  47. const el1HapCacheDir = context.cacheDir
  48. context.getApplicationContext().area = contextConstant.AreaMode.EL2;
  49. context.area = contextConstant.AreaMode.EL2;
  50. const el2AppCacheDir = context.getApplicationContext().cacheDir
  51. const el2HapCacheDir = context.cacheDir
  52. const task = [
  53. this.clearCacheTask(el1AppCacheDir),
  54. this.clearCacheTask(el1HapCacheDir),
  55. this.clearCacheTask(el2AppCacheDir),
  56. this.clearCacheTask(el2HapCacheDir)
  57. ]
  58. await Promise.all(task)
  59. let cache = this.getCache()
  60. // if (cache == '0.00MB') {
  61. // promptAction.showToast({ message: '清除完毕' })
  62. // } else {
  63. // //未清理完毕,递归
  64. // await this.clearCache()
  65. // }
  66. promptAction.showToast({ message: '清理成功' });
  67. yTToast.hide()
  68. },
  69. onCancel: () => {
  70. yTToast.hide()
  71. }
  72. })
  73. }
  74. build() {
  75. Column() {
  76. // Top BG (Simulated)
  77. Stack() {
  78. Image($r('app.media.logo')) // Assuming logo exists
  79. .width(118)
  80. .height(118)
  81. .borderRadius(12)
  82. .shadow({ radius: 10, color: '#00000010' })
  83. }
  84. .width('100%')
  85. .height('30%')
  86. .alignContent(Alignment.Center)
  87. .backgroundColor('#FBE3EB') // Simple background color
  88. Column() {
  89. Text('小易旅记').fontSize(16).fontWeight(FontWeight.Medium).margin({ top: 20, bottom: 40 })
  90. // Menu Items
  91. this.MenuItem('用户协议', () => {
  92. router.pushUrl({
  93. url: '/pages/UserAgreement'.slice(1)
  94. })
  95. })
  96. this.MenuItem('隐私协议', () => {
  97. router.pushUrl({
  98. url: '/pages/Privacy'.slice(1)
  99. })
  100. })
  101. this.MenuItem('意见反馈', () => {
  102. router.pushUrl({
  103. url: '/pages/SuggestionPage'.slice(1)
  104. })
  105. })
  106. this.MenuItem('数据缓存', () => {
  107. this.clearCache()
  108. }, this.cacheSize + ' MB')
  109. }
  110. .width('100%')
  111. .layoutWeight(1)
  112. .backgroundColor(Color.White)
  113. .borderRadius({ topLeft: 30, topRight: 30 })
  114. .margin({ top: -30 })
  115. .padding({ top: 30, left: 24, right: 24 })
  116. }
  117. .width('100%')
  118. .height('100%')
  119. .backgroundColor('#F5F7FA')
  120. }
  121. @Builder
  122. MenuItem(title: string, onClick: () => void, rightText?: string) {
  123. Row() {
  124. Text(title).fontSize(14).fontColor('#303030')
  125. Row() {
  126. if (rightText) {
  127. Text(rightText).fontSize(14).fontColor('#070202').margin({ right: 8 })
  128. }
  129. Image($r('app.media.right_arrow_icon'))
  130. .width(24).height(24)
  131. }
  132. }
  133. .width('100%')
  134. .height(48)
  135. .onClick(onClick)
  136. .border({ width: { bottom: 1 }, color: '#F2F2F7' })
  137. .margin({ bottom: 10 })
  138. .justifyContent(FlexAlign.SpaceBetween)
  139. }
  140. }