2021-11-11 13:06:27 +05:00
|
|
|
import React, {useEffect, useRef, useState} from 'react';
|
|
|
|
|
import {FlatList, RefreshControl} from 'react-native';
|
|
|
|
|
import {useTracked} from '../../provider';
|
|
|
|
|
import {eSendEvent} from '../../services/EventManager';
|
2020-12-20 23:01:35 +05:00
|
|
|
import Sync from '../../services/Sync';
|
2021-11-11 13:06:27 +05:00
|
|
|
import {eScrollEvent} from '../../utils/Events';
|
2021-04-11 14:04:14 +05:00
|
|
|
import JumpToDialog from '../JumpToDialog';
|
2021-11-11 13:06:27 +05:00
|
|
|
import {NotebookWrapper} from '../NotebookItem/wrapper';
|
|
|
|
|
import {NoteWrapper} from '../NoteItem/wrapper';
|
2021-07-13 10:19:06 +05:00
|
|
|
import SortDialog from '../SortDialog';
|
2020-11-14 10:06:32 +05:00
|
|
|
import TagItem from '../TagItem';
|
2021-11-11 13:06:27 +05:00
|
|
|
import {Empty} from './empty';
|
|
|
|
|
import {Footer} from './footer';
|
|
|
|
|
import {Header} from './header';
|
|
|
|
|
import {SectionHeader} from './section-header';
|
2020-09-18 20:47:52 +05:00
|
|
|
|
2021-06-05 01:35:58 +05:00
|
|
|
let renderItems = {
|
2021-06-13 14:58:54 +05:00
|
|
|
note: NoteWrapper,
|
|
|
|
|
notebook: NotebookWrapper,
|
|
|
|
|
topic: NotebookWrapper,
|
|
|
|
|
tag: TagItem,
|
2021-08-17 09:33:49 +05:00
|
|
|
section: SectionHeader
|
2021-06-05 01:35:58 +05:00
|
|
|
};
|
|
|
|
|
|
2021-07-13 10:19:06 +05:00
|
|
|
const RenderItem = ({item, index}) => {
|
2021-12-03 09:37:56 +05:00
|
|
|
if (!item) return <View/>
|
2021-12-03 09:34:44 +05:00
|
|
|
const Item = renderItems[item.itemType || item.type] || <View/>
|
2021-08-17 09:33:49 +05:00
|
|
|
|
|
|
|
|
return (
|
2021-08-17 12:45:23 +05:00
|
|
|
<Item item={item} tags={item.tags ? [...item.tags] : []} index={index} />
|
2021-08-17 09:33:49 +05:00
|
|
|
);
|
2021-07-13 10:19:06 +05:00
|
|
|
};
|
2021-06-13 14:58:54 +05:00
|
|
|
|
2021-06-05 01:35:58 +05:00
|
|
|
const SimpleList = ({
|
|
|
|
|
listData,
|
|
|
|
|
type,
|
|
|
|
|
customRefresh,
|
|
|
|
|
customRefreshing,
|
|
|
|
|
refreshCallback,
|
|
|
|
|
placeholderData,
|
|
|
|
|
loading,
|
|
|
|
|
headerProps = {
|
2021-08-17 09:33:49 +05:00
|
|
|
heading: 'Home'
|
2021-06-05 01:35:58 +05:00
|
|
|
},
|
2021-11-30 13:29:33 +05:00
|
|
|
screen,
|
|
|
|
|
ListHeader
|
2021-06-05 01:35:58 +05:00
|
|
|
}) => {
|
|
|
|
|
const [state] = useTracked();
|
|
|
|
|
const {colors} = state;
|
|
|
|
|
const scrollRef = useRef();
|
2021-06-05 21:10:20 +05:00
|
|
|
const [_loading, _setLoading] = useState(true);
|
2021-06-05 01:35:58 +05:00
|
|
|
const refreshing = false;
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2021-11-11 13:06:27 +05:00
|
|
|
let timeout = null;
|
2021-06-05 01:35:58 +05:00
|
|
|
if (!loading) {
|
2021-11-11 13:06:27 +05:00
|
|
|
timeout = setTimeout(
|
2021-07-17 20:40:49 +05:00
|
|
|
() => {
|
|
|
|
|
_setLoading(false);
|
|
|
|
|
},
|
2021-08-17 09:33:49 +05:00
|
|
|
listData.length === 0 ? 0 : 300
|
2021-07-17 20:40:49 +05:00
|
|
|
);
|
2021-06-05 01:35:58 +05:00
|
|
|
} else {
|
2021-07-13 10:46:21 +05:00
|
|
|
_setLoading(true);
|
2021-06-05 01:35:58 +05:00
|
|
|
}
|
2021-11-11 13:06:27 +05:00
|
|
|
return () => {
|
|
|
|
|
clearTimeout(timeout);
|
|
|
|
|
};
|
2021-07-13 10:46:21 +05:00
|
|
|
}, [loading]);
|
2021-06-05 01:35:58 +05:00
|
|
|
|
|
|
|
|
const renderItem = React.useCallback(
|
|
|
|
|
({item, index}) =>
|
|
|
|
|
item.type === 'header' ? (
|
|
|
|
|
<SectionHeader
|
|
|
|
|
item={item}
|
|
|
|
|
index={index}
|
2021-09-13 13:42:01 +05:00
|
|
|
color={headerProps.color}
|
2021-07-17 20:40:49 +05:00
|
|
|
title={headerProps.heading}
|
2021-07-13 14:10:51 +05:00
|
|
|
type={screen === 'Notes' ? 'home' : type}
|
2021-06-05 01:35:58 +05:00
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
<RenderItem item={item} index={index} />
|
|
|
|
|
),
|
2021-08-17 09:33:49 +05:00
|
|
|
[]
|
2021-06-05 01:35:58 +05:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const _onRefresh = async () => {
|
|
|
|
|
await Sync.run();
|
|
|
|
|
if (refreshCallback) {
|
|
|
|
|
refreshCallback();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const _onScroll = React.useCallback(
|
|
|
|
|
event => {
|
|
|
|
|
if (!event) return;
|
|
|
|
|
let y = event.nativeEvent.contentOffset.y;
|
|
|
|
|
eSendEvent(eScrollEvent, {
|
|
|
|
|
y,
|
2021-08-17 09:33:49 +05:00
|
|
|
screen
|
2021-06-05 01:35:58 +05:00
|
|
|
});
|
|
|
|
|
},
|
2021-08-17 09:33:49 +05:00
|
|
|
[screen]
|
2021-06-05 01:35:58 +05:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let styles = {
|
|
|
|
|
height: '100%',
|
|
|
|
|
width: '100%',
|
|
|
|
|
minHeight: 1,
|
2021-11-30 13:29:33 +05:00
|
|
|
minWidth: 1,
|
|
|
|
|
backgroundColor:colors.bg
|
2021-06-05 01:35:58 +05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const _keyExtractor = item => item.id || item.title;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<FlatList
|
|
|
|
|
style={styles}
|
|
|
|
|
keyExtractor={_keyExtractor}
|
|
|
|
|
ref={scrollRef}
|
2021-07-17 20:40:49 +05:00
|
|
|
data={_loading ? listData.slice(0, 9) : listData}
|
2021-06-05 01:35:58 +05:00
|
|
|
renderItem={renderItem}
|
|
|
|
|
onScroll={_onScroll}
|
|
|
|
|
initialNumToRender={10}
|
|
|
|
|
maxToRenderPerBatch={10}
|
|
|
|
|
refreshControl={
|
|
|
|
|
<RefreshControl
|
|
|
|
|
style={{
|
|
|
|
|
opacity: 0,
|
2021-08-17 09:33:49 +05:00
|
|
|
elevation: 0
|
2021-06-05 01:35:58 +05:00
|
|
|
}}
|
|
|
|
|
tintColor={colors.accent}
|
|
|
|
|
colors={[colors.accent]}
|
|
|
|
|
progressViewOffset={150}
|
|
|
|
|
onRefresh={customRefresh ? customRefresh : _onRefresh}
|
|
|
|
|
refreshing={customRefresh ? customRefreshing : refreshing}
|
|
|
|
|
/>
|
|
|
|
|
}
|
|
|
|
|
ListEmptyComponent={
|
|
|
|
|
<Empty
|
|
|
|
|
loading={loading || _loading}
|
|
|
|
|
placeholderData={placeholderData}
|
|
|
|
|
headerProps={headerProps}
|
2021-07-09 12:15:06 +05:00
|
|
|
type={type}
|
|
|
|
|
screen={screen}
|
2021-06-05 01:35:58 +05:00
|
|
|
/>
|
|
|
|
|
}
|
|
|
|
|
ListFooterComponent={<Footer />}
|
|
|
|
|
ListHeaderComponent={
|
2021-07-17 20:40:49 +05:00
|
|
|
<>
|
2021-11-30 13:29:33 +05:00
|
|
|
{ListHeader ? (
|
|
|
|
|
ListHeader
|
|
|
|
|
) : (
|
|
|
|
|
<Header
|
|
|
|
|
title={headerProps.heading}
|
|
|
|
|
paragraph={headerProps.paragraph}
|
|
|
|
|
onPress={headerProps.onPress}
|
|
|
|
|
icon={headerProps.icon}
|
|
|
|
|
color={headerProps.color}
|
|
|
|
|
type={type}
|
|
|
|
|
screen={screen}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2021-07-17 20:40:49 +05:00
|
|
|
</>
|
2021-06-05 01:35:58 +05:00
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
|
2021-07-13 10:19:06 +05:00
|
|
|
<SortDialog
|
|
|
|
|
screen={screen}
|
|
|
|
|
type={screen === 'Notes' ? 'home' : type}
|
|
|
|
|
colors={colors}
|
|
|
|
|
/>
|
|
|
|
|
<JumpToDialog
|
|
|
|
|
screen={screen}
|
2021-07-13 14:10:51 +05:00
|
|
|
data={listData}
|
2021-07-13 10:19:06 +05:00
|
|
|
type={screen === 'Notes' ? 'home' : type}
|
|
|
|
|
scrollRef={scrollRef}
|
|
|
|
|
/>
|
2021-06-05 01:35:58 +05:00
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2020-03-09 20:06:55 +05:00
|
|
|
export default SimpleList;
|