import React, {useEffect, useState} from 'react'; import {Platform, StatusBar, Text, TouchableOpacity, View} from 'react-native'; import * as Animatable from 'react-native-animatable'; import Icon from 'react-native-vector-icons/MaterialCommunityIcons'; import {SIZE, WEIGHT} from '../../common/common'; import {useTracked} from '../../provider'; import {ACTIONS} from '../../provider/actions'; import {eSendEvent} from '../../services/eventManager'; import {eOpenMoveNoteDialog, eOpenSimpleDialog} from '../../services/events'; import {db, selection, ToastEvent} from '../../utils/utils'; import {TEMPLATE_DELETE} from '../DialogManager/templates'; export const SelectionHeader = () => { // State const [state, dispatch] = useTracked(); const {colors, selectionMode, selectedItemsList, currentScreen} = state; const [selectAll, setSelectAll] = useState(false); useEffect(() => {}, [currentScreen]); return ( { 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, }}> {selectAll ? 'All' : selectedItemsList.length} {currentScreen === 'trash' || currentScreen === 'notebooks' ? null : ( { dispatch({type: ACTIONS.SELECTION_MODE, enabled: false}); dispatch({type: ACTIONS.CLEAR_SELECTION}); eSendEvent(eOpenMoveNoteDialog); }}> )} {currentScreen === 'trash' || currentScreen === 'notebooks' ? null : ( { let favCount = 0; let unFavCount = 0; if (selectedItemsList.length > 0) { selectedItemsList.forEach(async item => { if (!item.favorite) { favCount += 1; } else { return; } await db.notes.note(item.id).favorite(); dispatch({type: ACTIONS.NOTES}); dispatch({type: ACTIONS.FAVORITES}); }); dispatch({type: ACTIONS.SELECTION_MODE, enabled: false}); dispatch({type: ACTIONS.CLEAR_SELECTION}); ToastEvent.show( favCount + ' notes added to favorites', 'success', ); } }}> )} {currentScreen === 'trash' ? null : ( { eSendEvent(eOpenSimpleDialog, TEMPLATE_DELETE('item')); return; }}> )} {currentScreen === 'trash' ? ( { if (selectedItemsList.length > 0) { let noteIds = []; selectedItemsList.forEach(item => { noteIds.push(item.id); }); await db.trash.restore(...noteIds); dispatch({type: ACTIONS.TRASH}); dispatch({type: ACTIONS.SELECTION_MODE, enabled: false}); dispatch({type: ACTIONS.CLEAR_SELECTION}); ToastEvent.show('Restore complete', 'success'); } }}> ) : null} { 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', }}> Select All ); }; export default SelectionHeader;