import React, { useCallback, useEffect } from 'react'
import { BackHandler, StatusBar, StyleSheet, Text, View } from 'react-native'
import { State } from 'react-native-track-player'
import IconFA from 'react-native-vector-icons/FontAwesome'
import IconFA5 from 'react-native-vector-icons/FontAwesome5'
import Icon from 'react-native-vector-icons/Ionicons'
import IconMatCom from 'react-native-vector-icons/MaterialCommunityIcons'
import IconMat from 'react-native-vector-icons/MaterialIcons'
import colors from '@app/styles/colors'
import font from '@app/styles/font'
import formatDuration from '@app/util/formatDuration'
import CoverArt from '@app/components/CoverArt'
import ImageGradientBackground from '@app/components/ImageGradientBackground'
import PressableOpacity from '@app/components/PressableOpacity'
import dimensions from '@app/styles/dimensions'
import { NativeStackScreenProps } from 'react-native-screens/native-stack'
import { useFocusEffect } from '@react-navigation/native'
import { useStore } from '@app/state/store'
import { selectTrackPlayer } from '@app/state/trackplayer'
import { useNext, usePause, usePlay, usePrevious, useToggleShuffle } from '@app/hooks/trackplayer'
const NowPlayingHeader = React.memo<{
backHandler: () => void
}>(({ backHandler }) => {
const queueName = useStore(selectTrackPlayer.name)
return (
{queueName || 'Nothing playing...'}
)
})
const headerStyles = StyleSheet.create({
container: {
height: dimensions.header,
width: '100%',
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
},
icons: {
height: 42,
width: 42,
marginHorizontal: 8,
},
queueName: {
fontFamily: font.bold,
fontSize: 16,
color: colors.text.primary,
flex: 1,
textAlign: 'center',
},
})
const SongCoverArt = () => {
const track = useStore(selectTrackPlayer.currentTrack)
return (
)
}
const coverArtStyles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
paddingBottom: 20,
},
image: {
height: '100%',
width: '100%',
},
})
const SongInfo = () => {
const track = useStore(selectTrackPlayer.currentTrack)
return (
{track?.title}
{track?.artist}
)
}
const infoStyles = StyleSheet.create({
container: {
width: '100%',
flexDirection: 'row',
},
details: {
flex: 1,
marginRight: 20,
},
controls: {
justifyContent: 'center',
},
title: {
height: 28,
fontFamily: font.bold,
fontSize: 22,
color: colors.text.primary,
},
artist: {
height: 20,
fontFamily: font.regular,
fontSize: 16,
color: colors.text.secondary,
},
})
const SeekBar = () => {
const { position, duration } = useStore(selectTrackPlayer.progress)
let progress = 0
if (duration > 0) {
progress = position / duration
}
return (
{formatDuration(position)}
{formatDuration(duration)}
)
}
const seekStyles = StyleSheet.create({
container: {
width: '100%',
marginTop: 26,
},
barContainer: {
flexDirection: 'row',
alignItems: 'center',
marginBottom: 6,
},
bars: {
backgroundColor: colors.text.primary,
height: 4,
},
barLeft: {
marginRight: -6,
},
barRight: {
opacity: 0.3,
marginLeft: -6,
},
indicator: {
height: 12,
width: 12,
borderRadius: 6,
backgroundColor: colors.text.primary,
elevation: 1,
},
textContainer: {
flexDirection: 'row',
justifyContent: 'space-between',
},
text: {
fontFamily: font.regular,
fontSize: 15,
color: colors.text.primary,
},
})
const PlayerControls = () => {
const state = useStore(selectTrackPlayer.playerState)
const play = usePlay()
const pause = usePause()
const next = useNext()
const previous = usePrevious()
const shuffled = useStore(selectTrackPlayer.shuffled)
const toggleShuffle = useToggleShuffle()
let playPauseIcon: string
let playPauseAction: undefined | (() => void)
let disabled: boolean
switch (state) {
case State.Playing:
case State.Buffering:
case State.Connecting:
disabled = false
playPauseIcon = 'pause-circle'
playPauseAction = pause
break
default:
disabled = false
playPauseIcon = 'play-circle'
playPauseAction = play
break
}
return (
toggleShuffle()} disabled={disabled}>
)
}
const controlsStyles = StyleSheet.create({
container: {
width: '100%',
},
top: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
paddingBottom: 8,
},
bottom: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
paddingTop: 10,
paddingBottom: 54,
},
play: {
marginHorizontal: 30,
},
center: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
},
})
type RootStackParamList = {
main: undefined
'now-playing': undefined
}
type NowPlayingProps = NativeStackScreenProps
const NowPlayingView: React.FC = ({ navigation }) => {
const track = useStore(selectTrackPlayer.currentTrack)
const back = useCallback(() => {
if (navigation.canGoBack()) {
navigation.popToTop()
} else {
navigation.navigate('main')
}
return true
}, [navigation])
useEffect(() => {
if (!track) {
back()
}
})
useFocusEffect(
useCallback(() => {
BackHandler.addEventListener('hardwareBackPress', back)
return () => BackHandler.removeEventListener('hardwareBackPress', back)
}, [back]),
)
return (
)
}
const styles = StyleSheet.create({
container: {
flex: 1,
paddingTop: StatusBar.currentHeight,
},
content: {
flex: 1,
paddingHorizontal: 30,
},
})
export default NowPlayingView