import React, {useCallback, useEffect, useState} from 'react';
import {
ActivityIndicator,
Platform,
RefreshControl,
useWindowDimensions,
View,
} from 'react-native';
import {TouchableWithoutFeedback} from 'react-native-gesture-handler';
import {useSafeAreaInsets} from 'react-native-safe-area-context';
import {DataProvider, LayoutProvider, RecyclerListView} from 'recyclerlistview';
import {useTracked} from '../../provider';
import {Actions} from '../../provider/Actions';
import {DDS} from '../../services/DeviceDetection';
import {eSendEvent, ToastEvent} from '../../services/EventManager';
import {dHeight} from '../../utils';
import {COLORS_NOTE} from '../../utils/Colors';
import {db} from '../../utils/DB';
import {
eOpenJumpToDialog,
eOpenLoginDialog,
eScrollEvent,
} from '../../utils/Events';
import {SIZE} from '../../utils/SizeUtils';
import {Button} from '../Button';
import {HeaderMenu} from '../Header/HeaderMenu';
import Seperator from '../Seperator';
import TagItem from '../TagItem';
import Heading from '../Typography/Heading';
import Paragraph from '../Typography/Paragraph';
import {ListHeaderComponent} from './ListHeaderComponent';
import {NotebookItemWrapper} from './NotebookItemWrapper';
import {NoteItemWrapper} from './NoteItemWrapper';
const header = {
type: 'MAIN_HEADER',
};
const SimpleList = ({
data,
type,
customRefresh,
customRefreshing,
refreshCallback,
sortMenuButton,
scrollRef,
jumpToDialog,
placeholderData,
loading,
headerProps = {
heading: 'Home',
},
}) => {
const [state, dispatch] = useTracked();
const {colors, searchResults, headerTextState} = state;
const [refreshing, setRefreshing] = useState(false);
const [dataProvider, setDataProvider] = useState(
new DataProvider((r1, r2) => {
return r1 !== r2;
}).cloneWithRows([header, {type: 'empty'}]),
);
const insets = useSafeAreaInsets();
const {width, fontScale} = useWindowDimensions();
const listData = data;
const dataType = type;
const _onScroll = (event) => {
if (!event) return;
let y = event.nativeEvent.contentOffset.y;
eSendEvent(eScrollEvent, y);
};
useEffect(() => {
loadData();
}, [listData, searchResults.results]);
const loadData = () => {
let mainData = [header, {type: 'empty'}];
mainData =
!listData || listData.length === 0 ? mainData : [header, ...listData];
setDataProvider(dataProvider.cloneWithRows(mainData));
};
const RenderSectionHeader = ({item, index}) => (
{
if (jumpToDialog) {
eSendEvent(eOpenJumpToDialog);
}
}}
hitSlop={{top: 10, left: 10, right: 30, bottom: 15}}
style={{
height: '100%',
justifyContent: 'center',
}}>
{!item.title || item.title === "" ? "Pinned" : item.title}
{index === 1 && sortMenuButton ? : null}
);
const _onRefresh = useCallback(async () => {
if (Platform.OS === 'ios') {
dispatch({
type: Actions.SYNCING,
syncing: true,
});
} else {
setRefreshing(true);
}
try {
await db.sync();
ToastEvent.show('Sync Complete', 'success');
} catch (e) {
if (e.message === 'You need to login to sync.') {
ToastEvent.show(
e.message,
'error',
'global',
5000,
() => {
eSendEvent(eOpenLoginDialog);
},
'Login',
);
} else {
ToastEvent.show(e.message, 'error', 'global', 5000);
}
} finally {
if (Platform.OS === 'ios') {
dispatch({
type: Actions.SYNCING,
syncing: false,
});
} else {
setRefreshing(false);
}
if (refreshCallback) {
refreshCallback();
}
dispatch({type: Actions.LAST_SYNC, lastSync: await db.lastSynced()});
dispatch({type: Actions.ALL});
}
}, []);
const _ListEmptyComponent = (
{placeholderData.heading}
{loading ? placeholderData.loading : placeholderData.paragraph}
{placeholderData.button && !loading ? (
) : loading ? (
) : null}
);
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() ? 50 : 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 _ListEmptyComponent;
}
};
return (
}
scrollViewProps={{
refreshControl: (
),
overScrollMode: 'always',
contentContainerStyle: {
width: '100%',
alignSelf: 'center',
minHeight: '100%',
},
testID: 'list-' + type,
}}
style={{
height: '100%',
backgroundColor: colors.bg,
width: '100%',
}}
/>
);
};
export default SimpleList;