2021-06-08 12:26:55 +05:00
|
|
|
import React, {useEffect, useRef, useState} from 'react';
|
|
|
|
|
import {FlatList, RefreshControl} from 'react-native';
|
|
|
|
|
import {useTracked} from '../../provider';
|
|
|
|
|
import {eSendEvent} from '../../services/EventManager';
|
2021-06-03 12:01:40 +05:00
|
|
|
import SettingsService from '../../services/SettingsService';
|
2020-12-20 23:01:35 +05:00
|
|
|
import Sync from '../../services/Sync';
|
2021-06-08 12:26:55 +05:00
|
|
|
import {COLORS_NOTE} from '../../utils/Colors';
|
|
|
|
|
import {eScrollEvent} from '../../utils/Events';
|
2021-05-26 14:18:10 +05:00
|
|
|
import useAnnouncement from '../../utils/useAnnouncement';
|
2021-04-11 14:04:14 +05:00
|
|
|
import JumpToDialog from '../JumpToDialog';
|
2021-06-08 12:26:55 +05:00
|
|
|
import {NotebookWrapper} from '../NotebookItem/wrapper';
|
|
|
|
|
import {NoteWrapper} from '../NoteItem/wrapper';
|
2020-11-14 10:06:32 +05:00
|
|
|
import TagItem from '../TagItem';
|
2021-06-08 12:26:55 +05:00
|
|
|
import {Announcement} from './announcement';
|
|
|
|
|
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
|
|
|
const heights = {
|
|
|
|
|
note: 100,
|
|
|
|
|
notebook: 110,
|
|
|
|
|
tag: 80,
|
|
|
|
|
topic: 80,
|
|
|
|
|
header: 35,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const TrashI = ({item, index}) => {
|
|
|
|
|
return item.itemType === 'note' ? (
|
2021-06-07 11:53:27 +05:00
|
|
|
<NoteWrapper item={item} index={index} />
|
2021-06-05 01:35:58 +05:00
|
|
|
) : (
|
2021-06-07 11:53:27 +05:00
|
|
|
<NoteWrapper item={item} index={index} />
|
2021-06-05 01:35:58 +05:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let renderItems = {
|
|
|
|
|
notes: NoteWrapper,
|
|
|
|
|
notebooks: NotebookWrapper,
|
2021-06-07 11:53:27 +05:00
|
|
|
topics: NotebookWrapper,
|
2021-06-05 01:35:58 +05:00
|
|
|
tags: TagItem,
|
|
|
|
|
section: SectionHeader,
|
|
|
|
|
trash: TrashI,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const SimpleList = ({
|
|
|
|
|
listData,
|
|
|
|
|
type,
|
|
|
|
|
customRefresh,
|
|
|
|
|
customRefreshing,
|
|
|
|
|
refreshCallback,
|
|
|
|
|
sortMenuButton,
|
|
|
|
|
jumpToDialog,
|
|
|
|
|
placeholderData,
|
|
|
|
|
loading,
|
|
|
|
|
headerProps = {
|
|
|
|
|
heading: 'Home',
|
|
|
|
|
},
|
|
|
|
|
screen,
|
|
|
|
|
}) => {
|
|
|
|
|
const [state] = useTracked();
|
|
|
|
|
const {colors} = state;
|
2021-06-05 21:10:20 +05:00
|
|
|
|
2021-06-05 01:35:58 +05:00
|
|
|
const [dataProvider, setDataProvider] = useState([]);
|
|
|
|
|
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 RenderItem = renderItems[type];
|
|
|
|
|
const refreshing = false;
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!loading) {
|
|
|
|
|
setDataProvider(
|
2021-06-10 13:25:31 +05:00
|
|
|
dataProvider.length < 2 &&
|
|
|
|
|
listData.length >= 1 &&
|
|
|
|
|
SettingsService.get().homepage !== screen
|
2021-06-05 01:35:58 +05:00
|
|
|
? listData.slice(0, 1)
|
|
|
|
|
: listData,
|
|
|
|
|
);
|
2021-06-05 21:10:20 +05:00
|
|
|
|
2021-06-05 01:35:58 +05:00
|
|
|
if (
|
|
|
|
|
!listData ||
|
|
|
|
|
listData.length === 0 ||
|
|
|
|
|
SettingsService.get().homepage === screen
|
|
|
|
|
) {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
_setLoading(false);
|
2021-06-05 21:10:20 +05:00
|
|
|
}, 1);
|
2021-06-05 01:35:58 +05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
setDataProvider(listData);
|
|
|
|
|
_setLoading(false);
|
|
|
|
|
}, 150);
|
|
|
|
|
} else {
|
|
|
|
|
setDataProvider([]);
|
|
|
|
|
}
|
2021-06-07 11:53:27 +05:00
|
|
|
}, [loading, listData]);
|
2021-06-05 01:35:58 +05:00
|
|
|
|
|
|
|
|
const renderItem = React.useCallback(
|
|
|
|
|
({item, index}) =>
|
|
|
|
|
item.type === 'header' ? (
|
|
|
|
|
<SectionHeader
|
|
|
|
|
item={item}
|
|
|
|
|
index={index}
|
|
|
|
|
headerProps={headerProps}
|
|
|
|
|
jumpToDialog={jumpToDialog}
|
|
|
|
|
sortMenuButton={sortMenuButton}
|
|
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
<RenderItem item={item} index={index} />
|
|
|
|
|
),
|
|
|
|
|
[],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
screen,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
[screen],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let styles = {
|
|
|
|
|
height: '100%',
|
|
|
|
|
backgroundColor: colors.bg,
|
|
|
|
|
width: '100%',
|
|
|
|
|
minHeight: 1,
|
|
|
|
|
minWidth: 1,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const _keyExtractor = item => item.id || item.title;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<FlatList
|
|
|
|
|
style={styles}
|
|
|
|
|
keyExtractor={_keyExtractor}
|
|
|
|
|
ref={scrollRef}
|
|
|
|
|
data={dataProvider}
|
|
|
|
|
renderItem={renderItem}
|
|
|
|
|
onScroll={_onScroll}
|
|
|
|
|
initialNumToRender={10}
|
|
|
|
|
maxToRenderPerBatch={10}
|
|
|
|
|
refreshControl={
|
|
|
|
|
<RefreshControl
|
|
|
|
|
style={{
|
|
|
|
|
opacity: 0,
|
|
|
|
|
elevation: 0,
|
|
|
|
|
}}
|
|
|
|
|
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}
|
|
|
|
|
/>
|
|
|
|
|
}
|
|
|
|
|
ListFooterComponent={<Footer />}
|
|
|
|
|
ListHeaderComponent={
|
2021-06-08 12:26:55 +05:00
|
|
|
<Header
|
|
|
|
|
title={headerProps.heading}
|
|
|
|
|
paragraph={headerProps.paragraph}
|
|
|
|
|
onPress={headerProps.onPress}
|
|
|
|
|
icon={headerProps.icon}
|
|
|
|
|
type={type}
|
|
|
|
|
screen={screen}
|
|
|
|
|
/>
|
2021-06-05 01:35:58 +05:00
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<JumpToDialog scrollRef={scrollRef} />
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2020-03-09 20:06:55 +05:00
|
|
|
export default SimpleList;
|