refactor: 将 player.ts 里面的Mode.PLAYLIST改为 Mode.TrackList

This commit is contained in:
qier222 2022-04-16 21:35:37 +08:00
parent 7b6579e068
commit 0061a66124
No known key found for this signature in database
GPG key ID: 9C85007ED905F14D
6 changed files with 22 additions and 12 deletions

View file

@ -102,7 +102,7 @@ const Controls = () => {
return ( return (
<div className='flex items-center justify-center gap-2 text-white'> <div className='flex items-center justify-center gap-2 text-white'>
{mode === PlayerMode.PLAYLIST && ( {mode === PlayerMode.TrackList && (
<IconButton <IconButton
onClick={() => track && player.prevTrack()} onClick={() => track && player.prevTrack()}
disabled={!track} disabled={!track}

View file

@ -95,7 +95,7 @@ const MediaControls = () => {
return ( return (
<div className='flex items-center justify-center gap-2 text-black dark:text-white'> <div className='flex items-center justify-center gap-2 text-black dark:text-white'>
{mode === PlayerMode.PLAYLIST && ( {mode === PlayerMode.TrackList && (
<IconButton <IconButton
onClick={() => track && player.prevTrack()} onClick={() => track && player.prevTrack()}
disabled={!track} disabled={!track}

View file

@ -83,7 +83,7 @@ const Playlists = () => {
> >
<span className='line-clamp-1'>{playlist.name}</span> <span className='line-clamp-1'>{playlist.name}</span>
{playlistMode === TrackListSourceType.PLAYLIST && {playlistMode === TrackListSourceType.PLAYLIST &&
mode === Mode.PLAYLIST && mode === Mode.TrackList &&
currentPlaylistID === playlist.id && ( currentPlaylistID === playlist.id && (
<SvgIcon className='h-5 w-5' name='volume-half' /> <SvgIcon className='h-5 w-5' name='volume-half' />
)} )}

View file

@ -37,10 +37,15 @@ const PlayButton = ({
const playerSnapshot = useSnapshot(player) const playerSnapshot = useSnapshot(player)
const isThisAlbumPlaying = useMemo( const isThisAlbumPlaying = useMemo(
() => () =>
playerSnapshot.mode === PlayerMode.PLAYLIST && playerSnapshot.mode === PlayerMode.TrackList &&
playerSnapshot.trackListSource?.type === TrackListSourceType.ALBUM && playerSnapshot.trackListSource?.type === TrackListSourceType.ALBUM &&
playerSnapshot.trackListSource?.id === album?.id, playerSnapshot.trackListSource?.id === album?.id,
[playerSnapshot.trackListSource, album?.id] [
playerSnapshot.mode,
playerSnapshot.trackListSource?.type,
playerSnapshot.trackListSource?.id,
album?.id,
]
) )
const isPlaying = const isPlaying =

View file

@ -32,10 +32,15 @@ const PlayButton = ({
const playerSnapshot = useSnapshot(player) const playerSnapshot = useSnapshot(player)
const isThisPlaylistPlaying = useMemo( const isThisPlaylistPlaying = useMemo(
() => () =>
playerSnapshot.mode === PlayerMode.PLAYLIST && playerSnapshot.mode === PlayerMode.TrackList &&
playerSnapshot.trackListSource?.type === TrackListSourceType.PLAYLIST && playerSnapshot.trackListSource?.type === TrackListSourceType.PLAYLIST &&
playerSnapshot.trackListSource?.id === playlist?.id, playerSnapshot.trackListSource?.id === playlist?.id,
[playerSnapshot.trackListSource, playlist?.id] [
playerSnapshot.mode,
playerSnapshot.trackListSource?.id,
playerSnapshot.trackListSource?.type,
playlist?.id,
]
) )
const wrappedHandlePlay = () => { const wrappedHandlePlay = () => {

View file

@ -22,7 +22,7 @@ interface TrackListSource {
id: number id: number
} }
export enum Mode { export enum Mode {
PLAYLIST = 'playlist', TrackList = 'trackList',
FM = 'fm', FM = 'fm',
} }
export enum State { export enum State {
@ -49,7 +49,7 @@ export class Player {
private _volume: number = 1 // 0 to 1 private _volume: number = 1 // 0 to 1
state: State = State.INITIALIZING state: State = State.INITIALIZING
mode: Mode = Mode.PLAYLIST mode: Mode = Mode.TrackList
trackList: TrackID[] = [] trackList: TrackID[] = []
trackListSource: TrackListSource | null = null trackListSource: TrackListSource | null = null
fmTrackList: TrackID[] = [] fmTrackList: TrackID[] = []
@ -115,7 +115,7 @@ export class Player {
* Get current playing track ID * Get current playing track ID
*/ */
get trackID(): TrackID { get trackID(): TrackID {
if (this.mode === Mode.PLAYLIST) { if (this.mode === Mode.TrackList) {
const { trackList, _trackIndex } = this const { trackList, _trackIndex } = this
return trackList[_trackIndex] ?? 0 return trackList[_trackIndex] ?? 0
} }
@ -199,7 +199,7 @@ export class Player {
toast('加载歌曲信息失败') toast('加载歌曲信息失败')
return return
} }
if (this.mode === Mode.PLAYLIST) this._track = track if (this.mode === Mode.TrackList) this._track = track
if (this.mode === Mode.FM) this.fmTrack = track if (this.mode === Mode.FM) this.fmTrack = track
this._playAudio() this._playAudio()
} }
@ -370,7 +370,7 @@ export class Player {
* @param {null|number} autoPlayTrackID * @param {null|number} autoPlayTrackID
*/ */
playAList(list: TrackID[], autoPlayTrackID?: null | number) { playAList(list: TrackID[], autoPlayTrackID?: null | number) {
this.mode = Mode.PLAYLIST this.mode = Mode.TrackList
this.trackList = list this.trackList = list
this._trackIndex = autoPlayTrackID this._trackIndex = autoPlayTrackID
? list.findIndex(t => t === autoPlayTrackID) ? list.findIndex(t => t === autoPlayTrackID)