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

135 lines
4.0 KiB
TypeScript
Raw Normal View History

2020-12-26 23:05:17 +01:00
import Button from '@components/Button'
import { Slider } from '@sharcoux/slider'
import { StyleConstants } from '@utils/styles/constants'
import { useTheme } from '@utils/styles/ThemeManager'
import { Audio } from 'expo-av'
2020-12-25 21:09:43 +01:00
import { Surface } from 'gl-react-expo'
import { Blurhash } from 'gl-react-blurhash'
import React, { useCallback, useState } from 'react'
2021-01-16 00:00:31 +01:00
import { StyleSheet, View } from 'react-native'
import GracefullyImage from '@components/GracefullyImage'
2020-12-25 21:09:43 +01:00
export interface Props {
sensitiveShown: boolean
audio: Mastodon.AttachmentAudio
}
const AttachmentAudio: React.FC<Props> = ({ sensitiveShown, audio }) => {
const { theme } = useTheme()
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-16 00:00:31 +01:00
console.log(audio)
2020-12-25 21:09:43 +01:00
return (
<View style={[styles.base, { backgroundColor: theme.disabled }]}>
2020-12-26 23:05:17 +01:00
<View style={styles.overlay}>
2020-12-25 21:09:43 +01:00
{sensitiveShown ? (
audio.blurhash && (
<Surface
style={{
width: '100%',
height: '100%'
}}
>
<Blurhash hash={audio.blurhash} />
</Surface>
)
) : (
<>
{(audio.preview_url || audio.preview_remote_url) && (
2021-01-16 00:00:31 +01:00
<GracefullyImage
uri={{
original: audio.preview_url || audio.preview_remote_url
}}
2020-12-25 21:09:43 +01:00
style={styles.background}
/>
)}
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
2020-12-25 21:09:43 +01:00
{...(audioPlaying
? { onPress: pauseAudio }
: { onPress: playAudio })}
/>
</>
)}
2020-12-26 23:05:17 +01:00
</View>
2020-12-25 21:09:43 +01:00
<View
style={{
alignSelf: 'flex-end',
2020-12-28 16:54:19 +01:00
width: '100%',
height: StyleConstants.Spacing.M + StyleConstants.Spacing.S * 2,
2020-12-25 21:09:43 +01:00
backgroundColor: theme.backgroundOverlay,
paddingHorizontal: StyleConstants.Spacing.Global.PagePadding,
borderRadius: 100,
2020-12-25 21:09:43 +01:00
opacity: sensitiveShown ? 0.35 : undefined
}}
>
<Slider
minimumValue={0}
maximumValue={audio.meta.original.duration * 1000}
value={audioPosition}
2020-12-26 14:40:10 +01:00
minimumTrackTintColor={theme.secondary}
maximumTrackTintColor={theme.disabled}
// onSlidingStart={() => {
// console.log('yes!!!')
// audioPlayer?.pauseAsync()
// setAudioPlaying(false)
// }}
// onSlidingComplete={value => {
// console.log('no!!!')
// setAudioPosition(value)
// }}
enabled={false} // Bug in above sliding actions
thumbSize={StyleConstants.Spacing.M}
thumbTintColor={theme.primaryOverlay}
2020-12-25 21:09:43 +01:00
/>
</View>
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%',
aspectRatio: 16 / 9,
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