import React, {useEffect, useState} from 'react'; import { Keyboard, KeyboardAvoidingView, Platform, Text, TouchableOpacity, View, StatusBar, SafeAreaView, } from 'react-native'; import * as Animatable from 'react-native-animatable'; import Icon from 'react-native-vector-icons/MaterialCommunityIcons'; import {br, opacity, pv, SIZE, WEIGHT} from '../../common/common'; import {useTracked} from '../../provider'; import {ACTIONS} from '../../provider/actions'; import {eSubscribeEvent, eUnSubscribeEvent} from '../../services/eventManager'; import {eScrollEvent, eClearSearch} from '../../services/events'; import {db, getElevation, ToastEvent, DDS, selection} from '../../utils/utils'; import {Header} from '../header'; import {Search} from '../SearchInput'; import SelectionHeader from '../SelectionHeader'; import {inputRef} from '../../utils/refs'; const AnimatedTouchableOpacity = Animatable.createAnimatableComponent( TouchableOpacity, ); const AnimatedSafeAreaView = Animatable.createAnimatableComponent(SafeAreaView); export const Container = ({ children, bottomButtonOnPress, bottomButtonText, noBottomButton = false, data = [], heading, canGoBack = true, menu, customIcon, verticalMenu = false, preventDefaultMargins, navigation = null, isLoginNavigator, placeholder = '', noSearch = false, noSelectionHeader = false, headerColor = null, type = null, }) => { // State const [state, dispatch] = useTracked(); const {colors, selectionMode, searchResults, loading} = state; const [text, setText] = useState(''); const [hideHeader, setHideHeader] = useState(false); const [buttonHide, setButtonHide] = useState(false); let offsetY = 0; let countUp = 1; let countDown = 0; let searchResult = []; const onScroll = y => { if (searchResults.length > 0) return; if (y < 30) { countUp = 1; countDown = 0; setHideHeader(false); } if (y > offsetY) { if (y - offsetY < 150 || countDown > 0) return; countDown = 1; countUp = 0; setHideHeader(true); } else { if (offsetY - y < 50 || countUp > 0) return; countDown = 0; countUp = 1; setHideHeader(false); } offsetY = y; }; const onChangeText = value => { setText(value); }; const onSubmitEditing = async () => { if (!text || text.length < 1) { ToastEvent.show('Please enter a search keyword'); clearSearch(); return; } if (!type) return; searchResult = await db.lookup[type]( data[0].data ? db.notes.all : data, text, ); if (!searchResult || searchResult.length === 0) { ToastEvent.show('No search results found for ' + text, 'error'); return; } else { dispatch({ type: ACTIONS.SEARCH_RESULTS, results: { type, results: searchResult, keyword: text, }, }); } }; const onBlur = () => { if (text && text.length < 1) { clearSearch(); } }; const onFocus = () => { //setSearch(false); }; const clearSearch = () => { searchResult = null; setText(null); inputRef.current?.setNativeProps({ text: '', }); dispatch({ type: ACTIONS.SEARCH_RESULTS, results: { results: [], type: null, keyword: null, }, }); }; useEffect(() => { eSubscribeEvent(eClearSearch, clearSearch); Keyboard.addListener('keyboardDidShow', () => { setTimeout(() => { if (DDS.isTab) return; setButtonHide(true); }, 300); }); Keyboard.addListener('keyboardDidHide', () => { setTimeout(() => { if (DDS.isTab) return; setButtonHide(false); }, 0); }); return () => { eUnSubscribeEvent(eClearSearch, clearSearch); Keyboard.removeListener('keyboardDidShow', () => { setTimeout(() => { if (DDS.isTab) return; setButtonHide(true); }, 300); }); Keyboard.removeListener('keyboardDidHide', () => { setTimeout(() => { if (DDS.isTab) return; setButtonHide(false); }, 0); }); }; }, []); useEffect(() => { selection.data = data; selection.type = type; eSubscribeEvent(eScrollEvent, onScroll); return () => { eUnSubscribeEvent(eScrollEvent, onScroll); }; }); // Render return ( {noSelectionHeader ? null : }
{ setHideHeader(false); countUp = 0; countDown = 0; }} headerColor={headerColor} navigation={navigation} colors={colors} isLoginNavigator={isLoginNavigator} preventDefaultMargins={preventDefaultMargins} heading={heading} canGoBack={canGoBack} customIcon={customIcon} /> {data[0] && !noSearch ? ( setText('')} hide={hideHeader} onChangeText={onChangeText} headerColor={headerColor} onSubmitEditing={onSubmitEditing} placeholder={placeholder} onBlur={onBlur} onFocus={onFocus} clearSearch={clearSearch} value={text} /> ) : null} {children} {noBottomButton ? null : ( {' ' + bottomButtonText} )} ); }; export default Container;