import React, { useState, useCallback, useEffect, useRef } from 'react'; import TrackPlayer, { usePlaybackState, STATE_PLAYING, STATE_PAUSED } from 'react-native-track-player'; import { TouchableOpacity, useColorScheme } from 'react-native'; import styled from 'styled-components/native'; import { useHasQueue } from 'utility/useQueue'; import ForwardIcon from 'assets/forwards.svg'; import BackwardIcon from 'assets/backwards.svg'; import PlayIcon from 'assets/play.svg'; import PauseIcon from 'assets/pause.svg'; import RepeatIcon from 'assets/repeat.svg'; // import ShuffleIcon from 'assets/shuffle.svg'; import { THEME_COLOR } from 'CONSTANTS'; import Casting from './Casting'; const BUTTON_SIZE = 40; const BUTTON_SIZE_SMALL = 25; const pause = () => TrackPlayer.pause(); const play = () => TrackPlayer.play(); const next = () => TrackPlayer.skipToNext(); const previous = () => TrackPlayer.skipToPrevious(); const Container = styled.View` align-items: center; margin: 20px 0; `; const Buttons = styled.View` flex-direction: row; align-items: center; justify-content: space-between; width: 100%; `; const Button = styled.View` margin: 20px 40px; `; export default function MediaControls() { const scheme = useColorScheme(); const fill = scheme === 'dark' ? '#ffffff' : '#000000'; return ( ); } export function PreviousButton({ fill }: { fill: string }) { const hasQueue = useHasQueue(); return ( ); } export function NextButton({ fill }: { fill: string }) { const hasQueue = useHasQueue(); return ( ); } export function RepeatButton({ fill }: { fill: string}) { const [isRepeating, setRepeating] = useState(false); const handlePress = useCallback(() => setRepeating(!isRepeating), [isRepeating, setRepeating]); const listener = useRef(null); // The callback that should determine whether we need to repeeat or not const handleEndEvent = useCallback(async () => { if (isRepeating) { // Retrieve all current tracks const tracks = await TrackPlayer.getQueue(); // Then skip to the first track await TrackPlayer.skip(tracks[0].id); // Cautiously reset the seek time, as there might only be a single // item in queue. await TrackPlayer.seekTo(0); // Then play the item await TrackPlayer.play(); } }, [isRepeating]); // Subscribe to ended event handler so that we can restart the queue from // the start if looping is enabled useEffect(() => { // Set the event listener listener.current = TrackPlayer.addEventListener('playback-queue-ended', handleEndEvent); // Then clean up after return function cleanup() { listener?.current?.remove(); }; }, [handleEndEvent]); return ( ); } // export function ShuffleButton({ fill }: { fill: string}) { // const [isShuffling, setShuffling] = useState(false); // const handlePress = useCallback(() => setShuffling(!isShuffling), [isShuffling, setShuffling]); // return ( // // // // ); // } export function MainButton({ fill }: { fill: string }) { const state = usePlaybackState(); switch (state) { case STATE_PLAYING: return ( ); case STATE_PAUSED: return ( ); default: return ( ); } }