Jelajahi Sumber

feat(constants): 引入事件ID常量管理类

- 新增 EventConstants 类统一管理所有 emitter事件 ID
- 替换代码中硬编码的事件 ID 为常量引用
- 提高事件 ID 的可维护性和可读性
- 涉及媒体文件、文件扫描、应用设置、UI交互、用户状态、歌单相关等事件- 在 EntryAbility、NewIndex、PlaylistDetailPage、ScanFilePage、SettingPage、UserCenter 和 LocalMusic 等模块中应用新常量
chendeben 10 bulan lalu
induk
melakukan
6f8fcd463d

+ 50 - 0
entry/src/main/ets/common/constants/EventConstants.ets

@@ -0,0 +1,50 @@
+/**
+ * 事件ID常量管理
+ * 统一管理项目中所有的emitter事件ID
+ */
+export class EventConstants {
+  /**
+   * 媒体文件打开事件
+   */
+  // 视频打开广播事件
+  static readonly EVENT_VIDEO_OPEN: number = 1;
+
+  // 音频打开广播事件
+  static readonly EVENT_AUDIO_OPEN: number = 2;
+
+  /**
+   * 文件扫描事件
+   */
+  // 扫描文件更新事件
+  static readonly EVENT_SCAN_UPDATE: number = 101;
+
+  /**
+   * 应用设置事件
+   */
+  // 设置更新事件
+  static readonly EVENT_SETTING_UPDATE: number = 333;
+
+  /**
+   * UI交互事件
+   */
+  // SwipeBack状态更新事件
+  static readonly EVENT_SWIPE_BACK_UPDATE: number = 888;
+
+  /**
+   * 用户相关事件
+   */
+  // 用户状态改变事件(登录/登出)
+  static readonly EVENT_USER_STATE_CHANGE: number = 1001;
+
+  /**
+   * 歌单相关事件
+   */
+  // 歌单刷新事件
+  static readonly EVENT_PLAYLIST_REFRESH: number = 2001;
+
+  // 播放歌单事件
+  static readonly EVENT_PLAYLIST_PLAY: number = 2002;
+
+  // 播放状态变化事件
+  static readonly EVENT_PLAYBACK_STATUS: number = 2003;
+}

+ 4 - 3
entry/src/main/ets/entryability/EntryAbility.ets

@@ -15,6 +15,7 @@ import { AbilityConstant, Want } from '@kit.AbilityKit';
 import { BusinessError, emitter } from '@kit.BasicServicesKit';
 import { AppUtil, ArrayUtil, GlobalContext, LogUtil, StrUtil } from '@pura/harmony-utils';
 import { DemoConstants } from './DemoConstants';
+import { EventConstants } from '../common/constants/EventConstants';
 
 import { Utility } from '../common/util/Utility';
 import { WXApi, WXEventHandler } from '../common/util/WXApiWrap';
@@ -142,9 +143,9 @@ export default class EntryAbility extends UIAbility {
                 }
             };
             if(Utility.isMeidaByExtension(uri)){
-                emitter.emit({ eventId: 2 }, eventData); // 发送音频打开广播事件
+                emitter.emit({ eventId: EventConstants.EVENT_AUDIO_OPEN }, eventData); // 发送音频打开广播事件
             }else{
-                emitter.emit({ eventId: 1 }, eventData); // 发送视频打开广播事件
+                emitter.emit({ eventId: EventConstants.EVENT_VIDEO_OPEN }, eventData); // 发送视频打开广播事件
             }
         },300)
 
@@ -342,7 +343,7 @@ export default class EntryAbility extends UIAbility {
     //发送广播通知更新UI
     sendChangeEvent() {
         const eventData: emitter.EventData = {};
-        emitter.emit({ eventId: 333 }, eventData); // 发送广播通知更新doSwipBack
+        emitter.emit({ eventId: EventConstants.EVENT_SETTING_UPDATE }, eventData); // 发送广播通知更新doSwipBack
     }
 
 }

