From 4f3733eecf681a5d0e68e92b200fbd3bc581f89f Mon Sep 17 00:00:00 2001 From: Joshua Higgins Date: Thu, 5 Nov 2026 17:41:03 -0500 Subject: [PATCH] fix(mobile): animate artwork backgrounds on the UI thread --- .../app/(app)/(tabs)/home/album/[id].tsx | 21 ++++---- .../app/(app)/(tabs)/home/playlist/[id].tsx | 16 +++++-- apps/mobile/components/mini-player.tsx | 7 ++- apps/mobile/components/skeletons.tsx | 14 ++++-- apps/mobile/contexts/android-tab-theme.tsx | 48 +++++++++++++++++-- 5 files changed, 80 insertions(+), 26 deletions(-) diff --git a/apps/mobile/app/(app)/(tabs)/home/album/[id].tsx b/apps/mobile/app/(app)/(tabs)/home/album/[id].tsx index 509b9b9..ba37d59 100644 --- a/apps/mobile/app/(app)/(tabs)/home/album/[id].tsx +++ b/apps/mobile/app/(app)/(tabs)/home/album/[id].tsx @@ -1,16 +1,10 @@ +import Animated from "react-native-reanimated" import { useAndroidScreenPalette } from "@/contexts/android-tab-theme" import { MusicEntityView } from "@/components/music-entity-view" import { Stack, useIsFocused, useLocalSearchParams } from "expo-router" import { StatusBar } from "expo-status-bar" import { memo, useCallback, useEffect, useMemo } from "react" -import { - FlatList, - Platform, - Pressable, - StyleSheet, - Text, - View, -} from "react-native" +import { Platform, Pressable, StyleSheet, Text, View } from "react-native" import { Ionicons } from "@expo/vector-icons" import { @@ -70,7 +64,8 @@ export default function AlbumScreen() { coverUri, album ? album.cover != null : album === null ? false : undefined ) - const { palette, onLayout } = useAndroidScreenPalette(artworkPalette) + const { palette, onLayout, backgroundStyle } = + useAndroidScreenPalette(artworkPalette) const songIds = useMemo( () => album?.tracks.map((track) => track.id as Id<"songs">) ?? [], [album] @@ -145,6 +140,7 @@ export default function AlbumScreen() { /> - { if (songs) rememberSongs(songs) @@ -108,6 +109,7 @@ export default function PlaylistScreen() { /> void + backgroundStyle: ReturnType["backgroundStyle"] playlist: BrowsePlaylistDetail songs: BrowseSong[] coverUri: string @@ -333,13 +338,16 @@ function PlaylistContent({ onQueryChange={onQueryChange} onSearchSubmit={onSearchSubmit} /> - + {children} {hasArtworkPalette ? ( - + ) } diff --git a/apps/mobile/components/skeletons.tsx b/apps/mobile/components/skeletons.tsx index af12d8f..cacb8cc 100644 --- a/apps/mobile/components/skeletons.tsx +++ b/apps/mobile/components/skeletons.tsx @@ -1,4 +1,5 @@ -import { useEffect, useRef, type ReactNode } from "react" +import Reanimated from "react-native-reanimated" +import { useEffect, useRef, type ReactNode, type ComponentProps } from "react" import { Animated, ScrollView, @@ -197,10 +198,12 @@ export function HomeSkeleton() { /** Album and playlist pages: cover, title, play buttons, then tracks. */ export function CollectionDetailSkeleton({ + style, round = false, backgroundColor, onLayout, }: { + style?: ComponentProps["style"] round?: boolean backgroundColor?: string onLayout?: () => void @@ -208,9 +211,12 @@ export function CollectionDetailSkeleton({ const colors = useThemeColors() const coverSize = round ? 180 : 210 return ( - )} - + ) } diff --git a/apps/mobile/contexts/android-tab-theme.tsx b/apps/mobile/contexts/android-tab-theme.tsx index 492c132..5e50641 100644 --- a/apps/mobile/contexts/android-tab-theme.tsx +++ b/apps/mobile/contexts/android-tab-theme.tsx @@ -15,13 +15,21 @@ import { type PropsWithChildren, } from "react" import { Animated, Easing, Platform } from "react-native" -import { interpolateColor, useReducedMotion } from "react-native-reanimated" +import { + interpolateColor, + useReducedMotion, + useSharedValue, + useAnimatedStyle, + withTiming, + type SharedValue, +} from "react-native-reanimated" import { themeCoverPalette, type CoverPalette } from "@/lib/cover-palette" import { useIsDark, useThemeColors } from "@/lib/theme" type TabTheme = { palette: CoverPalette + background: SharedValue selectedColor: string hasArtworkPalette: boolean setPalette: (palette: CoverPalette | null, owner: string) => void @@ -40,6 +48,10 @@ export function AndroidTabThemeProvider({ children }: PropsWithChildren) { palette: CoverPalette owner: string } | null>(null) + const background = useFadingBackground( + screen?.palette.background ?? defaultPalette.background, + defaultPalette.background + ) const palette = useFadingPalette( screen?.palette ?? defaultPalette, defaultPalette @@ -63,11 +75,12 @@ export function AndroidTabThemeProvider({ children }: PropsWithChildren) { const value = useMemo( () => ({ palette: artworkPalette ?? defaultPalette, + background, selectedColor: artworkPalette?.foreground ?? defaultPalette.accentText, hasArtworkPalette: artworkPalette !== null, setPalette, }), - [defaultPalette, artworkPalette, setPalette] + [defaultPalette, artworkPalette, setPalette, background] ) return ( @@ -92,6 +105,25 @@ export function useAndroidTabSelectedColor() { return theme?.selectedColor ?? defaultPalette.accentText } +export function useAndroidTabBackgroundStyle() { + const theme = useContext(AndroidTabTheme) + const fallback = useDefaultPalette().background + return useAnimatedStyle(() => ({ + backgroundColor: theme?.background.value ?? fallback, + })) +} + +/** Run backgrounds on the UI thread even while React is rendering track rows. */ +function useFadingBackground(target: string, initial: string) { + const background = useSharedValue(initial) + useLayoutEffect(() => { + if (Platform.OS === "android") { + background.value = withTiming(target, { duration: PALETTE_FADE_MS }) + } + }, [background, target]) + return background +} + // Match the default Android medium animation time used by slide_from_right. // Continue from displayed colors when artwork arrives late or focus changes. const PALETTE_FADE_MS = 400 @@ -162,9 +194,14 @@ export function useAndroidScreenPalette(palette: CoverPalette) { const defaultPalette = useDefaultPalette() const theme = useContext(AndroidTabTheme) const setPalette = theme?.setPalette - const displayed = useFadingPalette( - isFocused && laidOut && !closing ? palette : defaultPalette, - defaultPalette + const target = isFocused && laidOut && !closing ? palette : defaultPalette + const displayed = useFadingPalette(target, defaultPalette) + const background = useFadingBackground( + target.background, + defaultPalette.background + ) + const backgroundStyle = useAnimatedStyle(() => + Platform.OS === "android" ? { backgroundColor: background.value } : {} ) useLayoutEffect(() => { @@ -182,6 +219,7 @@ export function useAndroidScreenPalette(palette: CoverPalette) { return { palette: displayed, + backgroundStyle, onLayout: Platform.OS === "android" ? onLayout : undefined, } }