xref: /MusicFree/src/components/dialogs/components/editSheetDetail.tsx (revision e26be108c965cd41dde5b06bd2e5d5eee097e759)
1*e26be108S猫头猫import React, {useState} from 'react';
2*e26be108S猫头猫import {Button, Dialog, TextInput} from 'react-native-paper';
3*e26be108S猫头猫import useColors from '@/hooks/useColors';
4*e26be108S猫头猫import rpx from '@/utils/rpx';
5*e26be108S猫头猫import {StyleSheet, View} from 'react-native';
6*e26be108S猫头猫import useDialog from '../useDialog';
7*e26be108S猫头猫import ThemeText from '@/components/base/themeText';
8*e26be108S猫头猫import {ImgAsset} from '@/constants/assetsConst';
9*e26be108S猫头猫import {TouchableOpacity} from 'react-native-gesture-handler';
10*e26be108S猫头猫import {launchImageLibrary} from 'react-native-image-picker';
11*e26be108S猫头猫import pathConst from '@/constants/pathConst';
12*e26be108S猫头猫import Image from '@/components/base/image';
13*e26be108S猫头猫import {copyFile} from 'react-native-fs';
14*e26be108S猫头猫import MusicSheet from '@/core/musicSheet';
15*e26be108S猫头猫import {addFileScheme} from '@/utils/fileUtils';
16*e26be108S猫头猫
17*e26be108S猫头猫interface IEditSheetDetailProps {
18*e26be108S猫头猫    musicSheet: IMusic.IMusicSheetItem;
19*e26be108S猫头猫}
20*e26be108S猫头猫export default function EditSheetDetailDialog(props: IEditSheetDetailProps) {
21*e26be108S猫头猫    const {musicSheet} = props;
22*e26be108S猫头猫    const colors = useColors();
23*e26be108S猫头猫    const {hideDialog} = useDialog();
24*e26be108S猫头猫
25*e26be108S猫头猫    const [coverImg, setCoverImg] = useState(musicSheet?.coverImg);
26*e26be108S猫头猫    const [title, setTitle] = useState(musicSheet?.title);
27*e26be108S猫头猫
28*e26be108S猫头猫    // onCover
29*e26be108S猫头猫
30*e26be108S猫头猫    const onChangeCoverPress = async () => {
31*e26be108S猫头猫        try {
32*e26be108S猫头猫            const result = await launchImageLibrary({
33*e26be108S猫头猫                mediaType: 'photo',
34*e26be108S猫头猫            });
35*e26be108S猫头猫            const uri = result.assets?.[0].uri;
36*e26be108S猫头猫            if (!uri) {
37*e26be108S猫头猫                return;
38*e26be108S猫头猫            }
39*e26be108S猫头猫            console.log(uri);
40*e26be108S猫头猫            setCoverImg(uri);
41*e26be108S猫头猫        } catch (e) {
42*e26be108S猫头猫            console.log(e);
43*e26be108S猫头猫        }
44*e26be108S猫头猫    };
45*e26be108S猫头猫
46*e26be108S猫头猫    function onTitleChange(_: string) {
47*e26be108S猫头猫        setTitle(_);
48*e26be108S猫头猫    }
49*e26be108S猫头猫
50*e26be108S猫头猫    async function onConfirm() {
51*e26be108S猫头猫        // 判断是否相同
52*e26be108S猫头猫        if (coverImg === musicSheet?.coverImg && title === musicSheet?.title) {
53*e26be108S猫头猫            hideDialog();
54*e26be108S猫头猫        }
55*e26be108S猫头猫
56*e26be108S猫头猫        let _coverImg = coverImg;
57*e26be108S猫头猫        if (_coverImg) {
58*e26be108S猫头猫            _coverImg = addFileScheme(
59*e26be108S猫头猫                `${pathConst.dataPath}background${_coverImg.substring(
60*e26be108S猫头猫                    _coverImg.lastIndexOf('.'),
61*e26be108S猫头猫                )}`,
62*e26be108S猫头猫            );
63*e26be108S猫头猫            await copyFile(coverImg!, _coverImg);
64*e26be108S猫头猫        }
65*e26be108S猫头猫        let _title = title;
66*e26be108S猫头猫        if (!title?.length) {
67*e26be108S猫头猫            _title = musicSheet.title;
68*e26be108S猫头猫        }
69*e26be108S猫头猫        // 更新歌单信息
70*e26be108S猫头猫        MusicSheet.updateAndSaveSheet(musicSheet.id, {
71*e26be108S猫头猫            basic: {
72*e26be108S猫头猫                coverImg: _coverImg,
73*e26be108S猫头猫                title: _title,
74*e26be108S猫头猫            },
75*e26be108S猫头猫        });
76*e26be108S猫头猫        hideDialog();
77*e26be108S猫头猫    }
78*e26be108S猫头猫
79*e26be108S猫头猫    return (
80*e26be108S猫头猫        <Dialog
81*e26be108S猫头猫            visible={true}
82*e26be108S猫头猫            onDismiss={hideDialog}
83*e26be108S猫头猫            style={{backgroundColor: colors.primary}}>
84*e26be108S猫头猫            <Dialog.Content style={style.content}>
85*e26be108S猫头猫                <View style={style.row}>
86*e26be108S猫头猫                    <ThemeText fontSize="subTitle">封面</ThemeText>
87*e26be108S猫头猫                    <TouchableOpacity
88*e26be108S猫头猫                        onPress={onChangeCoverPress}
89*e26be108S猫头猫                        onLongPress={() => {
90*e26be108S猫头猫                            setCoverImg(undefined);
91*e26be108S猫头猫                        }}>
92*e26be108S猫头猫                        <Image
93*e26be108S猫头猫                            style={style.coverImg}
94*e26be108S猫头猫                            uri={coverImg}
95*e26be108S猫头猫                            emptySrc={ImgAsset.albumDefault}
96*e26be108S猫头猫                        />
97*e26be108S猫头猫                    </TouchableOpacity>
98*e26be108S猫头猫                </View>
99*e26be108S猫头猫                <View style={style.row}>
100*e26be108S猫头猫                    <ThemeText fontSize="subTitle">歌单名</ThemeText>
101*e26be108S猫头猫                    <TextInput
102*e26be108S猫头猫                        value={title}
103*e26be108S猫头猫                        onChangeText={onTitleChange}
104*e26be108S猫头猫                        mode="outlined"
105*e26be108S猫头猫                        outlineColor="transparent"
106*e26be108S猫头猫                        dense
107*e26be108S猫头猫                        style={{
108*e26be108S猫头猫                            includeFontPadding: false,
109*e26be108S猫头猫                        }}
110*e26be108S猫头猫                    />
111*e26be108S猫头猫                </View>
112*e26be108S猫头猫            </Dialog.Content>
113*e26be108S猫头猫            <Dialog.Actions>
114*e26be108S猫头猫                <Button color={colors.textHighlight} onPress={onConfirm}>
115*e26be108S猫头猫                    确认
116*e26be108S猫头猫                </Button>
117*e26be108S猫头猫                <Button color={colors.text} onPress={hideDialog}>
118*e26be108S猫头猫                    取消
119*e26be108S猫头猫                </Button>
120*e26be108S猫头猫            </Dialog.Actions>
121*e26be108S猫头猫        </Dialog>
122*e26be108S猫头猫    );
123*e26be108S猫头猫}
124*e26be108S猫头猫
125*e26be108S猫头猫const style = StyleSheet.create({
126*e26be108S猫头猫    content: {
127*e26be108S猫头猫        height: rpx(450),
128*e26be108S猫头猫    },
129*e26be108S猫头猫    row: {
130*e26be108S猫头猫        marginTop: rpx(28),
131*e26be108S猫头猫        height: rpx(120),
132*e26be108S猫头猫        flexDirection: 'row',
133*e26be108S猫头猫        justifyContent: 'space-between',
134*e26be108S猫头猫        alignItems: 'center',
135*e26be108S猫头猫        paddingBottom: rpx(12),
136*e26be108S猫头猫    },
137*e26be108S猫头猫    coverImg: {
138*e26be108S猫头猫        width: rpx(100),
139*e26be108S猫头猫        height: rpx(100),
140*e26be108S猫头猫        borderRadius: rpx(28),
141*e26be108S猫头猫        marginRight: rpx(16),
142*e26be108S猫头猫    },
143*e26be108S猫头猫});
144