tooot/src/components/Timeline/Shared/Attachment/Audio.tsx

159 lines
4.7 KiB
TypeScript
Raw Normal View History

2020-12-26 23:05:17 +01:00
import Button from '@components/Button'
2021-01-18 00:23:40 +01:00
import GracefullyImage from '@components/GracefullyImage'
import { Slider } from '@sharcoux/slider'
import { StyleConstants } from '@utils/styles/constants'
import { useTheme } from '@utils/styles/ThemeManager'
import { Audio } from 'expo-av'
import React, { useCallback, useEffect, useRef, useState } from 'react'
import { AppState, AppStateStatus, StyleSheet, View } from 'react-native'
2021-01-28 01:31:19 +01:00
import { Blurhash } from 'react-native-blurhash'
2022-06-03 23:18:24 +02:00
import AttachmentAltText from './AltText'
2021-01-18 00:23:40 +01:00
import attachmentAspectRatio from './aspectRatio'
2020-12-25 21:09:43 +01:00
export interface Props {
2021-01-18 00:23:40 +01:00
total: number
index: number
2020-12-25 21:09:43 +01:00
sensitiveShown: boolean
audio: Mastodon.AttachmentAudio
}
const AttachmentAudio: React.FC<Props> = ({ total, index, sensitiveShown, audio }) => {
2022-02-12 14:51:01 +01:00
const { colors } = useTheme()
2020-12-25 21:09:43 +01:00
const [audioPlayer, setAudioPlayer] = useState<Audio.Sound>()
const [audioPlaying, setAudioPlaying] = useState(false)
const [audioPosition, setAudioPosition] = useState(0)
const playAudio = useCallback(async () => {
if (!audioPlayer) {
const { sound } = await Audio.Sound.createAsync(
{ uri: audio.url },
{},
// @ts-ignore
props => setAudioPosition(props.positionMillis)
)
setAudioPlayer(sound)
} else {
await audioPlayer.setPositionAsync(audioPosition)
audioPlayer.playAsync()
setAudioPlaying(true)
}
}, [audioPlayer, audioPosition])
const pauseAudio = useCallback(async () => {
audioPlayer!.pauseAsync()
setAudioPlaying(false)
}, [audioPlayer])
2021-01-18 00:23:40 +01:00
const appState = useRef(AppState.currentState)
useEffect(() => {
const appState = AppState.addEventListener('change', _handleAppStateChange)
return () => appState.remove()
}, [])
const _handleAppStateChange = async (nextAppState: AppStateStatus) => {
if (appState.current.match(/active/) && nextAppState.match(/inactive/)) {
await audioPlayer?.stopAsync()
}
appState.current = nextAppState
}
2020-12-25 21:09:43 +01:00
return (
2021-01-18 00:23:40 +01:00
<View
2021-04-09 21:43:12 +02:00
accessibilityLabel={audio.description}
2021-01-18 00:23:40 +01:00
style={[
styles.base,
{
2022-02-12 14:51:01 +01:00
backgroundColor: colors.disabled,
2021-01-18 00:23:40 +01:00
aspectRatio: attachmentAspectRatio({ total, index })
}
]}
>
2020-12-26 23:05:17 +01:00
<View style={styles.overlay}>
2020-12-25 21:09:43 +01:00
{sensitiveShown ? (
2021-01-28 01:31:19 +01:00
audio.blurhash ? (
<Blurhash
blurhash={audio.blurhash}
2020-12-25 21:09:43 +01:00
style={{
width: '100%',
height: '100%'
}}
2021-01-28 01:31:19 +01:00
/>
) : null
2020-12-25 21:09:43 +01:00
) : (
<>
2021-06-11 23:07:41 +02:00
{audio.preview_url ? (
2021-01-16 00:00:31 +01:00
<GracefullyImage
uri={{
2021-01-28 01:14:00 +01:00
original: audio.preview_url,
remote: audio.preview_remote_url
2021-01-16 00:00:31 +01:00
}}
2020-12-25 21:09:43 +01:00
style={styles.background}
/>
2021-06-11 23:07:41 +02:00
) : null}
2020-12-26 23:05:17 +01:00
<Button
type='icon'
content={audioPlaying ? 'PauseCircle' : 'PlayCircle'}
2020-12-25 21:09:43 +01:00
size='L'
2020-12-28 16:54:19 +01:00
round
2020-12-26 23:05:17 +01:00
overlay
{...(audioPlaying ? { onPress: pauseAudio } : { onPress: playAudio })}
2020-12-25 21:09:43 +01:00
/>
</>
)}
2020-12-26 23:05:17 +01:00
</View>
2022-01-08 11:22:07 +01:00
{audio.meta?.original?.duration ? (
2021-03-23 23:16:01 +01:00
<View
style={{
alignSelf: 'flex-end',
width: '100%',
height: StyleConstants.Spacing.M + StyleConstants.Spacing.S * 2,
2022-02-12 14:51:01 +01:00
backgroundColor: colors.backgroundOverlayInvert,
2021-03-23 23:16:01 +01:00
paddingHorizontal: StyleConstants.Spacing.Global.PagePadding,
borderRadius: 100,
opacity: sensitiveShown ? 0.35 : undefined
}}
>
<Slider
minimumValue={0}
maximumValue={audio.meta.original.duration * 1000}
value={audioPosition}
2022-02-12 14:51:01 +01:00
minimumTrackTintColor={colors.secondary}
maximumTrackTintColor={colors.disabled}
2021-03-23 23:16:01 +01:00
// onSlidingStart={() => {
// audioPlayer?.pauseAsync()
// setAudioPlaying(false)
// }}
// onSlidingComplete={value => {
// setAudioPosition(value)
// }}
enabled={false} // Bug in above sliding actions
thumbSize={StyleConstants.Spacing.M}
2022-02-12 14:51:01 +01:00
thumbTintColor={colors.primaryOverlay}
2021-03-23 23:16:01 +01:00
/>
</View>
) : null}
<AttachmentAltText sensitiveShown={sensitiveShown} text={audio.description} />
2020-12-28 16:54:19 +01:00
</View>
2020-12-25 21:09:43 +01:00
)
}
const styles = StyleSheet.create({
2020-12-28 16:54:19 +01:00
base: {
flex: 1,
flexBasis: '50%',
padding: StyleConstants.Spacing.XS / 2,
flexDirection: 'row'
2020-12-28 16:54:19 +01:00
},
2020-12-25 21:09:43 +01:00
background: { position: 'absolute', width: '100%', height: '100%' },
overlay: {
position: 'absolute',
width: '100%',
height: '100%',
flex: 1,
justifyContent: 'center',
alignItems: 'center'
}
})
export default AttachmentAudio