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

歌单删除增加 移除歌单选项 删除后刷新歌单

onecold 5 месяцев назад
Родитель
Сommit
9d57740434
2 измененных файлов с 123 добавлено и 7 удалено
  1. 115 5
      entry/src/main/ets/view/DeleteComptent.ets
  2. 8 2
      entry/src/main/ets/view/LocalMusic.ets

+ 115 - 5
entry/src/main/ets/view/DeleteComptent.ets

@@ -15,6 +15,7 @@ export struct DeleteComptent {
   @State isDeleteYuan: boolean = true
   @State isDeletePicture: boolean = true
   @State isDeleteLrc: boolean = true
+  @State onlyRemoveFromPlaylist: boolean = false
   @State packName: string = ''
   @State isDeleting: boolean = false // 删除状态
   @State deleteProgress: number = 0 // 删除进度 (0-100)
@@ -24,6 +25,8 @@ export struct DeleteComptent {
   onCancel = () => {
   }
   @Prop selectedFiles: Array<VideoItem>
+  @Prop isPlaylistMode: boolean = false
+  @Prop playlistId: string = ''
   @StorageProp('themeColor') themeColor: string = CommonConstants.DEFAULT_THEME_COLOR;
   context = this.getUIContext().getHostContext() as common.UIAbilityContext
 
@@ -33,6 +36,9 @@ export struct DeleteComptent {
     this.isDeleteYuan = PreferencesUtil.getBooleanSync('isDeleteYuan', true);
     this.isDeletePicture = PreferencesUtil.getBooleanSync('isDeletePicture', true);
     this.isDeleteLrc = PreferencesUtil.getBooleanSync('isDeleteLrc', true)
+    if (!this.isPlaylistMode) {
+      this.onlyRemoveFromPlaylist = false
+    }
 
   }
 
@@ -105,6 +111,47 @@ export struct DeleteComptent {
           Text('温馨提醒').fontSize(20).margin({ top: 10, bottom: 10 })
           Text('是否删除这些文件?').fontSize(16).margin({ top: 10, bottom: 10 })
           Column() {
+            // 只移除歌单选项(歌单场景)
+            Button({ type: ButtonType.Capsule, stateEffect: true }) {
+              Row() {
+                SymbolGlyph($r('sys.symbol.music_note_list'))
+                  .fontSize(20)
+                  .fontColor([this.themeColor])
+                  .alignSelf(ItemAlign.Center)
+                  .margin({ left: 15 })
+                Text("只移除歌单")
+                  .fontSize(16)
+                  .layoutWeight(1)
+                  .margin({ left: 10 })
+                Toggle({ type: ToggleType.Checkbox, isOn: this.onlyRemoveFromPlaylist })
+                  .onChange((isOn: boolean) => {
+                    this.onlyRemoveFromPlaylist = isOn;
+                    if (isOn) {
+                      this.isDeleteYuan = false
+                      this.isDeletePicture = false
+                      this.isDeleteLrc = false
+                    }
+                  })
+                  .margin({ right: 28 })
+                  .selectedColor(this.themeColor)
+
+              }
+            }
+            .backgroundColor(Color.Transparent)
+            .clickEffect({ level: ClickEffectLevel.MIDDLE, scale: 0.6 })
+            .onClick(() => {
+              this.onlyRemoveFromPlaylist = !this.onlyRemoveFromPlaylist
+              if (this.onlyRemoveFromPlaylist) {
+                this.isDeleteYuan = false
+                this.isDeletePicture = false
+                this.isDeleteLrc = false
+              }
+            })
+            .width('100%')
+            .padding(10)
+            .margin({ left: 18 })
+            .visibility(this.isPlaylistMode ? Visibility.Visible : Visibility.None)
+
             // 删除源文件选项
             Button({ type: ButtonType.Capsule, stateEffect: true }) {
               Row() {
@@ -135,6 +182,7 @@ export struct DeleteComptent {
             .width('100%')
             .padding(10)
             .margin({ left: 18 })
+            .visibility(this.onlyRemoveFromPlaylist ? Visibility.None : Visibility.Visible)
 
             // 删除封面选项
             Button({ type: ButtonType.Capsule, stateEffect: true }) {
@@ -165,6 +213,7 @@ export struct DeleteComptent {
             .width('100%')
             .padding(10)
             .margin({ left: 18 })
+            .visibility(this.onlyRemoveFromPlaylist ? Visibility.None : Visibility.Visible)
 
 
             // 删除歌词选项
@@ -197,6 +246,7 @@ export struct DeleteComptent {
             .width('100%')
             .padding(10)
             .margin({ left: 18, bottom: 10 })
+            .visibility(this.onlyRemoveFromPlaylist ? Visibility.None : Visibility.Visible)
 
           }
           .visibility(this.selectedFiles.length==1&&this.selectedFiles[0].type==CommonConstants.TYPE_IS_DIR?
@@ -233,6 +283,11 @@ export struct DeleteComptent {
 
 
   doDeleteTask(){
+    if (this.onlyRemoveFromPlaylist && StrUtil.isEmpty(this.playlistId)) {
+      ToastUtil.showToast('当前歌单信息无效')
+      return
+    }
+
     // 设置删除状态
     this.isDeleting = true;
     this.deleteProgress = 0;
@@ -255,11 +310,18 @@ export struct DeleteComptent {
       }
     }, 300); // 每300ms更新一次,模拟删除进度
 
-    const task = new taskpool.Task(
-      deleteMultipleFilesWithProgress,
-      JSON.stringify(this.selectedFiles),
-      this.isDeleteYuan,this.isDeletePicture,this.isDeleteLrc,this.context,this.packName
-    );
+    const task = this.onlyRemoveFromPlaylist ?
+      new taskpool.Task(
+        removeSongsFromPlaylistOnly,
+        JSON.stringify(this.selectedFiles),
+        this.context,
+        this.playlistId
+      ) :
+      new taskpool.Task(
+        deleteMultipleFilesWithProgress,
+        JSON.stringify(this.selectedFiles),
+        this.isDeleteYuan, this.isDeletePicture, this.isDeleteLrc, this.context, this.packName
+      );
     taskpool.execute(task, taskpool.Priority.HIGH).then((result) => {
       clearInterval(progressTimer);
       this.deleteProgress = 100;
@@ -283,6 +345,54 @@ export struct DeleteComptent {
 }
 
 
+// 仅从当前歌单移除歌曲
+@Concurrent
+async function removeSongsFromPlaylistOnly(
+  selectedFilesStr:string,
+  context:Context,
+  playlistId:string
+) {
+  const selectedFiles: VideoItem[] = JSON.parse(selectedFilesStr)
+  if (!ArrayUtil.isNotEmpty(selectedFiles)) {
+    return true
+  }
+  if (StrUtil.isEmpty(playlistId)) {
+    LogUtil.error('heanup DeleteComptent', 'removeSongsFromPlaylistOnly: playlistId 为空')
+    return false
+  }
+
+  const playlistTable: PlaylistTable = new PlaylistTable(context)
+  let hasPlaylistChanges = false
+  let hasError = false
+
+  for (let i = 0; i < selectedFiles.length; i++) {
+    const item = selectedFiles[i]
+    if (!item.filePath) {
+      continue
+    }
+    try {
+      const removed = await playlistTable.removeSongFromPlaylist(playlistId, item.filePath)
+      if (removed) {
+        hasPlaylistChanges = true
+      } else {
+        hasError = true
+      }
+    } catch (error) {
+      hasError = true
+      LogUtil.error('heanup DeleteComptent', `removeSongsFromPlaylistOnly error: ${(error as Error).message}`)
+    }
+    await new Promise<void>(resolve => setTimeout(resolve, 120))
+  }
+
+  if (hasPlaylistChanges) {
+    const eventRefresh: emitter.InnerEvent = { eventId: EventConstants.EVENT_PLAYLIST_REFRESH }
+    emitter.emit(eventRefresh, {})
+    emitter.emit({ eventId: EventConstants.EVENT_PLAYLIST_REFRESH_SORT }, {})
+  }
+  return !hasError
+}
+
+
 //多选删除文件(带进度显示的新版本)
 @Concurrent
 async function deleteMultipleFilesWithProgress(

+ 8 - 2
entry/src/main/ets/view/LocalMusic.ets

@@ -4985,6 +4985,8 @@ export struct LocalMusic {
   deleteComptentBuilder(){
     DeleteComptent({
       selectedFiles:this.selectedFiles,
+      isPlaylistMode: this.modeType == 4,
+      playlistId: this.currentSongListID,
       onCancel:()=>{
         this.isMultiSelect = false
         this.getUIContext().getPromptAction().closeCustomDialog(this.deleteComponentId)
@@ -4995,8 +4997,12 @@ export struct LocalMusic {
         if(result){//如果删除成功,更新数据
           this.selectedFiles = [];
           this.isAllSelected = false
-          this.cache.delete(this.currentPath);
-          this.getSortedFiles(this.currentPath, true);
+          if (this.modeType == 4) {
+            this.doSongListTask()
+          } else {
+            this.cache.delete(this.currentPath);
+            this.getSortedFiles(this.currentPath, true);
+          }
         }
 
       }