2021-02-15 11:09:17 +05:00
|
|
|
import React, {useEffect, useState} from 'react';
|
|
|
|
|
import {RefreshControl, useWindowDimensions} from 'react-native';
|
|
|
|
|
import {useSafeAreaInsets} from 'react-native-safe-area-context';
|
|
|
|
|
import {DataProvider, LayoutProvider, RecyclerListView} from 'recyclerlistview';
|
|
|
|
|
import {useTracked} from '../../provider';
|
|
|
|
|
import {DDS} from '../../services/DeviceDetection';
|
|
|
|
|
import {eSendEvent} from '../../services/EventManager';
|
2020-12-20 23:01:35 +05:00
|
|
|
import Sync from '../../services/Sync';
|
2021-02-15 11:09:17 +05:00
|
|
|
import {dHeight} from '../../utils';
|
|
|
|
|
import {eScrollEvent} from '../../utils/Events';
|
|
|
|
|
import {NotebookWrapper} from '../NotebookItem/wrapper';
|
|
|
|
|
import {NoteWrapper} from '../NoteItem/wrapper';
|
2020-11-14 10:06:32 +05:00
|
|
|
import TagItem from '../TagItem';
|
2021-02-15 11:09:17 +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
|
|
|
|
|
|
|
|
const header = {
|
2020-11-04 15:06:06 +05:00
|
|
|
type: 'MAIN_HEADER',
|
2020-09-18 20:47:52 +05:00
|
|
|
};
|
2021-02-15 11:09:17 +05:00
|
|
|
const emptyData = new DataProvider((r1, r2) => {
|
|
|
|
|
return r1 !== r2;
|
|
|
|
|
}).cloneWithRows([header, {type: 'empty'}]);
|
2020-05-10 22:19:23 +05:00
|
|
|
|
2020-03-09 20:06:55 +05:00
|
|
|
const SimpleList = ({
|
2021-02-15 11:09:17 +05:00
|
|
|
listData,
|
2020-11-04 15:06:06 +05:00
|
|
|
type,
|
|
|
|
|
customRefresh,
|
|
|
|
|
customRefreshing,
|
|
|
|
|
refreshCallback,
|
|
|
|
|
sortMenuButton,
|
|
|
|
|
scrollRef,
|
2020-11-04 20:29:45 +05:00
|
|
|
jumpToDialog,
|
2020-11-10 17:18:19 +05:00
|
|
|
placeholderData,
|
2020-11-24 16:59:12 +05:00
|
|
|
loading,
|
2020-12-08 12:49:02 +05:00
|
|
|
headerProps = {
|
|
|
|
|
heading: 'Home',
|
|
|
|
|
},
|
2020-11-04 15:06:06 +05:00
|
|
|
}) => {
|
2021-01-11 19:40:57 +05:00
|
|
|
const [state] = useTracked();
|
2021-02-09 10:15:21 +05:00
|
|
|
const {colors, deviceMode} = state;
|
2021-02-15 11:09:17 +05:00
|
|
|
const [_loading, setLoading] = useState(true);
|
2020-11-04 15:06:06 +05:00
|
|
|
const [dataProvider, setDataProvider] = useState(
|
|
|
|
|
new DataProvider((r1, r2) => {
|
|
|
|
|
return r1 !== r2;
|
2021-02-15 11:09:17 +05:00
|
|
|
}).cloneWithRows(
|
|
|
|
|
!listData || listData.length === 0
|
|
|
|
|
? [header, {type: 'empty'}]
|
|
|
|
|
: [header].concat(listData),
|
|
|
|
|
),
|
2020-11-04 15:06:06 +05:00
|
|
|
);
|
2020-12-07 14:45:44 +05:00
|
|
|
|
2021-01-12 21:43:34 +05:00
|
|
|
const insets = useSafeAreaInsets();
|
2021-01-11 19:40:57 +05:00
|
|
|
const {width, fontScale} = useWindowDimensions();
|
2021-01-12 21:43:34 +05:00
|
|
|
const refreshing = false;
|
2020-11-04 15:06:06 +05:00
|
|
|
const dataType = type;
|
2021-01-12 21:43:34 +05:00
|
|
|
|
2020-11-04 15:06:06 +05:00
|
|
|
useEffect(() => {
|
2021-02-15 11:09:17 +05:00
|
|
|
setDataProvider(
|
|
|
|
|
dataProvider.cloneWithRows(
|
|
|
|
|
!listData || listData.length === 0
|
|
|
|
|
? [header, {type: 'empty'}]
|
|
|
|
|
: [header].concat(listData),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
setLoading(false);
|
|
|
|
|
}, [listData, deviceMode]);
|
2020-10-14 11:47:29 +05:00
|
|
|
|
2021-02-15 11:09:17 +05:00
|
|
|
/*
|
2020-11-04 15:06:06 +05:00
|
|
|
const loadData = () => {
|
2020-12-08 12:09:34 +05:00
|
|
|
let mainData = [header, {type: 'empty'}];
|
|
|
|
|
mainData =
|
2021-02-15 11:09:17 +05:00
|
|
|
!listData || listData.length === 0 ? mainData : [header].concat(listData);
|
2020-12-08 12:09:34 +05:00
|
|
|
setDataProvider(dataProvider.cloneWithRows(mainData));
|
2021-02-15 11:09:17 +05:00
|
|
|
}; */
|
2020-10-18 13:15:24 +05:00
|
|
|
|
2020-12-20 23:01:35 +05:00
|
|
|
const _onRefresh = async () => {
|
|
|
|
|
await Sync.run();
|
|
|
|
|
if (refreshCallback) {
|
|
|
|
|
refreshCallback();
|
2020-11-04 15:06:06 +05:00
|
|
|
}
|
2020-12-20 23:01:35 +05:00
|
|
|
};
|
2020-11-04 15:06:06 +05:00
|
|
|
|
2021-01-12 21:43:34 +05:00
|
|
|
const _onScroll = (event) => {
|
|
|
|
|
if (!event) return;
|
|
|
|
|
let y = event.nativeEvent.contentOffset.y;
|
|
|
|
|
eSendEvent(eScrollEvent, y);
|
|
|
|
|
};
|
|
|
|
|
|
2021-02-15 11:09:17 +05:00
|
|
|
const emptyLayoutProvider = new LayoutProvider(
|
|
|
|
|
(index) => {
|
|
|
|
|
return emptyData.getDataForIndex(index).type;
|
|
|
|
|
},
|
|
|
|
|
(type, dim) => {
|
|
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
|
case 'empty':
|
|
|
|
|
dim.width = width;
|
|
|
|
|
dim.height = dHeight - 250 - insets.top;
|
|
|
|
|
break;
|
|
|
|
|
case 'MAIN_HEADER':
|
|
|
|
|
dim.width = width;
|
|
|
|
|
dim.height =
|
|
|
|
|
dataType === 'search' ? 0 : DDS.isLargeTablet() ? 50 : 195;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
dim.width = width;
|
|
|
|
|
dim.height = 0;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2020-11-04 15:06:06 +05:00
|
|
|
const _layoutProvider = new LayoutProvider(
|
|
|
|
|
(index) => {
|
|
|
|
|
return dataProvider.getDataForIndex(index).type;
|
|
|
|
|
},
|
|
|
|
|
(type, dim) => {
|
|
|
|
|
switch (type) {
|
|
|
|
|
case 'note':
|
|
|
|
|
dim.width = width;
|
|
|
|
|
dim.height = 100 * fontScale;
|
|
|
|
|
break;
|
|
|
|
|
case 'notebook':
|
|
|
|
|
dim.width = width;
|
|
|
|
|
dim.height = 110 * fontScale;
|
|
|
|
|
break;
|
|
|
|
|
case 'trash':
|
|
|
|
|
dim.width = width;
|
|
|
|
|
dim.height = 110 * fontScale;
|
|
|
|
|
break;
|
2020-11-24 16:59:12 +05:00
|
|
|
case 'empty':
|
|
|
|
|
dim.width = width;
|
2020-12-07 14:45:44 +05:00
|
|
|
dim.height = dHeight - 250 - insets.top;
|
2020-11-24 16:59:12 +05:00
|
|
|
break;
|
2020-11-04 15:06:06 +05:00
|
|
|
case 'topic':
|
|
|
|
|
dim.width = width;
|
|
|
|
|
dim.height = 80 * fontScale;
|
|
|
|
|
break;
|
|
|
|
|
case 'tag':
|
|
|
|
|
dim.width = width;
|
|
|
|
|
dim.height = 80 * fontScale;
|
|
|
|
|
break;
|
|
|
|
|
case 'header':
|
|
|
|
|
dim.width = width;
|
2020-12-16 20:02:24 +05:00
|
|
|
dim.height = 40 * fontScale;
|
2020-11-04 15:06:06 +05:00
|
|
|
break;
|
|
|
|
|
case 'MAIN_HEADER':
|
|
|
|
|
dim.width = width;
|
2020-11-20 01:23:05 +05:00
|
|
|
dim.height =
|
2020-12-16 20:02:24 +05:00
|
|
|
dataType === 'search' ? 0 : DDS.isLargeTablet() ? 50 : 195;
|
2020-11-04 15:06:06 +05:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
dim.width = width;
|
|
|
|
|
dim.height = 0;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
);
|
2020-11-01 09:22:28 +05:00
|
|
|
|
2020-11-04 15:06:06 +05:00
|
|
|
const _renderRow = (type, data, index) => {
|
|
|
|
|
switch (type) {
|
|
|
|
|
case 'note':
|
2021-02-09 11:33:57 +05:00
|
|
|
return <NoteWrapper item={data} pinned={data.pinned} index={index} />;
|
2020-11-04 15:06:06 +05:00
|
|
|
case 'notebook':
|
2020-11-14 10:06:32 +05:00
|
|
|
return (
|
2021-02-09 11:33:57 +05:00
|
|
|
<NotebookWrapper item={data} pinned={data.pinned} index={index} />
|
2020-11-14 10:06:32 +05:00
|
|
|
);
|
|
|
|
|
case 'tag':
|
|
|
|
|
return <TagItem item={data} index={index} />;
|
|
|
|
|
case 'topic':
|
|
|
|
|
return (
|
2021-02-09 11:33:57 +05:00
|
|
|
<NotebookWrapper
|
2020-11-14 10:06:32 +05:00
|
|
|
item={data}
|
|
|
|
|
isTopic={true}
|
|
|
|
|
pinned={data.pinned}
|
|
|
|
|
index={index}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
case 'trash':
|
|
|
|
|
return data.itemType === 'note' ? (
|
2021-02-09 11:33:57 +05:00
|
|
|
<NoteWrapper item={data} index={index} isTrash={true} />
|
2020-11-14 10:06:32 +05:00
|
|
|
) : (
|
2021-02-09 11:33:57 +05:00
|
|
|
<NotebookWrapper item={data} index={index} isTrash={true} />
|
2020-11-14 10:06:32 +05:00
|
|
|
);
|
2020-11-04 15:06:06 +05:00
|
|
|
case 'MAIN_HEADER':
|
|
|
|
|
return (
|
2021-02-09 11:33:57 +05:00
|
|
|
<Header
|
2020-12-08 12:49:02 +05:00
|
|
|
title={headerProps.heading}
|
|
|
|
|
type={dataType}
|
|
|
|
|
index={index}
|
|
|
|
|
data={listData}
|
|
|
|
|
/>
|
2020-11-04 15:06:06 +05:00
|
|
|
);
|
|
|
|
|
case 'header':
|
2021-01-12 21:39:49 +05:00
|
|
|
return (
|
2021-02-09 11:33:57 +05:00
|
|
|
<SectionHeader
|
2021-01-12 21:39:49 +05:00
|
|
|
item={data}
|
|
|
|
|
index={index}
|
|
|
|
|
headerProps={headerProps}
|
|
|
|
|
jumpToDialog={jumpToDialog}
|
2021-01-12 21:43:34 +05:00
|
|
|
sortMenuButton={sortMenuButton}
|
2021-01-12 21:39:49 +05:00
|
|
|
/>
|
|
|
|
|
);
|
2020-11-24 16:59:12 +05:00
|
|
|
case 'empty':
|
2021-02-15 11:09:17 +05:00
|
|
|
return (
|
|
|
|
|
<Empty
|
|
|
|
|
loading={loading || _loading}
|
|
|
|
|
placeholderData={placeholderData}
|
|
|
|
|
/>
|
|
|
|
|
);
|
2020-11-04 15:06:06 +05:00
|
|
|
}
|
|
|
|
|
};
|
2020-09-27 10:15:19 +05:00
|
|
|
|
2021-01-12 21:39:49 +05:00
|
|
|
let scrollProps = React.useMemo(() => {
|
|
|
|
|
return {
|
|
|
|
|
refreshControl: (
|
|
|
|
|
<RefreshControl
|
|
|
|
|
style={{
|
|
|
|
|
opacity: 0,
|
|
|
|
|
elevation: 0,
|
|
|
|
|
}}
|
|
|
|
|
tintColor={colors.accent}
|
|
|
|
|
colors={[colors.accent]}
|
|
|
|
|
progressViewOffset={150}
|
|
|
|
|
onRefresh={customRefresh ? customRefresh : _onRefresh}
|
|
|
|
|
refreshing={customRefresh ? customRefreshing : refreshing}
|
|
|
|
|
/>
|
|
|
|
|
),
|
2021-02-15 11:09:17 +05:00
|
|
|
|
2021-01-12 21:39:49 +05:00
|
|
|
overScrollMode: 'always',
|
|
|
|
|
contentContainerStyle: {
|
|
|
|
|
width: '100%',
|
|
|
|
|
alignSelf: 'center',
|
|
|
|
|
minHeight: '100%',
|
|
|
|
|
},
|
|
|
|
|
testID: 'list-' + type,
|
|
|
|
|
};
|
|
|
|
|
}, [colors.accent, type, customRefresh]);
|
|
|
|
|
|
|
|
|
|
let styles = {
|
|
|
|
|
height: '100%',
|
|
|
|
|
backgroundColor: colors.bg,
|
|
|
|
|
width: '100%',
|
|
|
|
|
};
|
2020-11-24 16:59:12 +05:00
|
|
|
return (
|
2020-11-04 15:06:06 +05:00
|
|
|
<RecyclerListView
|
|
|
|
|
ref={scrollRef}
|
2021-02-15 11:09:17 +05:00
|
|
|
layoutProvider={
|
|
|
|
|
loading || _loading ? emptyLayoutProvider : _layoutProvider
|
|
|
|
|
}
|
|
|
|
|
dataProvider={loading || _loading ? emptyData : dataProvider}
|
2020-11-04 15:06:06 +05:00
|
|
|
rowRenderer={_renderRow}
|
|
|
|
|
onScroll={_onScroll}
|
2021-02-09 10:15:21 +05:00
|
|
|
canChangeSize={true}
|
2021-02-15 11:09:17 +05:00
|
|
|
renderFooter={Footer}
|
2021-01-12 21:39:49 +05:00
|
|
|
scrollViewProps={scrollProps}
|
|
|
|
|
style={styles}
|
2020-11-04 15:06:06 +05:00
|
|
|
/>
|
|
|
|
|
);
|
2020-03-09 20:06:55 +05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default SimpleList;
|