xref: /MusicFree/src/components/musicBar/index.tsx (revision c15039e228b46df3f5b7c5a9eb747a6097ad7be8)
1c446f2b8S猫头猫import React, {memo, useEffect, useState} from 'react';
24060c00aS猫头猫import {Keyboard, Pressable, StyleSheet, Text, View} from 'react-native';
3bf6e62f2S猫头猫import rpx from '@/utils/rpx';
4bf6e62f2S猫头猫import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
5242960d3S猫头猫import MusicQueue from '@/core/musicQueue';
64060c00aS猫头猫import {Avatar, IconButton, useTheme} from 'react-native-paper';
7bf6e62f2S猫头猫import {CircularProgressBase} from 'react-native-circular-progress-indicator';
8e7fa3837S猫头猫import {ROUTE_PATH, useNavigate} from '@/entry/router';
9bf6e62f2S猫头猫
10bf6e62f2S猫头猫import musicIsPaused from '@/utils/musicIsPaused';
11*c15039e2S猫头猫
12bf6e62f2S猫头猫import Color from 'color';
1319dc08ecS猫头猫import ThemeText from '../base/themeText';
141befdbcdS猫头猫import {ImgAsset} from '@/constants/assetsConst';
15c446f2b8S猫头猫import {useSafeAreaInsets} from 'react-native-safe-area-context';
16*c15039e2S猫头猫import {showPanel} from '../panels/usePanel';
17bf6e62f2S猫头猫
18c9af9657S猫头猫function CircularPlayBtn() {
19c9af9657S猫头猫    const progress = MusicQueue.useProgress();
20c9af9657S猫头猫    const musicState = MusicQueue.usePlaybackState();
21c9af9657S猫头猫    const {colors} = useTheme();
22c9af9657S猫头猫
23c9af9657S猫头猫    return (
24c9af9657S猫头猫        <CircularProgressBase
25c9af9657S猫头猫            activeStrokeWidth={rpx(4)}
26c9af9657S猫头猫            inActiveStrokeWidth={rpx(2)}
27c9af9657S猫头猫            inActiveStrokeOpacity={0.2}
28c9af9657S猫头猫            value={
29c9af9657S猫头猫                progress?.duration
30c9af9657S猫头猫                    ? (100 * progress.position) / progress.duration
31c9af9657S猫头猫                    : 0
32c9af9657S猫头猫            }
33c9af9657S猫头猫            duration={100}
34c9af9657S猫头猫            radius={rpx(36)}
35c9af9657S猫头猫            activeStrokeColor={colors.text}
36c9af9657S猫头猫            inActiveStrokeColor={Color(colors.text).alpha(0.5).toString()}>
37c9af9657S猫头猫            {musicIsPaused(musicState) ? (
38c9af9657S猫头猫                <IconButton
39c9af9657S猫头猫                    icon="play"
40c9af9657S猫头猫                    size={rpx(48)}
41c9af9657S猫头猫                    onPress={async () => {
42c9af9657S猫头猫                        await MusicQueue.play();
43c9af9657S猫头猫                    }}
44c9af9657S猫头猫                />
45c9af9657S猫头猫            ) : (
46c9af9657S猫头猫                <IconButton
47c9af9657S猫头猫                    icon="pause"
48c9af9657S猫头猫                    size={rpx(48)}
49c9af9657S猫头猫                    onPress={async () => {
50c9af9657S猫头猫                        await MusicQueue.pause();
51c9af9657S猫头猫                    }}
52c9af9657S猫头猫                />
53c9af9657S猫头猫            )}
54c9af9657S猫头猫        </CircularProgressBase>
55c9af9657S猫头猫    );
56c9af9657S猫头猫}
57bec1e603S猫头猫function MusicBar() {
58bf6e62f2S猫头猫    const musicItem = MusicQueue.useCurrentMusicItem();
59c9af9657S猫头猫
6000d0309bS猫头猫    const [showKeyboard, setKeyboardStatus] = useState(false);
61*c15039e2S猫头猫
62e7fa3837S猫头猫    const navigate = useNavigate();
63bf6e62f2S猫头猫    const {colors} = useTheme();
64c446f2b8S猫头猫    const safeAreaInsets = useSafeAreaInsets();
65bf6e62f2S猫头猫
6600d0309bS猫头猫    useEffect(() => {
6700d0309bS猫头猫        const showSubscription = Keyboard.addListener('keyboardDidShow', () => {
6800d0309bS猫头猫            setKeyboardStatus(true);
6900d0309bS猫头猫        });
7000d0309bS猫头猫        const hideSubscription = Keyboard.addListener('keyboardDidHide', () => {
7100d0309bS猫头猫            setKeyboardStatus(false);
7200d0309bS猫头猫        });
7300d0309bS猫头猫
7400d0309bS猫头猫        return () => {
7500d0309bS猫头猫            showSubscription.remove();
7600d0309bS猫头猫            hideSubscription.remove();
7700d0309bS猫头猫        };
7800d0309bS猫头猫    }, []);
7900d0309bS猫头猫
80bf6e62f2S猫头猫    return (
81c446f2b8S猫头猫        <>
8200d0309bS猫头猫            {musicItem && !showKeyboard && (
83bf6e62f2S猫头猫                <Pressable
84bf6e62f2S猫头猫                    style={[
85bf6e62f2S猫头猫                        style.wrapper,
864060c00aS猫头猫                        {
874060c00aS猫头猫                            backgroundColor: Color(colors.primary)
884060c00aS猫头猫                                .alpha(0.66)
894060c00aS猫头猫                                .toString(),
90c446f2b8S猫头猫                            paddingLeft: safeAreaInsets.left + rpx(24),
913b155a65S猫头猫                            paddingRight: safeAreaInsets.right + rpx(24),
924060c00aS猫头猫                        },
93bf6e62f2S猫头猫                    ]}
94bf6e62f2S猫头猫                    onPress={() => {
95e7fa3837S猫头猫                        navigate(ROUTE_PATH.MUSIC_DETAIL);
96bf6e62f2S猫头猫                    }}>
97bf6e62f2S猫头猫                    <View style={style.artworkWrapper}>
98bf6e62f2S猫头猫                        <Avatar.Image
99bf6e62f2S猫头猫                            size={rpx(96)}
1001befdbcdS猫头猫                            source={
1015e267aa1S猫头猫                                musicItem?.artwork
1021befdbcdS猫头猫                                    ? {
103bf6e62f2S猫头猫                                          uri: musicItem.artwork,
1041befdbcdS猫头猫                                      }
1051befdbcdS猫头猫                                    : ImgAsset.albumDefault
1064060c00aS猫头猫                            }
1074060c00aS猫头猫                        />
108bf6e62f2S猫头猫                    </View>
109bf6e62f2S猫头猫                    <Text
110bf6e62f2S猫头猫                        ellipsizeMode="tail"
111bf6e62f2S猫头猫                        style={style.textWrapper}
112bf6e62f2S猫头猫                        numberOfLines={1}>
1134060c00aS猫头猫                        <ThemeText fontSize="content">
1145e267aa1S猫头猫                            {musicItem?.title}
1154060c00aS猫头猫                        </ThemeText>
116bf6e62f2S猫头猫                        {musicItem?.artist && (
1174060c00aS猫头猫                            <ThemeText
1184060c00aS猫头猫                                fontSize="description"
1194060c00aS猫头猫                                fontColor="secondary">
1201befdbcdS猫头猫                                {' '}
1211befdbcdS猫头猫                                -{musicItem.artist}
1221befdbcdS猫头猫                            </ThemeText>
123bf6e62f2S猫头猫                        )}
124bf6e62f2S猫头猫                    </Text>
125bf6e62f2S猫头猫                    <View style={style.actionGroup}>
126c9af9657S猫头猫                        <CircularPlayBtn />
127bf6e62f2S猫头猫
128bf6e62f2S猫头猫                        <Icon
129bf6e62f2S猫头猫                            name="playlist-music"
130bf6e62f2S猫头猫                            size={rpx(56)}
131bf6e62f2S猫头猫                            onPress={() => {
132bf6e62f2S猫头猫                                showPanel('PlayList');
133bf6e62f2S猫头猫                            }}
1344060c00aS猫头猫                            style={[style.actionIcon, {color: colors.text}]}
1354060c00aS猫头猫                        />
136bf6e62f2S猫头猫                    </View>
137bf6e62f2S猫头猫                </Pressable>
138bf6e62f2S猫头猫            )}
139c446f2b8S猫头猫        </>
140bf6e62f2S猫头猫    );
141bf6e62f2S猫头猫}
142bf6e62f2S猫头猫
143bec1e603S猫头猫export default memo(MusicBar, () => true);
144bec1e603S猫头猫
145bf6e62f2S猫头猫const style = StyleSheet.create({
146bf6e62f2S猫头猫    wrapper: {
147c446f2b8S猫头猫        width: '100%',
148bf6e62f2S猫头猫        height: rpx(120),
149bf6e62f2S猫头猫        flexDirection: 'row',
150bf6e62f2S猫头猫        alignItems: 'center',
151bf6e62f2S猫头猫        paddingHorizontal: rpx(24),
152bf6e62f2S猫头猫    },
153bf6e62f2S猫头猫    artworkWrapper: {
154bf6e62f2S猫头猫        height: rpx(120),
155bf6e62f2S猫头猫        width: rpx(120),
156bf6e62f2S猫头猫    },
157bf6e62f2S猫头猫    textWrapper: {
158bf6e62f2S猫头猫        flexGrow: 1,
159c446f2b8S猫头猫        flexShrink: 1,
160bf6e62f2S猫头猫    },
161bf6e62f2S猫头猫    actionGroup: {
162bf6e62f2S猫头猫        width: rpx(200),
163bf6e62f2S猫头猫        justifyContent: 'flex-end',
164bf6e62f2S猫头猫        flexDirection: 'row',
165bf6e62f2S猫头猫        alignItems: 'center',
166bf6e62f2S猫头猫    },
167bf6e62f2S猫头猫    actionIcon: {
168bf6e62f2S猫头猫        marginLeft: rpx(36),
169bf6e62f2S猫头猫    },
170bf6e62f2S猫头猫});
171