+ 9 - 8
entry/src/main/ets/pages/NewIndex.ets

@@ -5,6 +5,7 @@ import mainViewModel, { MainViewModel } from '../viewmodel/MainViewModel';
 import ItemData from '../viewmodel/ItemData';
 import type { sysResource } from '../viewmodel/ItemData';
 import { CommonConstants } from '../common/constants/CommonConstants';
+import { EventConstants } from '../common/constants/EventConstants';
 import { VideoItem } from '../viewmodel/VideoItem';
 import ScreenUtil from '../common/util/ScreenUtil';
 import { Utility } from '../common/util/Utility';
@@ -171,7 +172,7 @@ struct NewIndex {
       (this.modeType !== 0 && this.isCanBack)) {
       console.info('onecold 返回键处理逻辑:发送音频广播通知更新列表');
       const eventData: emitter.EventData = {};
-      emitter.emit({ eventId: 888 }, eventData); // 发送音频广播通知更新doSwipBack
+      emitter.emit({ eventId: EventConstants.EVENT_SWIPE_BACK_UPDATE }, eventData); // 发送音频广播通知更新doSwipBack
     }else if(this.mType > 0){
       this.getUIContext()?.animateTo({ duration: 555 }, () => {
         // 动画闭包内控制Image组件的出现和消失
@@ -255,12 +256,12 @@ struct NewIndex {
 
     await UserUtil.fetchUserInfo();
     this.refreshUserInfoState();
-    let changeUserState: emitter.InnerEvent = { eventId: 1001 }
+    let changeUserState: emitter.InnerEvent = { eventId: EventConstants.EVENT_USER_STATE_CHANGE }
     emitter.on(changeUserState, () => {
       this.refreshUserInfoState();
     });
 
-    let eventSetting: emitter.InnerEvent = { eventId: 333 }
+    let eventSetting: emitter.InnerEvent = { eventId: EventConstants.EVENT_SETTING_UPDATE }
     // 监听广播事件(通用设置配置更新)
     emitter.on(eventSetting, (eventData: emitter.EventData) => {
       this.isShowTitleBar = PreferencesUtil.getBooleanSync(SettingPage.IS_SHOW_TITLTBAR, true)
@@ -283,7 +284,7 @@ struct NewIndex {
     await this.initPlaylistTable();
 
     // 监听歌单刷新事件
-    emitter.on({ eventId: 2001 }, (eventData: emitter.EventData) => {
+    emitter.on({ eventId: EventConstants.EVENT_PLAYLIST_REFRESH }, (eventData: emitter.EventData) => {
       this.loadPlaylistList()
     });
   }
@@ -314,11 +315,11 @@ struct NewIndex {
   aboutToDisappear() {
     console.info('NewIndex aboutToDisappear');
     this.breakpointSystem.unregister();
-    emitter.off(888);
-    emitter.off(1001);
+    emitter.off(EventConstants.EVENT_SWIPE_BACK_UPDATE);
+    emitter.off(EventConstants.EVENT_USER_STATE_CHANGE);
     
     // 监听歌单刷新事件
-    emitter.off( 2001 );
+    emitter.off(EventConstants.EVENT_PLAYLIST_REFRESH);
 
   }
 
@@ -1126,7 +1127,7 @@ struct NewIndex {
             // 刷新歌单列表
             await this.loadPlaylistList()
             // 发送歌单刷新事件
-            emitter.emit({ eventId: 2001 }, {})
+            emitter.emit({ eventId: EventConstants.EVENT_PLAYLIST_REFRESH }, {})
           } else {
             ToastUtil.showToast('歌单创建失败')
           }

+ 7 - 6
entry/src/main/ets/pages/PlaylistDetailPage.ets

@@ -9,6 +9,7 @@ import { showAddSongsToPlaylistDialog } from '../dialog/AddSongsToPlaylistDialog
 import { router } from '@kit.ArkUI';
 import { GlobalContext } from '../common/util/GlobalContext';
 import { CommonConstants } from '../common/constants/CommonConstants';
+import { EventConstants } from '../common/constants/EventConstants';
 
 /**
  * 简化的歌单播放事件数据
@@ -162,7 +163,7 @@ export struct PlaylistDetailPage {
   setupPlaybackStatusListener() {
     try {
       // 监听播放状态变化事件
-      const eventPlaybackStatus: emitter.InnerEvent = { eventId: 2003 }
+      const eventPlaybackStatus: emitter.InnerEvent = { eventId: EventConstants.EVENT_PLAYBACK_STATUS }
       emitter.on(eventPlaybackStatus, (eventData: emitter.EventData) => {
         LogUtil.info('heanup PlaylistDetailPage 收到播放状态变化事件:'+ JSON.stringify(eventData))
         if (eventData.data) {
@@ -204,7 +205,7 @@ export struct PlaylistDetailPage {
    */
   removePlaybackStatusListener() {
     try {
-      emitter.off(2003)
+      emitter.off(EventConstants.EVENT_PLAYBACK_STATUS)
     } catch (error) {
       LogUtil.error('heanup 移除播放状态监听失败: ' + error)
     }
@@ -300,7 +301,7 @@ export struct PlaylistDetailPage {
     }
 
     // 发送播放歌单事件
-    const eventPlaylistPlay: emitter.InnerEvent = { eventId: 2002 }
+    const eventPlaylistPlay: emitter.InnerEvent = { eventId: EventConstants.EVENT_PLAYLIST_PLAY }
 
     LogUtil.info(`heanup 准备发送播放事件,playlist: ${this.playlist ? '存在' : '不存在'}, songs: ${this.songList.length}首`)
 
@@ -338,7 +339,7 @@ export struct PlaylistDetailPage {
     LogUtil.info(`heanup 所有歌曲文件路径: ${JSON.stringify(this.songList.map(s => s.filePath))}`)
 
     // 发送播放歌单事件,使用简化数据结构
-    const eventPlaylistPlay: emitter.InnerEvent = { eventId: 2002 }
+    const eventPlaylistPlay: emitter.InnerEvent = { eventId: EventConstants.EVENT_PLAYLIST_PLAY }
     const simplifiedData: SimplifiedPlaylistEventData = {
       playlistId: this.playlist?.id || '',
       playlistName: this.playlist?.name || '',
@@ -376,7 +377,7 @@ export struct PlaylistDetailPage {
             if (success) {
               ToastUtil.showToast('歌单更新成功')
               // 发送刷新事件
-              const eventRefresh: emitter.InnerEvent = { eventId: 2001 }
+              const eventRefresh: emitter.InnerEvent = { eventId: EventConstants.EVENT_PLAYLIST_REFRESH }
               emitter.emit(eventRefresh, {})
             } else {
               ToastUtil.showToast('歌单更新失败')
@@ -408,7 +409,7 @@ export struct PlaylistDetailPage {
             if (success) {
               ToastUtil.showToast('歌单删除成功')
               // 发送刷新事件
-              emitter.emit({ eventId: 2001 }, {})
+              emitter.emit({ eventId: EventConstants.EVENT_PLAYLIST_REFRESH }, {})
               router.back()
             } else {
               ToastUtil.showToast('歌单删除失败')

+ 2 - 1
entry/src/main/ets/pages/ScanFilePage.ets

@@ -10,6 +10,7 @@ import MediaTable from '../common/util/MediaTable'
 import { VideoItem } from '../viewmodel/VideoItem'
 import Logger from '../common/util/Logger'
 import { CommonConstants, STR_LOCK_VIDEO } from '../common/constants/CommonConstants'
+import { EventConstants } from '../common/constants/EventConstants'
 import { BusinessError, emitter } from '@kit.BasicServicesKit'
 import { BreakpointTypeEnum } from '../common/util/BreakpointSystem'
 import { resourceManager } from '@kit.LocalizationKit'
@@ -102,7 +103,7 @@ export struct ScanFilePage{
 
   endScan(isOnekey:boolean){
     const eventData: emitter.EventData = {};
-    emitter.emit({ eventId: 101 }, eventData); // 发送视频打开广播事件
+    emitter.emit({ eventId: EventConstants.EVENT_SCAN_UPDATE }, eventData); // 发送视频打开广播事件
     this.watchStatus(false)
     this.currentFilePath = '扫描文件入库成功!'
     if(isOnekey){

+ 2 - 1
entry/src/main/ets/pages/SettingPage.ets

@@ -1,6 +1,7 @@
 import TitleBar from '../view/TitleBar'
 import { promptAction, router } from '@kit.ArkUI'
 import { CommonConstants } from '../common/constants/CommonConstants'
+import { EventConstants } from '../common/constants/EventConstants'
 import { AppUtil, DeviceUtil, DisplayUtil, FileUtil, MD5, PreferencesUtil, ToastUtil } from '@pura/harmony-utils'
 import { CustomContentDialog } from '@kit.ArkUI'
 import ConfigurationConstant from '@ohos.app.ability.ConfigurationConstant';
@@ -2030,7 +2031,7 @@ export struct SettingPage {
   //发送广播通知更新UI
   sendChangeEvent() {
     const eventData: emitter.EventData = {};
-    emitter.emit({ eventId: 333 }, eventData); // 发送广播通知更新doSwipBack
+    emitter.emit({ eventId: EventConstants.EVENT_SETTING_UPDATE }, eventData); // 发送广播通知更新doSwipBack
   }
 
   goSelectImage() {

+ 4 - 3
entry/src/main/ets/pages/UserCenter.ets

@@ -9,6 +9,7 @@ import { http } from '@kit.NetworkKit';
 import { authentication } from '@kit.AccountKit';
 import { hilog } from '@kit.PerformanceAnalysisKit';
 import { BusinessError, emitter } from '@kit.BasicServicesKit';
+import { EventConstants } from '../common/constants/EventConstants';
 import { util } from '@kit.ArkTS';
 import { DialogHelper } from '@pura/harmony-dialog';
 import { Pay } from '@cashier_alipay/cashiersdk';
@@ -219,7 +220,7 @@ export struct UserCenter {
       }
     }
     await this.fetchUserInfo();
-    emitter.emit({ eventId: 1001 }, {})
+    emitter.emit({ eventId: EventConstants.EVENT_USER_STATE_CHANGE }, {})
   }
 
   // 通过华为账号信息换取用户信息和会员状态
@@ -234,7 +235,7 @@ export struct UserCenter {
       }
     }
     await this.fetchUserInfo();
-    emitter.emit({ eventId: 1001 }, {})
+    emitter.emit({ eventId: EventConstants.EVENT_USER_STATE_CHANGE }, {})
   }
 
   @Builder
@@ -562,7 +563,7 @@ export struct UserCenter {
                 this.subscriptionEndDate = '';
                 this.isForever=false;
                 UserUtil.logout();
-                emitter.emit({ eventId: 1001 }, {})
+                emitter.emit({ eventId: EventConstants.EVENT_USER_STATE_CHANGE }, {})
                 ToastUtil.showToast('已退出登录')
               })
           } else {

+ 11 - 10
entry/src/main/ets/view/LocalMusic.ets

@@ -22,6 +22,7 @@ import {
 import { imagePathToPixelMap } from '../common/util/CommUtils';
 import { unifiedDataChannel, uniformDataStruct, uniformTypeDescriptor } from '@kit.ArkData';
 import { CommonConstants } from '../common/constants/CommonConstants';
+import { EventConstants } from '../common/constants/EventConstants';
 import Logger from '../common/util/Logger';
 import { photoAccessHelper } from '@kit.MediaLibraryKit';
 import { BusinessError, emitter } from '@kit.BasicServicesKit';
@@ -544,7 +545,7 @@ export struct LocalMusic {
 
 
 
-    let eventMusic: emitter.InnerEvent = { eventId: 2 }
+    let eventMusic: emitter.InnerEvent = { eventId: EventConstants.EVENT_AUDIO_OPEN }
     // 监听广播事件(打开其他应用处理)
     emitter.on(eventMusic, (eventData: emitter.EventData) => {
       this.saveVideoDatas([eventData.data?.message], true)
@@ -552,7 +553,7 @@ export struct LocalMusic {
     });
 
     //侧滑广播接收时间
-    let eventUpdateSwipeBack: emitter.InnerEvent = { eventId: 888 }
+    let eventUpdateSwipeBack: emitter.InnerEvent = { eventId: EventConstants.EVENT_SWIPE_BACK_UPDATE }
     // 监听广播事件(打开其他应用处理)
     emitter.on(eventUpdateSwipeBack, (eventData: emitter.EventData) => {
 
@@ -563,14 +564,14 @@ export struct LocalMusic {
     });
 
     //ScanFilePage广播接收时间
-    let eventScanUpdate: emitter.InnerEvent = { eventId: 101 }
+    let eventScanUpdate: emitter.InnerEvent = { eventId: EventConstants.EVENT_SCAN_UPDATE }
     // 监听ScanFilePage广播事件(更新数据库)
     emitter.on(eventScanUpdate, (eventData: emitter.EventData) => {
       this.doUpdateData()
     });
 
     // 监听歌单播放请求事件
-    let eventPlaylistPlay: emitter.InnerEvent = { eventId: 2002 }
+    let eventPlaylistPlay: emitter.InnerEvent = { eventId: EventConstants.EVENT_PLAYLIST_PLAY }
     emitter.on(eventPlaylistPlay, (eventData: emitter.EventData) => {
       Logger.info('heanup eventPlaylistPlay received - full eventData: ' + JSON.stringify(eventData))
       Logger.info('heanup eventPlaylistPlay received - eventData.data: ' + JSON.stringify(eventData.data))
@@ -613,7 +614,7 @@ export struct LocalMusic {
       Logger.error('heanup eventPlaylistPlay: invalid data structure received')
     });
 
-    let eventSetting: emitter.InnerEvent = { eventId: 333 }
+    let eventSetting: emitter.InnerEvent = { eventId: EventConstants.EVENT_SETTING_UPDATE }
     // 监听广播事件(通用设置配置更新)
     emitter.on(eventSetting, (eventData: emitter.EventData) => {
       this.doChangeSetting()
@@ -1069,10 +1070,10 @@ export struct LocalMusic {
     console.info('LifeCycleComponent aboutToDisappear');
     this.knockController?.immersiveDisableListening();
     this.curIndex = 0
-    emitter.off(2);
-    emitter.off(101);
-    emitter.off(888);
-    emitter.off(333);
+    emitter.off(EventConstants.EVENT_AUDIO_OPEN);
+    emitter.off(EventConstants.EVENT_SCAN_UPDATE);
+    emitter.off(EventConstants.EVENT_SWIPE_BACK_UPDATE);
+    emitter.off(EventConstants.EVENT_SETTING_UPDATE);
     this.mDestroyPage = true;
     this.mIjkMediaPlayer.setScreenOnWhilePlaying(false);
     if (this.CONTROL_PlayStatus != PlayStatus.INIT) {
@@ -12027,7 +12028,7 @@ export struct LocalMusic {
 
     // 发送播放状态变化事件给歌单详情页面
     try {
-      const eventPlaybackStatus: emitter.InnerEvent = { eventId: 2003 }
+      const eventPlaybackStatus: emitter.InnerEvent = { eventId: EventConstants.EVENT_PLAYBACK_STATUS }
       const currentFilePath = this.currentSong?.filePath || ''
       const eventData: emitter.EventData = {
         data: {