Skip to content

Commit

Permalink
chore: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
lovegaoshi committed Jan 8, 2025
1 parent 2b35217 commit f252098
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 48 deletions.
34 changes: 5 additions & 29 deletions src/components/miniplayer/MiniControls.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import TrackPlayer, {
usePlaybackState,
State,
usePlayWhenReady,
} from 'react-native-track-player';
import { useDebouncedValue } from 'hooks';
import TrackPlayer from 'react-native-track-player';
import { ActivityIndicator, IconButton, Text } from 'react-native-paper';
import { Dimensions, TouchableWithoutFeedback, View } from 'react-native';
import Animated, {
Expand All @@ -13,6 +8,7 @@ import Animated, {

import { fadePause } from '@utils/RNTPUtils';
import useTPControls from '@hooks/useTPControls';
import usePlaybackState from '@hooks/usePlaybackState';
import { styles } from '../style';
import { useTrackStore } from '@hooks/useActiveTrack';
import { MinPlayerHeight } from './Constants';
Expand Down Expand Up @@ -96,37 +92,17 @@ export default ({ miniplayerHeight, expand }: Props) => {

const PlayPauseButton = () => {
const playerStyle = useNoxSetting(state => state.playerStyle);
const playback = usePlaybackState();
const playWhenReady = usePlayWhenReady();
const isLoading = useDebouncedValue(
playback.state === State.Loading, // || state === State.Buffering
250,
);
const isErrored = playback.state === State.Error;
const isEnded = playback.state === State.Ended;
const showPause = playWhenReady && !(isErrored || isEnded);
const showBuffering = isErrored || (playWhenReady && isLoading);
const { showPause, showBuffering } = usePlaybackState();

if (showBuffering) {
return <ActivityIndicator size={IconSize - 8} style={iconContainerStyle} />;
}

if (showPause) {
return (
<IconButton
iconColor={playerStyle.colors.primary}
icon="pause"
size={IconSize}
onPress={fadePause}
/>
);
}
return (
<IconButton
iconColor={playerStyle.colors.primary}
icon="play"
icon={showPause ? 'pause' : 'play'}
size={IconSize}
onPress={TrackPlayer.play}
onPress={showPause ? fadePause : TrackPlayer.play}
/>
);
};
23 changes: 5 additions & 18 deletions src/components/player/controls/PlayPauseButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,19 @@ import React from 'react';
import { StyleSheet } from 'react-native';
import { Image } from 'expo-image';
import { ActivityIndicator } from 'react-native-paper';
import TrackPlayer, {
State,
usePlayWhenReady,
} from 'react-native-track-player';
import TrackPlayer from 'react-native-track-player';

import { useDebouncedValue } from 'hooks';
import usePlaybackState from '@hooks/usePlaybackState';
import { useNoxSetting } from '@stores/useApp';
import LottieButtonAnimated from '@components/buttons/LottieButtonAnimated';
import { fadePause } from '@utils/RNTPUtils';

interface Props {
state: State | undefined;
iconSize?: number;
}
export const PlayPauseButton: React.FC<Props> = ({ state, iconSize = 50 }) => {
export const PlayPauseButton: React.FC<Props> = ({ iconSize = 50 }) => {
const playerStyle = useNoxSetting(state => state.playerStyle);
const playWhenReady = usePlayWhenReady();
const isLoading = useDebouncedValue(
state === State.Loading, // || state === State.Buffering
250,
);

const isErrored = state === State.Error;
const isEnded = state === State.Ended;
const showPause = playWhenReady && !(isErrored || isEnded);
const showBuffering = isErrored || (playWhenReady && isLoading);
const { showPause, showBuffering } = usePlaybackState();
const iconContainerStyle = { width: iconSize + 16, height: iconSize + 16 };

if (showBuffering) {
Expand All @@ -44,7 +31,7 @@ export const PlayPauseButton: React.FC<Props> = ({ state, iconSize = 50 }) => {
<LottieButtonAnimated
src={require('@assets/lottie/PauseGoAndBack.json')}
size={iconSize}
onPress={showPause ? fadePause : () => TrackPlayer.play()}
onPress={showPause ? fadePause : TrackPlayer.play}
clicked={!showPause}
strokes={['Play', 'Play 2', 'Pause', 'Pause 3']}
/>
Expand Down
2 changes: 1 addition & 1 deletion src/components/player/controls/PlayerControls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const PlayerControls: React.FC = () => {
style={{ backgroundColor: undefined }}
/>
<View style={styles.btnSpacer} />
<PlayPauseButton state={playback.state} />
<PlayPauseButton />
<View style={styles.btnSpacer} />
<LottieButton
src={require('@assets/lottie/skip-forwards.json')}
Expand Down
30 changes: 30 additions & 0 deletions src/hooks/usePlaybackState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {
usePlaybackState,
State,
usePlayWhenReady,
} from 'react-native-track-player';
import { useDebouncedValue } from 'hooks';

const NotLoading = [State.Paused, State.Playing, State.Stopped, State.Ended];

export default () => {
const playback = usePlaybackState();
const playWhenReady = usePlayWhenReady();
const isLoading = useDebouncedValue(
!NotLoading.includes(playback.state!),
250,
);
const isErrored = playback.state === State.Error;
const isEnded = playback.state === State.Ended;
const showPause = playWhenReady && !(isErrored || isEnded);
const showBuffering = isErrored || (playWhenReady && isLoading);

return {
isLoading,
isErrored,
isEnded,
showPause,
showBuffering,
playback,
};
};

0 comments on commit f252098

Please sign in to comment.