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 <tukon479@gmail.com>
This commit is contained in:
@@ -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<ViewStyle>
|
||||
containerClassName?: string
|
||||
}
|
||||
const PagerViewImpl: FC<PagerViewProps> = ({
|
||||
pageContainerStyle,
|
||||
pageContainerClassName,
|
||||
renderPage,
|
||||
pageTotal,
|
||||
pageGap,
|
||||
transitionStyle,
|
||||
containerStyle,
|
||||
containerClassName,
|
||||
page,
|
||||
onPageChange,
|
||||
onScroll,
|
||||
onScrollBegin,
|
||||
onScrollEnd,
|
||||
onPageWillAppear,
|
||||
}) => {
|
||||
const PagerViewImpl: FC<PagerViewProps> = (
|
||||
{
|
||||
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<PagerRef>(null)
|
||||
@@ -56,6 +63,14 @@ const PagerViewImpl: FC<PagerViewProps> = ({
|
||||
}
|
||||
}
|
||||
}, [currentPage])
|
||||
useImperativeHandle(ref, () => ({
|
||||
setPage: (index: number) => {
|
||||
setCurrentPage(index)
|
||||
nativeRef.current?.setPage(index)
|
||||
},
|
||||
getPage: () => currentPage,
|
||||
getState: () => nativeRef.current?.getState(),
|
||||
}))
|
||||
return (
|
||||
<EnhancePagerView
|
||||
transitionStyle={transitionStyle}
|
||||
@@ -70,15 +85,14 @@ const PagerViewImpl: FC<PagerViewProps> = ({
|
||||
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) => (
|
||||
|
||||
@@ -1,14 +1,19 @@
|
||||
import { requireNativeView } from "expo"
|
||||
import type { NativeSyntheticEvent, ViewProps } from "react-native"
|
||||
|
||||
export const EnhancePagerView = requireNativeView<ViewProps & PagerProps>("EnhancePagerView")
|
||||
export const EnhancePagerView = requireNativeView<
|
||||
ViewProps &
|
||||
PagerProps & {
|
||||
ref: React.RefObject<PagerRef>
|
||||
}
|
||||
>("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
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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<ViewStyle> | 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 (
|
||||
<PagerView
|
||||
onScrollBegin={() => 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) => (
|
||||
<PagerListVisibleContext.Provider
|
||||
value={index === activeViewIndex}
|
||||
key={activeViews[index]!.view}
|
||||
>
|
||||
<PagerListWillVisibleContext.Provider
|
||||
value={(index === activeViewIndex + 1 || index === activeViewIndex - 1) && dragging}
|
||||
>
|
||||
{renderItem(activeViews[index]!.view, index === activeViewIndex)}
|
||||
</PagerListWillVisibleContext.Provider>
|
||||
</PagerListVisibleContext.Provider>
|
||||
),
|
||||
[activeViews, activeViewIndex, dragging, renderItem],
|
||||
)}
|
||||
pageTotal={activeViews.length}
|
||||
/>
|
||||
)
|
||||
}
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user