Ver código fonte

歌单添加歌曲的时候 由于分页功能需要获取媒体库重新写

onecold 7 meses atrás
pai
commit
5de34c6334

+ 60 - 7
entry/src/main/ets/dialog/AddSongsToPlaylistDialog.ets

@@ -1,6 +1,6 @@
 import { DialogHelper } from '@pura/harmony-dialog';
 import { DialogHelper } from '@pura/harmony-dialog';
 import { Playlist } from '../viewmodel/Playlist';
 import { Playlist } from '../viewmodel/Playlist';
-import { ToastUtil, LogUtil, StrUtil, FileUtil, PreferencesUtil } from '@pura/harmony-utils';
+import { ToastUtil, LogUtil, StrUtil, FileUtil, PreferencesUtil, AppUtil } from '@pura/harmony-utils';
 import { VideoItem } from '../viewmodel/VideoItem';
 import { VideoItem } from '../viewmodel/VideoItem';
 import MediaTable from '../common/util/MediaTable';
 import MediaTable from '../common/util/MediaTable';
 import PlaylistTable from '../common/util/PlaylistTable';
 import PlaylistTable from '../common/util/PlaylistTable';
@@ -11,6 +11,7 @@ import { SegmentButton } from '@kit.ArkUI';
 import { SegmentButtonOptions, SegmentButtonItemTuple } from '@kit.ArkUI';
 import { SegmentButtonOptions, SegmentButtonItemTuple } from '@kit.ArkUI';
 import { Utility } from '../common/util/Utility';
 import { Utility } from '../common/util/Utility';
 import fs from '@ohos.file.fs';
 import fs from '@ohos.file.fs';
