* feat: added new check for isAudio and added the supported mime types * feat: adding the progress and audio on the audio file message * feat: finishing the layout of the audio_file * feat: play and pause audio * feat: update the progress bar when audio is playing * feat: update the timeframe of the audio * feat: update with the new design * feat: adding download and preview * feat: creates a hook for the file download and preview * feat: adding useCallback to make the return stable * fix: iOS issue when playing in loop * feat: adding localization for the error * fix: removing code tha was inserted for debug * feat: add a new line on the en.json * fix: fixing types * feat: adding the onSeek method inside the progress bar * feat: changing progress value to animated value * feat: changing to GestureDetector and making the seek method work * feat: adding a touchable without feedback to prevent the audio to triggering other page * feat: adding the drag on the seek method * feat: making the download button more gray * fix: fix tests * fix: prevent onProgress from clearing the seconds when the audio is paused * feat: clamping the position of the cursor to never be dragged offscreen * feat: enhancing the experience of dragging the cursor on the progress bar * feat: differentiate between the throttles * feat: remvoing the aac audio files support * feat: pausing if the focus has changed * feat: render differently the audio file when in the files list * refactor: optimize audio file component by using useCallback for event handlers * refactor: extract stopPropagation function for better readability in AudioFile component * feat: implement custom useThrottled hook and replace lodash throttle in AudioFile component * refactor: update useThrottled hook to accept a generic callback type * fix: loading of uri of audio files after merge * feat: add audioFile style to enhance layout for audio files * fix: tests
161 lines
5 KiB
TypeScript
161 lines
5 KiB
TypeScript
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
import React, {useCallback, useEffect} from 'react';
|
|
import {type LayoutChangeEvent, type StyleProp, View, type ViewStyle, StyleSheet} from 'react-native';
|
|
import {Gesture, GestureDetector} from 'react-native-gesture-handler';
|
|
import Animated, {clamp, runOnJS, useAnimatedStyle, useSharedValue, withTiming} from 'react-native-reanimated';
|
|
|
|
import {useTheme} from '@context/theme';
|
|
import {changeOpacity} from '@utils/theme';
|
|
|
|
type ProgressBarProps = {
|
|
color: string;
|
|
progress: number;
|
|
withCursor?: boolean;
|
|
style?: StyleProp<ViewStyle>;
|
|
onSeek?: (position: number) => void;
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
container: {
|
|
position: 'relative',
|
|
justifyContent: 'center',
|
|
paddingVertical: 10,
|
|
},
|
|
progressBarContainer: {
|
|
height: 4,
|
|
borderRadius: 2,
|
|
overflow: 'hidden',
|
|
width: '100%',
|
|
},
|
|
progressBar: {
|
|
flex: 1,
|
|
},
|
|
cursor: {
|
|
position: 'absolute',
|
|
borderRadius: 100,
|
|
width: 15,
|
|
height: 15,
|
|
},
|
|
});
|
|
|
|
const START_CURSOR_VALUE = 0;
|
|
const END_CURSOR_VALUE = 1;
|
|
|
|
const ProgressBar = ({color, progress, withCursor, style, onSeek}: ProgressBarProps) => {
|
|
const theme = useTheme();
|
|
const widthValue = useSharedValue(0);
|
|
const progressValue = useSharedValue(progress);
|
|
const isGestureActive = useSharedValue(false);
|
|
|
|
// eslint-disable-next-line new-cap
|
|
const panGesture = Gesture.Pan().
|
|
onStart(() => {
|
|
isGestureActive.value = true;
|
|
}).
|
|
onChange((e) => {
|
|
if (onSeek) {
|
|
const clampedSeekPosition = clamp(e.x / widthValue.value, START_CURSOR_VALUE, END_CURSOR_VALUE);
|
|
runOnJS(onSeek)(clampedSeekPosition);
|
|
}
|
|
}).
|
|
onEnd(() => {
|
|
isGestureActive.value = false;
|
|
}).
|
|
onFinalize(() => {
|
|
isGestureActive.value = false;
|
|
});
|
|
|
|
// eslint-disable-next-line new-cap
|
|
const tapGesture = Gesture.Tap().
|
|
onStart(() => {
|
|
isGestureActive.value = true;
|
|
}).
|
|
onEnd((e) => {
|
|
if (onSeek) {
|
|
const clampedSeekPosition = clamp(e.x / widthValue.value, START_CURSOR_VALUE, END_CURSOR_VALUE);
|
|
runOnJS(onSeek)(clampedSeekPosition);
|
|
}
|
|
isGestureActive.value = false;
|
|
}).
|
|
onFinalize(() => {
|
|
isGestureActive.value = false;
|
|
});
|
|
|
|
// eslint-disable-next-line new-cap
|
|
const composedGestures = Gesture.Race(tapGesture, panGesture);
|
|
|
|
const progressAnimatedStyle = useAnimatedStyle(() => {
|
|
const translateX = ((progressValue.value * 0.5) - 0.5) * widthValue.value;
|
|
const scaleX = progressValue.value ? progressValue.value : 0.0001;
|
|
|
|
return {
|
|
transform: [
|
|
{translateX: isGestureActive.value ? translateX : withTiming(translateX, {duration: 200})},
|
|
{scaleX: isGestureActive.value ? scaleX : withTiming(scaleX, {duration: 200})},
|
|
],
|
|
width: widthValue.value,
|
|
};
|
|
});
|
|
|
|
const cursorAnimatedStyle = useAnimatedStyle(() => {
|
|
const cursorWidth = 15;
|
|
const leftPosition = (progressValue.value * widthValue.value) - (cursorWidth / 2);
|
|
|
|
return {
|
|
left: isGestureActive.value ? leftPosition : withTiming(leftPosition, {duration: 100}),
|
|
};
|
|
});
|
|
|
|
useEffect(() => {
|
|
progressValue.value = progress;
|
|
}, [progress]);
|
|
|
|
const onLayout = useCallback((e: LayoutChangeEvent) => {
|
|
widthValue.value = e.nativeEvent.layout.width;
|
|
}, []);
|
|
|
|
return (
|
|
<GestureDetector gesture={composedGestures}>
|
|
<View
|
|
onTouchStart={(e) => e.stopPropagation()}
|
|
style={styles.container}
|
|
>
|
|
<View
|
|
onLayout={onLayout}
|
|
style={[
|
|
styles.progressBarContainer,
|
|
{
|
|
backgroundColor: withCursor ? changeOpacity(theme.centerChannelColor, 0.2) : 'rgba(255, 255, 255, 0.16)',
|
|
},
|
|
style,
|
|
]}
|
|
>
|
|
<Animated.View
|
|
style={[
|
|
styles.progressBar,
|
|
{
|
|
backgroundColor: color,
|
|
},
|
|
progressAnimatedStyle,
|
|
]}
|
|
/>
|
|
</View>
|
|
|
|
{withCursor &&
|
|
<Animated.View
|
|
style={[
|
|
styles.cursor,
|
|
{
|
|
backgroundColor: color,
|
|
},
|
|
cursorAnimatedStyle,
|
|
]}
|
|
/>}
|
|
</View>
|
|
</GestureDetector>
|
|
);
|
|
};
|
|
|
|
export default ProgressBar;
|