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