xref: /MusicFree/src/pages/musicDetail/components/bottom/seekBar.tsx (revision 26f326364ffffc501faab6b3f1563421273758ae)
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';
75500cea7S猫头猫import TrackPlayer from '@/core/trackPlayer';
8bf6e62f2S猫头猫
9b1581818S猫头猫interface ITimeLabelProps {
10b1581818S猫头猫    time: number;
11b1581818S猫头猫}
12b1581818S猫头猫
13b1581818S猫头猫function TimeLabel(props: ITimeLabelProps) {
14*26f32636S猫头猫    return (
15*26f32636S猫头猫        <Text style={style.text}>{timeformat(Math.max(props.time, 0))}</Text>
16*26f32636S猫头猫    );
17b1581818S猫头猫}
18b1581818S猫头猫
194060c00aS猫头猫export default function SeekBar() {
205500cea7S猫头猫    const progress = TrackPlayer.useProgress(1000);
21bf6e62f2S猫头猫    const [tmpProgress, setTmpProgress] = useState<number | null>(null);
22b1581818S猫头猫    const slidingRef = useRef(false);
23bf6e62f2S猫头猫
24bf6e62f2S猫头猫    return (
25bf6e62f2S猫头猫        <View style={style.wrapper}>
26b1581818S猫头猫            <TimeLabel time={tmpProgress ?? progress.position} />
27bf6e62f2S猫头猫            <Slider
28bf6e62f2S猫头猫                style={style.slider}
29a92b50b7S猫头猫                minimumTrackTintColor={'#cccccc'}
30a92b50b7S猫头猫                maximumTrackTintColor={'#999999'}
31a92b50b7S猫头猫                thumbTintColor={'#dddddd'}
32bf6e62f2S猫头猫                minimumValue={0}
33bf6e62f2S猫头猫                maximumValue={progress.duration}
34b1581818S猫头猫                onSlidingStart={() => {
35b1581818S猫头猫                    slidingRef.current = true;
36b1581818S猫头猫                }}
374060c00aS猫头猫                onValueChange={val => {
38b1581818S猫头猫                    if (slidingRef.current) {
39bf6e62f2S猫头猫                        setTmpProgress(val);
40b1581818S猫头猫                    }
41bf6e62f2S猫头猫                }}
424060c00aS猫头猫                onSlidingComplete={val => {
43b1581818S猫头猫                    slidingRef.current = false;
44bf6e62f2S猫头猫                    setTmpProgress(null);
459477b0e5S猫头猫                    if (val >= progress.duration - 2) {
469477b0e5S猫头猫                        val = progress.duration - 2;
47ef714860S猫头猫                    }
485500cea7S猫头猫                    TrackPlayer.seekTo(val);
49bf6e62f2S猫头猫                }}
504060c00aS猫头猫                value={progress.position}
514060c00aS猫头猫            />
52b1581818S猫头猫            <TimeLabel time={progress.duration} />
53bf6e62f2S猫头猫        </View>
54bf6e62f2S猫头猫    );
55bf6e62f2S猫头猫}
56bf6e62f2S猫头猫
57bf6e62f2S猫头猫const style = StyleSheet.create({
58bf6e62f2S猫头猫    wrapper: {
594245d81aS猫头猫        width: '100%',
60bf6e62f2S猫头猫        height: rpx(40),
61bf6e62f2S猫头猫        justifyContent: 'center',
62bf6e62f2S猫头猫        alignItems: 'center',
63bf6e62f2S猫头猫        flexDirection: 'row',
64bf6e62f2S猫头猫    },
65bf6e62f2S猫头猫    slider: {
664245d81aS猫头猫        width: '73%',
67bf6e62f2S猫头猫        height: rpx(40),
68bf6e62f2S猫头猫    },
69bf6e62f2S猫头猫    text: {
70ec26b768S猫头猫        fontSize: fontSizeConst.description,
71bf6e62f2S猫头猫        includeFontPadding: false,
724060c00aS猫头猫        color: '#cccccc',
734060c00aS猫头猫    },
74bf6e62f2S猫头猫});
75