فهرست منبع

修复点击歌单展示歌单的歌曲的bug
歌单的歌曲做缓存

onecold 10 ماه پیش
والد
کامیت
066c0df80c
2فایلهای تغییر یافته به همراه50 افزوده شده و 22 حذف شده
  1. 13 6
      entry/src/main/ets/pages/NewIndex.ets
  2. 37 16
      entry/src/main/ets/view/LocalMusic.ets

+ 13 - 6
entry/src/main/ets/pages/NewIndex.ets

@@ -60,7 +60,6 @@ const TAG = 'NewIndex'; // 日志标签
 struct NewIndex {
   /** 页面上下文 */
   context = this.getUIContext().getHostContext() as common.UIAbilityContext
-  @Provide currentSongList: Array<VideoItem> = []//当前歌单
   @Provide currentSongListName:string = '' //当前歌单名称
   @Provide  currentSongListID:string='' //当前歌单ID
   @State isDarkMode: boolean = false
@@ -159,8 +158,14 @@ struct NewIndex {
       start: LengthMetrics.vp(10)
     }
   })
-  @State tabSelectedIndexes: number[] = [0]
-  
+  @State @Watch('tabSelectedIndexesChanged') tabSelectedIndexes: number[] = [0]
+  //当胶囊按钮的选择发生变化时调用此函数
+  tabSelectedIndexesChanged() {
+    if(this.tabSelectedIndexes[0]==1){
+      this.mType = 0
+    }
+
+  }
   // 歌单相关状态变量
   @State playlistList: Playlist[] = []
   @State isShowCreatePlaylistDialog: boolean = false
@@ -1083,11 +1088,13 @@ struct NewIndex {
         .backgroundColor(Color.Transparent)
         .clickEffect({ level: ClickEffectLevel.MIDDLE, scale: 0.6 })
         .onClick(async () => {
+          this.doShowDrawer();
+          this.mType = 0
+          this.modeType = 4
           this.currentSongListName = playlist.name;
           this.currentSongListID = playlist.id;
-          this.mType === 0
-          this.modeType = 4
-          this.doShowDrawer();
+
+
 
 
         })

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

@@ -163,7 +163,7 @@ const ITEM_HEIGHT_BIG: number = 78; // 列表项大高度
 @Preview
 @Component
 export struct LocalMusic {
-  @Consume currentSongList: Array<VideoItem>//当前歌单
+  @State currentSongList: Array<VideoItem> = []//当前歌单
   @Consume currentSongListName:string //当前歌单名称
   @Consume @Watch('onIDChange') currentSongListID:string //当前歌单ID
   @State isRefreshing: boolean = false;
@@ -301,15 +301,23 @@ export struct LocalMusic {
     }
   }
 
-  //歌单id发生变化的时候回调
+  //点击某个歌单id发生变化的时候回调
   onIDChange(){
+    console.log('onecold 歌单id发生变化this.currentSongListID='+this.currentSongListID)
+    console.log('onecold 歌单id发生变化this.currentSongListName='+this.currentSongListName)
     if(this.modeType==4){
-
-      this.titleBarModel.setLeftIconMainNoAnimate($r('app.media.menu'))
-      this.updateListData(this.currentSongList, true)
       this.titleBarModel.setTitleName(this.currentSongListName)
-      this.isShowTitleBar = false
-      console.log('歌单id发生变化')
+      this.titleBarModel.setLeftIconMainNoAnimate($r('app.media.menu'))
+      console.log('onecold 歌单id发生变化获取缓存1')
+      //获取缓存
+      const cacheSongs:string =PreferencesUtil.getStringSync(this.currentSongListID+'currentSongList', '')
+      console.log('onecold 歌单id发生变化获取缓存2  cacheSongs = '+cacheSongs)
+      if(StrUtil.isNotEmpty(cacheSongs)){
+        this.currentSongList = JSON.parse(cacheSongs) as VideoItem[]
+        console.log('onecold 歌单id发生变化获取缓存3 this.currentSongList = '+this.currentSongList.length)
+        this.updateListData(this.currentSongList, true)
+        this.isShowTitleBar = false
+      }
       this.doSongListTask()//获取歌单信息
     }
 
@@ -318,13 +326,29 @@ export struct LocalMusic {
   async  doSongListTask() {
     // 初始化歌单数据库
     const playlistTable = new PlaylistTable(this.context);
+    console.log('onecold 歌单id发生变化获取缓存6')
     // 查询歌单中的所有歌曲
     const playlistSongs: PlaylistSong[] = await playlistTable.queryPlaylistSongs(this.currentSongListID);
+    console.log('onecold 歌单id发生变化获取缓存7 playlistSongs='+playlistSongs.length)
     if (ArrayUtil.isEmpty(playlistSongs)) {
-        ToastUtil.showToast('当前歌单为空')
+      ToastUtil.showToast('当前歌单为空')
+      this.updateListData([], true)
     } else {
       this.currentSongList = await convertPlaylistSongsToVideoItems(this.context, playlistSongs);
+      console.log('onecold 从数据库读取的歌曲为 playlistSongs='+JSON.stringify(this.currentSongList))
+      console.log('onecold 从数据库读取的歌曲为 playlistSongs='+this.currentSongList.length)
       this.updateListData(this.currentSongList, true)
+
+      if (ArrayUtil.isNotEmpty(this.currentSongList)&&this.currentSongList.length  > 100) {
+        // 只保存前100条记录到缓存中
+        let canceList = this.currentSongList.slice(0,  100);
+        PreferencesUtil.putSync(this.currentSongListID+'currentSongList',
+          JSON.stringify(canceList))
+      } else {
+        PreferencesUtil.putSync(this.currentSongListID+'currentSongList', JSON.stringify(this.currentSongList));
+      }
+
+
     }
   }
 
@@ -357,13 +381,6 @@ export struct LocalMusic {
         this.updateListData(this.albumList)
         this.titleBarModel.setTitleName(Utility.resourceToString(this.context, $r('app.string.album')))
         break
-      case 4://歌单
-        this.titleBarModel.setLeftIconMainNoAnimate($r('app.media.menu'))
-        this.updateListData(this.currentSongList, true)
-        this.titleBarModel.setTitleName(this.currentSongListName)
-        this.isShowTitleBar = false
-        this.doSongListTask()//获取歌单信息
-        break
     }
   }
 
@@ -3580,6 +3597,8 @@ export struct LocalMusic {
                 this.updateListData(this.artistList)
               } else if(this.modeType==3&&!this.isCanBack){
                 this.updateListData(this.albumList)
+              }else if(this.modeType==4){
+                this.doSongListTask()
               }
 
             })
@@ -3608,7 +3627,7 @@ export struct LocalMusic {
         Image($r("app.media.refresh"))
           .fillColor(this.themeColor)
           .width(25)
-          .visibility(this.isHistory || this.isCanBack || this.isSearchMode ? Visibility.None : Visibility.Visible)
+          .visibility(this.modeType ==4||this.isHistory || this.isCanBack || this.isSearchMode ? Visibility.None : Visibility.Visible)
           .animation({
             duration: 666,
             curve: 'ease-in-out' // 可选动画曲线
@@ -4834,6 +4853,8 @@ export struct LocalMusic {
         workerInstance.postMessage({ code: 3, data: this.context });
       }else if(this.modeType == 3){
         workerInstance.postMessage({ code: 4, data: this.context });
+      }else if(this.modeType == 4){
+        this.doSongListTask()
       }