import React, {useEffect, useRef, 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';
import Sync from '../../services/Sync';
import {dHeight, doInBackground, dWidth} from '../../utils';
import {COLORS_NOTE} from '../../utils/Colors';
import {eScrollEvent} from '../../utils/Events';
import useAnnouncement from '../../utils/useAnnouncement';
import JumpToDialog from '../JumpToDialog';
import {NotebookWrapper} from '../NotebookItem/wrapper';
import {NoteWrapper} from '../NoteItem/wrapper';
import TagItem from '../TagItem';
import {Announcement} from './announcement';
import {Empty} from './empty';
import {Footer} from './footer';
import {Header} from './header';
import {SectionHeader} from './section-header';
const header = {
type: 'MAIN_HEADER',
};
const empty = {
type: 'empty',
};
const SimpleList = ({
listData,
type,
customRefresh,
customRefreshing,
refreshCallback,
sortMenuButton,
jumpToDialog,
placeholderData,
loading,
headerProps = {
heading: 'Home',
},
screen,
}) => {
const [state] = useTracked();
const {colors, deviceMode, messageBoardState} = state;
const [_loading, _setLoading] = useState(true);
const [dataProvider, setDataProvider] = useState(
new DataProvider((r1, r2) => {
return r1 !== r2;
}).cloneWithRows([header, empty]),
);
const [width, setWidth] = useState(dWidth);
const scrollRef = useRef();
const insets = useSafeAreaInsets();
const {fontScale} = useWindowDimensions();
const refreshing = false;
const dataType = type;
const [announcement, remove] = useAnnouncement();
useEffect(() => {
setWidth(dWidth);
if (!loading) {
setDataProvider(
dataProvider.cloneWithRows(
!listData || listData.length === 0
? [header, empty]
: [header].concat(listData),
),
);
setTimeout(() => {
_setLoading(false);
}, 500);
} else {
_setLoading(true);
setDataProvider(dataProvider.cloneWithRows([header, empty]));
}
}, [listData, deviceMode, loading, announcement]);
const _onRefresh = async () => {
await Sync.run();
if (refreshCallback) {
refreshCallback();
}
};
const _onScroll = event => {
if (!event) return;
let y = event.nativeEvent.contentOffset.y;
eSendEvent(eScrollEvent, {
y,
screen,
});
};
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;
case 'empty':
dim.width = width;
dim.height = dHeight - 250 - insets.top;
break;
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;
dim.height = 40 * fontScale;
break;
case 'MAIN_HEADER':
dim.width = width;
dim.height =
dataType === 'search'
? 0
: DDS.isLargeTablet() || announcement
? messageBoardState.visible && !announcement
? 50
: 0
: 195;
break;
default:
dim.width = width;
dim.height = 0;
}
},
);
const _renderRow = (type, data, index) => {
switch (type) {
case 'note':
return ;
case 'notebook':
return (
);
case 'tag':
return ;
case 'topic':
return (
);
case 'trash':
return data.itemType === 'note' ? (
) : (
);
case 'MAIN_HEADER':
return (
);
case 'header':
return (
);
case 'empty':
return (
);
}
};
let scrollProps = React.useMemo(() => {
return {
refreshControl: (
),
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%',
minHeight: 1,
minWidth: 1,
};
return (
<>
{!loading && (
)}
>
);
};
export default SimpleList;