xref: /MusicFree/src/pages/musicDetail/components/bottom/seekBar.tsx (revision 5500cea7e936041b68a2f3709a583c2f0181b9e6)
1b1581818S猫头猫import React, {useRef, useState} from 'react';
2bf6e62f2S猫头猫import {StyleSheet, Text, View} from 'react-native';
3bf6e62f2S猫头猫import rpx from '@/utils/rpx';
4bf6e62f2S猫头猫import Slider from '@react-native-community/slider';
5bf6e62f2S猫头猫import timeformat from '@/utils/timeformat';
6bf6e62f2S猫头猫import {fontSizeConst} from '@/constants/uiConst';
7*5500cea7S猫头猫import TrackPlayer from '@/core/trackPlayer';
8bf6e62f2S猫头猫
9b1581818S猫头猫interface ITimeLabelProps {
10b1581818S猫头猫    time: number;
11b1581818S猫头猫}
12b1581818S猫头猫
13b1581818S猫头猫function TimeLabel(props: ITimeLabelProps) {
14b1581818S猫头猫    return <Text style={style.text}>{timeformat(props.time)}</Text>;
15b1581818S猫头猫}
16b1581818S猫头猫
174060c00aS猫头猫export default function SeekBar() {
18*5500cea7S猫头猫    const progress = TrackPlayer.useProgress(1000);
19bf6e62f2S猫头猫    const [tmpProgress, setTmpProgress] = useState<number | null>(null);
20b1581818S猫头猫    const slidingRef = useRef(false);
21bf6e62f2S猫头猫
22bf6e62f2S猫头猫    return (
23bf6e62f2S猫头猫        <View style={style.wrapper}>
24b1581818S猫头猫            <TimeLabel time={tmpProgress ?? progress.position} />
25bf6e62f2S猫头猫            <Slider
26bf6e62f2S猫头猫                style={style.slider}
27a92b50b7S猫头猫                minimumTrackTintColor={'#cccccc'}
28a92b50b7S猫头猫                maximumTrackTintColor={'#999999'}
29a92b50b7S猫头猫                thumbTintColor={'#dddddd'}
30bf6e62f2S猫头猫                minimumValue={0}
31bf6e62f2S猫头猫                maximumValue={progress.duration}
32b1581818S猫头猫                onSlidingStart={() => {
33b1581818S猫头猫                    slidingRef.current = true;
34b1581818S猫头猫                }}
354060c00aS猫头猫                onValueChange={val => {
36b1581818S猫头猫                    if (slidingRef.current) {
37bf6e62f2S猫头猫                        setTmpProgress(val);
38b1581818S猫头猫                    }
39bf6e62f2S猫头猫                }}
404060c00aS猫头猫                onSlidingComplete={val => {
41b1581818S猫头猫                    slidingRef.current = false;
42bf6e62f2S猫头猫                    setTmpProgress(null);
439477b0e5S猫头猫                    if (val >= progress.duration - 2) {
449477b0e5S猫头猫                        val = progress.duration - 2;
45ef714860S猫头猫                    }
46*5500cea7S猫头猫                    TrackPlayer.seekTo(val);
47bf6e62f2S猫头猫                }}
484060c00aS猫头猫                value={progress.position}
494060c00aS猫头猫            />
50b1581818S猫头猫            <TimeLabel time={progress.duration} />
51bf6e62f2S猫头猫        </View>
52bf6e62f2S猫头猫    );
53bf6e62f2S猫头猫}
54bf6e62f2S猫头猫
55bf6e62f2S猫头猫const style = StyleSheet.create({
56bf6e62f2S猫头猫    wrapper: {
574245d81aS猫头猫        width: '100%',
58bf6e62f2S猫头猫        height: rpx(40),
59bf6e62f2S猫头猫        justifyContent: 'center',
60bf6e62f2S猫头猫        alignItems: 'center',
61bf6e62f2S猫头猫        flexDirection: 'row',
62bf6e62f2S猫头猫    },
63bf6e62f2S猫头猫    slider: {
644245d81aS猫头猫        width: '73%',
65bf6e62f2S猫头猫        height: rpx(40),
66bf6e62f2S猫头猫    },
67bf6e62f2S猫头猫    text: {
68ec26b768S猫头猫        fontSize: fontSizeConst.description,
69bf6e62f2S猫头猫        includeFontPadding: false,
704060c00aS猫头猫        color: '#cccccc',
714060c00aS猫头猫    },
72bf6e62f2S猫头猫});
73