fix(mobile): animate artwork backgrounds on the UI thread

This commit is contained in:
joshuafhiggins committed 2026-11-05 17:41:03 -05:00 Verified
1 parent 08e742ef45
commit 4f3733eecf
5 files changed
+80 -26

No files matched your search

@@ -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() {
/>
<CollectionDetailSkeleton
onLayout={onLayout}
style={backgroundStyle}
backgroundColor={
Platform.OS === "android" ? palette.background : undefined
}
@@ -218,12 +214,15 @@ export default function AlbumScreen() {
),
}}
/>
<FlatList
<Animated.FlatList
onLayout={onLayout}
{...longListProps}
data={listItems}
keyExtractor={albumListItemKey}
style={{ flex: 1, backgroundColor: palette.background }}
style={[
{ flex: 1, backgroundColor: palette.background },
backgroundStyle,
]}
contentContainerStyle={{ paddingBottom: contentBottomInset }}
contentInsetAdjustmentBehavior="automatic"
indicatorStyle={palette.isDark ? "white" : "black"}
@@ -1,3 +1,4 @@
import Animated from "react-native-reanimated"
import {
PlaylistSearchBar,
usePlaylistSearch,
@@ -14,7 +15,6 @@ import {
} from "expo-router"
import { StatusBar } from "expo-status-bar"
import {
FlatList,
Platform,
Pressable,
StyleSheet,
@@ -85,7 +85,8 @@ export default function PlaylistScreen() {
coverUri,
playlist ? playlist.cover != null : playlist === null ? false : undefined
)
const { palette, onLayout } = useAndroidScreenPalette(artworkPalette)
const { palette, onLayout, backgroundStyle } =
useAndroidScreenPalette(artworkPalette)
useEffect(() => {
if (songs) rememberSongs(songs)
@@ -108,6 +109,7 @@ export default function PlaylistScreen() {
/>
<CollectionDetailSkeleton
onLayout={onLayout}
style={backgroundStyle}
backgroundColor={
Platform.OS === "android" ? palette.background : undefined
}
@@ -136,6 +138,7 @@ export default function PlaylistScreen() {
return (
<PlaylistContent
backgroundStyle={backgroundStyle}
onLayout={onLayout}
playlist={playlist}
songs={playlist.songs}
@@ -146,6 +149,7 @@ export default function PlaylistScreen() {
}
function PlaylistContent({
backgroundStyle,
onLayout,
playlist,
songs,
@@ -153,6 +157,7 @@ function PlaylistContent({
palette,
}: {
onLayout?: () => void
backgroundStyle: ReturnType<typeof useAndroidScreenPalette>["backgroundStyle"]
playlist: BrowsePlaylistDetail
songs: BrowseSong[]
coverUri: string
@@ -333,13 +338,16 @@ function PlaylistContent({
onQueryChange={onQueryChange}
onSearchSubmit={onSearchSubmit}
/>
<FlatList
<Animated.FlatList
onLayout={onLayout}
{...longListProps}
data={filteredSongs}
keyExtractor={songKey}
renderItem={renderItem}
style={{ flex: 1, backgroundColor: palette.background }}
style={[
{ flex: 1, backgroundColor: palette.background },
backgroundStyle,
]}
indicatorStyle={palette.isDark ? "white" : "black"}
contentContainerStyle={styles.listContent}
contentInsetAdjustmentBehavior="automatic"
+5 -2
View File
@@ -1,5 +1,7 @@
import Animated from "react-native-reanimated"
import {
useAndroidTabPalette,
useAndroidTabBackgroundStyle,
useAndroidTabHasArtworkPalette,
} from "@/contexts/android-tab-theme"
import { Ionicons } from "@expo/vector-icons"
@@ -167,9 +169,10 @@ function MiniPlayerContent({
function MiniPlayerChrome({ children }: { children: React.ReactNode }) {
const palette = useAndroidTabPalette()
const hasArtworkPalette = useAndroidTabHasArtworkPalette()
const backgroundStyle = useAndroidTabBackgroundStyle()
return (
<View style={[styles.androidBar, { backgroundColor: palette.background }]}>
<Animated.View style={[styles.androidBar, backgroundStyle]}>
{children}
{hasArtworkPalette ? (
<View
@@ -187,7 +190,7 @@ function MiniPlayerChrome({ children }: { children: React.ReactNode }) {
{ backgroundColor: palette.secondaryForeground },
]}
/>
</View>
</Animated.View>
)
}
+10 -4
View File
@@ -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<typeof Reanimated.ScrollView>["style"]
round?: boolean
backgroundColor?: string
onLayout?: () => void
@@ -208,9 +211,12 @@ export function CollectionDetailSkeleton({
const colors = useThemeColors()
const coverSize = round ? 180 : 210
return (
<ScrollView
<Reanimated.ScrollView
onLayout={onLayout}
style={{ flex: 1, backgroundColor: backgroundColor ?? colors.background }}
style={[
{ flex: 1, backgroundColor: backgroundColor ?? colors.background },
style,
]}
contentContainerStyle={{ paddingBottom: contentBottomInset }}
contentInsetAdjustmentBehavior="automatic"
scrollEnabled={false}
@@ -237,7 +243,7 @@ export function CollectionDetailSkeleton({
) : (
<SongListSkeleton rows={8} embedded />
)}
</ScrollView>
</Reanimated.ScrollView>
)
}
+43 -5
View File
@@ -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<string>
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 (
<AndroidTabTheme.Provider value={value}>
@@ -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,
}
}