fix(mobile): fade artwork colors during Android navigation
This commit is contained in:
6 files changed
+124
-29
No files matched your search
@@ -135,9 +135,20 @@ export default function AlbumScreen() {
|
||||
options={{
|
||||
title: "",
|
||||
headerLargeTitle: false,
|
||||
...(Platform.OS === "android"
|
||||
? {
|
||||
headerStyle: { backgroundColor: palette.background },
|
||||
headerTintColor: palette.foreground,
|
||||
}
|
||||
: {}),
|
||||
}}
|
||||
/>
|
||||
<CollectionDetailSkeleton />
|
||||
<CollectionDetailSkeleton
|
||||
onLayout={onLayout}
|
||||
backgroundColor={
|
||||
Platform.OS === "android" ? palette.background : undefined
|
||||
}
|
||||
/>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -98,9 +98,20 @@ export default function PlaylistScreen() {
|
||||
options={{
|
||||
title: "",
|
||||
headerLargeTitle: false,
|
||||
...(Platform.OS === "android"
|
||||
? {
|
||||
headerStyle: { backgroundColor: palette.background },
|
||||
headerTintColor: palette.foreground,
|
||||
}
|
||||
: {}),
|
||||
}}
|
||||
/>
|
||||
<CollectionDetailSkeleton />
|
||||
<CollectionDetailSkeleton
|
||||
onLayout={onLayout}
|
||||
backgroundColor={
|
||||
Platform.OS === "android" ? palette.background : undefined
|
||||
}
|
||||
/>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import { Ionicons } from "@expo/vector-icons"
|
||||
import { Image, type ImageContentFit, type ImageStyle } from "expo-image"
|
||||
import { memo, useMemo } from "react"
|
||||
import {
|
||||
Platform,
|
||||
PixelRatio,
|
||||
StyleProp,
|
||||
StyleSheet,
|
||||
@@ -9,6 +10,8 @@ import {
|
||||
type ViewStyle,
|
||||
} from "react-native"
|
||||
|
||||
import { prefetchCoverPalette } from "@/hooks/use-cover-palette"
|
||||
|
||||
import { useThemeColors } from "@/lib/theme"
|
||||
import {
|
||||
coverImageSizes,
|
||||
@@ -106,6 +109,9 @@ export const CoverImage = memo(function CoverImage({
|
||||
contentFit={contentFit}
|
||||
transition={150}
|
||||
blurRadius={blurRadius}
|
||||
onLoad={
|
||||
Platform.OS === "android" ? () => prefetchCoverPalette(uri) : undefined
|
||||
}
|
||||
cachePolicy="memory-disk"
|
||||
recyclingKey={sourceUri}
|
||||
enforceEarlyResizing
|
||||
|
||||
@@ -198,14 +198,19 @@ export function HomeSkeleton() {
|
||||
/** Album and playlist pages: cover, title, play buttons, then tracks. */
|
||||
export function CollectionDetailSkeleton({
|
||||
round = false,
|
||||
backgroundColor,
|
||||
onLayout,
|
||||
}: {
|
||||
round?: boolean
|
||||
backgroundColor?: string
|
||||
onLayout?: () => void
|
||||
}) {
|
||||
const colors = useThemeColors()
|
||||
const coverSize = round ? 180 : 210
|
||||
return (
|
||||
<ScrollView
|
||||
style={{ flex: 1, backgroundColor: colors.background }}
|
||||
onLayout={onLayout}
|
||||
style={{ flex: 1, backgroundColor: backgroundColor ?? colors.background }}
|
||||
contentContainerStyle={{ paddingBottom: contentBottomInset }}
|
||||
contentInsetAdjustmentBehavior="automatic"
|
||||
scrollEnabled={false}
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import {
|
||||
useIsFocused,
|
||||
useNavigation,
|
||||
usePreventRemove,
|
||||
useRoute,
|
||||
useNavigation,
|
||||
type NativeStackNavigationProp,
|
||||
} from "expo-router"
|
||||
import {
|
||||
@@ -12,9 +11,11 @@ import {
|
||||
useLayoutEffect,
|
||||
useMemo,
|
||||
useState,
|
||||
useRef,
|
||||
type PropsWithChildren,
|
||||
} from "react"
|
||||
import { Platform } from "react-native"
|
||||
import { Animated, Easing, Platform } from "react-native"
|
||||
import { interpolateColor, useReducedMotion } from "react-native-reanimated"
|
||||
|
||||
import { themeCoverPalette, type CoverPalette } from "@/lib/cover-palette"
|
||||
import { useIsDark, useThemeColors } from "@/lib/theme"
|
||||
@@ -39,7 +40,10 @@ export function AndroidTabThemeProvider({ children }: PropsWithChildren) {
|
||||
palette: CoverPalette
|
||||
owner: string
|
||||
} | null>(null)
|
||||
const palette = screen?.palette ?? null
|
||||
const palette = useFadingPalette(
|
||||
screen?.palette ?? defaultPalette,
|
||||
defaultPalette
|
||||
)
|
||||
const setPalette = useCallback(
|
||||
(palette: CoverPalette | null, owner: string) => {
|
||||
setScreen((current) =>
|
||||
@@ -88,33 +92,80 @@ export function useAndroidTabSelectedColor() {
|
||||
return theme?.selectedColor ?? defaultPalette.accentText
|
||||
}
|
||||
|
||||
/** Keep Android artwork screens and shared bars on the same displayed palette. */
|
||||
// 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
|
||||
|
||||
function useFadingPalette(target: CoverPalette, initial: CoverPalette) {
|
||||
const [displayed, setDisplayed] = useState(initial)
|
||||
const current = useRef(initial)
|
||||
const reducedMotion = useReducedMotion()
|
||||
|
||||
useLayoutEffect(() => {
|
||||
if (Platform.OS !== "android") return
|
||||
if (reducedMotion || current.current === target) {
|
||||
current.current = target
|
||||
setDisplayed(target)
|
||||
return
|
||||
}
|
||||
const from = current.current
|
||||
const progress = new Animated.Value(0)
|
||||
const listener = progress.addListener(({ value }) => {
|
||||
const color = (key: Exclude<keyof CoverPalette, "isDark">) =>
|
||||
interpolateColor(value, [0, 1], [from[key], target[key]], "RGB")
|
||||
const next: CoverPalette = {
|
||||
background: color("background"),
|
||||
foreground: color("foreground"),
|
||||
secondaryForeground: color("secondaryForeground"),
|
||||
accent: color("accent"),
|
||||
accentForeground: color("accentForeground"),
|
||||
accentText: color("accentText"),
|
||||
surface: color("surface"),
|
||||
divider: color("divider"),
|
||||
isDark: value < 0.5 ? from.isDark : target.isDark,
|
||||
}
|
||||
current.current = value === 1 ? target : next
|
||||
setDisplayed(current.current)
|
||||
})
|
||||
const animation = Animated.timing(progress, {
|
||||
toValue: 1,
|
||||
duration: PALETTE_FADE_MS,
|
||||
easing: Easing.out(Easing.cubic),
|
||||
useNativeDriver: false,
|
||||
})
|
||||
animation.start()
|
||||
return () => {
|
||||
animation.stop()
|
||||
progress.removeListener(listener)
|
||||
}
|
||||
}, [target, reducedMotion])
|
||||
|
||||
return Platform.OS === "android" ? displayed : target
|
||||
}
|
||||
|
||||
/** Fade each page independently; shared bars follow the focused page. */
|
||||
export function useAndroidScreenPalette(palette: CoverPalette) {
|
||||
const isFocused = useIsFocused()
|
||||
const owner = useRoute().key
|
||||
const navigation =
|
||||
useNavigation<NativeStackNavigationProp<Record<string, undefined>>>()
|
||||
const [appeared, setAppeared] = useState(false)
|
||||
const [leaving, setLeaving] = useState(false)
|
||||
const [laidOut, setLaidOut] = useState(false)
|
||||
const [closing, setClosing] = useState(false)
|
||||
const onLayout = useCallback(() => setLaidOut(true), [])
|
||||
const theme = useContext(AndroidTabTheme)
|
||||
const setPalette = theme?.setPalette
|
||||
|
||||
useLayoutEffect(() => {
|
||||
if (Platform.OS !== "android") return
|
||||
return navigation.addListener("transitionEnd", ({ data }) => {
|
||||
setAppeared(!data.closing)
|
||||
const start = navigation.addListener("transitionStart", ({ data }) => {
|
||||
setClosing(data.closing)
|
||||
})
|
||||
return start
|
||||
}, [navigation])
|
||||
|
||||
usePreventRemove(Platform.OS === "android" && !leaving, ({ repeat }) => {
|
||||
setLeaving(true)
|
||||
setPalette?.(null, owner)
|
||||
// Let the page and bars paint their shared destination colors before the
|
||||
// native stack removes the page. React blur happens too early for this.
|
||||
requestAnimationFrame(() => requestAnimationFrame(repeat))
|
||||
})
|
||||
const defaultPalette = useDefaultPalette()
|
||||
const theme = useContext(AndroidTabTheme)
|
||||
const setPalette = theme?.setPalette
|
||||
const displayed = useFadingPalette(
|
||||
isFocused && laidOut && !closing ? palette : defaultPalette,
|
||||
defaultPalette
|
||||
)
|
||||
|
||||
useLayoutEffect(() => {
|
||||
if (
|
||||
@@ -122,16 +173,15 @@ export function useAndroidScreenPalette(palette: CoverPalette) {
|
||||
!setPalette ||
|
||||
!isFocused ||
|
||||
!laidOut ||
|
||||
!appeared ||
|
||||
leaving
|
||||
closing
|
||||
)
|
||||
return
|
||||
setPalette(palette, owner)
|
||||
return () => setPalette(null, owner)
|
||||
}, [appeared, isFocused, laidOut, leaving, owner, palette, setPalette])
|
||||
}, [closing, laidOut, isFocused, owner, palette, setPalette])
|
||||
|
||||
return {
|
||||
palette: Platform.OS === "android" && theme ? theme.palette : palette,
|
||||
palette: displayed,
|
||||
onLayout: Platform.OS === "android" ? onLayout : undefined,
|
||||
}
|
||||
}
|
||||
@@ -95,6 +95,13 @@ function extractSwatches(key: string) {
|
||||
return request
|
||||
}
|
||||
|
||||
/** Warm visible artwork colors before a detail page is pushed. */
|
||||
export function prefetchCoverPalette(uri: string) {
|
||||
const key = withCoverImageSize(uri, 64)
|
||||
if (cachedSwatches(key) !== undefined) return
|
||||
void extractSwatches(key)
|
||||
}
|
||||
|
||||
/**
|
||||
* Colors for a screen tinted by `uri`'s cover art. Extraction runs once per
|
||||
* cover and is cached on device. Pass `hasCover` as `undefined` while it isn't
|
||||
@@ -138,7 +145,10 @@ export function useCoverPalette(
|
||||
// case the cover changed.
|
||||
const timer =
|
||||
known === undefined
|
||||
? setTimeout(() => !cancelled && setWaitedKey(key), COVER_PALETTE_WAIT_MS)
|
||||
? setTimeout(
|
||||
() => !cancelled && setWaitedKey(key),
|
||||
COVER_PALETTE_WAIT_MS
|
||||
)
|
||||
: null
|
||||
void extractSwatches(key).then((next) => {
|
||||
if (!cancelled) show(next ?? known ?? null)
|
||||
@@ -151,7 +161,9 @@ export function useCoverPalette(
|
||||
|
||||
const palette = useMemo(() => {
|
||||
const fromArt =
|
||||
hasCover !== false && swatches ? deriveCoverPalette(swatches, isDark) : null
|
||||
hasCover !== false && swatches
|
||||
? deriveCoverPalette(swatches, isDark)
|
||||
: null
|
||||
return fromArt ?? themeCoverPalette(theme, isDark)
|
||||
}, [hasCover, isDark, swatches, theme])
|
||||
|
||||
|
||||
Reference in new issue
Block a user