| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- 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<MediaTable> {
- const table = new MediaTable(context)
- return new Promise<MediaTable>((resolve, reject) => {
- table.getRdbStore(context, (error?: Error) => {
- if (error) {
- reject(error)
- return
- }
- resolve(table)
- })
- })
- }
- function updateFavoriteAsync(table: MediaTable, filePath: string, isFav: number): Promise<boolean> {
- return new Promise<boolean>((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<void> {
- 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<PlayerFavoriteToggleResult> {
- 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
- }
- }
|