Files
notesnook/apps/mobile/src/components/SelectionHeader/index.js

237 lines
7.3 KiB
JavaScript
Raw Normal View History

2020-03-18 11:58:56 +05:00
import React, {useEffect, useState} from 'react';
2020-03-21 10:03:11 +05:00
import {Platform, StatusBar, Text, TouchableOpacity, View} from 'react-native';
2020-01-18 01:04:33 +05:00
import * as Animatable from 'react-native-animatable';
2020-02-11 20:33:36 +05:00
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
import {SIZE, WEIGHT} from '../../common/common';
2020-01-24 23:13:09 +05:00
import {useTracked} from '../../provider';
import {ACTIONS} from '../../provider/actions';
2020-02-20 12:16:32 +05:00
import {eSendEvent} from '../../services/eventManager';
2020-03-02 10:25:38 +05:00
import {eOpenMoveNoteDialog, eOpenSimpleDialog} from '../../services/events';
2020-03-21 10:03:11 +05:00
import {db, selection, ToastEvent} from '../../utils/utils';
import {TEMPLATE_DELETE} from '../DialogManager/templates';
2020-03-18 11:58:56 +05:00
export const SelectionHeader = () => {
// State
2020-01-17 21:26:01 +05:00
const [state, dispatch] = useTracked();
2020-05-10 22:19:10 +05:00
const {colors, selectionMode, selectedItemsList, currentScreen,containerState} = state;
2020-03-18 11:58:56 +05:00
const [selectAll, setSelectAll] = useState(false);
2020-01-17 21:26:01 +05:00
2020-05-10 22:19:10 +05:00
return (containerState.noSelectionHeader ? null :
<Animatable.View
2020-01-31 18:40:56 +05:00
transition={['translateY']}
2020-02-01 12:56:30 +05:00
duration={300}
2020-01-31 18:40:56 +05:00
useNativeDriver={true}
style={{
width: '100%',
position: 'absolute',
2020-03-18 11:58:56 +05:00
height: Platform.OS === 'android' ? 80 + StatusBar.currentHeight : 80,
2020-01-18 00:46:29 +05:00
backgroundColor: colors.bg,
paddingTop: Platform.OS === 'ios' ? 0 : StatusBar.currentHeight,
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: [
{
translateY: selectionMode ? 0 : -150,
2020-01-31 18:40:56 +05:00
},
],
}}>
<View
style={{
2020-01-22 02:51:24 +05:00
width: '100%',
height: 50,
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
}}>
<View
style={{
justifyContent: 'space-between',
flexDirection: 'row',
alignItems: 'center',
}}>
<TouchableOpacity
onPress={() => {
2020-01-18 00:46:29 +05:00
dispatch({type: ACTIONS.SELECTION_MODE, enabled: !selectionMode});
dispatch({type: ACTIONS.CLEAR_SELECTION});
}}
hitSlop={{top: 20, bottom: 20, left: 50, right: 40}}
style={{
justifyContent: 'center',
alignItems: 'flex-start',
height: 40,
width: 50,
}}>
<Icon
style={{
marginLeft: -5,
}}
color={colors.pri}
name={'chevron-left'}
2020-02-20 12:16:32 +05:00
size={SIZE.xxxl}
/>
</TouchableOpacity>
2020-03-18 11:58:56 +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}
</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
onPress={() => {
dispatch({type: ACTIONS.SELECTION_MODE, enabled: false});
2020-03-02 10:25:38 +05:00
dispatch({type: ACTIONS.CLEAR_SELECTION});
2020-02-20 14:45:23 +05:00
eSendEvent(eOpenMoveNoteDialog);
}}>
<Icon
style={{
paddingLeft: 25,
}}
color={colors.accent}
name={'plus'}
size={SIZE.xl}
/>
</TouchableOpacity>
)}
{currentScreen === 'trash' || currentScreen === 'notebooks' ? null : (
<TouchableOpacity
onPress={async () => {
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) {
selectedItemsList.forEach(async item => {
if (!item.favorite) {
2020-03-02 12:07:19 +05:00
favCount += 1;
} else {
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(
favCount + ' notes added to favorites',
2020-03-02 12:07:19 +05:00
'success',
);
2020-02-20 14:45:23 +05:00
}
}}>
<Icon
style={{
paddingLeft: 25,
}}
color={colors.accent}
name={'star'}
size={SIZE.xl - 3}
/>
</TouchableOpacity>
)}
2020-02-20 14:45:23 +05:00
{currentScreen === 'trash' ? null : (
<TouchableOpacity
onPress={async () => {
2020-03-02 10:25:38 +05:00
eSendEvent(eOpenSimpleDialog, TEMPLATE_DELETE('item'));
return;
2020-02-20 14:45:23 +05:00
}}>
<Icon
style={{
paddingLeft: 25,
}}
color={colors.errorText}
name={'delete'}
size={SIZE.xl - 3}
/>
</TouchableOpacity>
)}
{currentScreen === 'trash' ? (
<TouchableOpacity
onPress={async () => {
if (selectedItemsList.length > 0) {
let noteIds = [];
selectedItemsList.forEach(item => {
noteIds.push(item.id);
});
await db.trash.restore(...noteIds);
2020-02-20 18:20:48 +05:00
2020-02-20 14:45:23 +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
style={{
paddingLeft: 25,
}}
color={colors.errorText}
name="delete-restore"
size={SIZE.xl - 3}
/>
</TouchableOpacity>
) : null}
</View>
</View>
2020-03-18 11:58:56 +05:00
<TouchableOpacity
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',
}}>
<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>
</TouchableOpacity>
</Animatable.View>
);
};
export default SelectionHeader;