Просмотр исходного кода

refactor(playlist):重构歌单播放事件数据结构

chendeben 10 месяцев назад
Родитель
Сommit
6f04a4a47a
2 измененных файлов с 24 добавлено и 78 удалено
  1. 10 12
      entry/src/main/ets/pages/PlaylistDetailPage.ets
  2. 14 66
      entry/src/main/ets/view/LocalMusic.ets

+ 10 - 12
entry/src/main/ets/pages/PlaylistDetailPage.ets

@@ -12,9 +12,9 @@ import { CommonConstants } from '../common/constants/CommonConstants';
 import { EventConstants } from '../common/constants/EventConstants';
 
 /**
- * 简化的歌单播放事件数据
+ * 歌单播放事件数据
  */
-interface SimplifiedPlaylistEventData {
+interface PlaylistEventData {
   playlistId: string;
   playlistName: string;
   songCount: number;
@@ -305,8 +305,8 @@ export struct PlaylistDetailPage {
 
     LogUtil.info(`heanup 准备发送播放事件,playlist: ${this.playlist ? '存在' : '不存在'}, songs: ${this.songList.length}首`)
 
-    // 尝试简化数据结构,只发送必要的信息
-    const simplifiedData: SimplifiedPlaylistEventData = {
+    // 构建歌单播放事件数据
+    const playlistData: PlaylistEventData = {
       playlistId: this.playlist?.id || '',
       playlistName: this.playlist?.name || '',
       songCount: this.songList.length,
@@ -315,10 +315,10 @@ export struct PlaylistDetailPage {
       songFilePaths: this.songList.map(song => song.filePath)
     };
 
-    LogUtil.info(`heanup 发送简化事件数据: ${JSON.stringify(simplifiedData)}`)
+    LogUtil.info(`heanup 发送歌单播放事件数据: ${JSON.stringify(playlistData)}`)
 
     const eventData: emitter.EventData = {
-      data: simplifiedData
+      data: playlistData
     };
 
     emitter.emit(eventPlaylistPlay, eventData)
@@ -338,9 +338,9 @@ export struct PlaylistDetailPage {
     // 检查歌曲文件路径
     LogUtil.info(`heanup 所有歌曲文件路径: ${JSON.stringify(this.songList.map(s => s.filePath))}`)
 
-    // 发送播放歌单事件,使用简化数据结构
+    // 发送播放歌单事件
     const eventPlaylistPlay: emitter.InnerEvent = { eventId: EventConstants.EVENT_PLAYLIST_PLAY }
-    const simplifiedData: SimplifiedPlaylistEventData = {
+    const playlistData: PlaylistEventData = {
       playlistId: this.playlist?.id || '',
       playlistName: this.playlist?.name || '',
       songCount: this.songList.length,
@@ -350,16 +350,14 @@ export struct PlaylistDetailPage {
     };
 
     LogUtil.info(`heanup 准备发送事件,eventId: ${eventPlaylistPlay.eventId}`)
-    LogUtil.info(`heanup 发送单歌曲播放事件数据: ${JSON.stringify(simplifiedData)}`)
+    LogUtil.info(`heanup 发送歌单播放事件数据: ${JSON.stringify(playlistData)}`)
 
     const eventData: emitter.EventData = {
-      data: simplifiedData
+      data: playlistData
     };
 
     LogUtil.info(`heanup eventData对象: ${JSON.stringify(eventData)}`)
-    LogUtil.info(`heanup 即将调用 emitter.emit`)
     emitter.emit(eventPlaylistPlay, eventData)
-    LogUtil.info(`heanup emitter.emit 调用完成`)
   }
 
   /**

+ 14 - 66
entry/src/main/ets/view/LocalMusic.ets

@@ -94,9 +94,9 @@ import { showCreatePlaylistDialog } from '../dialog/PlaylistDialog';
 const TAG = 'LocalMusic';
 
 /**
- * 简化的歌单播放事件数据
+ * 歌单播放事件数据
  */
-interface SimplifiedPlaylistEventData {
+interface PlaylistEventData {
   playlistId: string;
   playlistName: string;
   songCount: number;
@@ -583,11 +583,11 @@ export struct LocalMusic {
         return
       }
 
-      // 检查简化数据结构
+      // 检查歌单播放数据结构
       if (data.playlistId && data.songFilePaths && data.startIndex !== undefined) {
-        Logger.info('heanup eventPlaylistPlay: received simplified playlist data')
-        // 手动构建简化数据对象以避免类型转换问题
-        const simplifiedData: SimplifiedPlaylistEventData = {
+        Logger.info('heanup eventPlaylistPlay: 接收到歌单播放数据')
+        // 手动构建数据对象以避免类型转换问题
+        const playlistData: PlaylistEventData = {
           playlistId: data.playlistId as string,
           playlistName: data.playlistName as string,
           songCount: data.songCount as number,
@@ -595,23 +595,16 @@ export struct LocalMusic {
           songFilePaths: data.songFilePaths as string[]
         }
         // 根据文件路径重新构建歌曲列表
-        this.handleSimplifiedPlaylistPlayRequest(
-          simplifiedData.playlistId,
-          simplifiedData.playlistName,
-          simplifiedData.songFilePaths,
-          simplifiedData.startIndex
+        this.handlePlaylistPlayRequest(
+          playlistData.playlistId,
+          playlistData.playlistName,
+          playlistData.songFilePaths,
+          playlistData.startIndex
         )
         return
       }
 
-      // 检查原始数据结构(向后兼容)
-      if (data.playlist && data.songs && data.startIndex !== undefined) {
-        Logger.info('heanup eventPlaylistPlay: received original playlist data')
-        this.handlePlaylistPlayRequest(data.playlist as Playlist, data.songs as VideoItem[], data.startIndex as number)
-        return
-      }
-
-      Logger.error('heanup eventPlaylistPlay: invalid data structure received')
+      Logger.error('heanup eventPlaylistPlay: 接收到无效的数据结构')
     });
 
     let eventSetting: emitter.InnerEvent = { eventId: EventConstants.EVENT_SETTING_UPDATE }
@@ -13022,35 +13015,9 @@ export struct LocalMusic {
   /**
    * 处理歌单播放请求
    */
-  private  handlePlaylistPlayRequest(playlist: Playlist, songs: VideoItem[], startIndex: number) {
-  try {
-    // 1. 替换当前播放列表
-    this.songList = songs
-    this.sonDataSource.pushArrayData(songs)
-
-    // 2. 设置当前播放索引
-    this.curIndex = startIndex
-
-    // 3. 播放指定歌曲
-    if (songs[startIndex]) {
-      this.doPlay(songs[startIndex], startIndex)
-    }
-
-    // 4. 保存播放列表
-    PreferencesUtil.putSync('LastMusicList', this.songList)
-
-    console.info(`heanup 开始播放歌单: ${playlist.name}, 歌曲数量: ${songs.length}, 开始索引: ${startIndex}`)
-  } catch (error) {
-    console.error('heanup 处理歌单播放请求失败:', error)
-    ToastUtil.showToast('播放失败')
-  }
-}
-  /**
-   * 处理简化的歌单播放请求
-   */
-  private handleSimplifiedPlaylistPlayRequest(playlistId: string, playlistName: string, songFilePaths: string[], startIndex: number) {
+  private handlePlaylistPlayRequest(playlistId: string, playlistName: string, songFilePaths: string[], startIndex: number) {
     try {
-      Logger.info(`heanup 处理简化歌单播放请求: ${playlistName}, 歌曲数量: ${songFilePaths.length}, 开始索引: ${startIndex}`)
+      Logger.info(`heanup 处理歌单播放请求: ${playlistName}, 歌曲数量: ${songFilePaths.length}, 开始索引: ${startIndex}`)
 
       // 从数据库重新加载这些歌曲,使用索引记录位置以保持顺序
       const songMap: Map<number, VideoItem> = new Map()
@@ -13133,40 +13100,21 @@ export struct LocalMusic {
       ToastUtil.showToast('没有找到可播放的歌曲')
       return
     }
-
-    Logger.info(`heanup 找到 ${songs.length} 首可播放的歌曲`)
-
-    // 1. 替换当前播放列表并更新存储
-    Logger.info(`heanup 更新前 - 当前播放列表长度: ${this.songList.length}`)
     this.songList = songs
-    // 确保存储也更新(@StorageLink会自动同步)
-    Logger.info(`heanup 更新后 - 新播放列表长度: ${this.songList.length}`)
 
-    // 2. 更新数据源
-    Logger.info(`heanup 更新前 - 数据源长度: ${this.sonDataSource.totalCount()}`)
     this.sonDataSource.pushArrayData(songs)
-    Logger.info(`heanup 更新后 - 数据源长度: ${this.sonDataSource.totalCount()}`)
 
-    // 3. 强制触发UI重新渲染
-    Logger.info('heanup 触发UI重新渲染')
     this.sonDataSource.notifyDataReload()
 
-    // 4. 设置当前播放索引
     this.curIndex = startIndex
 
-    // 5. 播放指定歌曲
     if (songs[startIndex]) {
-      Logger.info(`heanup 开始播放歌曲: ${songs[startIndex].name}, 索引: ${startIndex}`)
-      Logger.info(`heanup 播放前 - 播放列表长度: ${this.songList.length}, 当前索引: ${this.curIndex}`)
       // 确保当前播放的歌曲也更新到存储
       AppStorage.setOrCreate('currentSong', songs[startIndex])
       // 设置isFromSonPlayList=true,避免doPlay方法重新从全局列表构建播放列表
-      Logger.info(`heanup 调用doPlay,参数: 歌曲=${songs[startIndex].name}, 索引=${startIndex}, isFromSonPlayList=true`)
       this.doPlay(songs[startIndex], startIndex, true)
-      Logger.info(`heanup doPlay调用完成`)
     }
 
-    // 5. 保存播放列表
     PreferencesUtil.putSync('LastMusicList', this.songList)
 
     ToastUtil.showToast(`开始播放歌单: ${playlistName}`)