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

224 lines
6.2 KiB
JavaScript
Raw Normal View History

import React, {useEffect, useState} from 'react';
import {View} from 'react-native';
import Animated, {Easing, useValue} from 'react-native-reanimated';
import {useSafeAreaInsets} from 'react-native-safe-area-context';
import {useTracked} from '../../provider';
import {Actions} from '../../provider/Actions';
import {
eSendEvent,
eSubscribeEvent,
eUnSubscribeEvent,
ToastEvent,
} from '../../services/EventManager';
import Navigation from '../../services/Navigation';
import {db} from '../../utils/DB';
2021-02-16 16:11:10 +05:00
import {eOpenMoveNoteDialog, eOpenSimpleDialog} from '../../utils/Events';
import {SIZE} from '../../utils/SizeUtils';
import {sleep} from '../../utils/TimeUtils';
import {ActionIcon} from '../ActionIcon';
import {TEMPLATE_DELETE} from '../DialogManager/Templates';
2020-11-04 20:49:21 +05:00
import Heading from '../Typography/Heading';
2020-03-18 11:58:56 +05:00
export const SelectionHeader = () => {
// State
2020-01-17 21:26:01 +05:00
const [state, dispatch] = useTracked();
const {colors, selectionMode, selectedItemsList} = state;
2020-09-07 19:19:20 +05:00
const insets = useSafeAreaInsets();
const translateY = useValue(-150);
const opacity = useValue(0);
2020-01-17 21:26:01 +05:00
const [headerTextState, setHeaderTextState] = useState(
Navigation.getHeaderState(),
);
const currentScreen = headerTextState.currentScreen;
const onHeaderStateChange = (event) => {
if (!event) return;
setHeaderTextState(event);
};
useEffect(() => {
eSubscribeEvent('onHeaderStateChange', onHeaderStateChange);
return () => {
eUnSubscribeEvent('onHeaderStateChange', onHeaderStateChange);
};
}, []);
2020-09-07 19:19:20 +05:00
useEffect(() => {
translateY.setValue(selectionMode ? 0 : -150);
Animated.timing(opacity, {
duration: 200,
toValue: selectionMode ? 1 : 0,
2020-09-09 14:55:59 +05:00
easing: Easing.in(Easing.ease),
}).start();
}, [selectionMode]);
2021-02-16 16:11:10 +05:00
const addToFavorite = async () => {
if (selectedItemsList.length > 0) {
selectedItemsList.forEach((item) => {
db.notes.note(item.id).favorite();
});
Navigation.setRoutesToUpdate([
Navigation.routeNames.Notes,
Navigation.routeNames.NotesPage,
Navigation.routeNames.Favorites,
]);
dispatch({type: Actions.SELECTION_MODE, enabled: false});
dispatch({type: Actions.CLEAR_SELECTION});
}
};
const restoreItem = async () => {
if (selectedItemsList.length > 0) {
let noteIds = [];
selectedItemsList.forEach((item) => {
noteIds.push(item.id);
});
await db.trash.restore(...noteIds);
Navigation.setRoutesToUpdate([
Navigation.routeNames.Tags,
Navigation.routeNames.Notes,
Navigation.routeNames.Notebooks,
Navigation.routeNames.NotesPage,
Navigation.routeNames.Favorites,
Navigation.routeNames.Trash,
]);
dispatch({type: Actions.SELECTION_MODE, enabled: false});
dispatch({type: Actions.CLEAR_SELECTION});
ToastEvent.show('Restore complete', 'success');
}
};
2020-09-27 10:15:19 +05:00
return (
2020-09-07 19:19:20 +05:00
<Animated.View
style={{
width: '100%',
position: 'absolute',
2020-11-04 20:49:21 +05:00
height: 50 + insets.top,
paddingTop: insets.top,
2021-02-16 16:11:10 +05:00
backgroundColor: colors.accent,
2020-11-04 20:49:21 +05:00
justifyContent: 'center',
alignItems: 'center',
flexDirection: 'row',
2020-01-31 18:40:56 +05:00
zIndex: 999,
opacity: opacity,
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
},
],
}}>
<View
style={{
flexDirection: 'row',
2020-11-04 20:49:21 +05:00
justifyContent: 'flex-start',
alignItems: 'center',
2020-11-04 20:49:21 +05:00
position: 'absolute',
left: 12,
paddingTop: insets.top,
}}>
2020-11-04 20:49:21 +05:00
<ActionIcon
customStyle={{
justifyContent: 'center',
alignItems: 'center',
2020-11-04 20:49:21 +05:00
height: 40,
width: 40,
borderRadius: 100,
marginLeft: -5,
marginRight: 25,
}}
onPress={() => {
dispatch({type: Actions.SELECTION_MODE, enabled: !selectionMode});
dispatch({type: Actions.CLEAR_SELECTION});
}}
2021-02-16 16:11:10 +05:00
color={colors.light}
name="close"
2020-11-04 20:49:21 +05:00
size={SIZE.xxxl}
/>
2020-03-02 11:13:40 +05:00
2020-11-04 20:49:21 +05:00
{Platform.OS === 'android' ? (
2021-02-16 16:11:10 +05:00
<Heading color={colors.light}>
{selectedItemsList.length + ' Selected'}
</Heading>
2020-11-04 20:49:21 +05:00
) : null}
</View>
2020-03-02 11:13:40 +05:00
2020-11-04 20:49:21 +05:00
{Platform.OS !== 'android' ? (
2021-02-16 16:11:10 +05:00
<Heading color={colors.light}>
{selectedItemsList.length + ' Selected'}
</Heading>
2020-11-04 20:49:21 +05:00
) : null}
2020-03-02 11:13:40 +05:00
2020-11-04 20:49:21 +05:00
<View
style={{
flexDirection: 'row',
justifyContent: 'flex-start',
alignItems: 'center',
position: 'absolute',
right: 12,
paddingTop: insets.top,
}}>
2021-02-16 16:11:10 +05:00
{currentScreen === 'Trash' ||
currentScreen === 'Notebooks' ||
currentScreen === 'Notebook' ? null : (
2020-11-04 20:49:21 +05:00
<ActionIcon
onPress={async () => {
2021-02-16 16:11:10 +05:00
//dispatch({type: Actions.SELECTION_MODE, enabled: false});
await sleep(100);
2020-11-04 20:49:21 +05:00
eSendEvent(eOpenMoveNoteDialog);
}}
customStyle={{
marginLeft: 10,
}}
2021-02-16 16:11:10 +05:00
color={colors.light}
2020-11-04 20:49:21 +05:00
name="plus"
size={SIZE.xl}
/>
)}
2021-02-16 16:11:10 +05:00
{currentScreen === 'Favorites' ? (
2020-12-16 13:12:35 +05:00
<ActionIcon
2021-02-16 16:11:10 +05:00
onPress={addToFavorite}
2020-12-16 13:12:35 +05:00
customStyle={{
marginLeft: 10,
}}
2021-02-16 16:11:10 +05:00
color={colors.light}
2020-12-16 13:12:35 +05:00
name="star-off"
size={SIZE.xl}
/>
) : null}
2021-02-16 16:11:10 +05:00
{currentScreen === 'Trash' ? null : (
2020-11-04 20:49:21 +05:00
<ActionIcon
customStyle={{
marginLeft: 10,
}}
onPress={async () => {
eSendEvent(eOpenSimpleDialog, TEMPLATE_DELETE('item'));
return;
}}
2021-02-16 16:11:10 +05:00
color={colors.light}
2020-11-04 20:49:21 +05:00
name="delete"
size={SIZE.xl}
/>
)}
2020-02-20 14:45:23 +05:00
2021-02-16 16:11:10 +05:00
{currentScreen === 'Trash' ? (
2020-11-04 20:49:21 +05:00
<ActionIcon
customStyle={{
marginLeft: 10,
}}
2021-02-16 16:11:10 +05:00
color={colors.light}
onPress={restoreItem}
2020-11-04 20:49:21 +05:00
name="delete-restore"
size={SIZE.xl - 3}
/>
) : null}
</View>
2020-09-07 19:19:20 +05:00
</Animated.View>
);
};
export default SelectionHeader;