mirror of
https://github.com/tooot-app/app
synced 2025-06-05 22:19:13 +02:00
@ -7,18 +7,19 @@ import {
|
||||
QueryKeyTimeline,
|
||||
useTimelineQuery
|
||||
} from '@utils/queryHooks/timeline'
|
||||
import { setAccountStorage } from '@utils/storage/actions'
|
||||
import { StyleConstants } from '@utils/styles/constants'
|
||||
import { useTheme } from '@utils/styles/ThemeManager'
|
||||
import React, { RefObject, useEffect, useRef, useState } from 'react'
|
||||
import React, { RefObject, useRef, useState } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { FlatList, Platform, Text, View } from 'react-native'
|
||||
import { Circle } from 'react-native-animated-spinkit'
|
||||
import Animated, {
|
||||
Extrapolate,
|
||||
interpolate,
|
||||
runOnJS,
|
||||
useAnimatedReaction,
|
||||
useAnimatedStyle,
|
||||
useDerivedValue,
|
||||
useSharedValue,
|
||||
withTiming
|
||||
} from 'react-native-reanimated'
|
||||
@ -26,9 +27,11 @@ import Animated, {
|
||||
export interface Props {
|
||||
flRef: RefObject<FlatList<any>>
|
||||
queryKey: QueryKeyTimeline
|
||||
fetchingActive: React.MutableRefObject<boolean>
|
||||
scrollY: Animated.SharedValue<number>
|
||||
fetchingType: Animated.SharedValue<0 | 1 | 2>
|
||||
disableRefresh?: boolean
|
||||
readMarker?: 'read_marker_following'
|
||||
}
|
||||
|
||||
const CONTAINER_HEIGHT = StyleConstants.Spacing.M * 2.5
|
||||
@ -38,9 +41,11 @@ export const SEPARATION_Y_2 = -(CONTAINER_HEIGHT * 1.5 + StyleConstants.Font.Siz
|
||||
const TimelineRefresh: React.FC<Props> = ({
|
||||
flRef,
|
||||
queryKey,
|
||||
fetchingActive,
|
||||
scrollY,
|
||||
fetchingType,
|
||||
disableRefresh = false
|
||||
disableRefresh = false,
|
||||
readMarker
|
||||
}) => {
|
||||
if (Platform.OS !== 'ios') {
|
||||
return null
|
||||
@ -55,7 +60,15 @@ const TimelineRefresh: React.FC<Props> = ({
|
||||
const prevStatusId = useRef<Mastodon.Status['id']>()
|
||||
|
||||
const queryClient = useQueryClient()
|
||||
const { refetch, isFetching } = useTimelineQuery({ ...queryKey[1] })
|
||||
const { refetch, isRefetching } = useTimelineQuery({ ...queryKey[1] })
|
||||
|
||||
useDerivedValue(() => {
|
||||
if (prevActive.current || isRefetching) {
|
||||
fetchingActive.current = true
|
||||
} else {
|
||||
fetchingActive.current = false
|
||||
}
|
||||
}, [prevActive.current, isRefetching])
|
||||
|
||||
const { t } = useTranslation('componentTimeline')
|
||||
const { colors } = useTheme()
|
||||
@ -83,7 +96,7 @@ const TimelineRefresh: React.FC<Props> = ({
|
||||
const arrowStage = useSharedValue(0)
|
||||
useAnimatedReaction(
|
||||
() => {
|
||||
if (isFetching) {
|
||||
if (fetchingActive.current) {
|
||||
return false
|
||||
}
|
||||
switch (arrowStage.value) {
|
||||
@ -116,7 +129,7 @@ const TimelineRefresh: React.FC<Props> = ({
|
||||
runOnJS(haptics)('Light')
|
||||
}
|
||||
},
|
||||
[isFetching]
|
||||
[fetchingActive.current]
|
||||
)
|
||||
|
||||
const fetchAndScrolled = useSharedValue(false)
|
||||
@ -201,11 +214,13 @@ const TimelineRefresh: React.FC<Props> = ({
|
||||
}
|
||||
prevActive.current = false
|
||||
})
|
||||
.catch(err => console.warn(err))
|
||||
}
|
||||
|
||||
const runFetchLatest = async () => {
|
||||
queryClient.invalidateQueries(queryKey)
|
||||
if (readMarker) {
|
||||
setAccountStorage([{ key: readMarker, value: undefined }])
|
||||
}
|
||||
await refetch()
|
||||
setTimeout(() => flRef.current?.scrollToOffset({ offset: 0 }), 50)
|
||||
}
|
||||
@ -239,61 +254,53 @@ const TimelineRefresh: React.FC<Props> = ({
|
||||
alignItems: 'center'
|
||||
}}
|
||||
>
|
||||
{prevActive.current || isFetching ? (
|
||||
<View style={{ height: CONTAINER_HEIGHT, justifyContent: 'center' }}>
|
||||
<Circle size={StyleConstants.Font.Size.L} color={colors.secondary} />
|
||||
</View>
|
||||
) : (
|
||||
<>
|
||||
<View style={{ flex: 1, flexDirection: 'row', height: CONTAINER_HEIGHT }}>
|
||||
<Text
|
||||
style={{
|
||||
fontSize: StyleConstants.Font.Size.S,
|
||||
lineHeight: CONTAINER_HEIGHT,
|
||||
color: colors.primaryDefault
|
||||
}}
|
||||
onLayout={({ nativeEvent }) => {
|
||||
if (nativeEvent.layout.x + nativeEvent.layout.width > textRight) {
|
||||
setTextRight(nativeEvent.layout.x + nativeEvent.layout.width)
|
||||
}
|
||||
}}
|
||||
children={t('refresh.fetchPreviousPage')}
|
||||
<View style={{ flex: 1, flexDirection: 'row', height: CONTAINER_HEIGHT }}>
|
||||
<Text
|
||||
style={{
|
||||
fontSize: StyleConstants.Font.Size.S,
|
||||
lineHeight: CONTAINER_HEIGHT,
|
||||
color: colors.primaryDefault
|
||||
}}
|
||||
onLayout={({ nativeEvent }) => {
|
||||
if (nativeEvent.layout.x + nativeEvent.layout.width > textRight) {
|
||||
setTextRight(nativeEvent.layout.x + nativeEvent.layout.width)
|
||||
}
|
||||
}}
|
||||
children={t('refresh.fetchPreviousPage')}
|
||||
/>
|
||||
<Animated.View
|
||||
style={[
|
||||
{
|
||||
position: 'absolute',
|
||||
left: textRight + StyleConstants.Spacing.S
|
||||
},
|
||||
arrowY,
|
||||
arrowTop
|
||||
]}
|
||||
children={
|
||||
<Icon
|
||||
name='ArrowLeft'
|
||||
size={StyleConstants.Font.Size.M}
|
||||
color={colors.primaryDefault}
|
||||
/>
|
||||
<Animated.View
|
||||
style={[
|
||||
{
|
||||
position: 'absolute',
|
||||
left: textRight + StyleConstants.Spacing.S
|
||||
},
|
||||
arrowY,
|
||||
arrowTop
|
||||
]}
|
||||
children={
|
||||
<Icon
|
||||
name='ArrowLeft'
|
||||
size={StyleConstants.Font.Size.M}
|
||||
color={colors.primaryDefault}
|
||||
/>
|
||||
}
|
||||
/>
|
||||
</View>
|
||||
<View style={{ height: CONTAINER_HEIGHT, justifyContent: 'center' }}>
|
||||
<Text
|
||||
style={{
|
||||
fontSize: StyleConstants.Font.Size.S,
|
||||
lineHeight: CONTAINER_HEIGHT,
|
||||
color: colors.primaryDefault
|
||||
}}
|
||||
onLayout={({ nativeEvent }) => {
|
||||
if (nativeEvent.layout.x + nativeEvent.layout.width > textRight) {
|
||||
setTextRight(nativeEvent.layout.x + nativeEvent.layout.width)
|
||||
}
|
||||
}}
|
||||
children={t('refresh.refetch')}
|
||||
/>
|
||||
</View>
|
||||
</>
|
||||
)}
|
||||
}
|
||||
/>
|
||||
</View>
|
||||
<View style={{ height: CONTAINER_HEIGHT, justifyContent: 'center' }}>
|
||||
<Text
|
||||
style={{
|
||||
fontSize: StyleConstants.Font.Size.S,
|
||||
lineHeight: CONTAINER_HEIGHT,
|
||||
color: colors.primaryDefault
|
||||
}}
|
||||
onLayout={({ nativeEvent }) => {
|
||||
if (nativeEvent.layout.x + nativeEvent.layout.width > textRight) {
|
||||
setTextRight(nativeEvent.layout.x + nativeEvent.layout.width)
|
||||
}
|
||||
}}
|
||||
children={t('refresh.refetch')}
|
||||
/>
|
||||
</View>
|
||||
</Animated.View>
|
||||
)
|
||||
}
|
||||
|
@ -1,9 +1,14 @@
|
||||
import ComponentSeparator from '@components/Separator'
|
||||
import TimelineDefault from '@components/Timeline/Default'
|
||||
import { useScrollToTop } from '@react-navigation/native'
|
||||
import { UseInfiniteQueryOptions } from '@tanstack/react-query'
|
||||
import { QueryKeyTimeline, useTimelineQuery } from '@utils/queryHooks/timeline'
|
||||
import { flattenPages } from '@utils/queryHooks/utils'
|
||||
import { useGlobalStorageListener } from '@utils/storage/actions'
|
||||
import {
|
||||
getAccountStorage,
|
||||
setAccountStorage,
|
||||
useGlobalStorageListener
|
||||
} from '@utils/storage/actions'
|
||||
import { StyleConstants } from '@utils/styles/constants'
|
||||
import { useTheme } from '@utils/styles/ThemeManager'
|
||||
import React, { RefObject, useRef } from 'react'
|
||||
@ -13,7 +18,7 @@ import TimelineEmpty from './Empty'
|
||||
import TimelineFooter from './Footer'
|
||||
import TimelineRefresh, { SEPARATION_Y_1, SEPARATION_Y_2 } from './Refresh'
|
||||
|
||||
const AnimatedFlatList = Animated.createAnimatedComponent(FlatList)
|
||||
const AnimatedFlatList = Animated.createAnimatedComponent(FlatList<any>)
|
||||
|
||||
export interface Props {
|
||||
flRef?: RefObject<FlatList<any>>
|
||||
@ -24,7 +29,8 @@ export interface Props {
|
||||
>
|
||||
disableRefresh?: boolean
|
||||
disableInfinity?: boolean
|
||||
customProps: Partial<FlatListProps<any>> & Pick<FlatListProps<any>, 'renderItem'>
|
||||
readMarker?: 'read_marker_following'
|
||||
customProps?: Partial<FlatListProps<any>>
|
||||
}
|
||||
|
||||
const Timeline: React.FC<Props> = ({
|
||||
@ -33,6 +39,7 @@ const Timeline: React.FC<Props> = ({
|
||||
queryOptions,
|
||||
disableRefresh = false,
|
||||
disableInfinity = false,
|
||||
readMarker = undefined,
|
||||
customProps
|
||||
}) => {
|
||||
const { colors } = useTheme()
|
||||
@ -56,6 +63,7 @@ const Timeline: React.FC<Props> = ({
|
||||
})
|
||||
|
||||
const flRef = useRef<FlatList>(null)
|
||||
const fetchingActive = useRef<boolean>(false)
|
||||
|
||||
const scrollY = useSharedValue(0)
|
||||
const fetchingType = useSharedValue<0 | 1 | 2>(0)
|
||||
@ -78,6 +86,32 @@ const Timeline: React.FC<Props> = ({
|
||||
[isFetching]
|
||||
)
|
||||
|
||||
const viewabilityConfigCallbackPairs = useRef<
|
||||
Pick<FlatListProps<any>, 'viewabilityConfigCallbackPairs'>['viewabilityConfigCallbackPairs']
|
||||
>(
|
||||
readMarker
|
||||
? [
|
||||
{
|
||||
viewabilityConfig: {
|
||||
minimumViewTime: 300,
|
||||
itemVisiblePercentThreshold: 80,
|
||||
waitForInteraction: true
|
||||
},
|
||||
onViewableItemsChanged: ({ viewableItems }) => {
|
||||
const marker = readMarker ? getAccountStorage.string(readMarker) : undefined
|
||||
|
||||
const firstItemId = viewableItems.filter(item => item.isViewable)[0]?.item.id
|
||||
if (!fetchingActive.current && firstItemId && firstItemId > (marker || '0')) {
|
||||
setAccountStorage([{ key: readMarker, value: firstItemId }])
|
||||
} else {
|
||||
// setAccountStorage([{ key: readMarker, value: '109519141378761752' }])
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
: undefined
|
||||
)
|
||||
|
||||
const androidRefreshControl = Platform.select({
|
||||
android: {
|
||||
refreshControl: (
|
||||
@ -102,9 +136,11 @@ const Timeline: React.FC<Props> = ({
|
||||
<TimelineRefresh
|
||||
flRef={flRef}
|
||||
queryKey={queryKey}
|
||||
fetchingActive={fetchingActive}
|
||||
scrollY={scrollY}
|
||||
fetchingType={fetchingType}
|
||||
disableRefresh={disableRefresh}
|
||||
readMarker={readMarker}
|
||||
/>
|
||||
<AnimatedFlatList
|
||||
ref={customFLRef || flRef}
|
||||
@ -112,6 +148,9 @@ const Timeline: React.FC<Props> = ({
|
||||
onScroll={onScroll}
|
||||
windowSize={7}
|
||||
data={flattenPages(data)}
|
||||
{...(customProps?.renderItem
|
||||
? { renderItem: customProps.renderItem }
|
||||
: { renderItem: ({ item }) => <TimelineDefault item={item} queryKey={queryKey} /> })}
|
||||
initialNumToRender={6}
|
||||
maxToRenderPerBatch={3}
|
||||
onEndReached={() => !disableInfinity && !isFetchingNextPage && fetchNextPage()}
|
||||
@ -129,6 +168,7 @@ const Timeline: React.FC<Props> = ({
|
||||
/>
|
||||
)
|
||||
}
|
||||
viewabilityConfigCallbackPairs={viewabilityConfigCallbackPairs.current}
|
||||
{...(!isLoading && {
|
||||
maintainVisibleContentPosition: {
|
||||
minIndexForVisible: 0
|
||||
|
Reference in New Issue
Block a user