Refreshing is working

This commit is contained in:
Zhiyuan Zheng 2020-12-20 23:49:35 +01:00
parent 8281d2b0b4
commit b972cd6ca7
No known key found for this signature in database
GPG Key ID: 078A93AB607D85E0
5 changed files with 54 additions and 40 deletions

View File

@ -72,4 +72,4 @@
"typescript": "~4.0.0" "typescript": "~4.0.0"
}, },
"private": true "private": true
} }

View File

@ -59,6 +59,21 @@ const Timelines: React.FC<Props> = ({ name, content }) => {
) )
} }
const screenComponent = useCallback(
() => (
<TabView
lazy
swipeEnabled
renderScene={renderScene}
renderTabBar={() => null}
onIndexChange={index => setSegment(index)}
navigationState={{ index: segment, routes }}
initialLayout={{ width: Dimensions.get('window').width }}
/>
),
[segment]
)
return ( return (
<Stack.Navigator> <Stack.Navigator>
<Stack.Screen <Stack.Screen
@ -85,21 +100,7 @@ const Timelines: React.FC<Props> = ({ name, content }) => {
}) })
}} }}
> >
{() => { {screenComponent}
return (
<TabView
style={styles.base}
navigationState={{ index: segment, routes }}
renderScene={renderScene}
renderTabBar={() => null}
onIndexChange={index => setSegment(index)}
initialLayout={{ width: Dimensions.get('window').width }}
lazy
swipeEnabled
swipeVelocityImpact={1}
/>
)
}}
</Stack.Screen> </Stack.Screen>
{sharedScreens(Stack)} {sharedScreens(Stack)}
@ -110,9 +111,6 @@ const Timelines: React.FC<Props> = ({ name, content }) => {
const styles = StyleSheet.create({ const styles = StyleSheet.create({
segmentsContainer: { segmentsContainer: {
flexBasis: '60%' flexBasis: '60%'
},
base: {
width: Dimensions.get('window').width
} }
}) })

View File

@ -1,5 +1,5 @@
import React, { useCallback, useEffect, useMemo, useRef } from 'react' import React, { useCallback, useEffect, useMemo, useRef } from 'react'
import { StyleSheet } from 'react-native' import { RefreshControl, StyleSheet } from 'react-native'
import { InfiniteData, useInfiniteQuery } from 'react-query' import { InfiniteData, useInfiniteQuery } from 'react-query'
import TimelineNotifications from '@components/Timelines/Timeline/Notifications' import TimelineNotifications from '@components/Timelines/Timeline/Notifications'
@ -57,6 +57,9 @@ const Timeline: React.FC<Props> = ({
fetchNextPage, fetchNextPage,
isFetchingNextPage isFetchingNextPage
} = useInfiniteQuery(queryKey, timelineFetch, { } = useInfiniteQuery(queryKey, timelineFetch, {
select: data => {
return { ...data, pages: data.pages.filter(page => page.toots.length) }
},
getPreviousPageParam: firstPage => ({ getPreviousPageParam: firstPage => ({
direction: 'prev', direction: 'prev',
id: firstPage.toots[0].id id: firstPage.toots[0].id
@ -137,6 +140,15 @@ const Timeline: React.FC<Props> = ({
() => (!disableRefresh ? <TimelineEnd hasNextPage={hasNextPage} /> : null), () => (!disableRefresh ? <TimelineEnd hasNextPage={hasNextPage} /> : null),
[hasNextPage] [hasNextPage]
) )
const flRefreshControl = useMemo(
() => (
<RefreshControl
refreshing={isFetchingPreviousPage}
onRefresh={flOnRefresh}
/>
),
[isFetchingPreviousPage]
)
const onScrollToIndexFailed = useCallback(error => { const onScrollToIndexFailed = useCallback(error => {
const offset = error.averageItemLength * error.index const offset = error.averageItemLength * error.index
flRef.current?.scrollToOffset({ offset }) flRef.current?.scrollToOffset({ offset })
@ -152,14 +164,16 @@ const Timeline: React.FC<Props> = ({
return ( return (
<FlatList <FlatList
ref={flRef} ref={flRef}
windowSize={11}
data={flattenData} data={flattenData}
initialNumToRender={5}
maxToRenderPerBatch={5}
style={styles.flatList} style={styles.flatList}
onRefresh={flOnRefresh}
renderItem={flRenderItem} renderItem={flRenderItem}
onEndReached={flOnEndReach} onEndReached={flOnEndReach}
keyExtractor={flKeyExtrator} keyExtractor={flKeyExtrator}
ListFooterComponent={flFooter} ListFooterComponent={flFooter}
refreshing={isFetchingPreviousPage} refreshControl={flRefreshControl}
ListEmptyComponent={flItemEmptyComponent} ListEmptyComponent={flItemEmptyComponent}
ItemSeparatorComponent={flItemSeparatorComponent} ItemSeparatorComponent={flItemSeparatorComponent}
onEndReachedThreshold={!disableRefresh ? 0.75 : null} onEndReachedThreshold={!disableRefresh ? 0.75 : null}

View File

@ -77,7 +77,7 @@ const styles = StyleSheet.create({
zIndex: 99, zIndex: 99,
borderTopWidth: StyleSheet.hairlineWidth, borderTopWidth: StyleSheet.hairlineWidth,
padding: StyleConstants.Spacing.Global.PagePadding, padding: StyleConstants.Spacing.Global.PagePadding,
paddingBottom: StyleConstants.Spacing.Global.PagePadding * 3 height: 33 + StyleConstants.Spacing.Global.PagePadding * 2
} }
}) })

View File

@ -1,6 +1,6 @@
import React, { Dispatch, useCallback, useState } from 'react' import React, { Dispatch } from 'react'
import { Dimensions, StyleSheet } from 'react-native' import { Dimensions, StyleSheet } from 'react-native'
import { SceneMap, TabView } from 'react-native-tab-view' import { TabView } from 'react-native-tab-view'
import Timeline from '@components/Timelines/Timeline' import Timeline from '@components/Timelines/Timeline'
import { AccountAction, AccountState } from '../Account' import { AccountAction, AccountState } from '../Account'
@ -17,33 +17,35 @@ const AccountToots: React.FC<Props> = ({
accountDispatch, accountDispatch,
id id
}) => { }) => {
const [routes] = useState([ const routes: { key: App.Pages }[] = [
{ key: 'Account_Default' }, { key: 'Account_Default' },
{ key: 'Account_All' }, { key: 'Account_All' },
{ key: 'Account_Media' } { key: 'Account_Media' }
]) ]
const singleScene = useCallback(
({ route }) => <Timeline page={route.key} account={id} disableRefresh />, const renderScene = ({
[] route
) }: {
const renderScene = SceneMap({ route: {
Account_Default: singleScene, key: App.Pages
Account_All: singleScene, }
Account_Media: singleScene }) => {
}) console.log(route)
return <Timeline page={route.key} account={id} disableRefresh />
}
return ( return (
<TabView <TabView
lazy
swipeEnabled
style={styles.base} style={styles.base}
navigationState={{ index: accountState.segmentedIndex, routes }}
renderScene={renderScene} renderScene={renderScene}
renderTabBar={() => null} renderTabBar={() => null}
initialLayout={{ width: Dimensions.get('window').width }}
navigationState={{ index: accountState.segmentedIndex, routes }}
onIndexChange={index => onIndexChange={index =>
accountDispatch({ type: 'segmentedIndex', payload: index }) accountDispatch({ type: 'segmentedIndex', payload: index })
} }
initialLayout={{ width: Dimensions.get('window').width }}
lazy
swipeEnabled
/> />
) )
} }