xref: /MusicFree/src/components/dialogs/components/editSheetDetail.tsx (revision 75d76114aff0fdc73e1194e45451b5011b83e6ae)
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 ThemeText from '@/components/base/themeText';
7e26be108S猫头猫import {ImgAsset} from '@/constants/assetsConst';
8476c9cacS猫头猫import {TouchableOpacity} from 'react-native';
9e26be108S猫头猫import {launchImageLibrary} from 'react-native-image-picker';
10e26be108S猫头猫import pathConst from '@/constants/pathConst';
11e26be108S猫头猫import Image from '@/components/base/image';
1229fe487bS猫头猫import {copyFile, exists, unlink} from 'react-native-fs';
13e26be108S猫头猫import MusicSheet from '@/core/musicSheet';
1429fe487bS猫头猫import {addFileScheme, addRandomHash} from '@/utils/fileUtils';
1529fe487bS猫头猫import Toast from '@/utils/toast';
16*75d76114S猫头猫import {hideDialog} from '../useDialog';
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猫头猫
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();
547a69a1e8S猫头猫            return;
55e26be108S猫头猫        }
56e26be108S猫头猫
57e26be108S猫头猫        let _coverImg = coverImg;
5829fe487bS猫头猫        if (_coverImg && _coverImg !== musicSheet?.coverImg) {
59e26be108S猫头猫            _coverImg = addFileScheme(
607a69a1e8S猫头猫                `${pathConst.dataPath}sheet${
617a69a1e8S猫头猫                    musicSheet.id
627a69a1e8S猫头猫                }${_coverImg.substring(_coverImg.lastIndexOf('.'))}`,
63e26be108S猫头猫            );
6429fe487bS猫头猫            try {
6529fe487bS猫头猫                if (await exists(_coverImg)) {
6629fe487bS猫头猫                    await unlink(_coverImg);
6729fe487bS猫头猫                }
68e26be108S猫头猫                await copyFile(coverImg!, _coverImg);
6929fe487bS猫头猫            } catch (e) {
7029fe487bS猫头猫                console.log(e);
7129fe487bS猫头猫            }
72e26be108S猫头猫        }
73e26be108S猫头猫        let _title = title;
7429fe487bS猫头猫        if (!_title?.length) {
75e26be108S猫头猫            _title = musicSheet.title;
76e26be108S猫头猫        }
77e26be108S猫头猫        // 更新歌单信息
78e26be108S猫头猫        MusicSheet.updateAndSaveSheet(musicSheet.id, {
79e26be108S猫头猫            basic: {
802aa88193S猫头猫                coverImg: _coverImg ? addRandomHash(_coverImg) : undefined,
81e26be108S猫头猫                title: _title,
82e26be108S猫头猫            },
8329fe487bS猫头猫        }).then(() => {
8429fe487bS猫头猫            Toast.success('更新歌单信息成功~');
85e26be108S猫头猫        });
86e26be108S猫头猫        hideDialog();
87e26be108S猫头猫    }
88e26be108S猫头猫
89e26be108S猫头猫    return (
90e26be108S猫头猫        <Dialog
91e26be108S猫头猫            visible={true}
92e26be108S猫头猫            onDismiss={hideDialog}
93e26be108S猫头猫            style={{backgroundColor: colors.primary}}>
94e26be108S猫头猫            <Dialog.Content style={style.content}>
95e26be108S猫头猫                <View style={style.row}>
96e26be108S猫头猫                    <ThemeText fontSize="subTitle">封面</ThemeText>
97e26be108S猫头猫                    <TouchableOpacity
98e26be108S猫头猫                        onPress={onChangeCoverPress}
99e26be108S猫头猫                        onLongPress={() => {
100e26be108S猫头猫                            setCoverImg(undefined);
101e26be108S猫头猫                        }}>
102e26be108S猫头猫                        <Image
103e26be108S猫头猫                            style={style.coverImg}
104e26be108S猫头猫                            uri={coverImg}
105e26be108S猫头猫                            emptySrc={ImgAsset.albumDefault}
106e26be108S猫头猫                        />
107e26be108S猫头猫                    </TouchableOpacity>
108e26be108S猫头猫                </View>
109e26be108S猫头猫                <View style={style.row}>
110e26be108S猫头猫                    <ThemeText fontSize="subTitle">歌单名</ThemeText>
111e26be108S猫头猫                    <TextInput
112e26be108S猫头猫                        value={title}
113e26be108S猫头猫                        onChangeText={onTitleChange}
114e26be108S猫头猫                        mode="outlined"
115e26be108S猫头猫                        outlineColor="transparent"
116e26be108S猫头猫                        dense
117e26be108S猫头猫                        style={{
118e26be108S猫头猫                            includeFontPadding: false,
119e26be108S猫头猫                        }}
120e26be108S猫头猫                    />
121e26be108S猫头猫                </View>
122e26be108S猫头猫            </Dialog.Content>
123e26be108S猫头猫            <Dialog.Actions>
124e26be108S猫头猫                <Button color={colors.textHighlight} onPress={onConfirm}>
125e26be108S猫头猫                    确认
126e26be108S猫头猫                </Button>
127e26be108S猫头猫                <Button color={colors.text} onPress={hideDialog}>
128e26be108S猫头猫                    取消
129e26be108S猫头猫                </Button>
130e26be108S猫头猫            </Dialog.Actions>
131e26be108S猫头猫        </Dialog>
132e26be108S猫头猫    );
133e26be108S猫头猫}
134e26be108S猫头猫
135e26be108S猫头猫const style = StyleSheet.create({
136e26be108S猫头猫    content: {
137e26be108S猫头猫        height: rpx(450),
138e26be108S猫头猫    },
139e26be108S猫头猫    row: {
140e26be108S猫头猫        marginTop: rpx(28),
141e26be108S猫头猫        height: rpx(120),
142e26be108S猫头猫        flexDirection: 'row',
143e26be108S猫头猫        justifyContent: 'space-between',
144e26be108S猫头猫        alignItems: 'center',
145e26be108S猫头猫        paddingBottom: rpx(12),
146e26be108S猫头猫    },
147e26be108S猫头猫    coverImg: {
148e26be108S猫头猫        width: rpx(100),
149e26be108S猫头猫        height: rpx(100),
150e26be108S猫头猫        borderRadius: rpx(28),
151e26be108S猫头猫        marginRight: rpx(16),
152e26be108S猫头猫    },
153e26be108S猫头猫});
154