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 <tukon479@gmail.com>
This commit is contained in:
Innei
2025-02-17 23:42:10 +08:00
parent c8f580c0a1
commit 1d4e37e35a
12 changed files with 773 additions and 277 deletions
@@ -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<View | null>(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 (
<>
<Pressable
onPress={() => {
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)
})
})
}}
>
<View ref={setImageRef}>
<Image
source={{ uri: imageUrl }}
placeholder={{
blurhash,
}}
className="w-full"
style={{
aspectRatio,
}}
placeholderContentFit="cover"
recyclingKey={imageUrl}
/>
</View>
</Pressable>
<Modal transparent visible={previewModalOpen}>
<Animated.View style={overlayStyle} className="absolute inset-0" />
<Animated.View style={modalStyle} className="w-full flex-1">
<GestureHandlerRootView className="w-full flex-1">
<View className="flex-1 items-center justify-center">
<Pressable
className="absolute right-2 top-safe-offset-2"
onPress={fadeOutCloseAnimation}
>
<CloseCuteReIcon color="#fff" />
</Pressable>
<GestureDetector gesture={composed}>
<Animated.View style={animatedStyle}>
<Animated.View
style={{
transform: [
{
scale: layoutScale,
},
{
translateX: layoutTransformX,
},
{
translateY: layoutTransformY,
},
],
}}
>
<Image
source={{ uri: imageUrl }}
className="w-full"
style={{
aspectRatio,
}}
placeholder={{
blurhash,
}}
/>
</Animated.View>
</Animated.View>
</GestureDetector>
</View>
</GestureHandlerRootView>
</Animated.View>
</Modal>
</>
)
}
@@ -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<string>) => <EntryNormalItem key={id} entryId={id} />,
[],
)
const ListFooterComponent = useMemo(
() => (isFetching ? <EntryItemSkeleton /> : null),
[isFetching],
)
const onViewableItemsChanged = useOnViewableItemsChanged()
return (
<TimelineSelectorList
onRefresh={() => {
refetch()
}}
isRefetching={isRefetching}
data={entryIds}
keyExtractor={(id) => id}
estimatedItemSize={100}
renderItem={renderItem}
onEndReached={() => {
fetchNextPage()
}}
onViewableItemsChanged={onViewableItemsChanged}
ItemSeparatorComponent={ItemSeparator}
ListFooterComponent={ListFooterComponent}
/>
)
}
export function EntryItemSkeleton() {
return (
<View className="bg-secondary-system-grouped-background flex flex-row items-center p-4">
<View className="flex flex-1 flex-col justify-between">
{/* Title skeleton */}
<View className="bg-system-fill h-6 w-3/4 animate-pulse rounded-md" />
{/* Description skeleton */}
<View className="bg-system-fill mt-2 w-full flex-1 animate-pulse rounded-md" />
</View>
{/* Image skeleton */}
<View className="bg-system-fill ml-2 size-20 animate-pulse rounded-md" />
</View>
)
}
@@ -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<MasonryFlashListProps<string>, "data" | "renderItem">) {
const { fetchNextPage, refetch, isRefetching } = useFetchEntriesControls()
const onViewableItemsChanged = useOnViewableItemsChanged()
return (
<TimelineSelectorMasonryList
isRefetching={isRefetching}
data={entryIds}
renderItem={useTypeScriptHappyCallback(({ item }) => {
return <EntryGridItem id={item} />
}, [])}
keyExtractor={(id) => id}
onViewableItemsChanged={onViewableItemsChanged}
onEndReached={() => {
fetchNextPage()
}}
numColumns={2}
estimatedItemSize={100}
{...rest}
onRefresh={refetch}
/>
)
}
@@ -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<string>) => <EntrySocialItem key={id} entryId={id} />,
[],
)
const ListFooterComponent = useMemo(
() => (isFetching ? <EntryItemSkeleton /> : null),
[isFetching],
)
const onViewableItemsChanged = useOnViewableItemsChanged()
return (
<TimelineSelectorList
onRefresh={() => {
refetch()
}}
isRefetching={isRefetching}
data={entryIds}
keyExtractor={(id) => id}
estimatedItemSize={100}
renderItem={renderItem}
onEndReached={() => {
fetchNextPage()
}}
onViewableItemsChanged={onViewableItemsChanged}
ItemSeparatorComponent={ItemSeparatorFullWidth}
ListFooterComponent={ListFooterComponent}
/>
)
}
export function EntryItemSkeleton() {
return (
<View className="flex flex-col gap-2 p-4">
{/* Header row with avatar, author, and date */}
<View className="flex flex-1 flex-row items-center gap-2">
<View className="bg-system-fill size-8 animate-pulse rounded-full" />
<View className="bg-system-fill h-4 w-24 animate-pulse rounded-md" />
<View className="bg-system-fill h-3 w-20 animate-pulse rounded-md" />
</View>
{/* Description area */}
<View className="ml-10 space-y-2">
<View className="bg-system-fill h-4 w-full animate-pulse rounded-md rounded-bl-none" />
<View className="bg-system-fill h-4 w-3/4 animate-pulse rounded-md rounded-tl-none" />
</View>
{/* Media preview area */}
<View className="ml-10 flex flex-row gap-2">
<View className="bg-system-fill size-20 animate-pulse rounded-md" />
<View className="bg-system-fill size-20 animate-pulse rounded-md" />
</View>
</View>
)
}
@@ -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
@@ -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<MasonryFlashListProps<string>, "data" | "renderItem">) {
const { fetchNextPage, refetch, isRefetching } = useFetchEntriesControls()
const onViewableItemsChanged = useOnViewableItemsChanged()
return (
<TimelineSelectorMasonryList
isRefetching={isRefetching}
onRefresh={
(() => {
refetch()
}) as any
}
data={entryIds}
renderItem={useTypeScriptHappyCallback(({ item }) => {
return <RenderEntryItem id={item} />
}, [])}
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 (
<ItemPressable className="m-1 overflow-hidden rounded-md">
<Link href={`/entries/${item.id}`} asChild>
<Pressable>
{imageUrl ? (
<Image
source={{ uri: imageUrl }}
placeholder={{
blurhash,
}}
style={{
width: "100%",
aspectRatio,
}}
placeholderContentFit="cover"
recyclingKey={imageUrl}
/>
) : (
<View className="aspect-video w-full items-center justify-center">
<ThemedText className="text-center">No media available</ThemedText>
</View>
)}
</Pressable>
</Link>
<ThemedText className="p-2">{item.title}</ThemedText>
</ItemPressable>
)
}
+6 -6
View File
@@ -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 <EntryListScreen entryIds={entryIds} />
return <EntryListSelector entryIds={entryIds} />
}
function FeedEntryList({ feedId }: { feedId: string }) {
const entryIds = useEntryIdsByFeedId(feedId)
return <EntryListScreen entryIds={entryIds} />
return <EntryListSelector entryIds={entryIds} />
}
function CategoryEntryList({ categoryName }: { categoryName: string }) {
const entryIds = useEntryIdsByCategory(categoryName)
return <EntryListScreen entryIds={entryIds} />
return <EntryListSelector entryIds={entryIds} />
}
function ListEntryList({ listId }: { listId: string }) {
const entryIds = useListEntryIds(listId)
if (!entryIds) return null
return <EntryListScreen entryIds={entryIds} />
return <EntryListSelector entryIds={entryIds} />
}
function InboxEntryList({ inboxId }: { inboxId: string }) {
const entryIds = useEntryIdsByInboxId(inboxId)
return <EntryListScreen entryIds={entryIds} />
return <EntryListSelector entryIds={entryIds} />
}
@@ -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 (
<>
<Modal
transparent
className="absolute inset-0 flex flex-1 items-center justify-center bg-white/20"
visible={previewModalOpen}
>
<View className="flex-1 items-center justify-center">
<Pressable
className="absolute right-2 top-safe-offset-2"
onPress={() => setPreviewModalOpen(false)}
>
<CloseCuteReIcon />
</Pressable>
<ReAnimatedPressable
style={{
transform: [
{ scale: scaleValue },
{ translateX: transformXValue },
{ translateY: transformYValue },
],
}}
>
<Image
source={{ uri: imageUrl }}
className="w-full"
style={{
aspectRatio,
}}
placeholder={{
blurhash,
}}
/>
</ReAnimatedPressable>
</View>
</Modal>
<ItemPressable
className="m-1 overflow-hidden rounded-md"
// onPress={() => {
// // 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 ? (
// <View ref={setImageRef}>
// <Image
// source={{ uri: imageUrl }}
// placeholder={{
// blurhash,
// }}
// className="w-full"
// style={{
// aspectRatio,
// }}
// placeholderContentFit="cover"
// recyclingKey={imageUrl}
// />
// </View>
<PreviewImage imageUrl={imageUrl} blurhash={blurhash} aspectRatio={aspectRatio} />
) : (
<View className="aspect-video w-full items-center justify-center">
<Text className="text-label text-center">No media available</Text>
</View>
)}
{view === FeedViewType.Videos && (
<Text className="text-label p-2 font-medium" numberOfLines={2}>
{item.title}
</Text>
)}
</ItemPressable>
</>
)
}
@@ -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<string>) => <EntryItem key={id} entryId={id} />,
[],
)
const ListFooterComponent = useMemo(
() => (isFetching ? <EntryItemSkeleton /> : null),
[isFetching],
)
const onViewableItemsChanged = useOnViewableItemsChanged()
return (
<TimelineSelectorList
onRefresh={() => {
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 }) {
</EntryItemContextMenu>
)
}
function EntryItemSkeleton() {
return (
<View className="bg-secondary-system-grouped-background flex flex-row items-center p-4">
<View className="flex flex-1 flex-col justify-between">
{/* Title skeleton */}
<View className="bg-system-fill h-6 w-3/4 animate-pulse rounded-md" />
{/* Description skeleton */}
<View className="bg-system-fill mt-2 w-full flex-1 animate-pulse rounded-md" />
</View>
{/* Image skeleton */}
<View className="bg-system-fill ml-2 size-20 animate-pulse rounded-md" />
</View>
)
}
@@ -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<string>) => <EntryItem key={id} entryId={id} />,
[],
)
const ListFooterComponent = useMemo(
() => (isFetching ? <EntryItemSkeleton /> : null),
[isFetching],
)
const onViewableItemsChanged = useOnViewableItemsChanged()
return (
<TimelineSelectorList
onRefresh={() => {
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 }) {
</EntryItemContextMenu>
)
}
function EntryItemSkeleton() {
return (
<View className="flex flex-col gap-2 p-4">
{/* Header row with avatar, author, and date */}
<View className="flex flex-1 flex-row items-center gap-2">
<View className="bg-system-fill size-8 animate-pulse rounded-full" />
<View className="bg-system-fill h-4 w-24 animate-pulse rounded-md" />
<View className="bg-system-fill h-3 w-20 animate-pulse rounded-md" />
</View>
{/* Description area */}
<View className="ml-10 space-y-2">
<View className="bg-system-fill h-4 w-full animate-pulse rounded-md rounded-bl-none" />
<View className="bg-system-fill h-4 w-3/4 animate-pulse rounded-md rounded-tl-none" />
</View>
{/* Media preview area */}
<View className="ml-10 flex flex-row gap-2">
<View className="bg-system-fill size-20 animate-pulse rounded-md" />
<View className="bg-system-fill size-20 animate-pulse rounded-md" />
</View>
</View>
)
}
@@ -17,63 +17,64 @@ type Props = {
isRefetching: boolean
}
export const TimelineSelectorList = forwardRef<FlashList<any>, Props & FlashListProps<any>>(
({ onRefresh, isRefetching, ...props }, ref) => {
const { refetch: unreadRefetch } = usePrefetchUnread()
const { refetch: subscriptionRefetch } = usePrefetchSubscription()
export const TimelineSelectorList = forwardRef<
FlashList<any>,
Props & Omit<FlashListProps<any>, "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<NativeScrollEvent>) => {
scrollY?.setValue(e.nativeEvent.contentOffset.y)
},
[scrollY],
)
const onScroll = useCallback(
(e: NativeSyntheticEvent<NativeScrollEvent>) => {
scrollY?.setValue(e.nativeEvent.contentOffset.y)
},
[scrollY],
)
const tabBarHeight = useBottomTabBarHeight()
const tabBarHeight = useBottomTabBarHeight()
const systemFill = useColor("secondaryLabel")
const systemFill = useColor("secondaryLabel")
return (
<FlashList
ref={ref}
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()
onRefresh()
}}
refreshing={isRefetching}
/>
}
onScroll={onScroll}
scrollIndicatorInsets={{
top: headerHeight - insets.top,
bottom: tabBarHeight ? tabBarHeight - insets.bottom : undefined,
}}
contentContainerStyle={{
paddingTop: headerHeight,
paddingBottom: tabBarHeight,
}}
{...props}
/>
)
},
)
return (
<FlashList
ref={ref}
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()
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<any>) => {
}: Props & Omit<MasonryFlashListProps<any>, "onRefresh">) => {
const { refetch: unreadRefetch } = usePrefetchUnread()
const { refetch: subscriptionRefetch } = usePrefetchSubscription()
@@ -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 (
<EntryListContext.Provider value={useMemo(() => ({ type: "feed" }), [])}>
<BottomTabBarHeightContext.Provider value={insets.bottom}>
<EntryListScreen
<EntryListSelector
entryIds={entryIdsByFeedId.length > 0 ? entryIdsByFeedId : entryIdsByCategory}
/>
</BottomTabBarHeightContext.Provider>