Преглед на файлове

支持桌面卡片点击收藏

chendeben преди 1 година
родител
ревизия
52cf17454b
променени са 2 файла, в които са добавени 33 реда и са изтрити 4 реда
  1. 24 3
      entry/src/main/ets/common/service/UnifiedPlayerService.ets
  2. 9 1
      entry/src/main/ets/entryability/EntryAbility.ets

+ 24 - 3
entry/src/main/ets/common/service/UnifiedPlayerService.ets

@@ -17,6 +17,7 @@ import { BusinessError } from '@kit.BasicServicesKit';
 import { http } from '@kit.NetworkKit';
 import json from '@ohos.util.json';
 import MediaTable from '../util/MediaTable';
+import { Utility } from '../util/Utility';
 
 /**
  * 服务就绪状态详情接口
@@ -318,6 +319,7 @@ interface widgeData {
   filePath: string;
   imgName: string;
   imageColorHex: string;
+  isFavorite: boolean;
 
   // PlayerState数据平铺
   isPlaying: boolean;
@@ -578,6 +580,23 @@ export class UnifiedPlayerService implements IPlayerService, PlaylistSyncListene
     // 同步更新所有桌面卡片状态
     this.updateAllForms();
   }
+  public toggleFavorite() {
+
+    let isFav = 0
+    let item=this.getCurrentSong() as VideoItem;
+    if (Utility.getIsFav(this.getFav(), item)) {
+      isFav = 0
+    } else {
+      isFav = 1
+    }
+    this.getTable().updateIsFavByFilePath(item.filePath, isFav, async (result: boolean) => {
+      if (result) {
+        // 刷新UnifiedPlayerService的收藏状态,这会自动更新AVSession
+        console.log("Heanup2  UnifiedPlayerService toggleFavorite: 收藏状态切换");
+        await this.refreshFavoriteStatus();
+      }
+    })
+  }
 
   /**
    * 从暂停状态恢复播放
@@ -1039,6 +1058,7 @@ export class UnifiedPlayerService implements IPlayerService, PlaylistSyncListene
           // 更新收藏列表后,立即更新AVSession状态以反映正确的收藏状态
           setTimeout(() => {
             this.updateSessionPlayState();
+            this.updateAllForms();
           }, 100);
 
           resolve();
@@ -2817,7 +2837,7 @@ export class UnifiedPlayerService implements IPlayerService, PlaylistSyncListene
         pixelMapPath: formData.pixelMapPath || '',
         duration: typeof formData.duration === 'string' ? parseInt(formData.duration || '0') : (formData.duration || 0),
         filePath: formData.filePath || '',
-        imgName: imgName, // 使用生成的唯一图片名称
+        imgName: imgName,
         imageColorHex: '2A2A2A',
 
         // PlayerState数据平铺
@@ -2844,7 +2864,8 @@ export class UnifiedPlayerService implements IPlayerService, PlaylistSyncListene
         timeInfo: widgetTimeInfo,
 
         // 卡片图片显示必填字段
-        formImages: imgMap
+        formImages: imgMap,
+        isFavorite: this.getCurrentSongFavoriteState() ,
       };
 
       // 记录更新统计
@@ -2858,7 +2879,7 @@ export class UnifiedPlayerService implements IPlayerService, PlaylistSyncListene
           const formBindingDataInstance = formBindingData.createFormBindingData(flatWidgetData);
           await formProvider.updateForm(formId, formBindingDataInstance);
           successCount++;
-          LogUtils.getInstance().LOGI(`UnifiedPlayerService updateAllForms: Updated form ${formId} - isPlaying=${flatWidgetData.isPlaying}, hasNext=${flatWidgetData.hasNext}, hasPrevious=${flatWidgetData.hasPrevious}`);
+          LogUtils.getInstance().LOGI(`UnifiedPlayerService updateAllForms: Updated form ${formId} -isFavorite=${flatWidgetData.isFavorite}, - isPlaying=${flatWidgetData.isPlaying}, hasNext=${flatWidgetData.hasNext}, hasPrevious=${flatWidgetData.hasPrevious}`);
         } catch (error) {
           failedCount++;
           const errorStr: string = error?.toString() || '';

+ 9 - 1
entry/src/main/ets/entryability/EntryAbility.ets

@@ -494,7 +494,12 @@ export default class EntryAbility extends UIAbility {
                 }
             });
             this.callee.on("toggleFavorite",(data:rpc.MessageSequence)=>{
+                const params: Record<string, Object> = JSON.parse(data.readString()) as Record<string, Object>;
                 hilog.info(0x0000, 'Heanup2', `toggleFavorite handler`);
+                // 异步发送上一首事件到主应用(包含服务就绪检查)
+                this.sendWidgetControlEvent('toggleFavorite', params).catch((error: Error) => {
+                    hilog.error(0x0000, 'Heanup2', `❌ prevSong async error: ${error}`);
+                });
                 return new MyParcelable(-5, 'toggleFavorite');
             })
 
@@ -512,6 +517,7 @@ export default class EntryAbility extends UIAbility {
             this.callee.off('playPause');
             this.callee.off('nextSong');
             this.callee.off('prevSong');
+            this.callee.off('toggleFavorite');
             // this.callee.off('openApp');
             // this.callee.off('playByAction');
             // this.callee.off('collectAction');
@@ -528,7 +534,6 @@ export default class EntryAbility extends UIAbility {
      */
     private async sendWidgetControlEvent(command: string, params: Record<string, Object>): Promise<void> {
         try {
-            hilog.info(0x0000, 'Heanup2', `🎵 收到桌面卡片指令: ${command}`);
 
             // 获取UnifiedPlayerService实例
             const unifiedService = UnifiedPlayerService.getInstance();
@@ -638,6 +643,9 @@ export default class EntryAbility extends UIAbility {
                 case 'OPEN_APP':
                     hilog.info(0x0000, 'Heanup2', '✅ Widget command: Open app (handled by UI)');
                     break;
+                case "toggleFavorite":
+                    await unifiedService.toggleFavorite();
+                    break;
                 default:
                     hilog.warn(0x0000, 'Heanup2', `❓ Unknown widget command: ${command}`);
                     break;