xref: /MusicFree/src/components/dialogs/components/editSheetDetail.tsx (revision e650bfb34226e2a09d15cbf7832c4805a87cd60e)
1e26be108S猫头猫import React, {useState} from 'react';
2*e650bfb3S猫头猫import {Button} 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';
1675d76114S猫头猫import {hideDialog} from '../useDialog';
17*e650bfb3S猫头猫import Dialog from './base';
18*e650bfb3S猫头猫import Input from '@/components/base/input';
19*e650bfb3S猫头猫import {fontSizeConst} from '@/constants/uiConst';
20e26be108S猫头猫
21e26be108S猫头猫interface IEditSheetDetailProps {
22e26be108S猫头猫    musicSheet: IMusic.IMusicSheetItem;
23e26be108S猫头猫}
24e26be108S猫头猫export default function EditSheetDetailDialog(props: IEditSheetDetailProps) {
25e26be108S猫头猫    const {musicSheet} = props;
26e26be108S猫头猫    const colors = useColors();
27e26be108S猫头猫
28e26be108S猫头猫    const [coverImg, setCoverImg] = useState(musicSheet?.coverImg);
29e26be108S猫头猫    const [title, setTitle] = useState(musicSheet?.title);
30e26be108S猫头猫
31e26be108S猫头猫    // onCover
32e26be108S猫头猫
33e26be108S猫头猫    const onChangeCoverPress = async () => {
34e26be108S猫头猫        try {
35e26be108S猫头猫            const result = await launchImageLibrary({
36e26be108S猫头猫                mediaType: 'photo',
37e26be108S猫头猫            });
38e26be108S猫头猫            const uri = result.assets?.[0].uri;
39e26be108S猫头猫            if (!uri) {
40e26be108S猫头猫                return;
41e26be108S猫头猫            }
42e26be108S猫头猫            console.log(uri);
43e26be108S猫头猫            setCoverImg(uri);
44e26be108S猫头猫        } catch (e) {
45e26be108S猫头猫            console.log(e);
46e26be108S猫头猫        }
47e26be108S猫头猫    };
48e26be108S猫头猫
49e26be108S猫头猫    function onTitleChange(_: string) {
50e26be108S猫头猫        setTitle(_);
51e26be108S猫头猫    }
52e26be108S猫头猫
53e26be108S猫头猫    async function onConfirm() {
54e26be108S猫头猫        // 判断是否相同
55e26be108S猫头猫        if (coverImg === musicSheet?.coverImg && title === musicSheet?.title) {
56e26be108S猫头猫            hideDialog();
577a69a1e8S猫头猫            return;
58e26be108S猫头猫        }
59e26be108S猫头猫
60e26be108S猫头猫        let _coverImg = coverImg;
6129fe487bS猫头猫        if (_coverImg && _coverImg !== musicSheet?.coverImg) {
62e26be108S猫头猫            _coverImg = addFileScheme(
637a69a1e8S猫头猫                `${pathConst.dataPath}sheet${
647a69a1e8S猫头猫                    musicSheet.id
657a69a1e8S猫头猫                }${_coverImg.substring(_coverImg.lastIndexOf('.'))}`,
66e26be108S猫头猫            );
6729fe487bS猫头猫            try {
6829fe487bS猫头猫                if (await exists(_coverImg)) {
6929fe487bS猫头猫                    await unlink(_coverImg);
7029fe487bS猫头猫                }
71e26be108S猫头猫                await copyFile(coverImg!, _coverImg);
7229fe487bS猫头猫            } catch (e) {
7329fe487bS猫头猫                console.log(e);
7429fe487bS猫头猫            }
75e26be108S猫头猫        }
76e26be108S猫头猫        let _title = title;
7729fe487bS猫头猫        if (!_title?.length) {
78e26be108S猫头猫            _title = musicSheet.title;
79e26be108S猫头猫        }
80e26be108S猫头猫        // 更新歌单信息
81e26be108S猫头猫        MusicSheet.updateAndSaveSheet(musicSheet.id, {
82e26be108S猫头猫            basic: {
832aa88193S猫头猫                coverImg: _coverImg ? addRandomHash(_coverImg) : undefined,
84e26be108S猫头猫                title: _title,
85e26be108S猫头猫            },
8629fe487bS猫头猫        }).then(() => {
8729fe487bS猫头猫            Toast.success('更新歌单信息成功~');
88e26be108S猫头猫        });
89e26be108S猫头猫        hideDialog();
90e26be108S猫头猫    }
91e26be108S猫头猫
92e26be108S猫头猫    return (
93*e650bfb3S猫头猫        <Dialog onDismiss={hideDialog}>
94e26be108S猫头猫            <Dialog.Content style={style.content}>
95e26be108S猫头猫                <View style={style.row}>
96*e650bfb3S猫头猫                    <ThemeText>封面</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}>
110*e650bfb3S猫头猫                    <ThemeText>歌单名</ThemeText>
111*e650bfb3S猫头猫                    <Input
112*e650bfb3S猫头猫                        numberOfLines={1}
113*e650bfb3S猫头猫                        textAlign="right"
114e26be108S猫头猫                        value={title}
115*e650bfb3S猫头猫                        hasHorizonalPadding={false}
116e26be108S猫头猫                        onChangeText={onTitleChange}
117e26be108S猫头猫                        style={{
118*e650bfb3S猫头猫                            height: fontSizeConst.content * 2.5,
119*e650bfb3S猫头猫                            width: '50%',
120*e650bfb3S猫头猫                            borderBottomWidth: 1,
121e26be108S猫头猫                            includeFontPadding: false,
122*e650bfb3S猫头猫                            borderBottomColor: colors.text,
123e26be108S猫头猫                        }}
124e26be108S猫头猫                    />
125e26be108S猫头猫                </View>
126e26be108S猫头猫            </Dialog.Content>
127e26be108S猫头猫            <Dialog.Actions>
128e26be108S猫头猫                <Button color={colors.textHighlight} onPress={onConfirm}>
129e26be108S猫头猫                    确认
130e26be108S猫头猫                </Button>
131e26be108S猫头猫                <Button color={colors.text} onPress={hideDialog}>
132e26be108S猫头猫                    取消
133e26be108S猫头猫                </Button>
134e26be108S猫头猫            </Dialog.Actions>
135e26be108S猫头猫        </Dialog>
136e26be108S猫头猫    );
137e26be108S猫头猫}
138e26be108S猫头猫
139e26be108S猫头猫const style = StyleSheet.create({
140e26be108S猫头猫    content: {
141e26be108S猫头猫        height: rpx(450),
142e26be108S猫头猫    },
143e26be108S猫头猫    row: {
144e26be108S猫头猫        marginTop: rpx(28),
145e26be108S猫头猫        height: rpx(120),
146e26be108S猫头猫        flexDirection: 'row',
147e26be108S猫头猫        justifyContent: 'space-between',
148e26be108S猫头猫        alignItems: 'center',
149e26be108S猫头猫        paddingBottom: rpx(12),
150e26be108S猫头猫    },
151e26be108S猫头猫    coverImg: {
152e26be108S猫头猫        width: rpx(100),
153e26be108S猫头猫        height: rpx(100),
154e26be108S猫头猫        borderRadius: rpx(28),
155e26be108S猫头猫    },
156e26be108S猫头猫});
157