2021-04-11 14:14:38 +05:00
|
|
|
import React, {useEffect} from 'react';
|
|
|
|
|
import {BackHandler} from 'react-native';
|
2021-02-08 19:02:47 +05:00
|
|
|
import {View} from 'react-native';
|
|
|
|
|
import {useSafeAreaInsets} from 'react-native-safe-area-context';
|
|
|
|
|
import {useTracked} from '../../provider';
|
|
|
|
|
import {Actions} from '../../provider/Actions';
|
2021-06-05 21:10:20 +05:00
|
|
|
import { useSelectionStore } from '../../provider/stores';
|
2021-04-11 14:04:14 +05:00
|
|
|
import {eSendEvent, ToastEvent} from '../../services/EventManager';
|
2021-02-08 19:02:47 +05:00
|
|
|
import Navigation from '../../services/Navigation';
|
|
|
|
|
import {db} from '../../utils/DB';
|
2021-06-03 12:07:07 +05:00
|
|
|
import {
|
|
|
|
|
eOpenMoveNoteDialog,
|
|
|
|
|
eOpenSimpleDialog,
|
|
|
|
|
refreshNotesPage,
|
|
|
|
|
} from '../../utils/Events';
|
2021-02-08 19:02:47 +05:00
|
|
|
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-01-13 14:47:28 +05:00
|
|
|
|
2021-06-03 12:07:07 +05:00
|
|
|
export const SelectionHeader = React.memo(({screen, type, extras}) => {
|
2020-01-17 21:26:01 +05:00
|
|
|
const [state, dispatch] = useTracked();
|
2021-06-05 21:10:20 +05:00
|
|
|
const {colors} = state;
|
|
|
|
|
|
|
|
|
|
const selectionMode = useSelectionStore(state => state.selectionMode);
|
|
|
|
|
const selectedItemsList = useSelectionStore(state => state.selectedItemsList);
|
|
|
|
|
const setSelectionMode = useSelectionStore(state => state.setSelectionMode);
|
|
|
|
|
const clearSelection = useSelectionStore(state => state.clearSelection)
|
|
|
|
|
|
2020-09-07 19:19:20 +05:00
|
|
|
const insets = useSafeAreaInsets();
|
2020-01-13 14:47:28 +05:00
|
|
|
|
2021-02-16 16:11:10 +05:00
|
|
|
const addToFavorite = async () => {
|
|
|
|
|
if (selectedItemsList.length > 0) {
|
2021-04-11 14:04:14 +05:00
|
|
|
selectedItemsList.forEach(item => {
|
2021-02-16 16:11:10 +05:00
|
|
|
db.notes.note(item.id).favorite();
|
|
|
|
|
});
|
|
|
|
|
Navigation.setRoutesToUpdate([
|
|
|
|
|
Navigation.routeNames.Notes,
|
|
|
|
|
Navigation.routeNames.NotesPage,
|
|
|
|
|
Navigation.routeNames.Favorites,
|
|
|
|
|
]);
|
2021-06-05 21:10:20 +05:00
|
|
|
setSelectionMode(false);
|
|
|
|
|
clearSelection();
|
2021-02-16 16:11:10 +05:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const restoreItem = async () => {
|
|
|
|
|
if (selectedItemsList.length > 0) {
|
|
|
|
|
let noteIds = [];
|
2021-04-11 14:04:14 +05:00
|
|
|
selectedItemsList.forEach(item => {
|
2021-02-16 16:11:10 +05:00
|
|
|
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,
|
|
|
|
|
]);
|
2021-06-05 21:10:20 +05:00
|
|
|
setSelectionMode(false);
|
|
|
|
|
clearSelection();
|
2021-02-20 15:03:02 +05:00
|
|
|
ToastEvent.show({
|
|
|
|
|
heading: 'Restore successful',
|
|
|
|
|
type: 'success',
|
|
|
|
|
});
|
2021-02-16 16:11:10 +05:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-04-11 14:14:38 +05:00
|
|
|
const onBackPress = () => {
|
2021-06-05 21:10:20 +05:00
|
|
|
setSelectionMode(false);
|
|
|
|
|
clearSelection();
|
2021-04-11 14:14:38 +05:00
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (selectionMode) {
|
|
|
|
|
BackHandler.addEventListener('hardwareBackPress', onBackPress);
|
|
|
|
|
} else {
|
|
|
|
|
BackHandler.removeEventListener('hardwareBackPress', onBackPress);
|
|
|
|
|
}
|
|
|
|
|
}, [selectionMode]);
|
|
|
|
|
|
2021-06-03 12:07:07 +05:00
|
|
|
return !selectionMode || Navigation.getCurrentScreen() !== screen ? null : (
|
2021-04-11 14:04:14 +05:00
|
|
|
<View
|
2020-01-13 14:47:28 +05:00
|
|
|
style={{
|
|
|
|
|
width: '100%',
|
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,
|
2020-01-22 02:51:24 +05:00
|
|
|
paddingHorizontal: 12,
|
2020-01-13 14:47:28 +05:00
|
|
|
}}>
|
|
|
|
|
<View
|
|
|
|
|
style={{
|
|
|
|
|
flexDirection: 'row',
|
2020-11-04 20:49:21 +05:00
|
|
|
justifyContent: 'flex-start',
|
2020-01-13 14:47:28 +05:00
|
|
|
alignItems: 'center',
|
2020-11-04 20:49:21 +05:00
|
|
|
position: 'absolute',
|
|
|
|
|
left: 12,
|
|
|
|
|
paddingTop: insets.top,
|
2020-01-13 14:47:28 +05:00
|
|
|
}}>
|
2020-11-04 20:49:21 +05:00
|
|
|
<ActionIcon
|
|
|
|
|
customStyle={{
|
|
|
|
|
justifyContent: 'center',
|
2020-01-13 14:47:28 +05:00
|
|
|
alignItems: 'center',
|
2020-11-04 20:49:21 +05:00
|
|
|
height: 40,
|
|
|
|
|
width: 40,
|
|
|
|
|
borderRadius: 100,
|
|
|
|
|
marginLeft: -5,
|
|
|
|
|
marginRight: 25,
|
|
|
|
|
}}
|
|
|
|
|
onPress={() => {
|
2021-06-05 21:10:20 +05:00
|
|
|
setSelectionMode(!selectionMode)
|
|
|
|
|
clearSelection();
|
2020-11-04 20:49:21 +05:00
|
|
|
}}
|
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-04-11 14:04:14 +05:00
|
|
|
{screen === 'Trash' ||
|
|
|
|
|
screen === 'Notebooks' ||
|
2021-06-03 12:07:07 +05:00
|
|
|
screen === 'Notebook' ||
|
|
|
|
|
type === 'topic' ? null : (
|
2020-11-04 20:49:21 +05:00
|
|
|
<ActionIcon
|
2020-12-16 11:08:58 +05:00
|
|
|
onPress={async () => {
|
2021-06-05 21:10:20 +05:00
|
|
|
//setSelectionMode(false);
|
2020-12-16 11:08:58 +05:00
|
|
|
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}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2020-01-13 14:47:28 +05:00
|
|
|
|
2021-06-03 12:07:07 +05:00
|
|
|
{type === 'topic' && (
|
|
|
|
|
<ActionIcon
|
|
|
|
|
onPress={async () => {
|
|
|
|
|
if (selectedItemsList.length > 0) {
|
|
|
|
|
await db.notebooks
|
|
|
|
|
.notebook(extras.notebook)
|
|
|
|
|
.topics.topic(extras.topic)
|
|
|
|
|
.delete(...selectedItemsList.map(item => item.id));
|
|
|
|
|
|
|
|
|
|
eSendEvent(refreshNotesPage);
|
|
|
|
|
Navigation.setRoutesToUpdate([
|
|
|
|
|
Navigation.routeNames.NotesPage,
|
|
|
|
|
Navigation.routeNames.Favorites,
|
|
|
|
|
Navigation.routeNames.Notes,
|
|
|
|
|
Navigation.routeNames.Notebook,
|
|
|
|
|
Navigation.routeNames.Notebooks,
|
|
|
|
|
]);
|
2021-06-05 21:10:20 +05:00
|
|
|
setSelectionMode(false);
|
|
|
|
|
clearSelection();
|
2021-06-03 12:07:07 +05:00
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
customStyle={{
|
|
|
|
|
marginLeft: 10,
|
|
|
|
|
}}
|
|
|
|
|
color={colors.light}
|
|
|
|
|
name="minus"
|
|
|
|
|
size={SIZE.xl}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
|
2021-04-11 14:04:14 +05:00
|
|
|
{screen === '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-04-11 14:04:14 +05:00
|
|
|
{screen === '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-04-11 14:04:14 +05:00
|
|
|
{screen === '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}
|
2020-01-13 14:47:28 +05:00
|
|
|
</View>
|
2021-04-11 14:04:14 +05:00
|
|
|
</View>
|
2020-01-13 14:47:28 +05:00
|
|
|
);
|
2021-06-02 17:34:39 +05:00
|
|
|
});
|
2020-01-13 14:47:28 +05:00
|
|
|
|
|
|
|
|
export default SelectionHeader;
|