From 1d4e37e35a36eaeacbea70723464a3759131cd2b Mon Sep 17 00:00:00 2001 From: Innei Date: Mon, 17 Feb 2025 23:42:10 +0800 Subject: [PATCH] feat(mobile): add preview image component with advanced gesture handling Introduce a new PreviewImage component with sophisticated gesture interactions for mobile image preview: - Implement pinch-to-zoom functionality - Add pan and double-tap gestures for image manipulation - Support smooth animations for zooming and closing - Handle image scaling, translation, and modal transitions - Refactor entry list components to use new preview image component Signed-off-by: Innei --- .../src/components/ui/image/PreviewImage.tsx | 405 ++++++++++++++++++ .../entry-list/EntryListContentArticle.tsx | 60 +++ .../entry-list/EntryListContentGrid.tsx | 37 ++ .../entry-list/EntryListContentSocial.tsx | 69 +++ .../{entry-list.tsx => EntryListSelector.tsx} | 8 +- .../modules/entry-list/entry-list-gird.tsx | 92 ---- apps/mobile/src/modules/entry-list/index.tsx | 12 +- .../entry-list/templates/EntryGridItem.tsx | 135 ++++++ .../EntryNormalItem.tsx} | 63 +-- .../EntrySocialItem.tsx} | 72 +--- .../modules/screen/TimelineSelectorList.tsx | 93 ++-- .../screens/(stack)/feeds/[feedId]/index.tsx | 4 +- 12 files changed, 773 insertions(+), 277 deletions(-) create mode 100644 apps/mobile/src/components/ui/image/PreviewImage.tsx create mode 100644 apps/mobile/src/modules/entry-list/EntryListContentArticle.tsx create mode 100644 apps/mobile/src/modules/entry-list/EntryListContentGrid.tsx create mode 100644 apps/mobile/src/modules/entry-list/EntryListContentSocial.tsx rename apps/mobile/src/modules/entry-list/{entry-list.tsx => EntryListSelector.tsx} (71%) delete mode 100644 apps/mobile/src/modules/entry-list/entry-list-gird.tsx create mode 100644 apps/mobile/src/modules/entry-list/templates/EntryGridItem.tsx rename apps/mobile/src/modules/entry-list/{entry-list-article.tsx => templates/EntryNormalItem.tsx} (57%) rename apps/mobile/src/modules/entry-list/{entry-list-social.tsx => templates/EntrySocialItem.tsx} (66%) diff --git a/apps/mobile/src/components/ui/image/PreviewImage.tsx b/apps/mobile/src/components/ui/image/PreviewImage.tsx new file mode 100644 index 000000000..77931d401 --- /dev/null +++ b/apps/mobile/src/components/ui/image/PreviewImage.tsx @@ -0,0 +1,405 @@ +import { Image } from "expo-image" +import { useEffect, useState } from "react" +import { Dimensions, Modal, Pressable, useWindowDimensions, View } from "react-native" +import { Gesture, GestureDetector, GestureHandlerRootView } from "react-native-gesture-handler" +import Animated, { + runOnJS, + useAnimatedStyle, + useSharedValue, + withSpring, + withTiming, +} from "react-native-reanimated" + +import { gentleSpringPreset } from "@/src/constants/spring" +import { CloseCuteReIcon } from "@/src/icons/close_cute_re" + +interface PreviewImageProps { + imageUrl: string + blurhash?: string | undefined + aspectRatio: number +} + +export const PreviewImage = ({ imageUrl, blurhash, aspectRatio }: PreviewImageProps) => { + const { width, height } = useWindowDimensions() + const [previewModalOpen, setPreviewModalOpen] = useState(false) + + const [imageRef, setImageRef] = useState(null) + const layoutScale = useSharedValue(1) + const layoutTransformX = useSharedValue(0) + const layoutTransformY = useSharedValue(0) + + const scale = useSharedValue(1) + const savedScale = useSharedValue(1) + const pinchCenter = useSharedValue({ x: 0, y: 0 }) + const translateX = useSharedValue(0) + const translateY = useSharedValue(0) + const savedTranslateX = useSharedValue(0) + const savedTranslateY = useSharedValue(0) + const pinchGesture = Gesture.Pinch() + .onStart((e) => { + pinchCenter.value = { x: e.focalX, y: e.focalY } + }) + .onUpdate((e) => { + scale.value = savedScale.value * e.scale + // Limit scale to 1 to 3 + scale.value = Math.min(Math.max(scale.value, 1), 3) + }) + .onEnd(() => { + savedScale.value = scale.value + }) + + // Animation closing state + const isClosing = useSharedValue(false) + + // Animation opacity value + const opacity = useSharedValue(1) + + // Overlay opacity animation value + const overlayOpacity = useSharedValue(0) + + const panGesture = Gesture.Pan() + .runOnJS(true) + .onUpdate((e) => { + if (isClosing.value) return + + if (scale.value === 1 && e.translationY > 0) { + translateY.value = savedTranslateY.value + e.translationY * 0.5 + translateX.value = savedTranslateX.value + e.translationX * 0.5 + const progress = Math.abs(e.translationY) / (height * 2) + scale.value = Math.max(0.8, 1 - progress) + opacity.value = Math.max(0.3, 1 - progress * 1.5) + // Update overlay opacity simultaneously + overlayOpacity.value = Math.max(0, 0.9 - progress * 1.5) + } else { + translateX.value = savedTranslateX.value + e.translationX + translateY.value = savedTranslateY.value + e.translationY + } + }) + .onEnd((e) => { + if (isClosing.value) return + + if (scale.value <= 1 && e.translationY > 100) { + isClosing.value = true + + // Measure original image position + imageRef?.measureInWindow((pageX, pageY, w1, h1) => { + const duration = 300 + + overlayOpacity.value = withTiming(0, { + duration, + }) + + opacity.value = withTiming(0, { + duration, + }) + + const { width: w2 } = Dimensions.get("window") + const s = w2 / w1 + + const x1 = pageX + w1 / 2 + const y1 = pageY + h1 / 2 + const x2 = w2 / 2 + const y2 = height / 2 + + const xDelta = (x1 - x2) * s + const yDelta = (y1 - y2) * s + + layoutScale.value = withTiming( + 1 / s, + { + duration, + }, + (finished) => { + if (finished) { + runOnJS(setPreviewModalOpen)(false) + } + }, + ) + layoutTransformX.value = withTiming(xDelta, { + duration, + }) + layoutTransformY.value = withTiming(yDelta, { + duration, + }) + + scale.value = withTiming(1, { + duration, + }) + translateX.value = withTiming(0, { + duration, + }) + translateY.value = withTiming(0, { + duration, + }) + }) + } else { + // Restore opacity + opacity.value = withTiming(1, { + duration: 100, + }) + overlayOpacity.value = withTiming(0.9, { + duration: 100, + }) + // Boundary check logic + const scaledWidth = width * scale.value + const scaledHeight = height * aspectRatio * scale.value + + // Calculate maximum allowed offset + const maxOffsetX = Math.max(0, (scaledWidth - width) / 2) + const maxOffsetY = Math.max(0, (scaledHeight - height) / 2) + + // Calculate target position (with bounce effect) + const targetX = Math.min(Math.max(translateX.value, -maxOffsetX), maxOffsetX) + const targetY = Math.min(Math.max(translateY.value, -maxOffsetY), maxOffsetY) + + // If scale is less than or equal to 1, force center + if (scale.value <= 1) { + translateX.value = withSpring(0, { + damping: 15, + stiffness: 150, + }) + translateY.value = withSpring(0, { + damping: 15, + stiffness: 150, + }) + savedTranslateX.value = 0 + savedTranslateY.value = 0 + } else { + translateX.value = withSpring(targetX, { + damping: 15, + stiffness: 150, + }) + translateY.value = withSpring(targetY, { + damping: 15, + stiffness: 150, + }) + savedTranslateX.value = targetX + savedTranslateY.value = targetY + } + } + }) + + const singleTapGesture = Gesture.Tap() + .numberOfTaps(1) + .runOnJS(true) + .onStart(() => { + if (scale.value <= 1) { + fadeOutCloseAnimation() + } + }) + + const doubleTapGesture = Gesture.Tap() + .numberOfTaps(2) + .onStart((e) => { + if (scale.value !== 1) { + // If current scale is not 1, reset to 1 and center + scale.value = withSpring(1, { + damping: 15, + stiffness: 150, + }) + savedScale.value = 1 + translateX.value = withSpring(0, { + damping: 15, + stiffness: 150, + }) + translateY.value = withSpring(0, { + damping: 15, + stiffness: 150, + }) + savedTranslateX.value = 0 + savedTranslateY.value = 0 + } else { + // If current scale is 1, zoom to 1.5x at double tap location + const targetScale = 1.5 + scale.value = withSpring(targetScale, { + damping: 15, + stiffness: 150, + }) + savedScale.value = targetScale + + // Calculate offset from double tap point to center + + const centerX = width / 2 + const centerY = height / 2 + + // Calculate required offset to make double tap point the new center + const targetX = (centerX - e.absoluteX) * targetScale + const targetY = (centerY - e.absoluteY) * targetScale + + translateX.value = withSpring(targetX, { + damping: 15, + stiffness: 150, + }) + translateY.value = withSpring(targetY, { + damping: 15, + stiffness: 150, + }) + savedTranslateX.value = targetX + savedTranslateY.value = targetY + } + }) + + const composed = Gesture.Simultaneous( + Gesture.Race(doubleTapGesture, panGesture, singleTapGesture), + pinchGesture, + ) + + const animatedStyle = useAnimatedStyle(() => ({ + transform: [ + { translateX: translateX.value }, + { translateY: translateY.value }, + { scale: scale.value }, + ], + })) + + // 更新动画样式 + const modalStyle = useAnimatedStyle(() => ({ + opacity: opacity.value, + })) + + const overlayStyle = useAnimatedStyle(() => ({ + backgroundColor: "black", + opacity: overlayOpacity.value, + })) + + useEffect(() => { + let timer: NodeJS.Timeout | null = null + if (!previewModalOpen) { + timer = setTimeout(() => { + resetValues() + }, 300) + } + return () => { + if (timer) { + clearTimeout(timer) + } + } + }, [previewModalOpen]) + + const resetValues = () => { + "worklet" + layoutScale.value = 1 + layoutTransformX.value = 0 + layoutTransformY.value = 0 + scale.value = 1 + savedScale.value = 1 + translateX.value = 0 + translateY.value = 0 + savedTranslateX.value = 0 + savedTranslateY.value = 0 + opacity.value = 1 + isClosing.value = false + overlayOpacity.value = 0 + } + + const fadeOutCloseAnimation = () => { + scale.value = withSpring(2, gentleSpringPreset, (finished) => { + if (finished) { + runOnJS(setPreviewModalOpen)(false) + } + }) + opacity.value = withSpring(0, gentleSpringPreset) + overlayOpacity.value = withSpring(0, gentleSpringPreset) + } + return ( + <> + { + imageRef?.measureInWindow((pageX, pageY, w1, h1) => { + setPreviewModalOpen(true) + + // 初始化 overlay 透明度 + overlayOpacity.value = 0 + requestAnimationFrame(() => { + overlayOpacity.value = withSpring(0.9, gentleSpringPreset) + }) + + const { width: w2, height: screenHeight } = Dimensions.get("window") + + const scale = w2 / w1 + + const x1 = pageX + w1 / 2 + const y1 = pageY + h1 / 2 + + const x2 = w2 / 2 + const y2 = screenHeight / 2 + + const xDelta = (x1 - x2) * scale + const yDelta = (y1 - y2) * scale + + layoutScale.value = 1 / scale + layoutTransformX.value = xDelta + layoutTransformY.value = yDelta + + requestAnimationFrame(() => { + layoutScale.value = withSpring(1, gentleSpringPreset) + layoutTransformX.value = withSpring(0, gentleSpringPreset) + layoutTransformY.value = withSpring(0, gentleSpringPreset) + }) + }) + }} + > + + + + + + + + + + + + + + + + + + + + + + + + + + ) +} diff --git a/apps/mobile/src/modules/entry-list/EntryListContentArticle.tsx b/apps/mobile/src/modules/entry-list/EntryListContentArticle.tsx new file mode 100644 index 000000000..20d1eaa2c --- /dev/null +++ b/apps/mobile/src/modules/entry-list/EntryListContentArticle.tsx @@ -0,0 +1,60 @@ +import type { ListRenderItemInfo } from "@shopify/flash-list" +import { useCallback, useMemo } from "react" +import { View } from "react-native" + +import { useFetchEntriesControls } from "../screen/atoms" +import { TimelineSelectorList } from "../screen/TimelineSelectorList" +import { useOnViewableItemsChanged } from "./hooks" +import { ItemSeparator } from "./ItemSeparator" +import { EntryNormalItem } from "./templates/EntryNormalItem" + +export function EntryListContentArticle({ entryIds }: { entryIds: string[] }) { + const { fetchNextPage, isFetching, refetch, isRefetching } = useFetchEntriesControls() + + const renderItem = useCallback( + ({ item: id }: ListRenderItemInfo) => , + [], + ) + + const ListFooterComponent = useMemo( + () => (isFetching ? : null), + [isFetching], + ) + + const onViewableItemsChanged = useOnViewableItemsChanged() + + return ( + { + refetch() + }} + isRefetching={isRefetching} + data={entryIds} + keyExtractor={(id) => id} + estimatedItemSize={100} + renderItem={renderItem} + onEndReached={() => { + fetchNextPage() + }} + onViewableItemsChanged={onViewableItemsChanged} + ItemSeparatorComponent={ItemSeparator} + ListFooterComponent={ListFooterComponent} + /> + ) +} + +export function EntryItemSkeleton() { + return ( + + + {/* Title skeleton */} + + {/* Description skeleton */} + + + + {/* Image skeleton */} + + + ) +} diff --git a/apps/mobile/src/modules/entry-list/EntryListContentGrid.tsx b/apps/mobile/src/modules/entry-list/EntryListContentGrid.tsx new file mode 100644 index 000000000..fa1fa74ab --- /dev/null +++ b/apps/mobile/src/modules/entry-list/EntryListContentGrid.tsx @@ -0,0 +1,37 @@ +import { useTypeScriptHappyCallback } from "@follow/hooks" +import type { MasonryFlashListProps } from "@shopify/flash-list" + +import { useFetchEntriesControls } from "@/src/modules/screen/atoms" + +import { TimelineSelectorMasonryList } from "../screen/TimelineSelectorList" +import { useOnViewableItemsChanged } from "./hooks" +import { EntryGridItem } from "./templates/EntryGridItem" + +export function EntryListContentGrid({ + entryIds, + ...rest +}: { + entryIds: string[] +} & Omit, "data" | "renderItem">) { + const { fetchNextPage, refetch, isRefetching } = useFetchEntriesControls() + const onViewableItemsChanged = useOnViewableItemsChanged() + + return ( + { + return + }, [])} + keyExtractor={(id) => id} + onViewableItemsChanged={onViewableItemsChanged} + onEndReached={() => { + fetchNextPage() + }} + numColumns={2} + estimatedItemSize={100} + {...rest} + onRefresh={refetch} + /> + ) +} diff --git a/apps/mobile/src/modules/entry-list/EntryListContentSocial.tsx b/apps/mobile/src/modules/entry-list/EntryListContentSocial.tsx new file mode 100644 index 000000000..d38cbc5db --- /dev/null +++ b/apps/mobile/src/modules/entry-list/EntryListContentSocial.tsx @@ -0,0 +1,69 @@ +import type { ListRenderItemInfo } from "@shopify/flash-list" +import { useCallback, useMemo } from "react" +import { View } from "react-native" + +import { useFetchEntriesControls } from "../screen/atoms" +import { TimelineSelectorList } from "../screen/TimelineSelectorList" +import { useOnViewableItemsChanged } from "./hooks" +import { ItemSeparatorFullWidth } from "./ItemSeparator" +import { EntrySocialItem } from "./templates/EntrySocialItem" + +export function EntryListContentSocial({ entryIds }: { entryIds: string[] }) { + const { fetchNextPage, isFetching, refetch, isRefetching } = useFetchEntriesControls() + + const renderItem = useCallback( + ({ item: id }: ListRenderItemInfo) => , + [], + ) + + const ListFooterComponent = useMemo( + () => (isFetching ? : null), + [isFetching], + ) + + const onViewableItemsChanged = useOnViewableItemsChanged() + + return ( + { + refetch() + }} + isRefetching={isRefetching} + data={entryIds} + keyExtractor={(id) => id} + estimatedItemSize={100} + renderItem={renderItem} + onEndReached={() => { + fetchNextPage() + }} + onViewableItemsChanged={onViewableItemsChanged} + ItemSeparatorComponent={ItemSeparatorFullWidth} + ListFooterComponent={ListFooterComponent} + /> + ) +} + +export function EntryItemSkeleton() { + return ( + + {/* Header row with avatar, author, and date */} + + + + + + + {/* Description area */} + + + + + + {/* Media preview area */} + + + + + + ) +} diff --git a/apps/mobile/src/modules/entry-list/entry-list.tsx b/apps/mobile/src/modules/entry-list/EntryListSelector.tsx similarity index 71% rename from apps/mobile/src/modules/entry-list/entry-list.tsx rename to apps/mobile/src/modules/entry-list/EntryListSelector.tsx index a1e132925..d66a5484b 100644 --- a/apps/mobile/src/modules/entry-list/entry-list.tsx +++ b/apps/mobile/src/modules/entry-list/EntryListSelector.tsx @@ -1,13 +1,13 @@ import { FeedViewType } from "@follow/constants" -import { EntryListContentGrid } from "@/src/modules/entry-list/entry-list-gird" +import { EntryListContentGrid } from "@/src/modules/entry-list/EntryListContentGrid" import { useSelectedView } from "@/src/modules/screen/atoms" import { TimelineSelectorHeader } from "../screen/TimelineSelectorHeader" -import { EntryListContentArticle } from "./entry-list-article" -import { EntryListContentSocial } from "./entry-list-social" +import { EntryListContentArticle } from "./EntryListContentArticle" +import { EntryListContentSocial } from "./EntryListContentSocial" -export function EntryListScreen({ entryIds }: { entryIds: string[] }) { +export function EntryListSelector({ entryIds }: { entryIds: string[] }) { const view = useSelectedView() let ContentComponent = EntryListContentArticle diff --git a/apps/mobile/src/modules/entry-list/entry-list-gird.tsx b/apps/mobile/src/modules/entry-list/entry-list-gird.tsx deleted file mode 100644 index 37a9b3722..000000000 --- a/apps/mobile/src/modules/entry-list/entry-list-gird.tsx +++ /dev/null @@ -1,92 +0,0 @@ -import { FeedViewType } from "@follow/constants" -import { useTypeScriptHappyCallback } from "@follow/hooks" -import type { MasonryFlashListProps } from "@shopify/flash-list" -import { Image } from "expo-image" -import { Link } from "expo-router" -import { Pressable, View } from "react-native" - -import { ThemedText } from "@/src/components/common/ThemedText" -import { ItemPressable } from "@/src/components/ui/pressable/ItemPressable" -import { useFetchEntriesControls, useSelectedView } from "@/src/modules/screen/atoms" -import { useEntry } from "@/src/store/entry/hooks" - -import { TimelineSelectorMasonryList } from "../screen/TimelineSelectorList" -import { useOnViewableItemsChanged } from "./hooks" - -export function EntryListContentGrid({ - entryIds, - ...rest -}: { - entryIds: string[] -} & Omit, "data" | "renderItem">) { - const { fetchNextPage, refetch, isRefetching } = useFetchEntriesControls() - const onViewableItemsChanged = useOnViewableItemsChanged() - - return ( - { - refetch() - }) as any - } - data={entryIds} - renderItem={useTypeScriptHappyCallback(({ item }) => { - return - }, [])} - keyExtractor={(id) => id} - onViewableItemsChanged={onViewableItemsChanged} - onEndReached={() => { - fetchNextPage() - }} - numColumns={2} - estimatedItemSize={100} - {...rest} - /> - ) -} - -function RenderEntryItem({ id }: { id: string }) { - const view = useSelectedView() - const item = useEntry(id) - if (!item) { - return null - } - const photo = item.media?.find((media) => media.type === "photo") - const video = item.media?.find((media) => media.type === "video") - const imageUrl = photo?.url || video?.preview_image_url - const blurhash = photo?.blurhash || video?.blurhash - const aspectRatio = - view === FeedViewType.Pictures && photo?.height && photo.width - ? photo.width / photo.height - : 16 / 9 - - return ( - - - - {imageUrl ? ( - - ) : ( - - No media available - - )} - - - - {item.title} - - ) -} diff --git a/apps/mobile/src/modules/entry-list/index.tsx b/apps/mobile/src/modules/entry-list/index.tsx index 724cf2928..2d60f1f89 100644 --- a/apps/mobile/src/modules/entry-list/index.tsx +++ b/apps/mobile/src/modules/entry-list/index.tsx @@ -11,7 +11,7 @@ import { } from "@/src/store/entry/hooks" import { useListEntryIds } from "@/src/store/list/hooks" -import { EntryListScreen } from "./entry-list" +import { EntryListSelector } from "./EntryListSelector" export function EntryList() { const setDrawerSwipeDisabled = useSetDrawerSwipeDisabled() @@ -49,26 +49,26 @@ export function EntryList() { function ViewEntryList({ viewId }: { viewId: FeedViewType }) { const entryIds = useEntryIdsByView(viewId) - return + return } function FeedEntryList({ feedId }: { feedId: string }) { const entryIds = useEntryIdsByFeedId(feedId) - return + return } function CategoryEntryList({ categoryName }: { categoryName: string }) { const entryIds = useEntryIdsByCategory(categoryName) - return + return } function ListEntryList({ listId }: { listId: string }) { const entryIds = useListEntryIds(listId) if (!entryIds) return null - return + return } function InboxEntryList({ inboxId }: { inboxId: string }) { const entryIds = useEntryIdsByInboxId(inboxId) - return + return } diff --git a/apps/mobile/src/modules/entry-list/templates/EntryGridItem.tsx b/apps/mobile/src/modules/entry-list/templates/EntryGridItem.tsx new file mode 100644 index 000000000..73a196a2b --- /dev/null +++ b/apps/mobile/src/modules/entry-list/templates/EntryGridItem.tsx @@ -0,0 +1,135 @@ +import { FeedViewType } from "@follow/constants" +import { Image } from "expo-image" +import { useState } from "react" +import { Modal, Pressable, Text, View } from "react-native" +import { useSharedValue } from "react-native-reanimated" + +import { ReAnimatedPressable } from "@/src/components/common/AnimatedComponents" +import { PreviewImage } from "@/src/components/ui/image/PreviewImage" +import { ItemPressable } from "@/src/components/ui/pressable/ItemPressable" +import { CloseCuteReIcon } from "@/src/icons/close_cute_re" +import { useEntry } from "@/src/store/entry/hooks" + +import { useSelectedView } from "../../screen/atoms" + +export function EntryGridItem({ id }: { id: string }) { + const view = useSelectedView() + const item = useEntry(id) + + const [previewModalOpen, setPreviewModalOpen] = useState(false) + const scaleValue = useSharedValue(1) + const transformXValue = useSharedValue(0) + const transformYValue = useSharedValue(0) + + if (!item) { + return null + } + const photo = item.media?.find((media) => media.type === "photo") + const video = item.media?.find((media) => media.type === "video") + const imageUrl = photo?.url || video?.preview_image_url + const blurhash = photo?.blurhash || video?.blurhash + const aspectRatio = + view === FeedViewType.Pictures && photo?.height && photo.width + ? photo.width / photo.height + : 16 / 9 + + return ( + <> + + + setPreviewModalOpen(false)} + > + + + + + + + + { + // // router.push(`/entries/${item.id}`) + + // imageRef?.measureInWindow((pageX, pageY, w1, h1) => { + // setPreviewModalOpen(true) + + // const { width: w2, height: screenHeight } = Dimensions.get("window") + + // const scale = w2 / w1 + + // const x1 = pageX + w1 / 2 + // const y1 = pageY + h1 / 2 + + // const x2 = w2 / 2 + // const y2 = screenHeight / 2 + + // const xDelta = (x1 - x2) * scale + // const yDelta = (y1 - y2) * scale + + // scaleValue.value = 1 / scale + // transformXValue.value = xDelta + // transformYValue.value = yDelta + + // requestAnimationFrame(() => { + // scaleValue.value = withTiming(1) + // transformXValue.value = withTiming(0) + // transformYValue.value = withTiming(0) + // }) + // }) + // }} + > + {imageUrl ? ( + // + // + // + + ) : ( + + No media available + + )} + + {view === FeedViewType.Videos && ( + + {item.title} + + )} + + + ) +} diff --git a/apps/mobile/src/modules/entry-list/entry-list-article.tsx b/apps/mobile/src/modules/entry-list/templates/EntryNormalItem.tsx similarity index 57% rename from apps/mobile/src/modules/entry-list/entry-list-article.tsx rename to apps/mobile/src/modules/entry-list/templates/EntryNormalItem.tsx index 813a1b442..039c7a48a 100644 --- a/apps/mobile/src/modules/entry-list/entry-list-article.tsx +++ b/apps/mobile/src/modules/entry-list/templates/EntryNormalItem.tsx @@ -1,7 +1,6 @@ -import type { ListRenderItemInfo } from "@shopify/flash-list" import { Image } from "expo-image" import { router } from "expo-router" -import { useCallback, useEffect, useMemo } from "react" +import { useCallback, useEffect } from "react" import { Text, View } from "react-native" import ReAnimated, { useAnimatedStyle, useSharedValue, withSpring } from "react-native-reanimated" @@ -12,48 +11,10 @@ import { gentleSpringPreset } from "@/src/constants/spring" import { useEntry } from "@/src/store/entry/hooks" import { useFeed } from "@/src/store/feed/hooks" -import { EntryItemContextMenu } from "../context-menu/entry" -import { useFetchEntriesControls } from "../screen/atoms" -import { TimelineSelectorList } from "../screen/TimelineSelectorList" -import { useOnViewableItemsChanged } from "./hooks" -import { ItemSeparator } from "./ItemSeparator" +import { EntryItemContextMenu } from "../../context-menu/entry" +import { EntryItemSkeleton } from "../EntryListContentArticle" -export function EntryListContentArticle({ entryIds }: { entryIds: string[] }) { - const { fetchNextPage, isFetching, refetch, isRefetching } = useFetchEntriesControls() - - const renderItem = useCallback( - ({ item: id }: ListRenderItemInfo) => , - [], - ) - - const ListFooterComponent = useMemo( - () => (isFetching ? : null), - [isFetching], - ) - - const onViewableItemsChanged = useOnViewableItemsChanged() - - return ( - { - refetch() - }} - isRefetching={isRefetching} - data={entryIds} - keyExtractor={(id) => id} - estimatedItemSize={100} - renderItem={renderItem} - onEndReached={() => { - fetchNextPage() - }} - onViewableItemsChanged={onViewableItemsChanged} - ItemSeparatorComponent={ItemSeparator} - ListFooterComponent={ListFooterComponent} - /> - ) -} - -function EntryItem({ entryId }: { entryId: string }) { +export function EntryNormalItem({ entryId }: { entryId: string }) { const entry = useEntry(entryId) const feed = useFeed(entry?.feedId as string) @@ -122,19 +83,3 @@ function EntryItem({ entryId }: { entryId: string }) { ) } - -function EntryItemSkeleton() { - return ( - - - {/* Title skeleton */} - - {/* Description skeleton */} - - - - {/* Image skeleton */} - - - ) -} diff --git a/apps/mobile/src/modules/entry-list/entry-list-social.tsx b/apps/mobile/src/modules/entry-list/templates/EntrySocialItem.tsx similarity index 66% rename from apps/mobile/src/modules/entry-list/entry-list-social.tsx rename to apps/mobile/src/modules/entry-list/templates/EntrySocialItem.tsx index 9461c9626..bf6a52014 100644 --- a/apps/mobile/src/modules/entry-list/entry-list-social.tsx +++ b/apps/mobile/src/modules/entry-list/templates/EntrySocialItem.tsx @@ -1,9 +1,8 @@ import { FeedViewType } from "@follow/constants" import { cn } from "@follow/utils" -import type { ListRenderItemInfo } from "@shopify/flash-list" import { Image } from "expo-image" import { router } from "expo-router" -import { useCallback, useEffect, useMemo, useState } from "react" +import { useCallback, useEffect, useState } from "react" import { Pressable, Text, View } from "react-native" import ReAnimated, { useAnimatedStyle, useSharedValue, withSpring } from "react-native-reanimated" @@ -19,48 +18,10 @@ import { useEntry } from "@/src/store/entry/hooks" import { useFeed } from "@/src/store/feed/hooks" import { unreadSyncService } from "@/src/store/unread/store" -import { EntryItemContextMenu } from "../context-menu/entry" -import { useFetchEntriesControls } from "../screen/atoms" -import { TimelineSelectorList } from "../screen/TimelineSelectorList" -import { useOnViewableItemsChanged } from "./hooks" -import { ItemSeparatorFullWidth } from "./ItemSeparator" +import { EntryItemContextMenu } from "../../context-menu/entry" +import { EntryItemSkeleton } from "../EntryListContentSocial" -export function EntryListContentSocial({ entryIds }: { entryIds: string[] }) { - const { fetchNextPage, isFetching, refetch, isRefetching } = useFetchEntriesControls() - - const renderItem = useCallback( - ({ item: id }: ListRenderItemInfo) => , - [], - ) - - const ListFooterComponent = useMemo( - () => (isFetching ? : null), - [isFetching], - ) - - const onViewableItemsChanged = useOnViewableItemsChanged() - - return ( - { - refetch() - }} - isRefetching={isRefetching} - data={entryIds} - keyExtractor={(id) => id} - estimatedItemSize={100} - renderItem={renderItem} - onEndReached={() => { - fetchNextPage() - }} - onViewableItemsChanged={onViewableItemsChanged} - ItemSeparatorComponent={ItemSeparatorFullWidth} - ListFooterComponent={ListFooterComponent} - /> - ) -} - -function EntryItem({ entryId }: { entryId: string }) { +export function EntrySocialItem({ entryId }: { entryId: string }) { const entry = useEntry(entryId) const feed = useFeed(entry?.feedId || "") @@ -186,28 +147,3 @@ function EntryItem({ entryId }: { entryId: string }) { ) } - -function EntryItemSkeleton() { - return ( - - {/* Header row with avatar, author, and date */} - - - - - - - {/* Description area */} - - - - - - {/* Media preview area */} - - - - - - ) -} diff --git a/apps/mobile/src/modules/screen/TimelineSelectorList.tsx b/apps/mobile/src/modules/screen/TimelineSelectorList.tsx index c953fa84a..499b176db 100644 --- a/apps/mobile/src/modules/screen/TimelineSelectorList.tsx +++ b/apps/mobile/src/modules/screen/TimelineSelectorList.tsx @@ -17,63 +17,64 @@ type Props = { isRefetching: boolean } -export const TimelineSelectorList = forwardRef, Props & FlashListProps>( - ({ onRefresh, isRefetching, ...props }, ref) => { - const { refetch: unreadRefetch } = usePrefetchUnread() - const { refetch: subscriptionRefetch } = usePrefetchSubscription() +export const TimelineSelectorList = forwardRef< + FlashList, + Props & Omit, "onRefresh"> +>(({ onRefresh, isRefetching, ...props }, ref) => { + const { refetch: unreadRefetch } = usePrefetchUnread() + const { refetch: subscriptionRefetch } = usePrefetchSubscription() - const insets = useSafeAreaInsets() + const insets = useSafeAreaInsets() - const headerHeight = useHeaderHeight() - const scrollY = useContext(NavigationContext)?.scrollY + const headerHeight = useHeaderHeight() + const scrollY = useContext(NavigationContext)?.scrollY - const onScroll = useCallback( - (e: NativeSyntheticEvent) => { - scrollY?.setValue(e.nativeEvent.contentOffset.y) - }, - [scrollY], - ) + const onScroll = useCallback( + (e: NativeSyntheticEvent) => { + scrollY?.setValue(e.nativeEvent.contentOffset.y) + }, + [scrollY], + ) - const tabBarHeight = useBottomTabBarHeight() + const tabBarHeight = useBottomTabBarHeight() - const systemFill = useColor("secondaryLabel") + const systemFill = useColor("secondaryLabel") - return ( - { - unreadRefetch() - subscriptionRefetch() - onRefresh() - }} - refreshing={isRefetching} - /> - } - onScroll={onScroll} - scrollIndicatorInsets={{ - top: headerHeight - insets.top, - bottom: tabBarHeight ? tabBarHeight - insets.bottom : undefined, - }} - contentContainerStyle={{ - paddingTop: headerHeight, - paddingBottom: tabBarHeight, - }} - {...props} - /> - ) - }, -) + return ( + { + unreadRefetch() + subscriptionRefetch() + onRefresh() + }} + refreshing={isRefetching} + /> + } + onScroll={onScroll} + scrollIndicatorInsets={{ + top: headerHeight - insets.top, + bottom: tabBarHeight ? tabBarHeight - insets.bottom : undefined, + }} + contentContainerStyle={{ + paddingTop: headerHeight, + paddingBottom: tabBarHeight, + }} + {...props} + /> + ) +}) export const TimelineSelectorMasonryList = ({ onRefresh, isRefetching, ...props -}: Props & MasonryFlashListProps) => { +}: Props & Omit, "onRefresh">) => { const { refetch: unreadRefetch } = usePrefetchUnread() const { refetch: subscriptionRefetch } = usePrefetchSubscription() diff --git a/apps/mobile/src/screens/(stack)/feeds/[feedId]/index.tsx b/apps/mobile/src/screens/(stack)/feeds/[feedId]/index.tsx index 2e6cd747d..dd3bd0b62 100644 --- a/apps/mobile/src/screens/(stack)/feeds/[feedId]/index.tsx +++ b/apps/mobile/src/screens/(stack)/feeds/[feedId]/index.tsx @@ -3,7 +3,7 @@ import { useLocalSearchParams } from "expo-router" import { useMemo } from "react" import { useSafeAreaInsets } from "react-native-safe-area-context" -import { EntryListScreen } from "@/src/modules/entry-list/entry-list" +import { EntryListSelector } from "@/src/modules/entry-list/EntryListSelector" import { EntryListContext } from "@/src/modules/screen/atoms" import { useEntryIdsByCategory, useEntryIdsByFeedId } from "@/src/store/entry/hooks" @@ -15,7 +15,7 @@ export default function Feed() { return ( ({ type: "feed" }), [])}> - 0 ? entryIdsByFeedId : entryIdsByCategory} />