perf: optimize performance in dev mode
Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
@@ -3,7 +3,7 @@ import type { PrimitiveAtom } from "jotai"
|
||||
import { atom, useAtomValue, useSetAtom } from "jotai"
|
||||
import type { FC, ReactNode } from "react"
|
||||
import { memo, useCallback, useContext, useMemo, useRef } from "react"
|
||||
import type { NativeSyntheticEvent } from "react-native"
|
||||
import type { NativeSyntheticEvent, StyleProp, ViewStyle } from "react-native"
|
||||
import { StyleSheet, View } from "react-native"
|
||||
import { useSharedValue } from "react-native-reanimated"
|
||||
import type { ScreenStackHeaderConfigProps, StackPresentationTypes } from "react-native-screens"
|
||||
@@ -33,6 +33,7 @@ export const WrappedScreenItem: FC<
|
||||
|
||||
headerConfig?: ScreenStackHeaderConfigProps
|
||||
screenOptions?: NavigationControllerViewExtraProps
|
||||
style?: StyleProp<ViewStyle>
|
||||
} & ScreenOptionsContextType
|
||||
> = memo(
|
||||
({
|
||||
@@ -41,6 +42,7 @@ export const WrappedScreenItem: FC<
|
||||
stackPresentation,
|
||||
headerConfig,
|
||||
screenOptions: screenOptionsProp,
|
||||
style,
|
||||
...rest
|
||||
}) => {
|
||||
const navigation = useNavigation()
|
||||
@@ -143,6 +145,7 @@ export const WrappedScreenItem: FC<
|
||||
style={[
|
||||
StyleSheet.absoluteFill,
|
||||
{ backgroundColor: screenOptionsProp?.transparent ? undefined : backgroundColor },
|
||||
style,
|
||||
]}
|
||||
{...rest}
|
||||
{...mergedScreenOptions}
|
||||
|
||||
@@ -95,15 +95,15 @@ export const TabScreen: FC<PropsWithChildren<Omit<TabScreenProps, "tabScreenInde
|
||||
)
|
||||
const shouldLoadReact = mergedProps.lazy ? isSelected || isLoadedBefore : true
|
||||
|
||||
const optimize = __DEV__ && isSelected
|
||||
return (
|
||||
<TabScreenNative style={StyleSheet.absoluteFill}>
|
||||
<TabScreenContext.Provider value={ctxValue}>
|
||||
{shouldLoadReact && (
|
||||
{shouldLoadReact && optimize && (
|
||||
<WrappedScreenItem screenId={`tab-screen-${tabScreenIndex}`}>
|
||||
{children}
|
||||
<ScreenNameRegister />
|
||||
<LifecycleEvents isSelected={isSelected} />
|
||||
{/* <CalculateTabBarOpacity /> */}
|
||||
</WrappedScreenItem>
|
||||
)}
|
||||
</TabScreenContext.Provider>
|
||||
|
||||
@@ -328,7 +328,7 @@ const PreviewFeeds = (props: { id: string; view: FeedViewType }) => {
|
||||
({ item: id }: ListRenderItemInfo<string>) => (
|
||||
<EntryNormalItem entryId={id} extraData="" view={props.view} />
|
||||
),
|
||||
[],
|
||||
[props.view],
|
||||
)
|
||||
return (
|
||||
<View className="bg-system-background size-full flex-1">
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { FeedViewType } from "@follow/constants"
|
||||
import type { ListRenderItemInfo } from "@shopify/flash-list"
|
||||
import type { ElementRef } from "react"
|
||||
import { forwardRef, useCallback, useMemo } from "react"
|
||||
import { forwardRef, useCallback, useImperativeHandle, useMemo } from "react"
|
||||
import { View } from "react-native"
|
||||
|
||||
import { usePlayingUrl } from "@/src/lib/player"
|
||||
@@ -10,14 +10,14 @@ import { usePrefetchEntryTranslation } from "@/src/store/translation/hooks"
|
||||
import { useFetchEntriesControls } from "../screen/atoms"
|
||||
import { TimelineSelectorList } from "../screen/TimelineSelectorList"
|
||||
import { EntryListFooter } from "./EntryListFooter"
|
||||
import { useOnViewableItemsChanged } from "./hooks"
|
||||
import { useOnViewableItemsChanged, usePagerListPerformanceHack } from "./hooks"
|
||||
import { ItemSeparator } from "./ItemSeparator"
|
||||
import { EntryNormalItem } from "./templates/EntryNormalItem"
|
||||
|
||||
export const EntryListContentArticle = forwardRef<
|
||||
ElementRef<typeof TimelineSelectorList>,
|
||||
{ entryIds: string[] | null; active?: boolean; view: FeedViewType }
|
||||
>(({ entryIds, active, view }, ref) => {
|
||||
>(({ entryIds, active, view }, forwardRef) => {
|
||||
const playingAudioUrl = usePlayingUrl()
|
||||
|
||||
const { fetchNextPage, isFetching, refetch, isRefetching, hasNextPage } =
|
||||
@@ -35,10 +35,15 @@ export const EntryListContentArticle = forwardRef<
|
||||
[hasNextPage],
|
||||
)
|
||||
|
||||
const { onScroll: hackOnScroll, ref, style: hackStyle } = usePagerListPerformanceHack()
|
||||
|
||||
const { onViewableItemsChanged, onScroll, viewableItems } = useOnViewableItemsChanged({
|
||||
disabled: active === false || isFetching,
|
||||
onScroll: hackOnScroll,
|
||||
})
|
||||
|
||||
useImperativeHandle(forwardRef, () => ref.current!)
|
||||
|
||||
usePrefetchEntryTranslation(active ? viewableItems.map((item) => item.key) : [])
|
||||
|
||||
return (
|
||||
@@ -48,7 +53,7 @@ export const EntryListContentArticle = forwardRef<
|
||||
isRefetching={isRefetching}
|
||||
data={entryIds}
|
||||
extraData={playingAudioUrl}
|
||||
keyExtractor={(id) => id}
|
||||
keyExtractor={defaultKeyExtractor}
|
||||
estimatedItemSize={100}
|
||||
renderItem={renderItem}
|
||||
onEndReached={fetchNextPage}
|
||||
@@ -56,10 +61,13 @@ export const EntryListContentArticle = forwardRef<
|
||||
onViewableItemsChanged={onViewableItemsChanged}
|
||||
ItemSeparatorComponent={ItemSeparator}
|
||||
ListFooterComponent={ListFooterComponent}
|
||||
style={hackStyle}
|
||||
/>
|
||||
)
|
||||
})
|
||||
|
||||
const defaultKeyExtractor = (id: string) => id
|
||||
|
||||
export function EntryItemSkeleton() {
|
||||
return (
|
||||
<View className="bg-system-background flex flex-row items-center p-4">
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { useTypeScriptHappyCallback } from "@follow/hooks"
|
||||
import type { MasonryFlashListProps } from "@shopify/flash-list"
|
||||
import type { ElementRef } from "react"
|
||||
import { forwardRef } from "react"
|
||||
import { forwardRef, useImperativeHandle } from "react"
|
||||
import { View } from "react-native"
|
||||
|
||||
import { PlatformActivityIndicator } from "@/src/components/ui/loading/PlatformActivityIndicator"
|
||||
@@ -10,7 +10,7 @@ import { usePrefetchEntryTranslation } from "@/src/store/translation/hooks"
|
||||
|
||||
import { TimelineSelectorMasonryList } from "../screen/TimelineSelectorList"
|
||||
import { GridEntryListFooter } from "./EntryListFooter"
|
||||
import { useOnViewableItemsChanged } from "./hooks"
|
||||
import { useOnViewableItemsChanged, usePagerListPerformanceHack } from "./hooks"
|
||||
// import type { MasonryItem } from "./templates/EntryGridItem"
|
||||
import { EntryPictureItem } from "./templates/EntryPictureItem"
|
||||
|
||||
@@ -20,11 +20,14 @@ export const EntryListContentPicture = forwardRef<
|
||||
MasonryFlashListProps<string>,
|
||||
"data" | "renderItem"
|
||||
>
|
||||
>(({ entryIds, active, ...rest }, ref) => {
|
||||
>(({ entryIds, active, ...rest }, forwardRef) => {
|
||||
const { onScroll: hackOnScroll, ref, style: hackStyle } = usePagerListPerformanceHack()
|
||||
useImperativeHandle(forwardRef, () => ref.current!)
|
||||
const { fetchNextPage, refetch, isRefetching, hasNextPage, isFetching } =
|
||||
useFetchEntriesControls()
|
||||
const { onViewableItemsChanged, onScroll, viewableItems } = useOnViewableItemsChanged({
|
||||
disabled: active === false || isFetching,
|
||||
onScroll: hackOnScroll,
|
||||
})
|
||||
|
||||
usePrefetchEntryTranslation(active ? viewableItems.map((item) => item.key) : [])
|
||||
@@ -42,6 +45,7 @@ export const EntryListContentPicture = forwardRef<
|
||||
onScroll={onScroll}
|
||||
onEndReached={fetchNextPage}
|
||||
numColumns={2}
|
||||
style={hackStyle}
|
||||
estimatedItemSize={100}
|
||||
ListFooterComponent={
|
||||
hasNextPage ? (
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { ListRenderItemInfo } from "@shopify/flash-list"
|
||||
import type { ElementRef } from "react"
|
||||
import { forwardRef, useCallback, useMemo } from "react"
|
||||
import { forwardRef, useCallback, useImperativeHandle, useMemo } from "react"
|
||||
import { View } from "react-native"
|
||||
|
||||
import { usePrefetchEntryTranslation } from "@/src/store/translation/hooks"
|
||||
@@ -8,17 +8,20 @@ import { usePrefetchEntryTranslation } from "@/src/store/translation/hooks"
|
||||
import { useFetchEntriesControls } from "../screen/atoms"
|
||||
import { TimelineSelectorList } from "../screen/TimelineSelectorList"
|
||||
import { EntryListFooter } from "./EntryListFooter"
|
||||
import { useOnViewableItemsChanged } from "./hooks"
|
||||
import { useOnViewableItemsChanged, usePagerListPerformanceHack } from "./hooks"
|
||||
import { ItemSeparatorFullWidth } from "./ItemSeparator"
|
||||
import { EntrySocialItem } from "./templates/EntrySocialItem"
|
||||
|
||||
export const EntryListContentSocial = forwardRef<
|
||||
ElementRef<typeof TimelineSelectorList>,
|
||||
{ entryIds: string[] | null; active?: boolean }
|
||||
>(({ entryIds, active }, ref) => {
|
||||
>(({ entryIds, active }, forwardRef) => {
|
||||
const { fetchNextPage, isFetching, refetch, isRefetching, hasNextPage } =
|
||||
useFetchEntriesControls()
|
||||
|
||||
const { onScroll: hackOnScroll, ref, style: hackStyle } = usePagerListPerformanceHack()
|
||||
useImperativeHandle(forwardRef, () => ref.current!)
|
||||
// eslint-disable-next-line @eslint-react/hooks-extra/no-unnecessary-use-callback
|
||||
const renderItem = useCallback(
|
||||
({ item: id }: ListRenderItemInfo<string>) => <EntrySocialItem entryId={id} />,
|
||||
[],
|
||||
@@ -31,6 +34,7 @@ export const EntryListContentSocial = forwardRef<
|
||||
|
||||
const { onViewableItemsChanged, onScroll, viewableItems } = useOnViewableItemsChanged({
|
||||
disabled: active === false || isFetching,
|
||||
onScroll: hackOnScroll,
|
||||
})
|
||||
|
||||
usePrefetchEntryTranslation(active ? viewableItems.map((item) => item.key) : [])
|
||||
@@ -51,6 +55,7 @@ export const EntryListContentSocial = forwardRef<
|
||||
onScroll={onScroll}
|
||||
ItemSeparatorComponent={ItemSeparatorFullWidth}
|
||||
ListFooterComponent={ListFooterComponent}
|
||||
style={hackStyle}
|
||||
/>
|
||||
)
|
||||
})
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { useTypeScriptHappyCallback } from "@follow/hooks"
|
||||
import type { MasonryFlashListProps } from "@shopify/flash-list"
|
||||
import type { ElementRef } from "react"
|
||||
import { forwardRef, useMemo } from "react"
|
||||
import { forwardRef, useImperativeHandle, useMemo } from "react"
|
||||
import { View } from "react-native"
|
||||
|
||||
import { useFetchEntriesControls } from "@/src/modules/screen/atoms"
|
||||
@@ -9,7 +9,7 @@ import { usePrefetchEntryTranslation } from "@/src/store/translation/hooks"
|
||||
|
||||
import { TimelineSelectorMasonryList } from "../screen/TimelineSelectorList"
|
||||
import { GridEntryListFooter } from "./EntryListFooter"
|
||||
import { useOnViewableItemsChanged } from "./hooks"
|
||||
import { useOnViewableItemsChanged, usePagerListPerformanceHack } from "./hooks"
|
||||
import { EntryVideoItem } from "./templates/EntryVideoItem"
|
||||
|
||||
export const EntryListContentVideo = forwardRef<
|
||||
@@ -18,11 +18,14 @@ export const EntryListContentVideo = forwardRef<
|
||||
MasonryFlashListProps<string>,
|
||||
"data" | "renderItem"
|
||||
>
|
||||
>(({ entryIds, active, ...rest }, ref) => {
|
||||
>(({ entryIds, active, ...rest }, forwardRef) => {
|
||||
const { onScroll: hackOnScroll, ref, style: hackStyle } = usePagerListPerformanceHack()
|
||||
useImperativeHandle(forwardRef, () => ref.current!)
|
||||
const { fetchNextPage, refetch, isRefetching, isFetching, hasNextPage } =
|
||||
useFetchEntriesControls()
|
||||
const { onViewableItemsChanged, onScroll, viewableItems } = useOnViewableItemsChanged({
|
||||
disabled: active === false || isFetching,
|
||||
onScroll: hackOnScroll,
|
||||
})
|
||||
|
||||
usePrefetchEntryTranslation(active ? viewableItems.map((item) => item.key) : [])
|
||||
@@ -57,6 +60,7 @@ export const EntryListContentVideo = forwardRef<
|
||||
ListFooterComponent={ListFooterComponent}
|
||||
{...rest}
|
||||
onRefresh={refetch}
|
||||
style={hackStyle}
|
||||
/>
|
||||
)
|
||||
})
|
||||
|
||||
@@ -1,18 +1,33 @@
|
||||
import type { FlashList } from "@shopify/flash-list"
|
||||
import type ViewToken from "@shopify/flash-list/dist/viewability/ViewToken"
|
||||
import { useCallback, useEffect, useInsertionEffect, useMemo, useRef, useState } from "react"
|
||||
import type { NativeScrollEvent, NativeSyntheticEvent } from "react-native"
|
||||
import type { RefObject } from "react"
|
||||
import {
|
||||
useCallback,
|
||||
useContext,
|
||||
useEffect,
|
||||
useInsertionEffect,
|
||||
useMemo,
|
||||
useRef,
|
||||
useState,
|
||||
} from "react"
|
||||
import type { NativeScrollEvent, NativeSyntheticEvent, StyleProp, ViewStyle } from "react-native"
|
||||
import { useEventCallback } from "usehooks-ts"
|
||||
|
||||
import { useGeneralSettingKey } from "@/src/atoms/settings/general"
|
||||
import { debouncedFetchEntryContentByStream } from "@/src/store/entry/store"
|
||||
import { unreadSyncService } from "@/src/store/unread/store"
|
||||
|
||||
import { PagerListVisibleContext, PagerListWillVisibleContext } from "../screen/PagerListContext"
|
||||
|
||||
const defaultIdExtractor = (item: ViewToken) => item.key
|
||||
export function useOnViewableItemsChanged({
|
||||
disabled,
|
||||
idExtractor = defaultIdExtractor,
|
||||
onScroll: onScrollProp,
|
||||
}: {
|
||||
disabled?: boolean
|
||||
idExtractor?: (item: ViewToken) => string
|
||||
onScroll?: (e: NativeSyntheticEvent<NativeScrollEvent>) => void
|
||||
} = {}) {
|
||||
const orientation = useRef<"down" | "up">("down")
|
||||
const lastOffset = useRef(0)
|
||||
@@ -82,12 +97,16 @@ export function useOnViewableItemsChanged({
|
||||
stableIdExtractor,
|
||||
])
|
||||
|
||||
const onScroll = useCallback((e: NativeSyntheticEvent<NativeScrollEvent>) => {
|
||||
const currentOffset = e.nativeEvent.contentOffset.y
|
||||
const currentOrientation = currentOffset > lastOffset.current ? "down" : "up"
|
||||
orientation.current = currentOrientation
|
||||
lastOffset.current = currentOffset
|
||||
}, [])
|
||||
const onScroll = useCallback(
|
||||
(e: NativeSyntheticEvent<NativeScrollEvent>) => {
|
||||
const currentOffset = e.nativeEvent.contentOffset.y
|
||||
const currentOrientation = currentOffset > lastOffset.current ? "down" : "up"
|
||||
orientation.current = currentOrientation
|
||||
lastOffset.current = currentOffset
|
||||
onScrollProp?.(e)
|
||||
},
|
||||
[onScrollProp],
|
||||
)
|
||||
|
||||
return useMemo(
|
||||
() => ({ onViewableItemsChanged, onScroll, viewableItems }),
|
||||
@@ -108,3 +127,33 @@ function useNonReactiveCallback<T extends (...args: any[]) => any>(fn: T): T {
|
||||
[ref],
|
||||
) as unknown as T
|
||||
}
|
||||
|
||||
export const usePagerListPerformanceHack = (provideRef?: RefObject<FlashList<any>>) => {
|
||||
const lastY = useRef(0)
|
||||
|
||||
const onScroll = useEventCallback((e: NativeSyntheticEvent<NativeScrollEvent>) => {
|
||||
if (!visible) return
|
||||
|
||||
lastY.current = e.nativeEvent.contentOffset.y
|
||||
})
|
||||
|
||||
const visible = useContext(PagerListVisibleContext)
|
||||
const willVisible = useContext(PagerListWillVisibleContext)
|
||||
|
||||
const nextVisible = visible || willVisible
|
||||
|
||||
const ref = useRef<FlashList<any>>(null)
|
||||
|
||||
const usingRef = provideRef ?? ref
|
||||
const [style, setStyle] = useState<StyleProp<ViewStyle>>({})
|
||||
useEffect(() => {
|
||||
setStyle({ display: nextVisible ? "flex" : "none" })
|
||||
if (nextVisible && lastY.current > 0) {
|
||||
requestAnimationFrame(() => {
|
||||
usingRef.current?.scrollToOffset({ offset: lastY.current, animated: false })
|
||||
})
|
||||
}
|
||||
}, [nextVisible, usingRef])
|
||||
|
||||
return { onScroll, ref, style }
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { FeedViewType } from "@follow/constants"
|
||||
import { EventBus } from "@follow/utils/src/event-bus"
|
||||
import * as Haptics from "expo-haptics"
|
||||
import { useCallback, useEffect, useId, useMemo, useRef } from "react"
|
||||
import { useCallback, useEffect, useId, useMemo, useRef, useState } from "react"
|
||||
import type { StyleProp, ViewStyle } from "react-native"
|
||||
import { Animated, StyleSheet } from "react-native"
|
||||
import PagerView from "react-native-pager-view"
|
||||
@@ -11,6 +11,7 @@ import { selectTimeline, useSelectedFeed } from "@/src/modules/screen/atoms"
|
||||
import { useViewWithSubscription } from "@/src/store/subscription/hooks"
|
||||
|
||||
import { setHorizontalScrolling } from "./atoms"
|
||||
import { PagerListVisibleContext, PagerListWillVisibleContext } from "./PagerListContext"
|
||||
|
||||
const AnimatedPagerView = Animated.createAnimatedComponent<typeof PagerView>(PagerView)
|
||||
|
||||
@@ -43,6 +44,7 @@ export function PagerList({
|
||||
}, [activeViews, pagerRef, rid])
|
||||
const userInitiatedDragRef = useSharedValue(false)
|
||||
|
||||
const [dragging, setDragging] = useState(false)
|
||||
const pageScrollHandler = useCallback(
|
||||
(e: {
|
||||
nativeEvent: {
|
||||
@@ -86,11 +88,16 @@ export function PagerList({
|
||||
overdrag
|
||||
onPageScroll={pageScrollHandler}
|
||||
onPageScrollStateChanged={(e) => {
|
||||
if (e.nativeEvent.pageScrollState === "dragging") {
|
||||
const { pageScrollState } = e.nativeEvent
|
||||
if (pageScrollState === "dragging") {
|
||||
setDragging(true)
|
||||
userInitiatedDragRef.value = true
|
||||
} else if (pageScrollState === "idle") {
|
||||
setDragging(false)
|
||||
}
|
||||
setHorizontalScrolling(e.nativeEvent.pageScrollState !== "idle")
|
||||
if (e.nativeEvent.pageScrollState === "settling") {
|
||||
|
||||
setHorizontalScrolling(pageScrollState !== "idle")
|
||||
if (pageScrollState === "settling") {
|
||||
Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light)
|
||||
}
|
||||
}}
|
||||
@@ -98,8 +105,17 @@ export function PagerList({
|
||||
orientation="horizontal"
|
||||
>
|
||||
{useMemo(
|
||||
() => activeViews.map((view, index) => renderItem(view.view, index === activeViewIndex)),
|
||||
[activeViews, activeViewIndex, renderItem],
|
||||
() =>
|
||||
activeViews.map((view, index) => (
|
||||
<PagerListVisibleContext.Provider value={index === activeViewIndex} key={view.view}>
|
||||
<PagerListWillVisibleContext.Provider
|
||||
value={(index === activeViewIndex + 1 || index === activeViewIndex - 1) && dragging}
|
||||
>
|
||||
{renderItem(view.view, index === activeViewIndex)}
|
||||
</PagerListWillVisibleContext.Provider>
|
||||
</PagerListVisibleContext.Provider>
|
||||
)),
|
||||
[activeViews, activeViewIndex, dragging, renderItem],
|
||||
)}
|
||||
</AnimatedPagerView>
|
||||
)
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
import { createContext } from "react"
|
||||
|
||||
export const PagerListVisibleContext = createContext(false)
|
||||
export const PagerListWillVisibleContext = createContext(false)
|
||||
@@ -8,15 +8,21 @@ import type {
|
||||
import { FlashList, MasonryFlashList } from "@shopify/flash-list"
|
||||
import * as Haptics from "expo-haptics"
|
||||
import type { ElementRef, RefObject } from "react"
|
||||
import { forwardRef, useCallback, useContext, useEffect, useImperativeHandle, useRef } from "react"
|
||||
import {
|
||||
forwardRef,
|
||||
useCallback,
|
||||
useContext,
|
||||
useImperativeHandle,
|
||||
useLayoutEffect,
|
||||
useRef,
|
||||
} from "react"
|
||||
import type { NativeScrollEvent, NativeSyntheticEvent } from "react-native"
|
||||
import { findNodeHandle, RefreshControl } from "react-native"
|
||||
import { RefreshControl, View } from "react-native"
|
||||
import { useSafeAreaInsets } from "react-native-safe-area-context"
|
||||
import { useColor } from "react-native-uikit-colors"
|
||||
|
||||
import { BottomTabBarBackgroundContext } from "@/src/components/layouts/tabbar/contexts/BottomTabBarBackgroundContext"
|
||||
import { useBottomTabBarHeight } from "@/src/components/layouts/tabbar/hooks"
|
||||
import { isScrollToEnd } from "@/src/lib/native"
|
||||
import { useTabScreenIsFocused } from "@/src/lib/navigation/bottom-tab/hooks"
|
||||
import { ScreenItemContext } from "@/src/lib/navigation/ScreenItemContext"
|
||||
import { useHeaderHeight } from "@/src/modules/screen/hooks/useHeaderHeight"
|
||||
@@ -40,36 +46,24 @@ export const TimelineSelectorList = forwardRef<
|
||||
const { refetch: subscriptionRefetch } = usePrefetchSubscription()
|
||||
|
||||
const headerHeight = useHeaderHeight()
|
||||
const { reAnimatedScrollY, scrollViewHeight, scrollViewContentHeight } =
|
||||
const { scrollViewHeight, scrollViewContentHeight, reAnimatedScrollY } =
|
||||
useContext(ScreenItemContext)!
|
||||
const { opacity } = useContext(BottomTabBarBackgroundContext)
|
||||
const checkScrollToBottom = useCallback(() => {
|
||||
const handle = findNodeHandle(ref.current!)
|
||||
if (!handle) {
|
||||
return
|
||||
}
|
||||
isScrollToEnd(handle).then((isEnd) => {
|
||||
opacity.value = isEnd ? 0 : 1
|
||||
})
|
||||
}, [opacity])
|
||||
const tabScreenIsFocus = useTabScreenIsFocused()
|
||||
useEffect(() => {
|
||||
useLayoutEffect(() => {
|
||||
if (tabScreenIsFocus) {
|
||||
checkScrollToBottom()
|
||||
opacity.value = 1
|
||||
}
|
||||
}, [checkScrollToBottom, tabScreenIsFocus])
|
||||
}, [tabScreenIsFocus, opacity])
|
||||
const tabBarHeight = useBottomTabBarHeight()
|
||||
const onScroll = useCallback(
|
||||
(e: NativeSyntheticEvent<NativeScrollEvent>) => {
|
||||
props.onScroll?.(e)
|
||||
|
||||
reAnimatedScrollY.value = e.nativeEvent.contentOffset.y
|
||||
checkScrollToBottom()
|
||||
},
|
||||
[props, reAnimatedScrollY, checkScrollToBottom],
|
||||
[props, reAnimatedScrollY],
|
||||
)
|
||||
|
||||
const tabBarHeight = useBottomTabBarHeight()
|
||||
|
||||
const systemFill = useColor("secondaryLabel")
|
||||
|
||||
const onLayout = useTypeScriptHappyCallback(
|
||||
@@ -91,42 +85,44 @@ export const TimelineSelectorList = forwardRef<
|
||||
}
|
||||
|
||||
return (
|
||||
<FlashList
|
||||
automaticallyAdjustsScrollIndicatorInsets={false}
|
||||
automaticallyAdjustContentInsets={false}
|
||||
ref={ref}
|
||||
onLayout={onLayout}
|
||||
onContentSizeChange={onContentSizeChange}
|
||||
refreshControl={
|
||||
<RefreshControl
|
||||
progressViewOffset={headerHeight}
|
||||
// // FIXME: not sure why we need set tintColor manually here, otherwise we can not see the refresh indicator
|
||||
tintColor={systemFill}
|
||||
onRefresh={() => {
|
||||
unreadRefetch()
|
||||
subscriptionRefetch()
|
||||
Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light)
|
||||
onRefresh()
|
||||
}}
|
||||
refreshing={isRefetching}
|
||||
/>
|
||||
}
|
||||
scrollIndicatorInsets={{
|
||||
top: headerHeight,
|
||||
bottom: tabBarHeight,
|
||||
}}
|
||||
contentContainerStyle={{
|
||||
paddingTop: headerHeight,
|
||||
paddingBottom: tabBarHeight,
|
||||
}}
|
||||
{...props}
|
||||
onScroll={onScroll}
|
||||
onEndReached={() => {
|
||||
nextFrame(() => {
|
||||
props.onEndReached?.()
|
||||
})
|
||||
}}
|
||||
/>
|
||||
<View style={props.style} className="flex-1">
|
||||
<FlashList
|
||||
automaticallyAdjustsScrollIndicatorInsets={false}
|
||||
automaticallyAdjustContentInsets={false}
|
||||
ref={ref}
|
||||
onLayout={onLayout}
|
||||
onContentSizeChange={onContentSizeChange}
|
||||
refreshControl={
|
||||
<RefreshControl
|
||||
progressViewOffset={headerHeight}
|
||||
// // FIXME: not sure why we need set tintColor manually here, otherwise we can not see the refresh indicator
|
||||
tintColor={systemFill}
|
||||
onRefresh={() => {
|
||||
unreadRefetch()
|
||||
subscriptionRefetch()
|
||||
Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light)
|
||||
onRefresh()
|
||||
}}
|
||||
refreshing={isRefetching}
|
||||
/>
|
||||
}
|
||||
scrollIndicatorInsets={{
|
||||
top: headerHeight,
|
||||
bottom: tabBarHeight,
|
||||
}}
|
||||
contentContainerStyle={{
|
||||
paddingTop: headerHeight,
|
||||
paddingBottom: tabBarHeight,
|
||||
}}
|
||||
{...props}
|
||||
onScroll={onScroll}
|
||||
onEndReached={() => {
|
||||
nextFrame(() => {
|
||||
props.onEndReached?.()
|
||||
})
|
||||
}}
|
||||
/>
|
||||
</View>
|
||||
)
|
||||
})
|
||||
|
||||
|
||||
@@ -1,89 +0,0 @@
|
||||
/*
|
||||
* @Author: czy0729
|
||||
* @Date: 2023-12-27 15:42:54
|
||||
* @Last Modified by: czy0729
|
||||
* @Last Modified time: 2024-08-13 16:14:43
|
||||
*
|
||||
* Ported from https://github.com/czy0729/Bangumi/blob/6f2c46bba0bb2c1ffa98fb54d9609d87cb5f06a2/src/components/nested-scroll/hooks/usePagerView.tsx
|
||||
* License: MIT
|
||||
*/
|
||||
import { useCallback, useMemo, useRef, useState } from "react"
|
||||
import { Animated, Platform, useAnimatedValue } from "react-native"
|
||||
import type {
|
||||
default as PagerView,
|
||||
PagerViewOnPageScrollEventData,
|
||||
PagerViewOnPageSelectedEvent,
|
||||
PageScrollStateChangedNativeEvent,
|
||||
} from "react-native-pager-view"
|
||||
|
||||
export function usePagerView({
|
||||
initialPage = 0,
|
||||
onIndexChange,
|
||||
}: {
|
||||
initialPage?: number
|
||||
onIndexChange?: (index: number) => void
|
||||
}) {
|
||||
const pagerRef = useRef<PagerView>(null)
|
||||
const [activePage, setActivePage] = useState(initialPage)
|
||||
const [isIdle, setIdle] = useState(true)
|
||||
|
||||
const setPage = useCallback((page: number, animated = true) => {
|
||||
if (animated) {
|
||||
pagerRef.current?.setPage(page)
|
||||
} else {
|
||||
pagerRef.current?.setPageWithoutAnimation(page)
|
||||
}
|
||||
setActivePage(page)
|
||||
}, [])
|
||||
|
||||
const offset = useAnimatedValue(0)
|
||||
const position = useAnimatedValue(initialPage)
|
||||
|
||||
const onPageScroll = useMemo(
|
||||
() =>
|
||||
Animated.event<PagerViewOnPageScrollEventData>(
|
||||
[
|
||||
{
|
||||
nativeEvent: {
|
||||
offset,
|
||||
position,
|
||||
},
|
||||
},
|
||||
],
|
||||
{
|
||||
useNativeDriver: true,
|
||||
},
|
||||
),
|
||||
[offset, position],
|
||||
)
|
||||
|
||||
const onPageSelected = useCallback(
|
||||
(e: PagerViewOnPageSelectedEvent) => {
|
||||
const { position } = e.nativeEvent
|
||||
setActivePage(position)
|
||||
if (Platform.OS === "ios") setIdle(true)
|
||||
|
||||
if (typeof onIndexChange === "function") onIndexChange(position)
|
||||
},
|
||||
[onIndexChange],
|
||||
)
|
||||
|
||||
const onPageScrollStateChanged = useCallback(
|
||||
({ nativeEvent: { pageScrollState } }: PageScrollStateChangedNativeEvent) => {
|
||||
setIdle(pageScrollState === "idle")
|
||||
},
|
||||
[],
|
||||
)
|
||||
|
||||
return {
|
||||
pagerRef,
|
||||
setPage,
|
||||
page: activePage,
|
||||
isIdle,
|
||||
position,
|
||||
offset,
|
||||
onPageScroll,
|
||||
onPageSelected,
|
||||
onPageScrollStateChanged,
|
||||
}
|
||||
}
|
||||
@@ -64,7 +64,7 @@ export const UserHeaderBanner = ({
|
||||
if (!gradientColors) return {}
|
||||
return {
|
||||
transform: [{ scale: scaleValue }],
|
||||
height: 250 + (scrollY.value < 0 ? -scrollY.value : 0),
|
||||
height: 250,
|
||||
}
|
||||
})
|
||||
|
||||
@@ -118,8 +118,8 @@ export const UserHeaderBanner = ({
|
||||
)}
|
||||
</ReAnimated.View>
|
||||
<ReAnimated.View
|
||||
className="bg-system-background overflow-hidden rounded-full"
|
||||
style={avatarStyles}
|
||||
className="bg-system-background overflow-hidden rounded-full"
|
||||
>
|
||||
<UserAvatar
|
||||
image={user?.image}
|
||||
|
||||
@@ -33,6 +33,7 @@ import {
|
||||
} from "@/src/store/subscription/hooks"
|
||||
import { subscriptionSyncService } from "@/src/store/subscription/store"
|
||||
|
||||
import { usePagerListPerformanceHack } from "../entry-list/hooks"
|
||||
import { useFeedListSortMethod, useFeedListSortOrder } from "./atoms"
|
||||
import { CategoryGrouped } from "./CategoryGrouped"
|
||||
import { InboxItem } from "./items/InboxItem"
|
||||
@@ -103,6 +104,8 @@ const SubscriptionListImpl = ({
|
||||
|
||||
const scrollViewRef = useRegisterNavigationScrollView<FlashList<any>>(active)
|
||||
|
||||
const { onScroll, style } = usePagerListPerformanceHack(scrollViewRef)
|
||||
|
||||
return (
|
||||
<TimelineSelectorList
|
||||
contentContainerClassName="pb-6"
|
||||
@@ -118,8 +121,9 @@ const SubscriptionListImpl = ({
|
||||
estimatedItemSize={50}
|
||||
renderItem={ItemRender}
|
||||
keyExtractor={keyExtractor}
|
||||
// itemLayoutAnimation={LinearTransition}
|
||||
extraData={extraData}
|
||||
onScroll={onScroll}
|
||||
style={style}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ export default function Discover() {
|
||||
() => ({ animatedX, currentTabAtom, headerHeightAtom, animatedY }),
|
||||
[animatedX, currentTabAtom, headerHeightAtom, animatedY],
|
||||
)
|
||||
|
||||
return (
|
||||
<DiscoverContext.Provider value={ctxValue}>
|
||||
<StackScreenHeaderPortal>
|
||||
|
||||
@@ -20,6 +20,7 @@ export const usePrefetchEntries = (props: Omit<FetchEntriesProps, "pageParam" |
|
||||
refetchOnWindowFocus: false,
|
||||
refetchOnReconnect: false,
|
||||
enabled: !!props,
|
||||
staleTime: unreadOnly ? undefined : 1000 * 60 * 5,
|
||||
})
|
||||
}
|
||||
export const usePrefetchEntryDetail = (entryId: string) => {
|
||||
|
||||
Reference in New Issue
Block a user