import MediaTable from '../../common/util/MediaTable' import { cloneVideoItem, isRemoteCloudType, isWebDavType } from '../../common/util/RemotePlayerUtil' import { VideoItem } from '../../viewmodel/VideoItem' import { StrUtil } from '@pura/harmony-utils' import { Context } from '@kit.AbilityKit' export interface PlayerFavoriteToggleResult { success: boolean message: string updatedItem?: VideoItem isFavorite: boolean } function openMediaTable(context: Context): Promise { const table = new MediaTable(context) return new Promise((resolve, reject) => { table.getRdbStore(context, (error?: Error) => { if (error) { reject(error) return } resolve(table) }) }) } function updateFavoriteAsync(table: MediaTable, filePath: string, isFav: number): Promise { return new Promise((resolve) => { table.updateIsFavByFilePath(filePath, isFav, (success: boolean) => { resolve(success) }) }) } function resolveRemoteStoragePath(item: VideoItem): string { if (!item || !isRemoteCloudType(item.type)) { return item?.filePath ?? '' } if (isWebDavType(item.type)) { const storagePath = item.remote_rel_path || item.filePath if (StrUtil.isNotEmpty(storagePath)) { item.remote_rel_path = storagePath return storagePath } return item.filePath } if (StrUtil.isNotEmpty(item.remote_rel_path)) { return item.remote_rel_path as string } return item.filePath } async function persistRemoteFavoriteTarget(context: Context, item: VideoItem): Promise { const storagePath = resolveRemoteStoragePath(item) if (StrUtil.isEmpty(storagePath)) { return } const table = await openMediaTable(context) const cloneItem = cloneVideoItem(item) cloneItem.filePath = storagePath cloneItem.remote_rel_path = storagePath await table.saveOrUpdateWebDavItem(cloneItem) } export function resolveNextFavoriteValue(isFavorite: boolean): number { return isFavorite ? 0 : 1 } export function resolvePlayerFavoriteTargetFilePath(item: VideoItem): string { if (!item) { return '' } if (!isRemoteCloudType(item.type)) { return item.filePath } return resolveRemoteStoragePath(item) } export async function toggleSongFavorite(context: Context, item: VideoItem, currentIsFavorite: boolean): Promise { if (!item || StrUtil.isEmpty(item.filePath)) { return { success: false, message: '当前歌曲不存在', isFavorite: currentIsFavorite } } const nextIsFav = resolveNextFavoriteValue(currentIsFavorite) const targetFilePath = resolvePlayerFavoriteTargetFilePath(item) const table = await openMediaTable(context) let updated = await updateFavoriteAsync(table, targetFilePath, nextIsFav) if (!updated && isRemoteCloudType(item.type)) { await persistRemoteFavoriteTarget(context, item) updated = await updateFavoriteAsync(table, targetFilePath, nextIsFav) } if (!updated) { return { success: false, message: nextIsFav === 1 ? '收藏失败' : '取消收藏失败', isFavorite: currentIsFavorite } } item.isFav = nextIsFav const updatedItem = cloneVideoItem(item) updatedItem.isFav = nextIsFav return { success: true, message: nextIsFav === 1 ? '收藏成功' : '取消收藏成功', updatedItem, isFavorite: nextIsFav === 1 } }