/
musicList.tsx
119 lines (111 loc) · 3.45 KB
/
musicList.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import React, {memo, useCallback} from 'react';
import {StyleSheet, View} from 'react-native';
import rpx from '@/utils/rpx';
import MusicItem from '@/components/mediaItem/musicItem';
import produce from 'immer';
import {useAtom, useSetAtom} from 'jotai';
import {Checkbox} from 'react-native-paper';
import DraggableFlatList from 'react-native-draggable-flatlist';
import {
editingMusicListAtom,
IEditorMusicItem,
musicListChangedAtom,
} from '../store/atom';
import {RenderItem} from 'react-native-draggable-flatlist/lib/typescript/types';
const ITEM_HEIGHT = rpx(120);
interface IMusicEditorItemProps {
getIndex: () => number | undefined;
editorMusicItem: IEditorMusicItem;
drag: () => void;
isActive: boolean;
}
function _MusicEditorItem(props: IMusicEditorItemProps) {
const {getIndex, editorMusicItem, drag, isActive} = props;
const setEditingMusicList = useSetAtom(editingMusicListAtom);
const index = getIndex()!;
const onPress = useCallback(() => {
setEditingMusicList(
produce(draft => {
draft[index].checked = !draft[index].checked;
}),
);
}, [index]);
return (
<MusicItem
musicItem={editorMusicItem.musicItem}
itemBackgroundColor={isActive ? '#999999' : undefined}
onItemLongPress={drag}
left={{
component: () => (
<View style={style.checkBox}>
<Checkbox
onPress={onPress}
status={
editorMusicItem.checked
? 'checked'
: 'unchecked'
}
/>
</View>
),
}}
onItemPress={onPress}
/>
);
}
const MusicEditorItem = memo(
_MusicEditorItem,
(prev, curr) =>
prev.editorMusicItem === curr.editorMusicItem &&
prev.isActive === curr.isActive,
);
export default function MusicList() {
const [editingMusicList, setEditingMusicList] =
useAtom(editingMusicListAtom);
const setMusicListChanged = useSetAtom(musicListChangedAtom);
const renderItem: RenderItem<IEditorMusicItem> = useCallback(
({item, getIndex, drag, isActive}) => {
return (
<MusicEditorItem
editorMusicItem={item}
getIndex={getIndex}
drag={drag}
isActive={isActive}
/>
);
},
[editingMusicList],
);
return editingMusicList?.length ? (
<DraggableFlatList
style={style.wrapper}
keyExtractor={item => item.musicItem.id}
getItemLayout={(_, index) => ({
length: ITEM_HEIGHT,
offset: ITEM_HEIGHT * index,
index,
})}
onDragEnd={params => {
setEditingMusicList([...params.data]);
setMusicListChanged(true);
}}
data={editingMusicList}
renderItem={renderItem}
/>
) : (
<View style={style.wrapper} />
);
}
const style = StyleSheet.create({
wrapper: {
width: rpx(750),
flex: 1,
},
checkBox: {
height: '100%',
justifyContent: 'center',
},
iconRight: {
textAlignVertical: 'center',
},
});