Răsfoiți Sursa

完善实现字母表索引

onecold 9 luni în urmă
părinte
comite
7aea23cca8

+ 10 - 0
entry/src/main/ets/Constants.ets

@@ -1,3 +1,5 @@
+import { pinyin4js } from "@ohos/pinyin4js"
+
 export const APP_ID = "wx263d1c80d204c3c0"
 export const APP_SECRET = "a29d0d08885334589698837e46d4573a"
 
@@ -38,3 +40,11 @@ export class Constants {
   public static readonly  COMMON_HIGHLIGHT_COLOR: string = '#e45050'
   public static readonly  COMMON_HIGHLIGHT_LIGHT_COLOR: string = '#f9d7d7'
 }
+
+// 本地化语言
+export function getFirstLetter(text: string){
+  if (!text) return '#';
+  const pinyin = pinyin4js.getShortPinyin(text).toUpperCase() as string
+  const firstLetter = pinyin[0];
+  return /^[A-Z]$/.test(firstLetter) ? firstLetter : '#';
+}

+ 40 - 34
entry/src/main/ets/view/LocalMusic.ets

@@ -122,6 +122,7 @@ import {
   HdsVisualComponentAttribute,
 } from '@kit.UIDesignKit';
 import { GlobalAlphabetIndexerModifier } from '../Styels/GlobalStyles'
+import { getFirstLetter } from '../Constants';
 
 const TAG = 'LocalMusic';
 
@@ -3447,10 +3448,14 @@ export struct LocalMusic {
     let targetIndex = -1;
     for (let i = 0; i < songs.length; i++) {
       let song = songs[i]
-      let text = song.name
-      const pyStr = song.pyStr
-      if(pyStr){
-        let firstPinyin = pyStr[0]
+      let text = this.isShowFileName?song.fileName: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 (firstPinyin&&firstPinyin === letter) {
           targetIndex = i;
           break;
@@ -3464,17 +3469,14 @@ export struct LocalMusic {
   }
   // 找到对应字母表的索引
   findAlphaBetIndex(song: VideoItem):number{
-    let text = song.name
-    // if(this.sortMode === SortMode.ByArtist){
-    //   text = song.artist
-    // }else if(this.sortMode === SortMode.ByTitle){
-    //   text = song.title
-    // }else if(this.sortMode === SortMode.ByRoot){
-    //   text = song.webFilePath.replace('/','')
-    // }
-    const pyStr = song.pyStr
-    if(pyStr){
-      let firstLetter = pyStr[0]
+    let text = this.isShowFileName?song.fileName: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 firstLetter = getFirstLetter(text)
       for(let i=0;i<this.alphaBet.length;i++){
         if(this.alphaBet[i] === firstLetter){
           return i
@@ -3496,29 +3498,33 @@ export struct LocalMusic {
         }
         let alphabetSet = new Set<string>();
         for (let song of songs) {
-          let text = song.name
-          // if(this.sortMode === SortMode.ByArtist){
-          //   text = song.artist
-          // }else if(this.sortMode === SortMode.ByTitle){
-          //   text = song.title
-          // }else if(this.sortMode === SortMode.ByRoot){
-          //   text = song.webFilePath.replace('/','')
-          // }
-          const pyStr = song.pyStr
-          if(pyStr){
-            if(!alphabetSet.has(pyStr)){
-              alphabetSet.add(pyStr)
+          let text = this.isShowFileName?song.fileName: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)
             }
           }
+          // const pyStr = song.pyStr
+          // if(pyStr){
+          //   if(!alphabetSet.has(pyStr)){
+          //     alphabetSet.add(pyStr)
+          //   }
+          // }
 
         }
-        // this.alphaBet = Array.from(alphabetSet).sort((a,b)=>{
-        //   if(this.upOrDown === SortModeUpOrDown.Down){
-        //     return b.localeCompare(a)
-        //   }else{
-        //     return a.localeCompare(b)
-        //   }
-        // })
+        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)
+          }
+        })
         if(this.videoLocalList.length > 0){
           this.listScroller.scrollToIndex(0)
           this.selectAlphaBetIndex = 0

+ 1 - 1
entry/src/main/ets/view/TagsContentCover.ets

@@ -13,6 +13,7 @@ import { DialogHelper } from '@pura/harmony-dialog';
 import PermissionUtil from '../common/util/PermissionUtil'
 import MediaTable from '../common/util/MediaTable';
 
+
 //批量编辑标签
 // 批量编辑标签
 @Component
@@ -544,7 +545,6 @@ export struct TagsContentCover {
           this.embedFailedCount++;
         }
       }
-
       // 完成处理
       ToastUtil.showToast(`嵌入完成: 成功 ${this.embedSuccessCount}, 失败 ${this.embedFailedCount}`)
       this.isEmbedding = false;