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

235 lines
7.3 KiB
JavaScript
Raw Normal View History

2020-02-20 14:45:23 +05:00
import React, {useEffect} from 'react';
2020-01-18 00:46:29 +05:00
import {
2020-01-18 01:04:33 +05:00
Platform,
2020-01-18 00:46:29 +05:00
SafeAreaView,
2020-01-18 01:04:33 +05:00
StatusBar,
Text,
2020-01-18 00:46:29 +05:00
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-03-14 13:54:16 +05:00
import {w, ToastEvent, db} from '../../utils/utils';
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';
import {TEMPLATE_DELETE} from '../DialogManager/templates';
export const AnimatedSafeAreaView = Animatable.createAnimatableComponent(
SafeAreaView,
);
export const SelectionHeader = ({navigation}) => {
// State
2020-01-17 21:26:01 +05:00
const [state, dispatch] = useTracked();
2020-02-20 14:45:23 +05:00
const {colors, selectionMode, selectedItemsList, currentScreen} = state;
2020-01-17 21:26:01 +05:00
2020-02-20 14:45:23 +05:00
useEffect(() => {
console.log(currentScreen);
}, [currentScreen]);
return (
<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-01-31 18:40:56 +05:00
height: Platform.OS === 'android' ? 50 + StatusBar.currentHeight : 50,
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 : -100,
},
],
}}>
<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>
<Text
style={{
fontSize: SIZE.lg,
fontFamily: WEIGHT.regular,
color: colors.pri,
textAlignVertical: 'center',
}}>
{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
if (selectedItemsList.length > 0) {
let noteIds = [];
selectedItemsList.forEach(item => {
noteIds.push(item.id);
});
if (currentScreen === 'notebooks') {
await db.notebooks.delete(...noteIds);
dispatch({type: ACTIONS.NOTEBOOKS});
ToastEvent.show('Notebooks moved to trash');
} else if (currentScreen === 'notebook') {
ToastEvent.show('Topics moved to trash');
// TODO
} else {
await db.notes.delete(...noteIds);
dispatch({type: ACTIONS.NOTES});
ToastEvent.show('Notes moved to trash');
}
2020-02-20 12:16:32 +05:00
2020-02-20 14:45:23 +05:00
dispatch({type: ACTIONS.SELECTION_MODE, enabled: false});
dispatch({type: ACTIONS.CLEAR_SELECTION});
}
}}>
<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
console.log(noteIds);
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>
</Animatable.View>
);
};
export default SelectionHeader;