feat(mobile): cache image info (#2899)

This commit is contained in:
Stephen Zhou
2025-02-27 11:10:32 +08:00
committed by GitHub
parent bd12076b21
commit a67d84b7d7
15 changed files with 825 additions and 75 deletions
@@ -5,6 +5,7 @@ import { Pressable, Text, View } from "react-native"
import Animated, { SlideInDown, SlideOutDown } from "react-native-reanimated"
import { useActiveTrack } from "@/src/lib/player"
import { usePrefetchImageColors } from "@/src/store/image/hooks"
import { PlayPauseButton, SeekButton } from "./control"
@@ -13,6 +14,9 @@ const allowedRoutes = new Set(["/", "/subscriptions", "/player"])
export function PlayerTabBar({ className }: { className?: string }) {
const activeTrack = useActiveTrack()
const pathname = usePathname()
usePrefetchImageColors(activeTrack?.artwork)
if (!activeTrack || !allowedRoutes.has(pathname)) {
return null
}
+29
View File
@@ -0,0 +1,29 @@
import { getLuminance, shadeColor } from "@follow/utils"
import { useMemo } from "react"
import { useImageColors } from "@/src/store/image/hooks"
const defaultBackgroundColor = "#000000"
export function useCoverGradient(url?: string) {
const imageColors = useImageColors(url)
const backgroundColor = useMemo(() => {
if (imageColors?.platform === "ios") {
return imageColors.background
} else if (imageColors?.platform === "android") {
return imageColors.average
}
return defaultBackgroundColor
}, [imageColors])
const isGradientLight = useMemo(() => {
return getLuminance(backgroundColor) > 0.5
}, [backgroundColor])
const gradientColors = useMemo(() => {
return [backgroundColor, shadeColor(backgroundColor, -50)] as const
}, [backgroundColor])
return { isGradientLight, gradientColors }
}
@@ -1,63 +1,47 @@
import { cn, getLuminance } from "@follow/utils"
import { LinearGradient } from "expo-linear-gradient"
import { useEffect, useState } from "react"
import { useMemo } from "react"
import { StyleSheet, Text, View } from "react-native"
import ImageColors from "react-native-image-colors"
import type { SharedValue } from "react-native-reanimated"
import ReAnimated, { FadeIn, FadeOut, interpolate, useAnimatedStyle } from "react-native-reanimated"
import { useSafeAreaInsets } from "react-native-safe-area-context"
import { UserAvatar } from "@/src/components/ui/avatar/UserAvatar"
import { useImageColors, usePrefetchImageColors } from "@/src/store/image/hooks"
import { useWhoami } from "@/src/store/user/hooks"
import { accentColor } from "@/src/theme/colors"
const defaultGradientColors = ["#000", "#100", "#200"]
export const UserHeaderBanner = ({ scrollY }: { scrollY: SharedValue<number> }) => {
const whoami = useWhoami()
usePrefetchImageColors(whoami?.image)
const insets = useSafeAreaInsets()
const MAX_PULL = 100
const SCALE_FACTOR = 1.8
const [gradientColors, setGradientColors] = useState<string[]>()
const [gradientLight, setGradientLight] = useState<boolean>(false)
useEffect(() => {
const extractColors = async () => {
if (!whoami?.image) return
try {
const result = await ImageColors.getColors(whoami.image, {
fallback: accentColor,
cache: true,
})
if (result.platform === "web") return
if (result.platform === "android") {
setGradientColors([
result.dominant,
result.average || result.vibrant,
result.vibrant || result.dominant,
])
const dominantLuminance = getLuminance(result.dominant)
const isLight = dominantLuminance > 0.5
setGradientLight(isLight)
} else {
const dominantLuminance = getLuminance(result.primary)
const isLight = dominantLuminance > 0.5
setGradientLight(isLight)
setGradientColors([result.primary, result.secondary, result.background])
}
} catch (error) {
console.warn("Failed to extract colors:", error)
}
const imageColors = useImageColors(whoami?.image)
const gradientColors = useMemo(() => {
if (!imageColors || imageColors.platform === "web") return defaultGradientColors
if (imageColors.platform === "android") {
return [
imageColors.dominant,
imageColors.average || imageColors.vibrant,
imageColors.vibrant || imageColors.dominant,
]
}
return [imageColors.primary, imageColors.secondary, imageColors.background]
}, [imageColors])
const gradientLight = useMemo(() => {
if (!imageColors) return false
if (imageColors.platform === "web") return false
const dominantLuminance = getLuminance(
imageColors.platform === "android" ? imageColors.dominant : imageColors.primary,
)
return dominantLuminance > 0.5
}, [imageColors])
extractColors()
}, [whoami?.image])
const styles = useAnimatedStyle(() => {
const scaleValue = interpolate(scrollY.value, [-MAX_PULL, 0], [SCALE_FACTOR, 1], {
extrapolateLeft: "extend",