Skip to content

Commit

Permalink
refactor: playSong
Browse files Browse the repository at this point in the history
  • Loading branch information
lovegaoshi committed Oct 18, 2023
1 parent f8c910d commit 676d55a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
27 changes: 6 additions & 21 deletions src/components/playlist/PlaylistList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { View, BackHandler, StyleSheet, ImageBackground } from 'react-native';
import { FlashList } from '@shopify/flash-list';
import Snackbar from 'react-native-snackbar';
import { IconButton } from 'react-native-paper';
import TrackPlayer, { RepeatMode } from 'react-native-track-player';
import { useTranslation } from 'react-i18next';
import { useDebounce } from 'use-debounce';
import { useNetInfo } from '@react-native-community/netinfo';
Expand All @@ -19,12 +18,8 @@ import { updateSubscribeFavList } from '@utils/BiliSubscribe';
import { PLAYLIST_ENUMS, SearchRegex } from '@enums/Playlist';
import { syncFavlist } from '@utils/Bilibili/bilifavOperate';
import noxCache, { noxCacheKey } from '@utils/Cache';
import noxPlayingList from '@stores/playingList';
import { NoxRepeatMode } from '@enums/RepeatMode';
import { i0hdslbHTTPResolve } from '@utils/Utils';
import { songlistToTracklist } from '@utils/RNTPUtils';

const { getState } = noxPlayingList;
import usePlayback from '@hooks/usePlayback';

interface BackgroundProps {
song: NoxMedia.Song;
Expand All @@ -48,14 +43,10 @@ const SongBackground = (props: BackgroundProps) => {

const PlaylistList = () => {
const { t } = useTranslation();
const setCurrentPlayingList = useNoxSetting(
state => state.setCurrentPlayingList
);
const currentPlayingList = useNoxSetting(state => state.currentPlayingList);
const currentPlayingId = useNoxSetting(state => state.currentPlayingId);
const playerSetting = useNoxSetting(state => state.playerSetting);
const playerStyle = useNoxSetting(state => state.playerStyle);
const setCurrentPlayingId = useNoxSetting(state => state.setCurrentPlayingId);
const currentPlaylist = useNoxSetting(state => state.currentPlaylist);
const playlistShouldReRender = useNoxSetting(
state => state.playlistShouldReRender
Expand All @@ -80,6 +71,7 @@ const PlaylistList = () => {
const togglePlaylistInfoUpdate = useNoxSetting(
state => state.togglePlaylistInfoUpdate
);
const { playFromPlaylist } = usePlayback();

const resetSelected = (val = false) =>
setSelected(Array(currentPlaylist.songList.length).fill(val));
Expand Down Expand Up @@ -200,18 +192,11 @@ const PlaylistList = () => {
currentPlaylist.id === currentPlayingList.id
)
return;
if (getState().playmode === NoxRepeatMode.REPEAT_TRACK) {
await TrackPlayer.setRepeatMode(RepeatMode.Off);
}
await TrackPlayer.reset();

const queuedSongList = playerSetting.keepSearchedSongListWhenPlaying
? currentRows
: currentPlaylist.songList;
setCurrentPlayingList({ ...currentPlaylist, songList: queuedSongList });
setCurrentPlayingId(song.id);
await TrackPlayer.add(await songlistToTracklist([song]));
TrackPlayer.play();
return;
playFromPlaylist({ ...currentPlaylist, songList: queuedSongList }, song);
};

const refreshPlaylist = async () => {
Expand Down Expand Up @@ -239,8 +224,8 @@ const PlaylistList = () => {
const currentIndex =
toIndex < 0
? currentPlaylist.songList.findIndex(
song => song.id === currentPlayingId
)
song => song.id === currentPlayingId
)
: toIndex;
if (currentIndex > -1) {
playlistRef.current?.scrollToIndex({
Expand Down
8 changes: 8 additions & 0 deletions src/hooks/usePlayback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@ import TrackPlayer, {
Event,
State,
useActiveTrack,
RepeatMode,
} from 'react-native-track-player';
import { useTranslation } from 'react-i18next';

import { useNoxSetting } from './useSetting';
import { randomChoice } from '../utils/Utils';
import logger from '../utils/Logger';
import { songlistToTracklist } from '@utils/RNTPUtils';
import { NoxRepeatMode } from '@enums/RepeatMode';
import noxPlayingList from '@stores/playingList';

const PLAYLIST_MEDIAID = 'playlist-';

const { getState } = noxPlayingList;

const usePlayback = () => {
const { t } = useTranslation();
const currentPlayingList = useNoxSetting(state => state.currentPlayingList);
Expand All @@ -39,6 +44,9 @@ const usePlayback = () => {
interruption = false
) => {
setCurrentPlayingList(playlist);
if (getState().playmode === NoxRepeatMode.REPEAT_TRACK) {
await TrackPlayer.setRepeatMode(RepeatMode.Off);
}
if (song === undefined) {
if (playlist.songList.length === 0) {
// no song exists.
Expand Down

0 comments on commit 676d55a

Please sign in to comment.