2020-09-10 10:35:02 +05:00
|
|
|
import React, {useEffect, useState} from 'react';
|
|
|
|
|
import {TouchableOpacity, View} from 'react-native';
|
2020-02-11 20:33:36 +05:00
|
|
|
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
|
2020-09-10 10:35:02 +05:00
|
|
|
import {useTracked} from '../../provider';
|
2020-10-13 17:02:14 +05:00
|
|
|
import {Actions} from '../../provider/Actions';
|
|
|
|
|
import {getElevation} from '../../utils';
|
2020-09-10 10:35:02 +05:00
|
|
|
import {PressableButton} from '../PressableButton';
|
2020-10-13 17:02:14 +05:00
|
|
|
import {ToastEvent} from "../../services/EventManager";
|
|
|
|
|
import {SIZE} from "../../utils/SizeUtils";
|
|
|
|
|
import {db} from "../../utils/DB";
|
2020-02-06 23:38:40 +05:00
|
|
|
|
2020-03-18 12:31:06 +05:00
|
|
|
const SelectionWrapper = ({
|
|
|
|
|
children,
|
|
|
|
|
item,
|
|
|
|
|
currentEditingNote,
|
|
|
|
|
index,
|
|
|
|
|
background,
|
|
|
|
|
pinned,
|
2020-09-09 14:55:59 +05:00
|
|
|
onLongPress,
|
2020-09-09 22:09:57 +05:00
|
|
|
onPress,
|
2020-03-18 12:31:06 +05:00
|
|
|
}) => {
|
2020-01-17 21:26:01 +05:00
|
|
|
const [state, dispatch] = useTracked();
|
|
|
|
|
const {colors, selectionMode, selectedItemsList} = state;
|
2020-01-31 18:22:20 +05:00
|
|
|
const [selected, setSelected] = useState(false);
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
let exists = selectedItemsList.filter(
|
2020-09-09 14:55:59 +05:00
|
|
|
(o) => o.dateCreated === item.dateCreated,
|
2020-01-31 18:22:20 +05:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (exists[0]) {
|
|
|
|
|
if (!selected) {
|
|
|
|
|
setSelected(true);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (selected) {
|
|
|
|
|
setSelected(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, [selectedItemsList]);
|
2020-01-17 00:23:16 +05:00
|
|
|
|
2020-09-09 22:09:57 +05:00
|
|
|
const onPressPin = async () => {
|
|
|
|
|
let func = async () => {
|
|
|
|
|
if (!item.id) return;
|
|
|
|
|
if (item.type === 'note') {
|
2020-09-10 10:29:51 +05:00
|
|
|
await db.notes.note(item.id).pin();
|
2020-10-13 17:02:14 +05:00
|
|
|
dispatch({type: Actions.PINNED});
|
|
|
|
|
dispatch({type: Actions.NOTES});
|
2020-09-09 22:09:57 +05:00
|
|
|
} else {
|
2020-09-10 10:29:51 +05:00
|
|
|
await db.notebooks.notebook(item.id).pin();
|
2020-10-13 17:02:14 +05:00
|
|
|
dispatch({type: Actions.PINNED});
|
|
|
|
|
dispatch({type: Actions.NOTEBOOKS});
|
2020-09-09 22:09:57 +05:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
func();
|
|
|
|
|
ToastEvent.show(
|
|
|
|
|
item.type + ' has been unpinned.',
|
|
|
|
|
'success',
|
|
|
|
|
'global',
|
|
|
|
|
6000,
|
|
|
|
|
() => {
|
|
|
|
|
func();
|
|
|
|
|
ToastEvent.hide('unpin');
|
|
|
|
|
},
|
|
|
|
|
'Undo',
|
|
|
|
|
);
|
|
|
|
|
};
|
2020-09-09 14:55:59 +05:00
|
|
|
|
2020-01-13 17:33:41 +05:00
|
|
|
return (
|
2020-09-09 14:55:59 +05:00
|
|
|
<PressableButton
|
2020-09-09 22:09:57 +05:00
|
|
|
color={
|
2020-10-13 17:02:14 +05:00
|
|
|
currentEditingNote || pinned
|
2020-09-09 22:09:57 +05:00
|
|
|
? colors.shade
|
|
|
|
|
: background
|
|
|
|
|
? background
|
|
|
|
|
: 'transparent'
|
2020-09-09 14:55:59 +05:00
|
|
|
}
|
|
|
|
|
onLongPress={onLongPress}
|
|
|
|
|
onPress={onPress}
|
2020-09-09 22:09:57 +05:00
|
|
|
selectedColor={
|
2020-10-13 17:02:14 +05:00
|
|
|
currentEditingNote || pinned
|
2020-09-09 22:09:57 +05:00
|
|
|
? colors.accent
|
|
|
|
|
: colors.nav
|
|
|
|
|
}
|
2020-09-09 14:55:59 +05:00
|
|
|
alpha={!colors.night ? -0.02 : 0.02}
|
2020-10-13 17:02:14 +05:00
|
|
|
opacity={currentEditingNote || pinned ? 0.12 : 1}
|
2020-09-09 14:55:59 +05:00
|
|
|
customStyle={{
|
2020-01-13 17:33:41 +05:00
|
|
|
flexDirection: 'row',
|
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
|
alignItems: 'center',
|
2020-01-22 02:51:24 +05:00
|
|
|
width: '100%',
|
|
|
|
|
paddingHorizontal: 12,
|
2020-09-18 20:54:31 +05:00
|
|
|
borderRadius: 0,
|
2020-03-18 12:31:06 +05:00
|
|
|
marginTop:
|
|
|
|
|
index === 0 && pinned && !selectionMode
|
|
|
|
|
? 15
|
|
|
|
|
: index === 0 && pinned && selectionMode
|
|
|
|
|
? 30
|
|
|
|
|
: 0,
|
2020-01-13 17:33:41 +05:00
|
|
|
}}>
|
2020-03-18 12:31:06 +05:00
|
|
|
{pinned ? (
|
2020-09-09 22:09:57 +05:00
|
|
|
<PressableButton
|
|
|
|
|
color={colors.accent}
|
|
|
|
|
selectedColor={colors.accent}
|
|
|
|
|
alpha={!colors.night ? -0.1 : 0.1}
|
|
|
|
|
onPress={onPressPin}
|
|
|
|
|
customStyle={{
|
2020-03-18 12:31:06 +05:00
|
|
|
...getElevation(3),
|
|
|
|
|
width: 30,
|
|
|
|
|
height: 30,
|
|
|
|
|
borderRadius: 100,
|
|
|
|
|
position: 'absolute',
|
2020-09-09 14:55:59 +05:00
|
|
|
right: 20,
|
2020-03-18 12:31:06 +05:00
|
|
|
top: -15,
|
|
|
|
|
justifyContent: 'center',
|
|
|
|
|
alignItems: 'center',
|
2020-09-09 22:09:57 +05:00
|
|
|
zIndex: 10,
|
2020-03-18 12:31:06 +05:00
|
|
|
}}>
|
|
|
|
|
<View
|
|
|
|
|
style={{
|
|
|
|
|
width: 5,
|
|
|
|
|
height: 5,
|
|
|
|
|
backgroundColor: 'white',
|
|
|
|
|
borderRadius: 100,
|
|
|
|
|
}}
|
|
|
|
|
/>
|
2020-09-09 22:09:57 +05:00
|
|
|
</PressableButton>
|
2020-03-18 12:31:06 +05:00
|
|
|
) : null}
|
|
|
|
|
|
2020-01-22 02:51:24 +05:00
|
|
|
<View
|
2020-01-13 17:33:41 +05:00
|
|
|
style={{
|
|
|
|
|
display: selectionMode ? 'flex' : 'none',
|
2020-01-17 00:23:16 +05:00
|
|
|
opacity: selectionMode ? 1 : 0,
|
2020-01-31 18:40:56 +05:00
|
|
|
width: '10%',
|
|
|
|
|
height: 70,
|
|
|
|
|
justifyContent: 'center',
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
paddingRight: 8,
|
2020-01-13 17:33:41 +05:00
|
|
|
}}>
|
2020-09-09 14:55:59 +05:00
|
|
|
<TouchableOpacity
|
|
|
|
|
activeOpacity={1}
|
2020-01-31 18:22:20 +05:00
|
|
|
onPress={() => {
|
2020-10-13 17:02:14 +05:00
|
|
|
dispatch({type: Actions.SELECTED_ITEMS, item: item});
|
2020-01-31 18:22:20 +05:00
|
|
|
}}
|
2020-01-13 17:33:41 +05:00
|
|
|
style={{
|
|
|
|
|
justifyContent: 'center',
|
|
|
|
|
alignItems: 'center',
|
2020-09-09 14:55:59 +05:00
|
|
|
height: 70,
|
2020-01-13 17:33:41 +05:00
|
|
|
}}>
|
2020-02-11 20:33:36 +05:00
|
|
|
<Icon
|
2020-02-20 12:17:05 +05:00
|
|
|
size={SIZE.lg}
|
2020-09-09 14:55:59 +05:00
|
|
|
color={selected ? colors.accent : colors.icon}
|
2020-02-11 20:33:36 +05:00
|
|
|
name={
|
|
|
|
|
selected
|
|
|
|
|
? 'check-circle-outline'
|
|
|
|
|
: 'checkbox-blank-circle-outline'
|
|
|
|
|
}
|
|
|
|
|
/>
|
2020-09-09 14:55:59 +05:00
|
|
|
</TouchableOpacity>
|
2020-01-22 02:51:24 +05:00
|
|
|
</View>
|
2020-02-06 23:38:40 +05:00
|
|
|
|
2020-01-13 17:33:41 +05:00
|
|
|
{children}
|
2020-09-09 14:55:59 +05:00
|
|
|
</PressableButton>
|
2020-01-13 17:33:41 +05:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default SelectionWrapper;
|