2022-06-01 23:13:43 +02:00
|
|
|
import haptics from '@components/haptics'
|
|
|
|
import Icon from '@components/Icon'
|
2022-12-28 23:41:36 +01:00
|
|
|
import { InfiniteData, useQueryClient } from '@tanstack/react-query'
|
2023-01-07 00:43:00 +01:00
|
|
|
import { PagedResponse } from '@utils/api/helpers'
|
|
|
|
import {
|
|
|
|
queryFunctionTimeline,
|
|
|
|
QueryKeyTimeline,
|
|
|
|
useTimelineQuery
|
|
|
|
} from '@utils/queryHooks/timeline'
|
2023-01-07 16:27:40 +01:00
|
|
|
import { setAccountStorage } from '@utils/storage/actions'
|
2022-06-01 23:13:43 +02:00
|
|
|
import { StyleConstants } from '@utils/styles/constants'
|
|
|
|
import { useTheme } from '@utils/styles/ThemeManager'
|
2023-01-07 16:27:40 +01:00
|
|
|
import React, { RefObject, useRef, useState } from 'react'
|
2022-06-01 23:13:43 +02:00
|
|
|
import { useTranslation } from 'react-i18next'
|
2023-01-06 22:58:01 +01:00
|
|
|
import { FlatList, Platform, Text, View } from 'react-native'
|
2022-06-01 23:13:43 +02:00
|
|
|
import Animated, {
|
|
|
|
Extrapolate,
|
|
|
|
interpolate,
|
|
|
|
runOnJS,
|
|
|
|
useAnimatedReaction,
|
|
|
|
useAnimatedStyle,
|
2023-01-07 16:27:40 +01:00
|
|
|
useDerivedValue,
|
2022-06-01 23:13:43 +02:00
|
|
|
useSharedValue,
|
|
|
|
withTiming
|
|
|
|
} from 'react-native-reanimated'
|
|
|
|
|
|
|
|
export interface Props {
|
2023-01-06 14:00:50 +01:00
|
|
|
flRef: RefObject<FlatList<any>>
|
2022-06-01 23:13:43 +02:00
|
|
|
queryKey: QueryKeyTimeline
|
2023-01-07 16:27:40 +01:00
|
|
|
fetchingActive: React.MutableRefObject<boolean>
|
2022-06-01 23:13:43 +02:00
|
|
|
scrollY: Animated.SharedValue<number>
|
|
|
|
fetchingType: Animated.SharedValue<0 | 1 | 2>
|
|
|
|
disableRefresh?: boolean
|
2023-01-07 16:27:40 +01:00
|
|
|
readMarker?: 'read_marker_following'
|
2022-06-01 23:13:43 +02:00
|
|
|
}
|
|
|
|
|
2023-01-07 12:10:41 +01:00
|
|
|
const CONTAINER_HEIGHT = StyleConstants.Spacing.M * 2.5
|
2022-12-11 01:08:38 +01:00
|
|
|
export const SEPARATION_Y_1 = -(CONTAINER_HEIGHT / 2 + StyleConstants.Font.Size.S / 2)
|
|
|
|
export const SEPARATION_Y_2 = -(CONTAINER_HEIGHT * 1.5 + StyleConstants.Font.Size.S / 2)
|
2022-06-01 23:13:43 +02:00
|
|
|
|
|
|
|
const TimelineRefresh: React.FC<Props> = ({
|
|
|
|
flRef,
|
|
|
|
queryKey,
|
2023-01-07 16:27:40 +01:00
|
|
|
fetchingActive,
|
2022-06-01 23:13:43 +02:00
|
|
|
scrollY,
|
|
|
|
fetchingType,
|
2023-01-07 16:27:40 +01:00
|
|
|
disableRefresh = false,
|
|
|
|
readMarker
|
2022-06-01 23:13:43 +02:00
|
|
|
}) => {
|
|
|
|
if (Platform.OS !== 'ios') {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
if (disableRefresh) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2023-01-07 00:43:00 +01:00
|
|
|
const PREV_PER_BATCH = 1
|
|
|
|
const prevActive = useRef<boolean>(false)
|
|
|
|
const prevCache = useRef<(Mastodon.Status | Mastodon.Notification | Mastodon.Conversation)[]>()
|
|
|
|
const prevStatusId = useRef<Mastodon.Status['id']>()
|
|
|
|
|
|
|
|
const queryClient = useQueryClient()
|
2023-01-07 16:27:40 +01:00
|
|
|
const { refetch, isRefetching } = useTimelineQuery({ ...queryKey[1] })
|
|
|
|
|
|
|
|
useDerivedValue(() => {
|
|
|
|
if (prevActive.current || isRefetching) {
|
|
|
|
fetchingActive.current = true
|
|
|
|
} else {
|
|
|
|
fetchingActive.current = false
|
|
|
|
}
|
|
|
|
}, [prevActive.current, isRefetching])
|
2022-06-01 23:13:43 +02:00
|
|
|
|
|
|
|
const { t } = useTranslation('componentTimeline')
|
|
|
|
const { colors } = useTheme()
|
|
|
|
|
|
|
|
const [textRight, setTextRight] = useState(0)
|
|
|
|
const arrowY = useAnimatedStyle(() => ({
|
|
|
|
transform: [
|
|
|
|
{
|
|
|
|
translateY: interpolate(
|
|
|
|
scrollY.value,
|
|
|
|
[0, SEPARATION_Y_1],
|
|
|
|
[
|
|
|
|
-CONTAINER_HEIGHT / 2 - StyleConstants.Font.Size.M / 2,
|
|
|
|
CONTAINER_HEIGHT / 2 - StyleConstants.Font.Size.S / 2
|
|
|
|
],
|
|
|
|
Extrapolate.CLAMP
|
|
|
|
)
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}))
|
|
|
|
const arrowTop = useAnimatedStyle(() => ({
|
2022-12-11 01:08:38 +01:00
|
|
|
marginTop: scrollY.value < SEPARATION_Y_2 ? withTiming(CONTAINER_HEIGHT) : withTiming(0)
|
2022-06-01 23:13:43 +02:00
|
|
|
}))
|
|
|
|
|
|
|
|
const arrowStage = useSharedValue(0)
|
|
|
|
useAnimatedReaction(
|
|
|
|
() => {
|
2023-01-07 16:27:40 +01:00
|
|
|
if (fetchingActive.current) {
|
2022-06-01 23:13:43 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
switch (arrowStage.value) {
|
|
|
|
case 0:
|
|
|
|
if (scrollY.value < SEPARATION_Y_1) {
|
|
|
|
arrowStage.value = 1
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
case 1:
|
|
|
|
if (scrollY.value < SEPARATION_Y_2) {
|
|
|
|
arrowStage.value = 2
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if (scrollY.value > SEPARATION_Y_1) {
|
|
|
|
arrowStage.value = 0
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
case 2:
|
|
|
|
if (scrollY.value > SEPARATION_Y_2) {
|
|
|
|
arrowStage.value = 1
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
data => {
|
|
|
|
if (data) {
|
|
|
|
runOnJS(haptics)('Light')
|
|
|
|
}
|
|
|
|
},
|
2023-01-07 16:27:40 +01:00
|
|
|
[fetchingActive.current]
|
2022-06-01 23:13:43 +02:00
|
|
|
)
|
2023-01-06 22:58:01 +01:00
|
|
|
|
2023-01-07 12:10:41 +01:00
|
|
|
const fetchAndScrolled = useSharedValue(false)
|
2023-01-07 00:43:00 +01:00
|
|
|
const runFetchPrevious = async () => {
|
|
|
|
if (prevActive.current) return
|
2023-01-06 22:58:01 +01:00
|
|
|
|
2023-01-07 00:43:00 +01:00
|
|
|
const firstPage =
|
|
|
|
queryClient.getQueryData<
|
|
|
|
InfiniteData<
|
|
|
|
PagedResponse<(Mastodon.Status | Mastodon.Notification | Mastodon.Conversation)[]>
|
|
|
|
>
|
|
|
|
>(queryKey)?.pages[0]
|
|
|
|
|
|
|
|
prevActive.current = true
|
|
|
|
prevStatusId.current = firstPage?.body[0].id
|
2023-01-06 22:58:01 +01:00
|
|
|
|
2023-01-07 00:43:00 +01:00
|
|
|
await queryFunctionTimeline({
|
|
|
|
queryKey,
|
2023-01-07 18:01:08 +01:00
|
|
|
pageParam: firstPage?.links?.prev,
|
2023-01-07 00:43:00 +01:00
|
|
|
meta: {}
|
2023-01-06 22:58:01 +01:00
|
|
|
})
|
2023-01-07 12:10:41 +01:00
|
|
|
.then(res => {
|
2023-01-07 12:15:07 +01:00
|
|
|
if (!res.body.length) return
|
|
|
|
|
2023-01-07 00:43:00 +01:00
|
|
|
queryClient.setQueryData<
|
|
|
|
InfiniteData<
|
|
|
|
PagedResponse<(Mastodon.Status | Mastodon.Notification | Mastodon.Conversation)[]>
|
|
|
|
>
|
|
|
|
>(queryKey, old => {
|
|
|
|
if (!old) return old
|
|
|
|
|
2023-01-07 12:10:41 +01:00
|
|
|
prevCache.current = res.body.slice(0, -PREV_PER_BATCH)
|
2023-01-07 00:43:00 +01:00
|
|
|
return {
|
|
|
|
...old,
|
2023-01-07 12:10:41 +01:00
|
|
|
pages: [{ ...res, body: res.body.slice(-PREV_PER_BATCH) }, ...old.pages]
|
2023-01-07 00:43:00 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-01-07 12:10:41 +01:00
|
|
|
return res.body.length - PREV_PER_BATCH
|
|
|
|
})
|
|
|
|
.then(async nextLength => {
|
2023-01-07 12:15:07 +01:00
|
|
|
if (!nextLength) {
|
|
|
|
prevActive.current = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-07 12:10:41 +01:00
|
|
|
for (let [index] of Array(Math.ceil(nextLength / PREV_PER_BATCH)).entries()) {
|
|
|
|
if (!fetchAndScrolled.value && index < 3 && scrollY.value > 15) {
|
|
|
|
fetchAndScrolled.value = true
|
|
|
|
flRef.current?.scrollToOffset({ offset: scrollY.value - 15, animated: true })
|
|
|
|
}
|
|
|
|
|
|
|
|
await new Promise(promise => setTimeout(promise, 32))
|
|
|
|
queryClient.setQueryData<
|
|
|
|
InfiniteData<
|
|
|
|
PagedResponse<(Mastodon.Status | Mastodon.Notification | Mastodon.Conversation)[]>
|
|
|
|
>
|
|
|
|
>(queryKey, old => {
|
|
|
|
if (!old) return old
|
|
|
|
|
|
|
|
return {
|
|
|
|
...old,
|
|
|
|
pages: old.pages.map((page, index) => {
|
|
|
|
if (index === 0) {
|
|
|
|
const insert = prevCache.current?.slice(-PREV_PER_BATCH)
|
|
|
|
prevCache.current = prevCache.current?.slice(0, -PREV_PER_BATCH)
|
|
|
|
if (insert) {
|
|
|
|
return { ...page, body: [...insert, ...page.body] }
|
|
|
|
} else {
|
|
|
|
return page
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return page
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
prevActive.current = false
|
|
|
|
})
|
|
|
|
}
|
2023-01-07 00:43:00 +01:00
|
|
|
|
|
|
|
const runFetchLatest = async () => {
|
|
|
|
queryClient.invalidateQueries(queryKey)
|
2023-01-07 16:27:40 +01:00
|
|
|
if (readMarker) {
|
|
|
|
setAccountStorage([{ key: readMarker, value: undefined }])
|
|
|
|
}
|
2023-01-06 22:58:01 +01:00
|
|
|
await refetch()
|
|
|
|
setTimeout(() => flRef.current?.scrollToOffset({ offset: 0 }), 50)
|
2022-06-01 23:13:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
useAnimatedReaction(
|
|
|
|
() => {
|
|
|
|
return fetchingType.value
|
|
|
|
},
|
|
|
|
data => {
|
|
|
|
fetchingType.value = 0
|
|
|
|
switch (data) {
|
|
|
|
case 1:
|
2023-01-06 22:58:01 +01:00
|
|
|
runOnJS(runFetchPrevious)()
|
|
|
|
return
|
2022-06-01 23:13:43 +02:00
|
|
|
case 2:
|
2023-01-07 00:43:00 +01:00
|
|
|
runOnJS(runFetchLatest)()
|
2023-01-06 22:58:01 +01:00
|
|
|
return
|
2022-06-01 23:13:43 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
|
|
|
|
return (
|
2023-01-06 22:58:01 +01:00
|
|
|
<Animated.View
|
|
|
|
style={{
|
|
|
|
position: 'absolute',
|
|
|
|
top: 0,
|
|
|
|
left: 0,
|
|
|
|
right: 0,
|
|
|
|
height: CONTAINER_HEIGHT * 2,
|
|
|
|
alignItems: 'center'
|
|
|
|
}}
|
|
|
|
>
|
2023-01-07 16:27:40 +01:00
|
|
|
<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}
|
2023-01-06 22:58:01 +01:00
|
|
|
/>
|
2023-01-07 16:27:40 +01:00
|
|
|
}
|
|
|
|
/>
|
|
|
|
</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>
|
2022-06-01 23:13:43 +02:00
|
|
|
</Animated.View>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default TimelineRefresh
|