Przeglądaj źródła

refactor(LocalMusic): 优化字母索引显示逻辑

- 添加 alphaBetHideTimer 定时器变量控制字母表隐藏
- 实现 showAlphabetIndex 方法统一处理字母表显示
- 实现 hideAlphabetIndexAfterDelay 方法延迟隐藏字母表
- 实现 ensureFullListForAlphabetIndex 方法确保完整列表加载
- 移除原有的循环加载逻辑简化索引查找
- 在滚动开始和结束时使用新的显示/隐藏方法
- 在触摸事件中添加延迟隐藏功能
chendeben 7 miesięcy temu
rodzic
commit
c9340dce0f
1 zmienionych plików z 81 dodań i 16 usunięć
  1. 81 16
      entry/src/main/ets/view/LocalMusic.ets

+ 81 - 16
entry/src/main/ets/view/LocalMusic.ets

@@ -312,6 +312,7 @@ export struct LocalMusic {
   @State isTouchingAlphaBet: boolean = false
   @State alphabetHeight: number = 0
   public enableShowAlphabet: boolean = true  // 是否显示字母表
+  private alphaBetHideTimer: number = 0
   @State isShowPrecious: boolean = false //播控条显示上一首按钮
   //背景两侧流光控制器
   @State sceneBgController: HdsSceneController|undefined = deviceInfo.sdkApiVersion>=20
@@ -3239,9 +3240,10 @@ export struct LocalMusic {
               void this.onSelectIndexItem(this.alphaBet[index])
             })
             .onTouch((event: TouchEvent) =>{
-              this.isShowAlphaBet = true
+              this.showAlphabetIndex()
               if(event.type === TouchType.Up || event.type === TouchType.Cancel){
                 this.isTouchingAlphaBet = false
+                this.hideAlphabetIndexAfterDelay(1500)
               }else{
                 this.isTouchingAlphaBet = true
               }
@@ -3278,6 +3280,7 @@ export struct LocalMusic {
   }
 
   async onSelectIndexItem(letter: string) {
+    await this.ensureFullListForAlphabetIndex();
     const findTargetIndex = (): number => {
       const songs = this.videoLocalList;
       for (let i = 0; i < songs.length; i++) {
@@ -3299,19 +3302,7 @@ export struct LocalMusic {
       return -1;
     };
 
-    let targetIndex = findTargetIndex();
-    let attempts = 0;
-    const canLoadMore = () =>
-      !this.isCanBack &&
-      (this.modeType === 1 || this.modeType === 2 || this.modeType === 3) &&
-      this.hasMoreData &&
-      !this.isLoadingPage;
-
-    while (targetIndex === -1 && canLoadMore() && attempts < 10) {
-      await this.loadNextPage();
-      targetIndex = findTargetIndex();
-      attempts++;
-    }
+    const targetIndex = findTargetIndex();
 
     if (targetIndex !== -1) {
       if (this.isGridMusic || this.twoFingerType == 4) {
@@ -3321,6 +3312,80 @@ export struct LocalMusic {
       }
     }
   }
+
+  private showAlphabetIndex(): void {
+    this.isShowAlphaBet = true;
+    if (this.alphaBetHideTimer) {
+      clearTimeout(this.alphaBetHideTimer);
+      this.alphaBetHideTimer = 0;
+    }
+  }
+
+  private hideAlphabetIndexAfterDelay(delayMs: number): void {
+    if (this.alphaBetHideTimer) {
+      clearTimeout(this.alphaBetHideTimer);
+    }
+    this.alphaBetHideTimer = setTimeout(() => {
+      if (!this.isTouchingAlphaBet) {
+        this.isShowAlphaBet = false;
+      }
+      this.alphaBetHideTimer = 0;
+    }, delayMs) as number;
+  }
+
+  private async ensureFullListForAlphabetIndex(): Promise<void> {
+    if (this.isLoadingPage) {
+      return;
+    }
+    if (this.isCanBack || this.isHistory || this.isFavMusic) {
+      return;
+    }
+    if (this.totalCount === 0 || this.videoLocalList.length >= this.totalCount) {
+      return;
+    }
+    if (this.modeType !== 0 && this.modeType !== 1) {
+      return;
+    }
+    this.isLoadingPage = true;
+    try {
+      const options: PageQueryOptions = {
+        pageIndex: 0,
+        pageSize: this.totalCount,
+        sortType: this.sortType,
+        searchKeyword: this.isSearchMode ? this.searchText : undefined
+      };
+      if (this.modeType === 0) {
+        options.parentPath = this.currentPath;
+      }
+      let result: PageQueryResult;
+      if (this.modeType === 0) {
+        result = await this.table.queryByParentPathPaged(options);
+        if (!this.isSearchMode) {
+          const directories: VideoItem[] = await this.loadDirectories(this.currentPath);
+          result.items = [...directories, ...result.items];
+          result.totalCount += directories.length;
+        }
+      } else {
+        result = await this.table.queryMediaLibraryPaged(options);
+      }
+      this.videoLocalList = result.items;
+      this.dataSource.pushArrayData(this.videoLocalList);
+      this.pageCache.clear();
+      this.currentPage = 0;
+      this.totalCount = result.totalCount;
+      this.hasMoreData = false;
+      if (this.modeType === 1) {
+        this.mediaKuCount = result.totalCount;
+      }
+      this.setButtonStatus();
+      this.setAlphaBet();
+    } catch (err) {
+      const error = err as Error;
+      Logger.warn('heanup LocalMusic', `ensureFullListForAlphabetIndex error: ${error.message}`);
+    } finally {
+      this.isLoadingPage = false;
+    }
+  }
   // 找到对应字母表的索引
   findAlphaBetIndex(song: VideoItem):number{
     let text = this.isShowFileName?song.fileName:
@@ -6230,7 +6295,7 @@ export struct LocalMusic {
     }
     .onScrollStart(() => {
       this.isScrolling = true
-      this.isShowAlphaBet = true
+      this.showAlphabetIndex()
     })
     .onScrollIndex((start: number,center:number,end:number) => {
       this.selectedIndex = start
@@ -6242,7 +6307,7 @@ export struct LocalMusic {
       if(this.isScrolling){
         this.isScrolling = false
       }
-      this.isShowAlphaBet = false
+      this.hideAlphabetIndexAfterDelay(1500)
     })
     .onReachEnd(() => {
       // 触底加载下一页