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