Răsfoiți Sursa

feat(LocalMusic): 优化字母索引功能

- 添加 alphaBetCacheKey 缓存机制避免重复计算
- 实现 refreshAlphaBetFromMediaLibrary 方法从媒体库刷新字母索引
- 在数据量超过40000时清空字母索引提升性能
- 根据排序类型和显示模式动态生成字母索引列表
- 添加缓存键值比较机制提高索引生成效率
- 修复在特定模式下字母索引未正确刷新的问题
chendeben 7 luni în urmă
părinte
comite
b8d234f9fa
1 a modificat fișierele cu 50 adăugiri și 0 ștergeri
  1. 50 0
      entry/src/main/ets/view/LocalMusic.ets

+ 50 - 0
entry/src/main/ets/view/LocalMusic.ets

@@ -313,6 +313,7 @@ export struct LocalMusic {
   @State alphabetHeight: number = 0
   public enableShowAlphabet: boolean = true  // 是否显示字母表
   private alphaBetHideTimer: number = 0
+  private alphaBetCacheKey: string = ''
   @State isShowPrecious: boolean = false //播控条显示上一首按钮
   //背景两侧流光控制器
   @State sceneBgController: HdsSceneController|undefined = deviceInfo.sdkApiVersion>=20
@@ -3411,6 +3412,10 @@ export struct LocalMusic {
     if(!this.showZMIndex){
       return
     }
+    if (this.modeType === 1 && this.totalCount > this.videoLocalList.length) {
+      await this.refreshAlphaBetFromMediaLibrary()
+      return
+    }
     return new Promise<void>((resolve,reject) => {
       try {
         let songs = this.videoLocalList
@@ -3461,6 +3466,51 @@ export struct LocalMusic {
     })
   }
 
+  private async refreshAlphaBetFromMediaLibrary(): Promise<void> {
+    if (this.totalCount <= 0) {
+      return
+    }
+    if (this.totalCount > 40000) {
+      this.alphaBet = []
+      return
+    }
+    const keyword = this.isSearchMode ? this.searchText : ''
+    const cacheKey = `${this.modeType}-${this.sortType}-${this.isShowFileName}-${keyword}-${this.totalCount}`
+    if (this.alphaBetCacheKey === cacheKey && this.alphaBet.length > 0) {
+      return
+    }
+    const result = await this.table.queryMediaLibraryPaged({
+      pageIndex: 0,
+      pageSize: this.totalCount,
+      sortType: this.sortType,
+      searchKeyword: this.isSearchMode ? this.searchText : undefined
+    })
+    let alphabetSet = new Set<string>()
+    for (let song of result.items) {
+      let text = this.isShowFileName ? song.fileName :
+        (song.type != CommonConstants.TYPE_IS_DIR && song.pyStr) ? song.pyStr : song.name
+      if (this.sortType === 0 || this.sortType == 1) {
+        text = song.artist
+      } else if (this.sortType === 2 || this.sortType == 3) {
+        text = song.album
+      }
+      if (text) {
+        let firstPinyin = getFirstLetter(text)
+        if (!alphabetSet.has(firstPinyin)) {
+          alphabetSet.add(firstPinyin)
+        }
+      }
+    }
+    this.alphaBet = Array.from(alphabetSet).sort((a, b) => {
+      if (this.sortType === 4 || this.sortType == 0 || this.sortType == 2 || this.sortType == 6) {
+        return a.localeCompare(b)
+      } else {
+        return b.localeCompare(a)
+      }
+    })
+    this.alphaBetCacheKey = cacheKey
+  }
+
 
   @Builder
   topTitleBar(){