From 7f984ecf1ca02e6fdf6e49f3537c7941d364592b Mon Sep 17 00:00:00 2001 From: Whitewater Date: Tue, 20 May 2025 13:05:06 +0800 Subject: [PATCH] feat: implement pull up to next functionality (#3760) * feat: implement pull-up-to-next functionality in EntryDetailScreen * feat: add onScroll callback support in SafeNavigationScrollView * refactor: utilize EntryExtraData * feat: refactor Entry components to utilize EntryExtraData type * refactor: replace useSharedValue with useRef and useState for pull-up functionality * feat: enhance pull-up-to-next functionality with enabled prop * fix: types --- .../views/SafeNavigationScrollView.tsx | 4 +- .../mobile/src/modules/context-menu/feeds.tsx | 6 +- .../entry-content/use-pull-up-to-next.tsx | 88 ++++++ .../entry-list/EntryListContentArticle.tsx | 9 +- .../entry-list/EntryListContentSocial.tsx | 7 +- .../entry-list/templates/EntryNormalItem.tsx | 22 +- .../entry-list/templates/EntrySocialItem.tsx | 264 +++++++++--------- apps/mobile/src/modules/entry-list/types.ts | 4 + .../entries/[entryId]/EntryDetailScreen.tsx | 32 ++- 9 files changed, 292 insertions(+), 144 deletions(-) create mode 100644 apps/mobile/src/modules/entry-content/use-pull-up-to-next.tsx create mode 100644 apps/mobile/src/modules/entry-list/types.ts diff --git a/apps/mobile/src/components/layouts/views/SafeNavigationScrollView.tsx b/apps/mobile/src/components/layouts/views/SafeNavigationScrollView.tsx index bba50ae88..0e9fb9f56 100644 --- a/apps/mobile/src/components/layouts/views/SafeNavigationScrollView.tsx +++ b/apps/mobile/src/components/layouts/views/SafeNavigationScrollView.tsx @@ -94,7 +94,9 @@ export const SafeNavigationScrollView = ({ if (reanimatedScrollY) { reanimatedScrollY.value = event.contentOffset.y } - + if (onScroll) { + runOnJS(onScroll)(event) + } runOnJS(checkScrollToBottom)() screenCtxValue.reAnimatedScrollY.value = event.contentOffset.y }, diff --git a/apps/mobile/src/modules/context-menu/feeds.tsx b/apps/mobile/src/modules/context-menu/feeds.tsx index 58a033768..9aa9697e1 100644 --- a/apps/mobile/src/modules/context-menu/feeds.tsx +++ b/apps/mobile/src/modules/context-menu/feeds.tsx @@ -343,7 +343,11 @@ const PreviewFeeds = (props: { id: string; view: FeedViewType }) => { const renderItem = useCallback( ({ item: id }: ListRenderItemInfo) => ( - + ), [props.view], ) diff --git a/apps/mobile/src/modules/entry-content/use-pull-up-to-next.tsx b/apps/mobile/src/modules/entry-content/use-pull-up-to-next.tsx new file mode 100644 index 000000000..46bc73ddc --- /dev/null +++ b/apps/mobile/src/modules/entry-content/use-pull-up-to-next.tsx @@ -0,0 +1,88 @@ +import * as Haptics from "expo-haptics" +import { useCallback, useRef, useState } from "react" +import type { NativeScrollEvent, NativeSyntheticEvent } from "react-native" +import type { ReanimatedScrollEvent } from "react-native-reanimated/lib/typescript/hook/commonTypes" + +export const usePullUpToNext = ({ + enabled: enabled = true, + onRefresh, + progressViewOffset = 70, +}: { + enabled?: boolean + onRefresh?: (() => void) | undefined + progressViewOffset?: number +} = {}) => { + const dragging = useRef(false) + const isOverThreshold = useRef(false) + const [refreshing, setRefreshing] = useState(false) + + const onScroll = useCallback( + (e: ReanimatedScrollEvent) => { + if (!dragging.current) return + const overOffset = e.contentOffset.y - e.contentSize.height + e.layoutMeasurement.height + + if (overOffset > progressViewOffset) { + if (!isOverThreshold.current && onRefresh) { + Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Heavy) + } + isOverThreshold.current = true + setRefreshing(true) + } else { + if (isOverThreshold.current && onRefresh) { + Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Soft) + } + isOverThreshold.current = false + setRefreshing(false) + } + return + }, + [dragging, onRefresh, progressViewOffset], + ) + + const onScrollBeginDrag = useCallback( + (e: NativeSyntheticEvent) => { + const beginOffset = + e.nativeEvent.contentOffset.y - + e.nativeEvent.contentSize.height + + e.nativeEvent.layoutMeasurement.height + if (beginOffset < -50) { + // Maybe user is pulling down fast for overview + return + } + dragging.current = true + }, + [dragging], + ) + + const onScrollEndDrag = useCallback( + (event: NativeSyntheticEvent) => { + dragging.current = false + const velocity = event.nativeEvent.velocity?.y || 0 + if (isOverThreshold.current && velocity < 3) { + onRefresh?.() + } + isOverThreshold.current = false + setRefreshing(false) + }, + [dragging, onRefresh], + ) + + if (!enabled) { + return { + scrollViewEventHandlers: {}, + pullUpViewProps: {}, + EntryPullUpToNext: () => null, + } + } + + return { + scrollViewEventHandlers: { + onScroll, + onScrollBeginDrag, + onScrollEndDrag, + }, + pullUpViewProps: { + refreshing, + }, + } +} diff --git a/apps/mobile/src/modules/entry-list/EntryListContentArticle.tsx b/apps/mobile/src/modules/entry-list/EntryListContentArticle.tsx index a727bca65..051a301fa 100644 --- a/apps/mobile/src/modules/entry-list/EntryListContentArticle.tsx +++ b/apps/mobile/src/modules/entry-list/EntryListContentArticle.tsx @@ -13,6 +13,7 @@ import { EntryListFooter } from "./EntryListFooter" import { useOnViewableItemsChanged, usePagerListPerformanceHack } from "./hooks" import { ItemSeparator } from "./ItemSeparator" import { EntryNormalItem } from "./templates/EntryNormalItem" +import type { EntryExtraData } from "./types" export const EntryListContentArticle = ({ ref: forwardRef, @@ -23,13 +24,17 @@ export const EntryListContentArticle = ({ ref?: React.Ref | null> }) => { const playingAudioUrl = usePlayingUrl() + const extraData: EntryExtraData = useMemo( + () => ({ playingAudioUrl, entryIds }), + [playingAudioUrl, entryIds], + ) const { fetchNextPage, isFetching, refetch, isRefetching, hasNextPage } = useFetchEntriesControls() const renderItem = useCallback( ({ item: id, extraData }: ListRenderItemInfo) => ( - + ), [view], ) @@ -56,7 +61,7 @@ export const EntryListContentArticle = ({ onRefresh={refetch} isRefetching={isRefetching} data={entryIds} - extraData={playingAudioUrl} + extraData={extraData} keyExtractor={defaultKeyExtractor} estimatedItemSize={100} renderItem={renderItem} diff --git a/apps/mobile/src/modules/entry-list/EntryListContentSocial.tsx b/apps/mobile/src/modules/entry-list/EntryListContentSocial.tsx index f72906040..a8ff9cf10 100644 --- a/apps/mobile/src/modules/entry-list/EntryListContentSocial.tsx +++ b/apps/mobile/src/modules/entry-list/EntryListContentSocial.tsx @@ -11,6 +11,7 @@ import { EntryListFooter } from "./EntryListFooter" import { useOnViewableItemsChanged, usePagerListPerformanceHack } from "./hooks" import { ItemSeparatorFullWidth } from "./ItemSeparator" import { EntrySocialItem } from "./templates/EntrySocialItem" +import type { EntryExtraData } from "./types" export const EntryListContentSocial = ({ ref: forwardRef, @@ -21,12 +22,15 @@ export const EntryListContentSocial = ({ }) => { const { fetchNextPage, isFetching, refetch, isRefetching, hasNextPage } = useFetchEntriesControls() + const extraData: EntryExtraData = useMemo(() => ({ playingAudioUrl: null, entryIds }), [entryIds]) 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) => , + ({ item: id, extraData }: ListRenderItemInfo) => ( + + ), [], ) @@ -50,6 +54,7 @@ export const EntryListContentSocial = ({ }} isRefetching={isRefetching} data={entryIds} + extraData={extraData} keyExtractor={(id) => id} estimatedItemSize={100} renderItem={renderItem} diff --git a/apps/mobile/src/modules/entry-list/templates/EntryNormalItem.tsx b/apps/mobile/src/modules/entry-list/templates/EntryNormalItem.tsx index 4eca6e656..dab310564 100644 --- a/apps/mobile/src/modules/entry-list/templates/EntryNormalItem.tsx +++ b/apps/mobile/src/modules/entry-list/templates/EntryNormalItem.tsx @@ -27,10 +27,19 @@ import { useEntryTranslation } from "@/src/store/translation/hooks" import { EntryItemContextMenu } from "../../context-menu/entry" import { EntryItemSkeleton } from "../EntryListContentArticle" +import type { EntryExtraData } from "../types" import { EntryTranslation } from "./EntryTranslation" export const EntryNormalItem = memo( - ({ entryId, extraData, view }: { entryId: string; extraData: string; view: FeedViewType }) => { + ({ + entryId, + extraData, + view, + }: { + entryId: string + extraData: EntryExtraData + view: FeedViewType + }) => { const entry = useEntry(entryId) const translation = useEntryTranslation(entryId) const from = getInboxFrom(entry) @@ -46,10 +55,11 @@ export const EntryNormalItem = memo( navigation.pushControllerView(EntryDetailScreen, { entryId, + entryIds: extraData.entryIds ?? [], view, }) } - }, [entryId, entry, navigation, view]) + }, [entry, navigation, entryId, extraData.entryIds, view]) const audio = entry?.attachments?.find((attachment) => attachment.mime_type?.startsWith("audio/"), @@ -125,7 +135,7 @@ export const EntryNormalItem = memo( )} {view !== FeedViewType.Notifications && ( - + )} @@ -137,11 +147,11 @@ EntryNormalItem.displayName = "EntryNormalItem" const ThumbnailImage = ({ view, - extraData, + playingAudioUrl, entry, }: { view: FeedViewType - extraData: string + playingAudioUrl: string | null entry: EntryModel }) => { const feed = useFeed(entry?.feedId as string) @@ -152,7 +162,7 @@ const ThumbnailImage = ({ const blurhash = coverImage?.blurhash const audio = entry?.attachments?.find((attachment) => attachment.mime_type?.startsWith("audio/")) - const audioState = getAttachmentState(extraData, audio) + const audioState = getAttachmentState(playingAudioUrl ?? undefined, audio) const isPlaying = audioState === "playing" const isLoading = audioState === "loading" diff --git a/apps/mobile/src/modules/entry-list/templates/EntrySocialItem.tsx b/apps/mobile/src/modules/entry-list/templates/EntrySocialItem.tsx index 5b728d008..20d71484a 100644 --- a/apps/mobile/src/modules/entry-list/templates/EntrySocialItem.tsx +++ b/apps/mobile/src/modules/entry-list/templates/EntrySocialItem.tsx @@ -23,154 +23,160 @@ import { unreadSyncService } from "@/src/store/unread/store" import { EntryItemContextMenu } from "../../context-menu/entry" import { EntryItemSkeleton } from "../EntryListContentSocial" +import type { EntryExtraData } from "../types" import { EntryTranslation } from "./EntryTranslation" -export const EntrySocialItem = memo(({ entryId }: { entryId: string }) => { - const entry = useEntry(entryId) - const translation = useEntryTranslation(entryId) +export const EntrySocialItem = memo( + ({ entryId, extraData }: { entryId: string; extraData: EntryExtraData }) => { + const entry = useEntry(entryId) + const translation = useEntryTranslation(entryId) - const feed = useFeed(entry?.feedId || "") + const feed = useFeed(entry?.feedId || "") - const navigation = useNavigation() - const handlePress = useCallback(() => { - unreadSyncService.markEntryAsRead(entryId) - tracker.navigateEntry({ - feedId: entry?.feedId!, - entryId, - }) - navigation.pushControllerView(EntryDetailScreen, { - entryId, - view: FeedViewType.SocialMedia, - }) - }, [entry?.feedId, entryId, navigation]) + const navigation = useNavigation() + const handlePress = useCallback(() => { + unreadSyncService.markEntryAsRead(entryId) + tracker.navigateEntry({ + feedId: entry?.feedId!, + entryId, + }) + navigation.pushControllerView(EntryDetailScreen, { + entryId, + entryIds: extraData.entryIds ?? [], + view: FeedViewType.SocialMedia, + }) + }, [entry?.feedId, entryId, extraData.entryIds, navigation]) - const autoExpandLongSocialMedia = useGeneralSettingKey("autoExpandLongSocialMedia") + const autoExpandLongSocialMedia = useGeneralSettingKey("autoExpandLongSocialMedia") - const memoedMediaUrlList = useMemo(() => { - return (entry?.media - ?.map((i) => - i.type === "video" ? i.preview_image_url : i.type === "photo" ? i.url : undefined, - ) - .filter(Boolean) || []) as string[] - }, [entry]) + const memoedMediaUrlList = useMemo(() => { + return (entry?.media + ?.map((i) => + i.type === "video" ? i.preview_image_url : i.type === "photo" ? i.url : undefined, + ) + .filter(Boolean) || []) as string[] + }, [entry]) - const navigationToFeedEntryList = useCallback(() => { - if (!entry) return - if (!entry.feedId) return - navigation.pushControllerView(FeedScreen, { - feedId: entry.feedId, - }) - }, [entry?.feedId, navigation]) + const navigationToFeedEntryList = useCallback(() => { + if (!entry) return + if (!entry.feedId) return + navigation.pushControllerView(FeedScreen, { + feedId: entry.feedId, + }) + }, [entry?.feedId, navigation]) - if (!entry) return + if (!entry) return - const { description, publishedAt, media } = entry + const { description, publishedAt, media } = entry - return ( - - - {!entry.read && } + return ( + + + {!entry.read && ( + + )} - - - {entry.authorAvatar ? ( - - ) : ( - feed && - )} - - - + - - {entry.author || feed?.title} - + {entry.authorAvatar ? ( + + ) : ( + feed && + )} - · - + + + + + {entry.author || feed?.title} + + + · + + - - - - + + + - {media && media.length > 0 && ( - - - {media.map((mediaItem, index) => { - const imageUrl = - mediaItem.type === "video" - ? mediaItem.preview_image_url - : mediaItem.type === "photo" - ? mediaItem.url - : undefined - const fullWidth = index === media.length - 1 && media.length % 2 === 1 - if (!imageUrl) return null + {media && media.length > 0 && ( + + + {media.map((mediaItem, index) => { + const imageUrl = + mediaItem.type === "video" + ? mediaItem.preview_image_url + : mediaItem.type === "photo" + ? mediaItem.url + : undefined + const fullWidth = index === media.length - 1 && media.length % 2 === 1 + if (!imageUrl) return null - const ImageItem = ( - - - - ) - - if (mediaItem.type === "video") { - return ( - - + - + ) - } - return ( - - {ImageItem} - - ) - })} - - - )} - - - ) -}) + + if (mediaItem.type === "video") { + return ( + + + + ) + } + return ( + + {ImageItem} + + ) + })} + + + )} + + + ) + }, +) EntrySocialItem.displayName = "EntrySocialItem" diff --git a/apps/mobile/src/modules/entry-list/types.ts b/apps/mobile/src/modules/entry-list/types.ts new file mode 100644 index 000000000..271b4cd35 --- /dev/null +++ b/apps/mobile/src/modules/entry-list/types.ts @@ -0,0 +1,4 @@ +export type EntryExtraData = { + playingAudioUrl: string | null + entryIds: string[] | null +} diff --git a/apps/mobile/src/screens/(stack)/entries/[entryId]/EntryDetailScreen.tsx b/apps/mobile/src/screens/(stack)/entries/[entryId]/EntryDetailScreen.tsx index 2295bdbf7..88cc91755 100644 --- a/apps/mobile/src/screens/(stack)/entries/[entryId]/EntryDetailScreen.tsx +++ b/apps/mobile/src/screens/(stack)/entries/[entryId]/EntryDetailScreen.tsx @@ -1,7 +1,7 @@ import { FeedViewType } from "@follow/constants" import { PortalProvider } from "@gorhom/portal" import { atom, useAtomValue, useSetAtom } from "jotai" -import { useEffect, useMemo } from "react" +import { useCallback, useEffect, useMemo } from "react" import { Text, View } from "react-native" import { useSafeAreaInsets } from "react-native-safe-area-context" import { useColor } from "react-native-uikit-colors" @@ -16,10 +16,12 @@ import { ItemPressableStyle } from "@/src/components/ui/pressable/enum" import { ItemPressable } from "@/src/components/ui/pressable/ItemPressable" import { CalendarTimeAddCuteReIcon } from "@/src/icons/calendar_time_add_cute_re" import { openLink } from "@/src/lib/native" +import { useNavigation } from "@/src/lib/navigation/hooks" import type { NavigationControllerView } from "@/src/lib/navigation/types" import { EntryContentContext, useEntryContentContext } from "@/src/modules/entry-content/ctx" import { EntryAISummary } from "@/src/modules/entry-content/EntryAISummary" import { EntryNavigationHeader } from "@/src/modules/entry-content/EntryNavigationHeader" +import { usePullUpToNext } from "@/src/modules/entry-content/use-pull-up-to-next" import { useEntry, usePrefetchEntryDetail } from "@/src/store/entry/hooks" import { entrySyncServices } from "@/src/store/entry/store" import type { EntryWithTranslation } from "@/src/store/entry/types" @@ -31,8 +33,9 @@ import { EntrySocialTitle, EntryTitle } from "../../../../modules/entry-content/ export const EntryDetailScreen: NavigationControllerView<{ entryId: string + entryIds?: string[] view: FeedViewType -}> = ({ entryId, view: viewType }) => { +}> = ({ entryId, entryIds, view: viewType }) => { useAutoMarkAsRead(entryId) const entry = useEntry(entryId) const translation = useEntryTranslation(entryId) @@ -56,6 +59,26 @@ export const EntryDetailScreen: NavigationControllerView<{ [entry?.settings?.readability, entry?.settings?.summary, entry?.settings?.translation], ) + const navigation = useNavigation() + const nextEntryId = useMemo(() => { + if (!entryIds) return + const currentEntryIdx = entryIds.indexOf(entryId) + return entryIds[currentEntryIdx + 1] + }, [entryId, entryIds]) + + const { scrollViewEventHandlers } = usePullUpToNext({ + enabled: !!nextEntryId, + onRefresh: useCallback(() => { + if (!nextEntryId) return + navigation.back() + navigation.pushControllerView(EntryDetailScreen, { + entryId: nextEntryId, + entryIds, + view: viewType, + }) + }, [entryIds, navigation, nextEntryId, viewType]), + }) + return ( @@ -63,12 +86,13 @@ export const EntryDetailScreen: NavigationControllerView<{ } automaticallyAdjustContentInsets={false} - className="bg-system-background" + contentContainerClassName="flex min-h-full pb-16" + {...scrollViewEventHandlers} > entry?.url && openLink(entry.url)} - className="relative rounded-xl py-4" + className="rounded-xl py-4" > {viewType === FeedViewType.SocialMedia ? (