import React, {createRef, useState} from 'react';
import {
FlatList,
Platform,
RefreshControl,
SectionList,
Text,
View,
} from 'react-native';
import {useSafeArea} from 'react-native-safe-area-context';
import {SIZE, WEIGHT} from '../../common/common';
import {useTracked} from '../../provider';
import {ACTIONS} from '../../provider/actions';
import {eSendEvent} from '../../services/eventManager';
import {eClearSearch, eScrollEvent} from '../../services/events';
import {db, hexToRGBA, ToastEvent} from '../../utils/utils';
import {NotebookItem} from '../NotebookItem';
import NoteItem from '../NoteItem';
import SelectionWrapper from '../SelectionWrapper';
const sectionListRef = createRef();
const SimpleList = ({
data,
type,
placeholder,
renderItem,
focused,
placeholderText,
pinned = null,
customRefresh,
customRefreshing,
isMove,
hideMore,
noteToMove,
isHome = false,
}) => {
const [state, dispatch] = useTracked();
const {colors, selectionMode, syncing} = state;
const searchResults = {...state.searchResults};
const [refreshing, setRefreshing] = useState(false);
const insets = useSafeArea();
const _onScroll = event => {
if (!event) return;
let y = event.nativeEvent.contentOffset.y;
eSendEvent(eScrollEvent, y);
};
const _ListFooterComponent = data[0] ? (
- End -
) : null;
const _renderSectionHeader = ({section: {title}}) => (
{title}
);
const _onRefresh = async () => {
if (Platform.OS === 'ios') {
dispatch({
type: ACTIONS.SYNCING,
syncing: true,
});
} else {
setRefreshing(true);
}
try {
let user = await db.user.get();
dispatch({type: ACTIONS.USER, user: user});
await db.sync();
if (Platform.OS === 'ios') {
dispatch({
type: ACTIONS.SYNCING,
syncing: false,
});
} else {
setRefreshing(false);
}
ToastEvent.show('Sync Complete', 'success');
} catch (e) {
if (Platform.OS === 'ios') {
dispatch({
type: ACTIONS.SYNCING,
syncing: false,
});
} else {
setRefreshing(false);
}
ToastEvent.show(e.message, 'error');
}
dispatch({type: ACTIONS.ALL});
};
const _ListHeaderComponent_S =
searchResults.type === type && searchResults.results.length > 0 ? (
Showing Results for {searchResults.keyword}
{
eSendEvent(eClearSearch);
}}
style={{
fontFamily: WEIGHT.regular,
color: colors.errorText,
fontSize: SIZE.xs,
}}>
Clear
) : (
{pinned && pinned.length > 0 ? (
<>
item.id.toString()}
renderItem={({item, index}) =>
item.type === 'notebook' ? (
{
if (!selectionMode) {
dispatch({
type: ACTIONS.SELECTION_MODE,
enabled: true,
});
}
dispatch({type: ACTIONS.SELECTED_ITEMS, item: item});
}}
noteToMove={noteToMove}
item={item}
pinned={true}
index={index}
colors={colors}
/>
) : (
{
if (!selectionMode) {
dispatch({
type: ACTIONS.SELECTION_MODE,
enabled: true,
});
}
dispatch({type: ACTIONS.SELECTED_ITEMS, item: item});
}}
update={() => {
dispatch({type: ACTIONS.NOTES});
}}
item={item}
index={index}
/>
)
}
/>
>
) : null}
);
const _ListEmptyComponent = (
<>
{placeholder}
{placeholderText}
>
);
const _listKeyExtractor = (item, index) =>
item.id.toString() + index.toString();
return isHome && searchResults.type !== 'notes' ? (
}
keyExtractor={_listKeyExtractor}
renderSectionHeader={_renderSectionHeader}
onScroll={_onScroll}
stickySectionHeadersEnabled={false}
ListEmptyComponent={_ListEmptyComponent}
ListHeaderComponent={_ListHeaderComponent_S}
contentContainerStyle={{
width: '100%',
alignSelf: 'center',
minHeight: '100%',
}}
style={{
height: '100%',
}}
removeClippedSubviews={true}
ListFooterComponent={_ListFooterComponent}
renderItem={renderItem}
/>
) : (
0
? searchResults.results
: data
}
refreshControl={
}
keyExtractor={_listKeyExtractor}
ListFooterComponent={_ListFooterComponent}
onScroll={_onScroll}
ListHeaderComponent={_ListHeaderComponent_S}
ListEmptyComponent={_ListEmptyComponent}
contentContainerStyle={{
width: '100%',
alignSelf: 'center',
minHeight: '100%',
}}
style={{
height: '100%',
}}
renderItem={renderItem}
/>
);
};
export default SimpleList;