2020-09-14 17:17:17 +05:00
|
|
|
import React, {useEffect, useState} from 'react';
|
|
|
|
|
import {Text, TouchableOpacity, View} from 'react-native';
|
|
|
|
|
import Animated, {Easing, useValue} from 'react-native-reanimated';
|
2020-09-09 14:55:59 +05:00
|
|
|
import {useSafeAreaInsets} from 'react-native-safe-area-context';
|
2020-02-11 20:33:36 +05:00
|
|
|
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
|
2020-09-09 14:55:59 +05:00
|
|
|
import {useTracked} from '../../provider';
|
2020-10-13 17:02:14 +05:00
|
|
|
import {Actions} from '../../provider/Actions';
|
|
|
|
|
import {eSendEvent, ToastEvent} from '../../services/EventManager';
|
|
|
|
|
import {eOpenMoveNoteDialog, eOpenSimpleDialog} from '../../utils/Events';
|
|
|
|
|
import {TEMPLATE_DELETE} from '../DialogManager/Templates';
|
|
|
|
|
import {SIZE, WEIGHT} from "../../utils/SizeUtils";
|
|
|
|
|
import {db} from "../../utils/DB";
|
2020-01-13 14:47:28 +05:00
|
|
|
|
2020-03-18 11:58:56 +05:00
|
|
|
export const SelectionHeader = () => {
|
2020-01-13 14:47:28 +05:00
|
|
|
// State
|
2020-01-17 21:26:01 +05:00
|
|
|
const [state, dispatch] = useTracked();
|
2020-09-07 19:19:20 +05:00
|
|
|
const {
|
|
|
|
|
colors,
|
|
|
|
|
selectionMode,
|
|
|
|
|
selectedItemsList,
|
|
|
|
|
currentScreen,
|
|
|
|
|
premiumUser,
|
|
|
|
|
} = state;
|
2020-03-18 11:58:56 +05:00
|
|
|
const [selectAll, setSelectAll] = useState(false);
|
2020-09-07 19:19:20 +05:00
|
|
|
const insets = useSafeAreaInsets();
|
|
|
|
|
const translateY = useValue(-150);
|
2020-01-17 21:26:01 +05:00
|
|
|
|
2020-09-07 19:19:20 +05:00
|
|
|
useEffect(() => {
|
2020-09-09 14:55:59 +05:00
|
|
|
Animated.timing(translateY, {
|
|
|
|
|
duration: 300,
|
|
|
|
|
toValue: selectionMode ? 0 : -150,
|
|
|
|
|
easing: Easing.in(Easing.ease),
|
|
|
|
|
}).start();
|
|
|
|
|
}, [selectionMode]);
|
2020-01-13 14:47:28 +05:00
|
|
|
|
2020-09-27 10:15:19 +05:00
|
|
|
return (
|
2020-09-07 19:19:20 +05:00
|
|
|
<Animated.View
|
2020-01-13 14:47:28 +05:00
|
|
|
style={{
|
|
|
|
|
width: '100%',
|
|
|
|
|
position: 'absolute',
|
2020-09-14 17:17:17 +05:00
|
|
|
height: 50,
|
2020-01-18 00:46:29 +05:00
|
|
|
backgroundColor: colors.bg,
|
2020-09-14 09:42:36 +05:00
|
|
|
marginTop: insets.top,
|
2020-01-13 14:47:28 +05:00
|
|
|
justifyContent: 'flex-end',
|
2020-01-31 18:40:56 +05:00
|
|
|
zIndex: 999,
|
2020-01-22 02:51:24 +05:00
|
|
|
paddingHorizontal: 12,
|
2020-01-31 18:40:56 +05:00
|
|
|
transform: [
|
|
|
|
|
{
|
2020-09-07 19:19:20 +05:00
|
|
|
translateY: translateY,
|
2020-01-31 18:40:56 +05:00
|
|
|
},
|
|
|
|
|
],
|
2020-01-13 14:47:28 +05:00
|
|
|
}}>
|
|
|
|
|
<View
|
|
|
|
|
style={{
|
2020-01-22 02:51:24 +05:00
|
|
|
width: '100%',
|
2020-01-13 14:47:28 +05:00
|
|
|
height: 50,
|
|
|
|
|
flexDirection: 'row',
|
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
}}>
|
|
|
|
|
<View
|
|
|
|
|
style={{
|
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
|
flexDirection: 'row',
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
}}>
|
|
|
|
|
<TouchableOpacity
|
|
|
|
|
onPress={() => {
|
2020-10-13 17:02:14 +05:00
|
|
|
dispatch({type: Actions.SELECTION_MODE, enabled: !selectionMode});
|
|
|
|
|
dispatch({type: Actions.CLEAR_SELECTION});
|
2020-01-13 14:47:28 +05:00
|
|
|
}}
|
|
|
|
|
hitSlop={{top: 20, bottom: 20, left: 50, right: 40}}
|
|
|
|
|
style={{
|
|
|
|
|
justifyContent: 'center',
|
|
|
|
|
alignItems: 'flex-start',
|
|
|
|
|
height: 40,
|
2020-09-07 19:19:20 +05:00
|
|
|
width: 60,
|
2020-01-13 14:47:28 +05:00
|
|
|
}}>
|
|
|
|
|
<Icon
|
|
|
|
|
style={{
|
|
|
|
|
marginLeft: -5,
|
|
|
|
|
}}
|
|
|
|
|
color={colors.pri}
|
|
|
|
|
name={'chevron-left'}
|
2020-02-20 12:16:32 +05:00
|
|
|
size={SIZE.xxxl}
|
2020-01-13 14:47:28 +05:00
|
|
|
/>
|
|
|
|
|
</TouchableOpacity>
|
2020-03-18 11:58:56 +05:00
|
|
|
|
2020-01-13 14:47:28 +05:00
|
|
|
<Text
|
|
|
|
|
style={{
|
|
|
|
|
fontSize: SIZE.lg,
|
|
|
|
|
fontFamily: WEIGHT.regular,
|
|
|
|
|
color: colors.pri,
|
|
|
|
|
textAlignVertical: 'center',
|
|
|
|
|
}}>
|
2020-03-18 11:58:56 +05:00
|
|
|
{selectAll ? 'All' : selectedItemsList.length}
|
2020-01-13 14:47:28 +05:00
|
|
|
</Text>
|
|
|
|
|
</View>
|
|
|
|
|
|
|
|
|
|
<View
|
|
|
|
|
style={{
|
|
|
|
|
flexDirection: 'row',
|
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
}}>
|
2020-02-20 14:45:23 +05:00
|
|
|
{currentScreen === 'trash' || currentScreen === 'notebooks' ? null : (
|
|
|
|
|
<TouchableOpacity
|
2020-09-07 19:19:20 +05:00
|
|
|
style={{
|
|
|
|
|
justifyContent: 'center',
|
|
|
|
|
alignItems: 'flex-end',
|
|
|
|
|
height: 40,
|
|
|
|
|
width: 50,
|
|
|
|
|
paddingRight: 0,
|
|
|
|
|
}}
|
2020-02-20 14:45:23 +05:00
|
|
|
onPress={() => {
|
2020-10-13 17:02:14 +05:00
|
|
|
dispatch({type: Actions.SELECTION_MODE, enabled: false});
|
|
|
|
|
dispatch({type: Actions.CLEAR_SELECTION});
|
2020-02-20 14:45:23 +05:00
|
|
|
eSendEvent(eOpenMoveNoteDialog);
|
|
|
|
|
}}>
|
2020-09-09 14:55:59 +05:00
|
|
|
<Icon color={colors.heading} name={'plus'} size={SIZE.xl} />
|
2020-02-20 14:45:23 +05:00
|
|
|
</TouchableOpacity>
|
|
|
|
|
)}
|
2020-09-14 17:17:17 +05:00
|
|
|
{/* {currentScreen === 'trash' || currentScreen === 'notebooks' ? null : (
|
2020-02-20 14:45:23 +05:00
|
|
|
<TouchableOpacity
|
2020-09-07 19:19:20 +05:00
|
|
|
style={{
|
|
|
|
|
justifyContent: 'center',
|
|
|
|
|
alignItems: 'flex-end',
|
|
|
|
|
height: 40,
|
|
|
|
|
width: 50,
|
|
|
|
|
paddingRight: 0,
|
|
|
|
|
}}
|
2020-02-20 14:45:23 +05:00
|
|
|
onPress={async () => {
|
2020-08-16 18:59:07 +05:00
|
|
|
if (!premiumUser) {
|
|
|
|
|
eSendEvent(eOpenPremiumDialog);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-03-02 12:07:19 +05:00
|
|
|
let favCount = 0;
|
|
|
|
|
let unFavCount = 0;
|
2020-02-20 14:45:23 +05:00
|
|
|
if (selectedItemsList.length > 0) {
|
2020-09-07 19:19:20 +05:00
|
|
|
selectedItemsList.forEach(async (item) => {
|
2020-03-17 15:30:32 +05:00
|
|
|
if (!item.favorite) {
|
2020-03-02 12:07:19 +05:00
|
|
|
favCount += 1;
|
|
|
|
|
} else {
|
2020-03-17 15:30:32 +05:00
|
|
|
return;
|
2020-03-02 12:07:19 +05:00
|
|
|
}
|
2020-02-20 14:45:23 +05:00
|
|
|
await db.notes.note(item.id).favorite();
|
2020-03-02 11:13:40 +05:00
|
|
|
dispatch({type: ACTIONS.NOTES});
|
|
|
|
|
dispatch({type: ACTIONS.FAVORITES});
|
2020-02-20 14:45:23 +05:00
|
|
|
});
|
2020-03-02 11:13:40 +05:00
|
|
|
|
2020-02-20 14:45:23 +05:00
|
|
|
dispatch({type: ACTIONS.SELECTION_MODE, enabled: false});
|
2020-03-02 11:13:40 +05:00
|
|
|
|
2020-02-20 14:45:23 +05:00
|
|
|
dispatch({type: ACTIONS.CLEAR_SELECTION});
|
2020-03-02 11:13:40 +05:00
|
|
|
|
2020-03-02 12:07:19 +05:00
|
|
|
ToastEvent.show(
|
2020-03-17 15:30:32 +05:00
|
|
|
favCount + ' notes added to favorites',
|
2020-03-02 12:07:19 +05:00
|
|
|
'success',
|
|
|
|
|
);
|
2020-02-20 14:45:23 +05:00
|
|
|
}
|
|
|
|
|
}}>
|
2020-09-09 14:55:59 +05:00
|
|
|
<Icon color={colors.heading} name={'star'} size={SIZE.xl - 3} />
|
2020-02-20 14:45:23 +05:00
|
|
|
</TouchableOpacity>
|
2020-09-09 14:55:59 +05:00
|
|
|
)} */}
|
2020-01-13 14:47:28 +05:00
|
|
|
|
2020-02-20 14:45:23 +05:00
|
|
|
{currentScreen === 'trash' ? null : (
|
|
|
|
|
<TouchableOpacity
|
2020-09-07 19:19:20 +05:00
|
|
|
style={{
|
|
|
|
|
justifyContent: 'center',
|
|
|
|
|
alignItems: 'flex-end',
|
|
|
|
|
height: 40,
|
|
|
|
|
width: 50,
|
|
|
|
|
paddingRight: 0,
|
|
|
|
|
}}
|
2020-02-20 14:45:23 +05:00
|
|
|
onPress={async () => {
|
2020-03-02 10:25:38 +05:00
|
|
|
eSendEvent(eOpenSimpleDialog, TEMPLATE_DELETE('item'));
|
|
|
|
|
return;
|
2020-02-20 14:45:23 +05:00
|
|
|
}}>
|
2020-09-09 14:55:59 +05:00
|
|
|
<Icon color={colors.heading} name={'delete'} size={SIZE.xl - 3} />
|
2020-02-20 14:45:23 +05:00
|
|
|
</TouchableOpacity>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{currentScreen === 'trash' ? (
|
|
|
|
|
<TouchableOpacity
|
2020-09-07 19:19:20 +05:00
|
|
|
style={{
|
|
|
|
|
justifyContent: 'center',
|
|
|
|
|
alignItems: 'flex-end',
|
|
|
|
|
height: 40,
|
|
|
|
|
width: 50,
|
|
|
|
|
paddingRight: 0,
|
|
|
|
|
}}
|
2020-02-20 14:45:23 +05:00
|
|
|
onPress={async () => {
|
|
|
|
|
if (selectedItemsList.length > 0) {
|
|
|
|
|
let noteIds = [];
|
2020-09-07 19:19:20 +05:00
|
|
|
selectedItemsList.forEach((item) => {
|
2020-02-20 14:45:23 +05:00
|
|
|
noteIds.push(item.id);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await db.trash.restore(...noteIds);
|
2020-02-20 18:20:48 +05:00
|
|
|
|
2020-10-13 17:02:14 +05:00
|
|
|
dispatch({type: Actions.TRASH});
|
|
|
|
|
dispatch({type: Actions.SELECTION_MODE, enabled: false});
|
|
|
|
|
dispatch({type: Actions.CLEAR_SELECTION});
|
2020-02-20 18:20:48 +05:00
|
|
|
ToastEvent.show('Restore complete', 'success');
|
2020-02-20 14:45:23 +05:00
|
|
|
}
|
|
|
|
|
}}>
|
|
|
|
|
<Icon
|
2020-09-09 14:55:59 +05:00
|
|
|
color={colors.heading}
|
2020-02-20 14:45:23 +05:00
|
|
|
name="delete-restore"
|
|
|
|
|
size={SIZE.xl - 3}
|
|
|
|
|
/>
|
|
|
|
|
</TouchableOpacity>
|
|
|
|
|
) : null}
|
2020-01-13 14:47:28 +05:00
|
|
|
</View>
|
|
|
|
|
</View>
|
2020-03-18 11:58:56 +05:00
|
|
|
|
2020-09-07 19:19:20 +05:00
|
|
|
{/* <TouchableOpacity
|
2020-03-18 11:58:56 +05:00
|
|
|
onPress={() => {
|
|
|
|
|
if (selectAll) {
|
|
|
|
|
dispatch({type: ACTIONS.SELECT_ALL, selected: []});
|
|
|
|
|
} else {
|
|
|
|
|
dispatch({
|
|
|
|
|
type: ACTIONS.SELECT_ALL,
|
|
|
|
|
selected:
|
|
|
|
|
selection.type === 'notes' ? db.notes.all : selection.data,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setSelectAll(!selectAll);
|
|
|
|
|
}}
|
|
|
|
|
hitSlop={{top: 20, bottom: 20, left: 20, right: 40}}
|
|
|
|
|
style={{
|
|
|
|
|
justifyContent: 'center',
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
height: 40,
|
|
|
|
|
flexDirection: 'row',
|
|
|
|
|
alignSelf: 'flex-start',
|
2020-08-16 18:59:07 +05:00
|
|
|
marginLeft:5,
|
|
|
|
|
marginBottom:5
|
2020-03-18 11:58:56 +05:00
|
|
|
}}>
|
|
|
|
|
<Icon
|
|
|
|
|
style={{}}
|
|
|
|
|
color={selectAll ? colors.accent : colors.icon}
|
|
|
|
|
name={
|
|
|
|
|
selectAll ? 'check-circle-outline' : 'checkbox-blank-circle-outline'
|
|
|
|
|
}
|
|
|
|
|
size={SIZE.lg}
|
|
|
|
|
/>
|
|
|
|
|
<Text
|
|
|
|
|
style={{
|
|
|
|
|
marginLeft: 10,
|
|
|
|
|
}}>
|
|
|
|
|
Select All
|
|
|
|
|
</Text>
|
2020-09-07 19:19:20 +05:00
|
|
|
</TouchableOpacity> */}
|
|
|
|
|
</Animated.View>
|
2020-01-13 14:47:28 +05:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default SelectionHeader;
|