From 05a22d3f305bb01954a29fcd7d654b060b40cdc1 Mon Sep 17 00:00:00 2001 From: Innei Date: Tue, 15 Apr 2025 19:49:22 +0800 Subject: [PATCH] refactor(ios): replace native PagerView - Added `cssInterop` for styling in `PagerView`. - Updated `onScrollEnd` to pass the current index in `PagerView`. - Introduced `PagerList` component for better feed view management. - Refactored imports to ensure consistency across modules. - Enhanced type definitions for better TypeScript support. Signed-off-by: Innei --- .../src/components/native/PagerView/index.tsx | 60 ++++++++++++------- .../src/components/native/PagerView/specs.ts | 9 ++- apps/mobile/src/modules/entry-list/index.tsx | 2 +- .../src/modules/screen/PagerList.ios.tsx | 58 ++++++++++++++++++ .../screen/{PageList.tsx => PagerList.tsx} | 0 .../screens/(stack)/(tabs)/subscriptions.tsx | 2 +- 6 files changed, 104 insertions(+), 27 deletions(-) create mode 100644 apps/mobile/src/modules/screen/PagerList.ios.tsx rename apps/mobile/src/modules/screen/{PageList.tsx => PagerList.tsx} (100%) diff --git a/apps/mobile/src/components/native/PagerView/index.tsx b/apps/mobile/src/components/native/PagerView/index.tsx index d0a789482..7b60871eb 100644 --- a/apps/mobile/src/components/native/PagerView/index.tsx +++ b/apps/mobile/src/components/native/PagerView/index.tsx @@ -1,4 +1,5 @@ import { cn } from "@follow/utils" +import { cssInterop } from "nativewind" import type { FC, ForwardRefExoticComponent, @@ -6,13 +7,16 @@ import type { ReactNode, RefAttributes, } from "react" -import { forwardRef, useEffect, useRef, useState } from "react" -import type { ViewStyle } from "react-native" +import { forwardRef, useEffect, useImperativeHandle, useRef, useState } from "react" +import type { StyleProp, ViewStyle } from "react-native" import { StyleSheet } from "react-native" import type { PagerRef } from "./specs" import { EnhancePagerView, EnhancePageView } from "./specs" +cssInterop(EnhancePagerView, { + className: "style", +}) interface PagerViewProps { pageContainerStyle?: ViewStyle pageContainerClassName?: string @@ -24,27 +28,30 @@ interface PagerViewProps { onPageChange?: (index: number) => void onScroll?: (percent: number, direction: "left" | "right") => void onScrollBegin?: () => void - onScrollEnd?: () => void + onScrollEnd?: (index: number) => void onPageWillAppear?: (index: number) => void - containerStyle?: ViewStyle + containerStyle?: StyleProp containerClassName?: string } -const PagerViewImpl: FC = ({ - pageContainerStyle, - pageContainerClassName, - renderPage, - pageTotal, - pageGap, - transitionStyle, - containerStyle, - containerClassName, - page, - onPageChange, - onScroll, - onScrollBegin, - onScrollEnd, - onPageWillAppear, -}) => { +const PagerViewImpl: FC = ( + { + pageContainerStyle, + pageContainerClassName, + renderPage, + pageTotal, + pageGap, + transitionStyle, + containerStyle, + containerClassName, + page, + onPageChange, + onScroll, + onScrollBegin, + onScrollEnd, + onPageWillAppear, + }, + ref: any, +) => { const [currentPage, setCurrentPage] = useState(page ?? 0) const nativeRef = useRef(null) @@ -56,6 +63,14 @@ const PagerViewImpl: FC = ({ } } }, [currentPage]) + useImperativeHandle(ref, () => ({ + setPage: (index: number) => { + setCurrentPage(index) + nativeRef.current?.setPage(index) + }, + getPage: () => currentPage, + getState: () => nativeRef.current?.getState(), + })) return ( = ({ onScrollBegin={() => { onScrollBegin?.() }} - onScrollEnd={() => { - onScrollEnd?.() + onScrollEnd={(e) => { + onScrollEnd?.(e.nativeEvent.index) }} onPageWillAppear={(e) => { onPageWillAppear?.(e.nativeEvent.index) }} className={cn("flex-1", containerClassName)} style={containerStyle} - // @ts-expect-error ref={nativeRef} > {Array.from({ length: pageTotal }).map((_, index) => ( diff --git a/apps/mobile/src/components/native/PagerView/specs.ts b/apps/mobile/src/components/native/PagerView/specs.ts index 17cb03d2a..6e13d9903 100644 --- a/apps/mobile/src/components/native/PagerView/specs.ts +++ b/apps/mobile/src/components/native/PagerView/specs.ts @@ -1,14 +1,19 @@ import { requireNativeView } from "expo" import type { NativeSyntheticEvent, ViewProps } from "react-native" -export const EnhancePagerView = requireNativeView("EnhancePagerView") +export const EnhancePagerView = requireNativeView< + ViewProps & + PagerProps & { + ref: React.RefObject + } +>("EnhancePagerView") export const EnhancePageView = requireNativeView("EnhancePageView") export interface PagerProps { onPageChange?: (e: NativeSyntheticEvent<{ index: number }>) => void onScroll?: (e: NativeSyntheticEvent<{ percent: number; direction: "left" | "right" }>) => void onScrollBegin?: () => void - onScrollEnd?: () => void + onScrollEnd?: (e: NativeSyntheticEvent<{ index: number }>) => void onPageWillAppear?: (e: NativeSyntheticEvent<{ index: number }>) => void page?: number pageGap?: number diff --git a/apps/mobile/src/modules/entry-list/index.tsx b/apps/mobile/src/modules/entry-list/index.tsx index c23aa75da..a3cb47ece 100644 --- a/apps/mobile/src/modules/entry-list/index.tsx +++ b/apps/mobile/src/modules/entry-list/index.tsx @@ -2,7 +2,7 @@ import { FeedViewType } from "@follow/constants" import { memo, useMemo } from "react" import { useSelectedFeed, useSelectedView } from "@/src/modules/screen/atoms" -import { PagerList } from "@/src/modules/screen/PageList" +import { PagerList } from "@/src/modules/screen/PagerList" import { TimelineHeader } from "@/src/modules/screen/TimelineSelectorProvider" import { useEntryIdsByCategory, diff --git a/apps/mobile/src/modules/screen/PagerList.ios.tsx b/apps/mobile/src/modules/screen/PagerList.ios.tsx new file mode 100644 index 000000000..d2985a7b9 --- /dev/null +++ b/apps/mobile/src/modules/screen/PagerList.ios.tsx @@ -0,0 +1,58 @@ +import type { FeedViewType } from "@follow/constants" +import { useTypeScriptHappyCallback } from "@follow/hooks" +import { useId, useMemo, useState } from "react" +import type { StyleProp, ViewStyle } from "react-native" + +import { PagerView } from "@/src/components/native/PagerView" +import { useViewWithSubscription } from "@/src/store/subscription/hooks" + +import { selectTimeline, useSelectedFeed } from "./atoms" +import { PagerListVisibleContext, PagerListWillVisibleContext } from "./PagerListContext" + +export function PagerList({ + renderItem, + style, +}: { + renderItem: (view: FeedViewType, active: boolean) => React.ReactNode + style?: StyleProp | undefined +}) { + const selectedFeed = useSelectedFeed() + const viewId = selectedFeed?.type === "view" ? selectedFeed.viewId : undefined + + const activeViews = useViewWithSubscription() + const activeViewIndex = useMemo( + () => activeViews.findIndex((view) => view.view === viewId), + [activeViews, viewId], + ) + const [dragging, setDragging] = useState(false) + const rid = useId() + + return ( + setDragging(true)} + onScrollEnd={() => setDragging(false)} + pageContainerClassName="flex-1" + containerClassName="flex-1 absolute inset-0" + containerStyle={style} + onPageChange={(targetIndex) => { + selectTimeline({ type: "view", viewId: activeViews[targetIndex]!.view }, rid) + }} + renderPage={useTypeScriptHappyCallback( + (index) => ( + + + {renderItem(activeViews[index]!.view, index === activeViewIndex)} + + + ), + [activeViews, activeViewIndex, dragging, renderItem], + )} + pageTotal={activeViews.length} + /> + ) +} diff --git a/apps/mobile/src/modules/screen/PageList.tsx b/apps/mobile/src/modules/screen/PagerList.tsx similarity index 100% rename from apps/mobile/src/modules/screen/PageList.tsx rename to apps/mobile/src/modules/screen/PagerList.tsx diff --git a/apps/mobile/src/screens/(stack)/(tabs)/subscriptions.tsx b/apps/mobile/src/screens/(stack)/(tabs)/subscriptions.tsx index db049f416..d0f54e580 100644 --- a/apps/mobile/src/screens/(stack)/(tabs)/subscriptions.tsx +++ b/apps/mobile/src/screens/(stack)/(tabs)/subscriptions.tsx @@ -11,7 +11,7 @@ import { BlackBoard2CuteFiIcon } from "@/src/icons/black_board_2_cute_fi" import { BlackBoard2CuteReIcon } from "@/src/icons/black_board_2_cute_re" import type { TabScreenComponent } from "@/src/lib/navigation/bottom-tab/types" import { EntryListContext } from "@/src/modules/screen/atoms" -import { PagerList } from "@/src/modules/screen/PageList" +import { PagerList } from "@/src/modules/screen/PagerList" import { TimelineHeader } from "@/src/modules/screen/TimelineSelectorProvider" import { SubscriptionList } from "@/src/modules/subscription/SubscriptionLists" import { useWhoami } from "@/src/store/user/hooks"