diff --git a/apps/mobile/src/modules/entry-list/EntryListContentArticle.tsx b/apps/mobile/src/modules/entry-list/EntryListContentArticle.tsx index 33171b662..b25a21a67 100644 --- a/apps/mobile/src/modules/entry-list/EntryListContentArticle.tsx +++ b/apps/mobile/src/modules/entry-list/EntryListContentArticle.tsx @@ -1,8 +1,8 @@ import type { FeedViewType } from "@follow/constants" import { usePrefetchEntryTranslation } from "@follow/store/translation/hooks" -import type { ListRenderItemInfo } from "@shopify/flash-list" +import type { FlashList, ListRenderItemInfo } from "@shopify/flash-list" import type { ElementRef } from "react" -import { useCallback, useImperativeHandle, useMemo } from "react" +import { useCallback, useImperativeHandle, useMemo, useRef } from "react" import { View } from "react-native" import { useActionLanguage, useGeneralSettingKey } from "@/src/atoms/settings/general" @@ -13,7 +13,7 @@ import { useHeaderHeight } from "@/src/modules/screen/hooks/useHeaderHeight" import { useEntries } from "../screen/atoms" import { TimelineSelectorList } from "../screen/TimelineSelectorList" import { EntryListFooter } from "./EntryListFooter" -import { useOnViewableItemsChanged, usePagerListPerformanceHack } from "./hooks" +import { useOnViewableItemsChanged } from "./hooks" import { ItemSeparator } from "./ItemSeparator" import { EntryNormalItem } from "./templates/EntryNormalItem" import type { EntryExtraData } from "./types" @@ -43,11 +43,10 @@ export const EntryListContentArticle = ({ [hasNextPage, fetchedTime], ) - const { onScroll: hackOnScroll, ref, style: hackStyle } = usePagerListPerformanceHack() + const ref = useRef>(null) const { onViewableItemsChanged, onScroll, viewableItems } = useOnViewableItemsChanged({ disabled: active === false || isFetching, - onScroll: hackOnScroll, }) useImperativeHandle(forwardRef, () => ref.current!) @@ -90,7 +89,6 @@ export const EntryListContentArticle = ({ onViewableItemsChanged={onViewableItemsChanged} ItemSeparatorComponent={ItemSeparator} ListFooterComponent={ListFooterComponent} - style={hackStyle} /> ) } diff --git a/apps/mobile/src/modules/entry-list/EntryListContentPicture.tsx b/apps/mobile/src/modules/entry-list/EntryListContentPicture.tsx index 9f44768f0..5448284ce 100644 --- a/apps/mobile/src/modules/entry-list/EntryListContentPicture.tsx +++ b/apps/mobile/src/modules/entry-list/EntryListContentPicture.tsx @@ -1,8 +1,8 @@ import { useTypeScriptHappyCallback } from "@follow/hooks" import { usePrefetchEntryTranslation } from "@follow/store/translation/hooks" -import type { MasonryFlashListProps } from "@shopify/flash-list" +import type { FlashList, MasonryFlashListProps } from "@shopify/flash-list" import type { ElementRef } from "react" -import { useImperativeHandle } from "react" +import { useImperativeHandle, useRef } from "react" import { StyleSheet, View } from "react-native" import { useActionLanguage, useGeneralSettingKey } from "@/src/atoms/settings/general" @@ -14,7 +14,7 @@ import { useHeaderHeight } from "@/src/modules/screen/hooks/useHeaderHeight" import { TimelineSelectorMasonryList } from "../screen/TimelineSelectorList" import { GridEntryListFooter } from "./EntryListFooter" -import { useOnViewableItemsChanged, usePagerListPerformanceHack } from "./hooks" +import { useOnViewableItemsChanged } from "./hooks" // import type { MasonryItem } from "./templates/EntryGridItem" import { EntryPictureItem } from "./templates/EntryPictureItem" @@ -27,12 +27,12 @@ export const EntryListContentPicture = ({ MasonryFlashListProps, "data" | "renderItem" > & { ref?: React.Ref | null> }) => { - const { onScroll: hackOnScroll, ref, style: hackStyle } = usePagerListPerformanceHack() + const ref = useRef>(null) + useImperativeHandle(forwardRef, () => ref.current!) const { fetchNextPage, refetch, isRefetching, hasNextPage, isFetching, isReady } = useEntries() const { onViewableItemsChanged, onScroll, viewableItems } = useOnViewableItemsChanged({ disabled: active === false || isFetching, - onScroll: hackOnScroll, }) const translation = useGeneralSettingKey("translation") const actionLanguage = useActionLanguage() @@ -84,7 +84,6 @@ export const EntryListContentPicture = ({ onScroll={onScroll} onEndReached={fetchNextPage} numColumns={2} - style={hackStyle} estimatedItemSize={100} contentContainerStyle={styles.contentContainer} ListFooterComponent={ diff --git a/apps/mobile/src/modules/entry-list/EntryListContentSocial.tsx b/apps/mobile/src/modules/entry-list/EntryListContentSocial.tsx index ae7c882fc..7e1bd29f1 100644 --- a/apps/mobile/src/modules/entry-list/EntryListContentSocial.tsx +++ b/apps/mobile/src/modules/entry-list/EntryListContentSocial.tsx @@ -1,7 +1,7 @@ import { usePrefetchEntryTranslation } from "@follow/store/translation/hooks" -import type { ListRenderItemInfo } from "@shopify/flash-list" +import type { FlashList, ListRenderItemInfo } from "@shopify/flash-list" import type { ElementRef } from "react" -import { useCallback, useImperativeHandle, useMemo } from "react" +import { useCallback, useImperativeHandle, useMemo, useRef } from "react" import { View } from "react-native" import { useActionLanguage, useGeneralSettingKey } from "@/src/atoms/settings/general" @@ -12,7 +12,7 @@ import { useHeaderHeight } from "@/src/modules/screen/hooks/useHeaderHeight" import { useEntries } from "../screen/atoms" import { TimelineSelectorList } from "../screen/TimelineSelectorList" import { EntryListFooter } from "./EntryListFooter" -import { useOnViewableItemsChanged, usePagerListPerformanceHack } from "./hooks" +import { useOnViewableItemsChanged } from "./hooks" import { ItemSeparatorFullWidth } from "./ItemSeparator" import { EntrySocialItem } from "./templates/EntrySocialItem" import type { EntryExtraData } from "./types" @@ -27,7 +27,7 @@ export const EntryListContentSocial = ({ const { fetchNextPage, isFetching, refetch, isRefetching, hasNextPage, isReady } = useEntries() const extraData: EntryExtraData = useMemo(() => ({ entryIds }), [entryIds]) - const { onScroll: hackOnScroll, ref, style: hackStyle } = usePagerListPerformanceHack() + const ref = useRef>(null) useImperativeHandle(forwardRef, () => ref.current!) // eslint-disable-next-line @eslint-react/hooks-extra/no-unnecessary-use-callback const renderItem = useCallback( @@ -44,7 +44,6 @@ export const EntryListContentSocial = ({ const { onViewableItemsChanged, onScroll, viewableItems } = useOnViewableItemsChanged({ disabled: active === false || isFetching, - onScroll: hackOnScroll, }) const translation = useGeneralSettingKey("translation") @@ -87,7 +86,6 @@ export const EntryListContentSocial = ({ onScroll={onScroll} ItemSeparatorComponent={ItemSeparatorFullWidth} ListFooterComponent={ListFooterComponent} - style={hackStyle} /> ) } diff --git a/apps/mobile/src/modules/entry-list/EntryListContentVideo.tsx b/apps/mobile/src/modules/entry-list/EntryListContentVideo.tsx index 958c4d145..f9ccbcc5c 100644 --- a/apps/mobile/src/modules/entry-list/EntryListContentVideo.tsx +++ b/apps/mobile/src/modules/entry-list/EntryListContentVideo.tsx @@ -1,8 +1,8 @@ import { useTypeScriptHappyCallback } from "@follow/hooks" import { usePrefetchEntryTranslation } from "@follow/store/translation/hooks" -import type { MasonryFlashListProps } from "@shopify/flash-list" +import type { FlashList, MasonryFlashListProps } from "@shopify/flash-list" import type { ElementRef } from "react" -import { useImperativeHandle, useMemo } from "react" +import { useImperativeHandle, useMemo, useRef } from "react" import { StyleSheet, View } from "react-native" import { useActionLanguage, useGeneralSettingKey } from "@/src/atoms/settings/general" @@ -13,7 +13,7 @@ import { useHeaderHeight } from "@/src/modules/screen/hooks/useHeaderHeight" import { TimelineSelectorMasonryList } from "../screen/TimelineSelectorList" import { GridEntryListFooter } from "./EntryListFooter" -import { useOnViewableItemsChanged, usePagerListPerformanceHack } from "./hooks" +import { useOnViewableItemsChanged } from "./hooks" import { EntryVideoItem } from "./templates/EntryVideoItem" export const EntryListContentVideo = ({ @@ -25,12 +25,11 @@ export const EntryListContentVideo = ({ MasonryFlashListProps, "data" | "renderItem" > & { ref?: React.Ref | null> }) => { - const { onScroll: hackOnScroll, ref, style: hackStyle } = usePagerListPerformanceHack() + const ref = useRef>(null) useImperativeHandle(forwardRef, () => ref.current!) const { fetchNextPage, refetch, isRefetching, isFetching, hasNextPage, isReady } = useEntries() const { onViewableItemsChanged, onScroll, viewableItems } = useOnViewableItemsChanged({ disabled: active === false || isFetching, - onScroll: hackOnScroll, }) const translation = useGeneralSettingKey("translation") @@ -100,7 +99,6 @@ export const EntryListContentVideo = ({ ListFooterComponent={ListFooterComponent} {...rest} onRefresh={refetch} - style={hackStyle} contentContainerStyle={styles.contentContainer} /> ) diff --git a/apps/mobile/src/modules/entry-list/hooks.ts b/apps/mobile/src/modules/entry-list/hooks.ts index 938846d94..989cbaeb8 100644 --- a/apps/mobile/src/modules/entry-list/hooks.ts +++ b/apps/mobile/src/modules/entry-list/hooks.ts @@ -1,18 +1,12 @@ import { debouncedFetchEntryContentByStream } from "@follow/store/entry/store" import { unreadSyncService } from "@follow/store/unread/store" -import type { FlashList } from "@shopify/flash-list" import type ViewToken from "@shopify/flash-list/dist/viewability/ViewToken" import { fetch as expoFetch } from "expo/fetch" -import type { RefObject } from "react" -import { use, useCallback, useEffect, useInsertionEffect, useMemo, useRef, useState } from "react" -import type { NativeScrollEvent, NativeSyntheticEvent, ViewStyle } from "react-native" -import { useEventCallback } from "usehooks-ts" +import { useCallback, useEffect, useInsertionEffect, useMemo, useRef, useState } from "react" +import type { NativeScrollEvent, NativeSyntheticEvent } from "react-native" import { useGeneralSettingKey } from "@/src/atoms/settings/general" import { getCookie } from "@/src/lib/auth" -import { isAndroid } from "@/src/lib/platform" - -import { PagerListVisibleContext, PagerListWillVisibleContext } from "../screen/PagerListContext" const defaultIdExtractor = (item: ViewToken) => item.key export function useOnViewableItemsChanged({ @@ -127,36 +121,3 @@ function useNonReactiveCallback any>(fn: T): T { [ref], ) as unknown as T } - -export const usePagerListPerformanceHack = (provideRef?: RefObject | null>) => { - const lastY = useRef(0) - - const onScroll = useEventCallback((e: NativeSyntheticEvent) => { - if (!visible) return - - lastY.current = e.nativeEvent.contentOffset.y - }) - - const visible = use(PagerListVisibleContext) - const willVisible = use(PagerListWillVisibleContext) - - const nextVisible = visible || willVisible - - const ref = useRef>(null) - - const usingRef = provideRef ?? ref - const [style, setStyle] = useState({}) - useEffect(() => { - if (!isAndroid) { - // Always show the pager list on Android to avoid blank screen when switching tabs - 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 } -} diff --git a/apps/mobile/src/modules/screen/PagerList.ios.tsx b/apps/mobile/src/modules/screen/PagerList.ios.tsx index 893dc8eb7..7585dd19d 100644 --- a/apps/mobile/src/modules/screen/PagerList.ios.tsx +++ b/apps/mobile/src/modules/screen/PagerList.ios.tsx @@ -4,6 +4,7 @@ import { useViewWithSubscription } from "@follow/store/subscription/hooks" import { EventBus } from "@follow/utils/event-bus" import * as Haptics from "expo-haptics" import { useCallback, useId, useLayoutEffect, useMemo, useRef, useState } from "react" +import { Freeze } from "react-freeze" import type { StyleProp, ViewStyle } from "react-native" import { clamp, withTiming } from "react-native-reanimated" @@ -76,15 +77,20 @@ export function PagerList({ [activeViews, rid], )} renderPage={useTypeScriptHappyCallback( - (index) => ( - - - {renderItem(activeViews[index]!, index === activeViewIndex)} - - - ), + (index) => { + const isActive = index === activeViewIndex + const willVisible = + (index === activeViewIndex + 1 || index === activeViewIndex - 1) && dragging + const freeze = !(isActive || willVisible) + return ( + + + {renderItem(activeViews[index]!, isActive)} + + + ) + }, + [activeViews, activeViewIndex, dragging, renderItem], )} pageTotal={activeViews.length} diff --git a/apps/mobile/src/modules/subscription/SubscriptionLists.tsx b/apps/mobile/src/modules/subscription/SubscriptionLists.tsx index 32c34828a..ac5cafafe 100644 --- a/apps/mobile/src/modules/subscription/SubscriptionLists.tsx +++ b/apps/mobile/src/modules/subscription/SubscriptionLists.tsx @@ -35,7 +35,6 @@ import { selectFeed } from "@/src/modules/screen/atoms" import { TimelineSelectorList } from "@/src/modules/screen/TimelineSelectorList" import { FeedScreen } from "@/src/screens/(stack)/feeds/[feedId]/FeedScreen" -import { usePagerListPerformanceHack } from "../entry-list/hooks" import { useFeedListSortMethod, useFeedListSortOrder } from "./atoms" import { CategoryGrouped } from "./CategoryGrouped" import { InboxItem } from "./items/InboxItem" @@ -129,7 +128,7 @@ const SubscriptionListImpl = ({ return subscriptionSyncService.fetch(view) }) const scrollViewRef = useRegisterNavigationScrollView | null>(active) - const { onScroll, style } = usePagerListPerformanceHack(scrollViewRef) + return ( ) }