+import Logger from '../common/util/Logger';
 
 
 // 工具函数:将 #RRGGBB 字符串和透明度转为 'rgba(r,g,b,a)' 字符串,深色模式下可返回深色变体
 // 工具函数:将 #RRGGBB 字符串和透明度转为 'rgba(r,g,b,a)' 字符串,深色模式下可返回深色变体
 function themeColorWithAlpha(themeColor: string, alpha: number, isDarkMode: boolean = false): string {
 function themeColorWithAlpha(themeColor: string, alpha: number, isDarkMode: boolean = false): string {
@@ -26,7 +27,7 @@ function themeColorWithAlpha(themeColor: string, alpha: number, isDarkMode: bool
 }
 }
 
 
 // 定义SegmentButton按钮元组类型
 // 定义SegmentButton按钮元组类型
-const viewModeButtons: SegmentButtonItemTuple = [{ text: '全部' }, { text: '目录' }];
+const viewModeButtons: SegmentButtonItemTuple = [{ text: '目录' }, { text: '全部' }];
 
 
 /**
 /**
  * 添加歌曲到歌单对话框内容组件
  * 添加歌曲到歌单对话框内容组件
@@ -50,7 +51,7 @@ struct AddSongsToPlaylistDialogContent {
   private listScroller: ListScroller = new ListScroller()
   private listScroller: ListScroller = new ListScroller()
   @State isSearchMode: boolean = false
   @State isSearchMode: boolean = false
   @Prop playlist: Playlist
   @Prop playlist: Playlist
-  @StorageProp('mediaKuList') mediaKuList: Array<VideoItem> = []; //媒体库文件
+  @State mediaKuList: Array<VideoItem> = []; //媒体库文件
   private mediaTable: MediaTable = new MediaTable(getContext(this))
   private mediaTable: MediaTable = new MediaTable(getContext(this))
   private playlistTable: PlaylistTable = new PlaylistTable(getContext(this))
   private playlistTable: PlaylistTable = new PlaylistTable(getContext(this))
   @State dataSource: LazyDataSource<VideoItem> = new LazyDataSource([])
   @State dataSource: LazyDataSource<VideoItem> = new LazyDataSource([])
@@ -66,7 +67,7 @@ struct AddSongsToPlaylistDialogContent {
   onCancel?: () => void
   onCancel?: () => void
 
 
   // 添加目录浏览相关状态
   // 添加目录浏览相关状态
-  @State @Watch('onViewModeChange') viewMode: number[] = [0]; // 0: 全部歌曲, 1: 目录模式
+  @State @Watch('onViewModeChange') viewMode: number[] = [0]; // 0: 目录模式, 1: 全部歌曲
   @State currentPath: string = '';
   @State currentPath: string = '';
   @State folderItems: VideoItem[] = [];
   @State folderItems: VideoItem[] = [];
   @State selectedFolders: Set<string> = new Set();
   @State selectedFolders: Set<string> = new Set();
@@ -87,6 +88,18 @@ struct AddSongsToPlaylistDialogContent {
   });
   });
 
 
   async aboutToAppear() {
   async aboutToAppear() {
+
+    // 查询媒体库列表 - 调用现有的 queryMediaKuList 函数
+    try {
+      const context = getContext(this)
+      const packageName =  AppUtil.getBundleName()
+      this.mediaKuList = await queryMediaKuList(context, packageName)
+      LogUtil.info('heanup AddSongsToPlaylistDialog', '成功获取媒体库列表, 数量: ' + this.mediaKuList.length)
+    } catch (error) {
+      LogUtil.error('heanup AddSongsToPlaylistDialog', '获取媒体库列表失败: ' + (error as Error).message)
+      this.mediaKuList = []
+    }
+
     // 初始化深色模式状态
     // 初始化深色模式状态
     this.isDarkMode = this.currentMode === ConfigurationConstant.ColorMode.COLOR_MODE_DARK
     this.isDarkMode = this.currentMode === ConfigurationConstant.ColorMode.COLOR_MODE_DARK
     LogUtil.info('heanup  AddSongsToPlaylistDialogContent aboutToAppear 开始')
     LogUtil.info('heanup  AddSongsToPlaylistDialogContent aboutToAppear 开始')
@@ -235,7 +248,7 @@ struct AddSongsToPlaylistDialogContent {
   }
   }
 
 
   onViewModeChange() {
   onViewModeChange() {
-    if (this.viewMode && this.viewMode[0] === 1) {
+    if (this.viewMode && this.viewMode[0] === 0) {
       this.loadFile(this.currentPath)
       this.loadFile(this.currentPath)
     } else {
     } else {
       this.updateSelectedCount()
       this.updateSelectedCount()
@@ -623,7 +636,7 @@ struct AddSongsToPlaylistDialogContent {
 
 
           Blank()
           Blank()
 
 
-          if (this.viewMode[0] === 0) {
+          if (this.viewMode[0] === 1) {
             Button((this.selectedSongs.length === this.dataSource.dataArray.length
             Button((this.selectedSongs.length === this.dataSource.dataArray.length
               && this.dataSource.dataArray.length > 0) ? '全不选' : '全选')
               && this.dataSource.dataArray.length > 0) ? '全不选' : '全选')
               .fontSize(12)
               .fontSize(12)
@@ -660,7 +673,7 @@ struct AddSongsToPlaylistDialogContent {
       }
       }
 
 
       // 歌曲列表或目录列表
       // 歌曲列表或目录列表
-      if (this.viewMode[0] === 0) {
+      if (this.viewMode[0] === 1) {
         // 全部歌曲模式
         // 全部歌曲模式
         if (this.dataSource.dataArray.length === 0 && this.isSearchMode) {
         if (this.dataSource.dataArray.length === 0 && this.isSearchMode) {
           Column({ space: 12 }) {
           Column({ space: 12 }) {
@@ -1061,3 +1074,43 @@ export function showAddSongsToPlaylistDialog(
 }
 }
 
 
 
 
+//获取当前媒体库歌曲列表
+async function queryMediaKuList(context: Context, packageName: string): Promise<VideoItem[]> {
+  const table: MediaTable = new MediaTable(context);
+  try {
+
+    await new Promise<void>((resolve, reject) => {
+      table.getRdbStore(context, (err: Error) => {
+        err ? reject(err) : resolve();
+      });
+    });
+
+    // 2. 查询媒体库(isVideo=1)音乐
+    const originalList: VideoItem[] = await new Promise((resolve, reject) => {
+      table.query(0, (result: VideoItem[] | null, err?: Error) => {
+        if (err) {
+          reject(err);
+        } else {
+          resolve(result || []);
+        }
+      });
+    });
+
+    // 3. 过滤出本地音乐(filePath包含包名且type为TYPE_LOCAL)
+    const filteredList: VideoItem[] = originalList.filter((item: VideoItem) => {
+      // 检查是否是本地音乐(filePath包含包名)
+      if (item.filePath && item.type === CommonConstants.TYPE_LOCAL && item.filePath.includes(packageName)) {
+        return true;
+      }
+      return false;
+    });
+
+    Logger.info('heanup queryMediaKuList', '成功查询到 ' + originalList.length + ' 首歌曲, 过滤后本地音乐: ' + filteredList.length + ' 首')
+    return filteredList;
+
+  } catch (err) {
+    Logger.error('heanup queryMediaKuList', `查询失败: ${(err as Error).message}`);
+    return [];
+  }
+}
+

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

@@ -104,7 +104,7 @@ export struct ScanFilePage{
     setTimeout(()=>{
     setTimeout(()=>{
       // 显示校正数据对话框
       // 显示校正数据对话框
       this.showCorrectDataDialog(false)
       this.showCorrectDataDialog(false)
-    },50000)
+    },30000)
 
 
 
 
   }
   }

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

@@ -399,8 +399,7 @@ export struct LocalMusic {
   @Consume videoLocalList:VideoItem[]
   @Consume videoLocalList:VideoItem[]
   @State dataSource: LazyDataSource<VideoItem> = new LazyDataSource(this.videoLocalList)
   @State dataSource: LazyDataSource<VideoItem> = new LazyDataSource(this.videoLocalList)
   @State listRefreshKey: number = 0
   @State listRefreshKey: number = 0
-  // @State mediaKuList: Array<VideoItem> = []; //媒体库文件
-  @StorageProp('mediaKuList')  mediaKuList: Array<VideoItem> = []; //媒体库文件
+  @State mediaKuList: Array<VideoItem> = []; //媒体库文件
   @State artistList: Array<VideoItem> = []; //艺术家文件
   @State artistList: Array<VideoItem> = []; //艺术家文件
   @State artistMap: Map<string, VideoItem[]> = new Map(); //艺术家Map
   @State artistMap: Map<string, VideoItem[]> = new Map(); //艺术家Map
   @State artistSongCountMap: Map<string, number> = new Map();
   @State artistSongCountMap: Map<string, number> = new Map();