change FlatList to RecyclerListVIew

This commit is contained in:
ammarahm-ed
2020-09-18 20:47:52 +05:00
parent 2373c06483
commit 0b731f5bae

View File

@@ -1,31 +1,32 @@
import React, { createRef, useState } from 'react'; import React, {useEffect, useState} from 'react';
import { import {
FlatList, Dimensions,
LayoutAnimation,
Platform, Platform,
RefreshControl, RefreshControl,
SectionList,
Text, Text,
View View,
} from 'react-native'; } from 'react-native';
import * as Animatable from 'react-native-animatable'; import {useSafeAreaInsets} from 'react-native-safe-area-context';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import Icon from 'react-native-vector-icons/MaterialCommunityIcons'; import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
import { SIZE, WEIGHT } from '../../common/common'; import {DataProvider, LayoutProvider, RecyclerListView} from 'recyclerlistview';
import { useTracked } from '../../provider'; import {COLORS_NOTE, SIZE, WEIGHT} from '../../common/common';
import { ACTIONS } from '../../provider/actions'; import {useTracked} from '../../provider';
import { eSendEvent } from '../../services/eventManager'; import {ACTIONS} from '../../provider/actions';
import {eSendEvent} from '../../services/eventManager';
import { import {
eClearSearch, eClearSearch,
eOpenLoginDialog, eOpenLoginDialog,
eScrollEvent eScrollEvent,
} from '../../services/events'; } from '../../services/events';
import { db, ToastEvent } from '../../utils/utils'; import {db, hexToRGBA, RGB_Linear_Shade, ToastEvent} from '../../utils/utils';
import { PressableButton } from '../PressableButton'; import {PressableButton} from '../PressableButton';
import { PinnedItemList } from './PinnedItemList'; let {width, height} = Dimensions.get('window');
const sectionListRef = createRef();
const header = {
type: 'MAIN_HEADER',
};
const AnimatedFlatlist = Animatable.createAnimatableComponent(FlatList);
const AnimatedSectionList = Animatable.createAnimatableComponent(SectionList);
const SimpleList = ({ const SimpleList = ({
data, data,
type, type,
@@ -42,9 +43,10 @@ const SimpleList = ({
isHome = false, isHome = false,
}) => { }) => {
const [state, dispatch] = useTracked(); const [state, dispatch] = useTracked();
const {colors, selectionMode, user} = state; const {colors, selectionMode, user, currentScreen} = state;
const searchResults = {...state.searchResults}; const searchResults = {...state.searchResults};
const [refreshing, setRefreshing] = useState(false); const [refreshing, setRefreshing] = useState(false);
const [dataProvider, setDataProvider] = useState(null);
const insets = useSafeAreaInsets(); const insets = useSafeAreaInsets();
const _onScroll = (event) => { const _onScroll = (event) => {
if (!event) return; if (!event) return;
@@ -52,6 +54,34 @@ const SimpleList = ({
eSendEvent(eScrollEvent, y); eSendEvent(eScrollEvent, y);
}; };
const NOOP = () => undefined;
const layoutItemAnimator = {
animateDidMount: NOOP,
animateShift: NOOP,
animateWillMount: NOOP,
animateWillUnmount: NOOP,
animateWillUpdate: () =>
LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut),
};
useEffect(() => {
let mainData =
searchResults.type === type && focused && searchResults.results.length > 0
? searchResults.results
: data;
let d = [header, ...mainData];
/* for (var i = 0; i < 10000; i++) {
d = [...d,...data];
} */
setDataProvider(
new DataProvider((r1, r2) => {
return r1 !== r2;
}).cloneWithRows(d),
);
}, [data]);
const _ListFooterComponent = data[0] ? ( const _ListFooterComponent = data[0] ? (
<View <View
style={{ style={{
@@ -70,7 +100,7 @@ const SimpleList = ({
</View> </View>
) : null; ) : null;
const _renderSectionHeader = ({section: {title}}) => ( const RenderSectionHeader = ({item}) => (
<Text <Text
style={{ style={{
fontFamily: WEIGHT.bold, fontFamily: WEIGHT.bold,
@@ -80,9 +110,10 @@ const SimpleList = ({
width: '100%', width: '100%',
alignSelf: 'center', alignSelf: 'center',
marginTop: 15, marginTop: 15,
height: 30,
paddingBottom: 5, paddingBottom: 5,
}}> }}>
{title} {item.title}
</Text> </Text>
); );
@@ -131,22 +162,15 @@ const SimpleList = ({
dispatch({type: ACTIONS.ALL}); dispatch({type: ACTIONS.ALL});
}; };
const _ListHeaderComponent_S = const ListHeaderComponent = () => {
searchResults.type === type && searchResults.results.length > 0 ? ( return searchResults.type === type && searchResults.results.length > 0 ? (
<View <View
style={{ style={{
marginTop:
Platform.OS == 'ios'
? data[0] && !selectionMode
? 115
: 115 - 60
: data[0] && !selectionMode
? 155 - insets.top
: 155 - insets.top - 60,
flexDirection: 'row', flexDirection: 'row',
alignItems: 'center', alignItems: 'center',
justifyContent: 'space-between', justifyContent: 'space-between',
paddingHorizontal: 12, paddingHorizontal: 12,
height: 40,
}}> }}>
<Text <Text
style={{ style={{
@@ -169,39 +193,41 @@ const SimpleList = ({
</Text> </Text>
</View> </View>
) : ( ) : (
<View <View>
style={{
marginTop:
Platform.OS == 'ios'
? data[0] && !selectionMode
? 135
: 135 - 60
: data[0] && !selectionMode
? 155 - insets.top
: 155 - 60 - insets.top,
}}>
{user || !data[0] || selectionMode ? null : ( {user || !data[0] || selectionMode ? null : (
<PressableButton <PressableButton
onPress={() => { onPress={() => {
eSendEvent(eOpenLoginDialog); eSendEvent(eOpenLoginDialog);
}} }}
color={colors.shade} color={
selectedColor={colors.accent} COLORS_NOTE[currentScreen]
? COLORS_NOTE[currentScreen]
: colors.shade
}
selectedColor={
COLORS_NOTE[currentScreen]
? COLORS_NOTE[currentScreen]
: colors.accent
}
alpha={!colors.night ? -0.02 : 0.1} alpha={!colors.night ? -0.02 : 0.1}
opacity={0.12} opacity={0.12}
customStyle={{ customStyle={{
paddingVertical: 6,
width: '100%', width: '100%',
flexDirection: 'row', flexDirection: 'row',
alignItems: 'center', alignItems: 'center',
justifyContent: 'flex-start', justifyContent: 'flex-start',
paddingHorizontal: 12, paddingHorizontal: 12,
alignSelf: 'center', alignSelf: 'center',
height: 40,
borderRadius: 0,
position: 'relative',
}}> }}>
<View <View
style={{ style={{
width: 25, width: 25,
backgroundColor: colors.accent, backgroundColor: COLORS_NOTE[currentScreen]
? COLORS_NOTE[currentScreen]
: colors.accent,
height: 25, height: 25,
borderRadius: 100, borderRadius: 100,
alignItems: 'center', alignItems: 'center',
@@ -231,7 +257,9 @@ const SimpleList = ({
</Text> </Text>
<Text <Text
style={{ style={{
color: colors.accent, color: COLORS_NOTE[currentScreen]
? COLORS_NOTE[currentScreen]
: colors.accent,
fontSize: SIZE.xxs, fontSize: SIZE.xxs,
}}> }}>
Login to sync your {type}. Login to sync your {type}.
@@ -239,96 +267,138 @@ const SimpleList = ({
</View> </View>
</PressableButton> </PressableButton>
)} )}
{pinned ? <PinnedItemList type={type} /> : null}
</View> </View>
); );
};
const _ListEmptyComponent = ( const _ListEmptyComponent = (
<View <View
style={{ style={{
height: '80%', height: '100%',
width: '100%', width: '100%',
alignItems: 'center', alignItems: 'center',
alignSelf: 'center', alignSelf: 'center',
justifyContent: 'center', justifyContent: 'center',
opacity: 1, opacity: 1,
backgroundColor: colors.bg,
}}> }}>
<>{placeholder}</> <>{placeholder}</>
</View> </View>
); );
const _listKeyExtractor = (item, index) => const _listKeyExtractor = (item, index) =>
item.id.toString() + index.toString(); item.type === 'header' ? item.title : item.id.toString() + index.toString();
return isHome && searchResults.type !== 'notes' ? ( const _layoutProvider = new LayoutProvider(
<AnimatedSectionList (index) => {
transition="backgroundColor" console.log(dataProvider.getDataForIndex(index).type, 'TYPE');
duration={300} return dataProvider.getDataForIndex(index).type;
ref={sectionListRef} },
sections={data} (type, dim) => {
refreshControl={ switch (type) {
<RefreshControl case 'note':
tintColor={colors.accent} dim.width = width;
colors={[colors.accent]} dim.height = 100;
progressViewOffset={150} break;
onRefresh={_onRefresh} case 'notebook':
refreshing={refreshing} dim.width = width;
/> dim.height = 110;
break;
case 'topic':
dim.width = width;
dim.height = 80;
break;
case 'tag':
dim.width = width;
dim.height = 80;
break;
case 'header':
dim.width = width;
dim.height = 40;
break;
case 'MAIN_HEADER':
dim.width = width;
dim.height = 40;
break;
default:
dim.width = width;
dim.height = 0;
} }
keyExtractor={_listKeyExtractor} },
renderSectionHeader={_renderSectionHeader} );
onScroll={_onScroll}
stickySectionHeadersEnabled={false} const _renderRow = (type, data, index) => {
ListEmptyComponent={_ListEmptyComponent} switch (type) {
ListHeaderComponent={_ListHeaderComponent_S} case 'note':
contentContainerStyle={{ return <RenderItem item={data} pinned={data.pinned} index={index} />;
width: '100%', case 'MAIN_HEADER':
alignSelf: 'center', return <ListHeaderComponent />;
minHeight: '100%', case 'header':
}} return <RenderSectionHeader item={data} />;
style={{
height: '100%', default:
backgroundColor: colors.bg, return null;
}} }
removeClippedSubviews={true} };
ListFooterComponent={_ListFooterComponent}
renderItem={({item, index}) => <RenderItem item={item} index={index} />} return !data || data.length === 0 ? (
/> _ListEmptyComponent
) : ( ) : (
<AnimatedFlatlist <RecyclerListView
transition="backgroundColor" layoutProvider={_layoutProvider}
duration={300} dataProvider={dataProvider}
data={ rowRenderer={_renderRow}
onScroll={_onScroll}
scrollViewProps={{
refreshControl: (
<RefreshControl
tintColor={colors.accent}
colors={[colors.accent]}
progressViewOffset={150}
onRefresh={customRefresh ? customRefresh : _onRefresh}
refreshing={customRefresh ? customRefreshing : refreshing}
/>
),
contentContainerStyle: {
width: '100%',
alignSelf: 'center',
minHeight: '100%',
},
}}
/* data={
searchResults.type === type && searchResults.type === type &&
focused && focused &&
searchResults.results.length > 0 searchResults.results.length > 0
? searchResults.results ? searchResults.results
: data : data
} }
refreshControl={
<RefreshControl keyExtractor={_listKeyExtractor} */
tintColor={colors.accent} //
colors={[colors.accent]} //onScroll={_onScroll}
progressViewOffset={150} //
onRefresh={customRefresh ? customRefresh : _onRefresh} //
refreshing={customRefresh ? customRefreshing : refreshing}
/>
}
keyExtractor={_listKeyExtractor}
ListFooterComponent={_ListFooterComponent}
onScroll={_onScroll}
ListHeaderComponent={_ListHeaderComponent_S}
ListEmptyComponent={_ListEmptyComponent}
contentContainerStyle={{
width: '100%',
alignSelf: 'center',
minHeight: '100%',
}}
style={{ style={{
height: '100%', height: '100%',
backgroundColor: colors.bg, backgroundColor: colors.bg,
width: '100%',
paddingTop:
Platform.OS == 'ios'
? data[0] && !selectionMode
? 115
: 115 - 60
: data[0] && !selectionMode
? 155 - insets.top
: 155 - insets.top - 60,
}} }}
renderItem={({item, index}) => <RenderItem item={item} index={index} />} /* renderItem={({item, index}) =>
item.type === 'header' ? (
<RenderSectionHeader item={item} />
) : (
<RenderItem item={item} index={index} />
)
} */
/> />
); );
}; };