From 0cc756396f0d4bbe6cc540903978d580a2018497 Mon Sep 17 00:00:00 2001 From: Joshua Higgins Date: Mon, 16 Nov 2026 22:07:51 -0500 Subject: [PATCH] fix(mobile): fast Android page fades with artwork colors (#108) --- apps/mobile/app.json | 1 + apps/mobile/app/(app)/(tabs)/_layout.tsx | 37 ++-- .../app/(app)/(tabs)/home/album/[id].tsx | 91 +++------- .../app/(app)/(tabs)/home/playlist/[id].tsx | 64 ++----- .../mobile/app/(app)/(tabs)/home/settings.tsx | 2 + apps/mobile/components/audio-format-badge.tsx | 12 +- apps/mobile/components/collection-list.tsx | 10 +- apps/mobile/components/mini-player.tsx | 46 ++--- apps/mobile/components/palette-text.tsx | 49 ----- apps/mobile/components/player.tsx | 20 +-- apps/mobile/components/tab-screen-content.tsx | 4 +- apps/mobile/components/ui/primitives.tsx | 17 +- apps/mobile/contexts/android-tab-theme.tsx | 167 ++++-------------- apps/mobile/hooks/cover-palette-source.ts | 8 +- apps/mobile/hooks/use-cover-palette.ts | 12 ++ apps/mobile/hooks/use-legacy-cover-palette.ts | 2 + apps/mobile/lib/navigation.ts | 2 +- apps/mobile/plugins/with-android-fast-fade.js | 36 ++++ packages/convex/client/persistent-cache.ts | 9 + 19 files changed, 198 insertions(+), 391 deletions(-) delete mode 100644 apps/mobile/components/palette-text.tsx create mode 100644 apps/mobile/plugins/with-android-fast-fade.js diff --git a/apps/mobile/app.json b/apps/mobile/app.json index c43ce32..371f94d 100644 --- a/apps/mobile/app.json +++ b/apps/mobile/app.json @@ -54,6 +54,7 @@ "expo-status-bar", "expo-app-intents", "./plugins/with-android-colors", + "./plugins/with-android-fast-fade", [ "expo-navigation-bar", { diff --git a/apps/mobile/app/(app)/(tabs)/_layout.tsx b/apps/mobile/app/(app)/(tabs)/_layout.tsx index 512741d..3465dd5 100644 --- a/apps/mobile/app/(app)/(tabs)/_layout.tsx +++ b/apps/mobile/app/(app)/(tabs)/_layout.tsx @@ -6,8 +6,8 @@ import { NavigationBar } from "expo-navigation-bar" import { ScreenPaletteProvider, useAndroidTabPalette, - useAndroidTabBackgroundStyle, - useAndroidNativeTabColors, + useAndroidTabHasArtworkPalette, + useTabletBackgroundStyle, } from "@/contexts/android-tab-theme" import { DarkTheme, DefaultTheme, ThemeProvider } from "expo-router" import { NativeTabs } from "expo-router/native-tabs" @@ -46,12 +46,13 @@ function useRouterTheme() { } function useNativeTabColors() { - // These props initialize native tabs; the worklet owns subsequent colors. - const selectedColor = colors.dark.primary const themeIsDark = useIsDark() - const androidPalette = colors.dark - const artworkPalette = useAndroidTabPalette() - const isDark = Platform.OS === "android" ? artworkPalette.isDark : themeIsDark + const androidPalette = useAndroidTabPalette() + const hasArtworkPalette = useAndroidTabHasArtworkPalette() + const selectedColor = hasArtworkPalette + ? androidPalette.foreground + : androidPalette.accentText + const isDark = Platform.OS === "android" ? androidPalette.isDark : themeIsDark const inactive = appleMusic.tabInactive const iconColor = @@ -61,14 +62,14 @@ function useNativeTabColors() { selected: appleMusic.pink, } : { - default: androidPalette.mutedForeground, + default: androidPalette.secondaryForeground, selected: selectedColor, } const labelColor = Platform.OS === "ios" ? DynamicColorIOS({ dark: inactive, light: "#636366" }) - : androidPalette.mutedForeground + : androidPalette.secondaryForeground return { isDark, @@ -85,20 +86,17 @@ function useNativeTabColors() { blurEffect: isDark ? "systemChromeMaterialDark" : "systemChromeMaterialLight", - // Reveal the shared UI-thread background beneath the native tab bar. - backgroundColor: Platform.OS === "ios" ? undefined : "transparent", + backgroundColor: + Platform.OS === "ios" ? undefined : androidPalette.background, } } function TabsContent() { const tabletLayout = useTabletLayout() - const homeColors = useAndroidNativeTabColors() - const libraryColors = useAndroidNativeTabColors() - const searchColors = useAndroidNativeTabColors() const keyboardVisible = useAndroidKeyboardVisible() const routerTheme = useRouterTheme() const tabColors = useNativeTabColors() - const backgroundStyle = useAndroidTabBackgroundStyle() + const backgroundStyle = useTabletBackgroundStyle() const { currentSong, queueSongIds } = usePlaybackQueue() const showBottomAccessory = Boolean(currentSong && queueSongIds.length > 0) @@ -140,9 +138,6 @@ function TabsContent() { @@ -152,9 +147,6 @@ function TabsContent() { @@ -169,9 +161,6 @@ function TabsContent() { diff --git a/apps/mobile/app/(app)/(tabs)/home/album/[id].tsx b/apps/mobile/app/(app)/(tabs)/home/album/[id].tsx index 6ed00dc..512b580 100644 --- a/apps/mobile/app/(app)/(tabs)/home/album/[id].tsx +++ b/apps/mobile/app/(app)/(tabs)/home/album/[id].tsx @@ -7,21 +7,14 @@ import { CollectionHero, useCollectionHeroLayout, } from "@/components/collection-hero" -import { - PaletteText, - PaletteIcon, - renderArtworkBackButton, -} from "@/components/palette-text" import Animated from "react-native-reanimated" -import { - renderAndroidArtworkBackground, - useScreenPalette, -} from "@/contexts/android-tab-theme" +import { useScreenPalette } from "@/contexts/android-tab-theme" import { MusicEntityView } from "@/components/music-entity-view" import { Link, Stack, useIsFocused, useLocalSearchParams } from "expo-router" import { StatusBar } from "expo-status-bar" import { memo, useCallback, useEffect, useMemo } from "react" -import { Platform, Pressable, StyleSheet, View } from "react-native" +import { Platform, Pressable, StyleSheet, Text, View } from "react-native" +import { Ionicons } from "@expo/vector-icons" import { CollectionDivider, @@ -142,10 +135,7 @@ export default function AlbumScreen() { headerLargeTitle: false, ...(Platform.OS === "android" ? { - headerStyle: { backgroundColor: "transparent" }, - headerBackground: renderAndroidArtworkBackground, - headerBackVisible: false, - headerLeft: renderArtworkBackButton, + headerStyle: { backgroundColor: palette.background }, headerTintColor: palette.foreground, } : {}), @@ -173,9 +163,9 @@ export default function AlbumScreen() { }} /> - + Album not found. - + ) @@ -196,10 +186,7 @@ export default function AlbumScreen() { headerTintColor: palette.foreground, ...(Platform.OS === "android" ? { - headerStyle: { backgroundColor: "transparent" }, - headerBackground: renderAndroidArtworkBackground, - headerBackVisible: false, - headerLeft: renderArtworkBackButton, + headerStyle: { backgroundColor: palette.background }, statusBarStyle: palette.isDark ? ("light" as const) : ("dark" as const), @@ -221,8 +208,7 @@ export default function AlbumScreen() { } > - - - } > - {album.title} - + push(`/artist/${album.artist.id}`)} style={styles.artistPressable} > - {album.artist.name} - + {metadata ? ( - + {metadata} - + ) : null} {album.hasLossless ? ( <> {metadata ? ( - + · - + ) : null} {metadata || album.hasLossless ? ( - + · - + ) : null} void onPlay(track.id as Id<"songs">, songIds)} > - {number} - + - {track.title} - + {track.artists ? ( - {track.artists.join(", ")} - + ) : null} - + {formatDuration(track.durationSeconds)} - + diff --git a/apps/mobile/app/(app)/(tabs)/home/playlist/[id].tsx b/apps/mobile/app/(app)/(tabs)/home/playlist/[id].tsx index 159885d..c93839f 100644 --- a/apps/mobile/app/(app)/(tabs)/home/playlist/[id].tsx +++ b/apps/mobile/app/(app)/(tabs)/home/playlist/[id].tsx @@ -2,20 +2,12 @@ import { CollectionHero, useCollectionHeroLayout, } from "@/components/collection-hero" -import { - PaletteText, - PaletteIcon, - renderArtworkBackButton, -} from "@/components/palette-text" import Animated from "react-native-reanimated" import { PlaylistSearchBar, usePlaylistSearch, } from "@/components/playlist-search-bar" -import { - renderAndroidArtworkBackground, - useScreenPalette, -} from "@/contexts/android-tab-theme" +import { useScreenPalette } from "@/contexts/android-tab-theme" import { MusicEntityView } from "@/components/music-entity-view" import { useMutation } from "convex/react" import { memo, useCallback, useEffect, useMemo } from "react" @@ -31,10 +23,12 @@ import { Platform, Pressable, StyleSheet, + Text, View, type ListRenderItemInfo, } from "react-native" import ReanimatedSwipeable from "react-native-gesture-handler/ReanimatedSwipeable" +import { Ionicons } from "@expo/vector-icons" import { CollectionDivider } from "@/components/collection-list" import { CoverImage } from "@/components/cover-image" @@ -111,10 +105,7 @@ export default function PlaylistScreen() { headerLargeTitle: false, ...(Platform.OS === "android" ? { - headerStyle: { backgroundColor: "transparent" }, - headerBackground: renderAndroidArtworkBackground, - headerBackVisible: false, - headerLeft: renderArtworkBackButton, + headerStyle: { backgroundColor: palette.background }, headerTintColor: palette.foreground, } : {}), @@ -142,9 +133,9 @@ export default function PlaylistScreen() { }} /> - + Playlist not found. - + ) @@ -279,10 +270,7 @@ function PlaylistContent({ headerTintColor: palette.foreground, ...(Platform.OS === "android" ? { - headerStyle: { backgroundColor: "transparent" }, - headerBackground: renderAndroidArtworkBackground, - headerBackVisible: false, - headerLeft: renderArtworkBackButton, + headerStyle: { backgroundColor: palette.background }, statusBarStyle: palette.isDark ? ("light" as const) : ("dark" as const), @@ -301,8 +289,7 @@ function PlaylistContent({ onAction={handlePlaylistMenuAction} > - {songs.length ? "No songs found." : "No songs in this playlist."} - + } ListHeaderComponent={ - - + Remove - + )} > @@ -565,8 +547,7 @@ const PlaylistHero = memo(function PlaylistHero({ }, ]} > - - + } > - {title} - - + {ownerName} - + diff --git a/apps/mobile/app/(app)/(tabs)/home/settings.tsx b/apps/mobile/app/(app)/(tabs)/home/settings.tsx index 03bf82e..29f1034 100644 --- a/apps/mobile/app/(app)/(tabs)/home/settings.tsx +++ b/apps/mobile/app/(app)/(tabs)/home/settings.tsx @@ -26,6 +26,7 @@ import { } from "@/lib/artwork-color-preference" import { initialsFromName } from "@/lib/format" import { useCachedQuery } from "@/hooks/use-cached-query" +import { clearCoverPaletteCache } from "@/hooks/use-cover-palette" import { useProfileImageUrl } from "@/hooks/use-profile-image-url" import { changeServer, signOut } from "@/lib/session" import { radius, spacing, useThemeColors } from "@/lib/theme" @@ -185,6 +186,7 @@ function AppearanceSection() { setSaving(true) try { await setArtworkColorsEnabled(checked) + clearCoverPaletteCache() } catch { showError("Could not save artwork colors preference.") } finally { diff --git a/apps/mobile/components/audio-format-badge.tsx b/apps/mobile/components/audio-format-badge.tsx index c63decc..b035f7d 100644 --- a/apps/mobile/components/audio-format-badge.tsx +++ b/apps/mobile/components/audio-format-badge.tsx @@ -1,5 +1,4 @@ -import { PaletteText } from "@/components/palette-text" -import { StyleSheet, View } from "react-native" +import { StyleSheet, Text, View } from "react-native" import Svg, { G, Path } from "react-native-svg" import { @@ -69,12 +68,10 @@ function LosslessMark({ /** The Dolby Atmos or Lossless label shown under the progress bar. */ export function AudioFormatBadge({ - artwork = false, format, color, backgroundColor, }: { - artwork?: boolean format: PlaybackFormat color: string backgroundColor: string @@ -87,12 +84,7 @@ export function AudioFormatBadge({ ) : ( )} - - {label} - + {label} ) } diff --git a/apps/mobile/components/collection-list.tsx b/apps/mobile/components/collection-list.tsx index a11ac37..ff04e4c 100644 --- a/apps/mobile/components/collection-list.tsx +++ b/apps/mobile/components/collection-list.tsx @@ -1,6 +1,5 @@ -import { PaletteText } from "@/components/palette-text" import { memo } from "react" -import { StyleSheet, View } from "react-native" +import { StyleSheet, Text, View } from "react-native" import { useThemeColors } from "@/lib/theme" @@ -26,12 +25,9 @@ export const DiscHeader = memo(function DiscHeader({ }) { const colors = useThemeColors() return ( - + {label} - + ) }) diff --git a/apps/mobile/components/mini-player.tsx b/apps/mobile/components/mini-player.tsx index 8f936fb..8923c2c 100644 --- a/apps/mobile/components/mini-player.tsx +++ b/apps/mobile/components/mini-player.tsx @@ -1,13 +1,11 @@ -import { PaletteText, PaletteIcon } from "@/components/palette-text" -import Animated from "react-native-reanimated" import { useAndroidTabPalette, - useAndroidTabBackgroundStyle, useAndroidTabHasArtworkPalette, } from "@/contexts/android-tab-theme" +import { Ionicons } from "@expo/vector-icons" import { NativeTabs } from "expo-router/native-tabs" import { useRouter } from "expo-router" -import { Platform, Pressable, StyleSheet, View } from "react-native" +import { Platform, Pressable, StyleSheet, Text, View } from "react-native" import { useSafeAreaInsets } from "react-native-safe-area-context" @@ -67,8 +65,7 @@ function MiniPlayerContent({ - {currentSong.title} - + - - {currentSong.title} - - + {currentSong.artist.name} - + {Platform.OS === "android" && !isAccessory ? ( @@ -138,12 +132,7 @@ function MiniPlayerContent({ }} hitSlop={6} > - + ) : null} - - + @@ -184,10 +167,9 @@ function MiniPlayerContent({ function MiniPlayerChrome({ children }: { children: React.ReactNode }) { const palette = useAndroidTabPalette() const hasArtworkPalette = useAndroidTabHasArtworkPalette() - const backgroundStyle = useAndroidTabBackgroundStyle() return ( - + {children} {hasArtworkPalette ? ( - + ) } diff --git a/apps/mobile/components/palette-text.tsx b/apps/mobile/components/palette-text.tsx deleted file mode 100644 index 0c7934c..0000000 --- a/apps/mobile/components/palette-text.tsx +++ /dev/null @@ -1,49 +0,0 @@ -import { Ionicons } from "@expo/vector-icons" -import type { ComponentProps } from "react" -import { Pressable, type TextProps } from "react-native" -import { useRouter } from "expo-router" -import Animated from "react-native-reanimated" - -import { - useAnimatedPaletteColor, - type PaletteColor, -} from "@/contexts/android-tab-theme" - -const AnimatedIcon = Animated.createAnimatedComponent(Ionicons) - -/** Keep artwork text colors on the same UI-thread animation as the background. */ -export function PaletteText({ - tone, - style, - ...props -}: TextProps & { tone?: PaletteColor }) { - const colorStyle = useAnimatedPaletteColor(tone) - return -} - -export function PaletteIcon({ - tone, - style, - ...props -}: ComponentProps & { tone?: PaletteColor }) { - const colorStyle = useAnimatedPaletteColor(tone) - return -} - -function ArtworkBackButton() { - const router = useRouter() - return ( - router.back()} - > - - - ) -} - -export function renderArtworkBackButton() { - return -} diff --git a/apps/mobile/components/player.tsx b/apps/mobile/components/player.tsx index 556fe20..401a4a8 100644 --- a/apps/mobile/components/player.tsx +++ b/apps/mobile/components/player.tsx @@ -1,6 +1,5 @@ import { SongSwipeRow } from "@/components/song-swipe-row" import type { SwipeRowGroup } from "@/components/ios-swipe-to-remove-row" -import { PaletteText } from "@/components/palette-text" import { MusicEntityView } from "@/components/music-entity-view" import { collectionInset } from "@/components/collection-list" import { CoverImage } from "@/components/cover-image" @@ -16,7 +15,7 @@ import { useThemeColors, } from "@/lib/theme" import { memo, useCallback } from "react" -import { Platform, Pressable, StyleSheet, View } from "react-native" +import { Platform, Pressable, StyleSheet, Text, View } from "react-native" import { MediaContextMenu, @@ -64,27 +63,22 @@ export const SongRow = memo(function SongRow({ const content = ( <> - {song.title} - - + {song.artist.name} · {song.album.title} - + - + {formatDuration(song.durationSeconds)} - + {trailing} ) diff --git a/apps/mobile/components/tab-screen-content.tsx b/apps/mobile/components/tab-screen-content.tsx index 66cb748..a5e8a3f 100644 --- a/apps/mobile/components/tab-screen-content.tsx +++ b/apps/mobile/components/tab-screen-content.tsx @@ -3,13 +3,13 @@ import { SafeAreaView } from "react-native-safe-area-context" import { Platform } from "react-native" import type { ReactNode } from "react" -import { useAndroidTabBackgroundStyle } from "@/contexts/android-tab-theme" +import { useTabletBackgroundStyle } from "@/contexts/android-tab-theme" import { TabletPlayerLayoutSync } from "@/contexts/tablet-player-layout" import { TabletSidebar } from "@/components/tablet-sidebar" /** Apply sidebar insets natively, before Yoga measures the content pane. */ export function TabScreenContent({ children }: { children: ReactNode }) { - const backgroundStyle = useAndroidTabBackgroundStyle() + const backgroundStyle = useTabletBackgroundStyle() if (Platform.OS !== "ios" || !Platform.isPad) return <>{children} return ( diff --git a/apps/mobile/components/ui/primitives.tsx b/apps/mobile/components/ui/primitives.tsx index 3ec1da4..2ab15f4 100644 --- a/apps/mobile/components/ui/primitives.tsx +++ b/apps/mobile/components/ui/primitives.tsx @@ -1,4 +1,3 @@ -import { PaletteText, PaletteIcon } from "@/components/palette-text" import { ActivityIndicator, Platform, @@ -74,20 +73,8 @@ export function Button({ ) : ( - {icon ? ( - - ) : null} - - {title} - + {icon ? : null} + {title} )} diff --git a/apps/mobile/contexts/android-tab-theme.tsx b/apps/mobile/contexts/android-tab-theme.tsx index efd0cb2..5e33131 100644 --- a/apps/mobile/contexts/android-tab-theme.tsx +++ b/apps/mobile/contexts/android-tab-theme.tsx @@ -14,14 +14,10 @@ import { useState, type PropsWithChildren, } from "react" -import { Platform, StyleSheet, type HostInstance } from "react-native" -import Reanimated, { +import { Platform } from "react-native" +import { useSharedValue, useAnimatedStyle, - useAnimatedReaction, - useAnimatedRef, - setNativeProps, - processColor, withTiming, type SharedValue, } from "react-native-reanimated" @@ -29,19 +25,17 @@ import Reanimated, { import { themeCoverPalette, type CoverPalette } from "@/lib/cover-palette" import { useIsDark, useThemeColors } from "@/lib/theme" -export type PaletteColor = Exclude -type PaletteColors = Pick & { - selectedColor: string -} - type TabTheme = { palette: CoverPalette - colors: SharedValue hasArtworkPalette: boolean + tabletBackground: SharedValue setPalette: (palette: CoverPalette | null, owner: string) => void } const AndroidTabTheme = createContext(null) +const isTablet = Platform.OS === "ios" && Platform.isPad +const sharesPalette = Platform.OS === "android" || isTablet + function useDefaultPalette() { const theme = useThemeColors() const isDark = useIsDark() @@ -60,52 +54,37 @@ export function ScreenPaletteProvider({ children }: PropsWithChildren) { palette.foreground !== defaultPalette.foreground || palette.secondaryForeground !== defaultPalette.secondaryForeground || palette.accent !== defaultPalette.accent - const { isDark: _defaultIsDark, ...defaultColors } = defaultPalette - const animatedColors = useSharedValue({ - ...defaultColors, - selectedColor: defaultPalette.accentText, - }) + const tabletBackground = useSharedValue(defaultPalette.background) useLayoutEffect(() => { - const { isDark: _isDark, ...colors } = palette - animatedColors.value = withTiming( - { - ...colors, - selectedColor: hasArtwork - ? palette.foreground - : defaultPalette.accentText, - }, - { duration: PALETTE_FADE_MS } - ) - }, [animatedColors, palette, defaultPalette, hasArtwork]) + if (!isTablet) return + tabletBackground.value = withTiming(palette.background, { + duration: TABLET_FADE_MS, + }) + }, [tabletBackground, palette.background]) const paletteOwner = useRef(null) const setPalette = useCallback( (palette: CoverPalette | null, owner: string) => { if (!palette && paletteOwner.current !== owner) return paletteOwner.current = palette ? owner : null - // Start on the UI thread immediately. React may defer commits while - // the native stack is animating, including transitionStart state updates. - const target = palette ?? defaultPalette - const { isDark: _isDark, ...colors } = target - animatedColors.value = withTiming( - { - ...colors, - selectedColor: palette ? target.foreground : target.accentText, - }, - { duration: PALETTE_FADE_MS } - ) + if (isTablet) { + // React may defer commits while the iPad stack is animating. + tabletBackground.value = withTiming( + (palette ?? defaultPalette).background, + { duration: TABLET_FADE_MS } + ) + } setScreen(palette ? { palette, owner } : null) }, - [animatedColors, defaultPalette] + [tabletBackground, defaultPalette] ) - const artworkPalette = hasArtwork ? palette : null const value = useMemo( () => ({ - palette: artworkPalette ?? defaultPalette, - colors: animatedColors, - hasArtworkPalette: artworkPalette !== null, + palette: hasArtwork ? palette : defaultPalette, + hasArtworkPalette: hasArtwork, + tabletBackground, setPalette, }), - [defaultPalette, artworkPalette, setPalette, animatedColors] + [defaultPalette, hasArtwork, palette, setPalette, tabletBackground] ) return ( @@ -114,50 +93,6 @@ export function ScreenPaletteProvider({ children }: PropsWithChildren) { ) } -/** - * NativeTabs forwards raw screen props to its Fabric screen. Bind that host - * directly so appearance updates stay on the UI thread with the page colors. - */ -export function useAndroidNativeTabColors() { - const colors = useContext(AndroidTabTheme)?.colors - const ref = useAnimatedRef() - const mounted = useSharedValue(false) - const attach = useCallback( - (host: HostInstance | null) => { - if (host) ref(host) - mounted.value = host !== null - }, - [ref, mounted] - ) - - useAnimatedReaction( - () => ({ colors: colors?.value, mounted: mounted.value }), - ({ colors: palette, mounted: ready }) => { - if (Platform.OS !== "android" || !ready || !palette) return - const normal = processColor(palette.secondaryForeground) - const selected = processColor(palette.selectedColor) - setNativeProps(ref, { - standardAppearance: { - tabBarBackgroundColor: processColor("transparent"), - tabBarItemRippleColor: selected, - tabBarItemActiveIndicatorEnabled: false, - tabBarItemTitleSmallLabelFontSize: 10, - tabBarItemTitleLargeLabelFontSize: 10, - normal: { - tabBarItemTitleFontColor: normal, - tabBarItemIconColor: normal, - }, - selected: { - tabBarItemTitleFontColor: selected, - tabBarItemIconColor: selected, - }, - }, - }) - } - ) - return { ref: attach, style: { backgroundColor: "transparent" } } -} - export function useAndroidTabPalette() { const theme = useContext(AndroidTabTheme) const defaultPalette = useDefaultPalette() @@ -168,53 +103,29 @@ export function useAndroidTabHasArtworkPalette() { return useContext(AndroidTabTheme)?.hasArtworkPalette ?? false } -export function useAndroidTabBackgroundStyle() { - const theme = useContext(AndroidTabTheme) - const fallback = useDefaultPalette().background - const colors = theme?.colors +/** iPad fades the sidebar and content pane together; other layouts skip it. */ +export function useTabletBackgroundStyle() { + const background = useContext(AndroidTabTheme)?.tabletBackground return useAnimatedStyle(() => - Platform.OS === "android" || (Platform.OS === "ios" && Platform.isPad) - ? { backgroundColor: colors?.value.background ?? fallback } - : {} + isTablet && background ? { backgroundColor: background.value } : {} ) } -/** Color tokens share the background's UI-thread timing. */ -export function useAnimatedPaletteColor(tone: PaletteColor | undefined) { - const colors = useContext(AndroidTabTheme)?.colors - return useAnimatedStyle(() => - Platform.OS === "android" && tone && colors - ? { color: colors.value[tone] } - : {} - ) -} +const TABLET_FADE_MS = 400 -/** Native headers draw this same UI-thread background as the page and bars. */ -function AndroidArtworkBackground() { - const style = useAndroidTabBackgroundStyle() - return -} - -export function renderAndroidArtworkBackground() { - return -} - -const PALETTE_FADE_MS = 400 - -/** The focused artwork screen selects the shared background for page and bars. */ +/** + * Artwork screens always paint their own palette so it slides with the page. + * Shared chrome (tab bar, mini player) switches when the transition starts. + */ export function useScreenPalette(palette: CoverPalette) { const isFocused = useIsFocused() - const isTablet = Platform.OS === "ios" && Platform.isPad - const sharesPalette = - Platform.OS === "android" || (Platform.OS === "ios" && Platform.isPad) const owner = useRoute().key const navigation = useNavigation>>() const [laidOut, setLaidOut] = useState(false) const [closing, setClosing] = useState(false) const onLayout = useCallback(() => setLaidOut(true), []) - const theme = useContext(AndroidTabTheme) - const setPalette = theme?.setPalette + const setPalette = useContext(AndroidTabTheme)?.setPalette useLayoutEffect(() => { if (!sharesPalette) return const start = navigation.addListener("transitionStart", ({ data }) => { @@ -230,20 +141,18 @@ export function useScreenPalette(palette: CoverPalette) { start() cancel() } - }, [navigation, sharesPalette, setPalette, owner, palette]) - const defaultPalette = useDefaultPalette() + }, [navigation, setPalette, owner, palette]) const ready = laidOut || isTablet - const target = isFocused && ready && !closing ? palette : defaultPalette - const backgroundStyle = useAndroidTabBackgroundStyle() + const backgroundStyle = useTabletBackgroundStyle() useLayoutEffect(() => { if (!sharesPalette || !setPalette || !isFocused || !ready || closing) return setPalette(palette, owner) return () => setPalette(null, owner) - }, [closing, ready, isFocused, owner, palette, setPalette, sharesPalette]) + }, [closing, ready, isFocused, owner, palette, setPalette]) return { - palette: Platform.OS === "android" ? target : palette, + palette, backgroundStyle, onLayout: sharesPalette ? onLayout : undefined, } diff --git a/apps/mobile/hooks/cover-palette-source.ts b/apps/mobile/hooks/cover-palette-source.ts index 31e1d35..aab94e2 100644 --- a/apps/mobile/hooks/cover-palette-source.ts +++ b/apps/mobile/hooks/cover-palette-source.ts @@ -134,7 +134,13 @@ export function createCoverPaletteSource({ } } - return { prefetch, usePalette } + /** Forget in-memory swatches so covers are re-extracted. */ + function clear() { + swatchesByKey.clear() + pending.clear() + } + + return { prefetch, usePalette, clear } } function sameSwatches(a: unknown, b: unknown) { diff --git a/apps/mobile/hooks/use-cover-palette.ts b/apps/mobile/hooks/use-cover-palette.ts index 6cea962..f496871 100644 --- a/apps/mobile/hooks/use-cover-palette.ts +++ b/apps/mobile/hooks/use-cover-palette.ts @@ -3,6 +3,7 @@ import { type CoverPaletteResult, } from "@/hooks/cover-palette-source" import { + clearLegacyCoverPaletteCache, prefetchLegacyCoverPalette, useLegacyCoverPalette, } from "@/hooks/use-legacy-cover-palette" @@ -12,6 +13,7 @@ import { } from "@/lib/artwork-color-preference" import { extractCoverSwatches } from "@/lib/artwork-colors" import { deriveCoverPalette, type CoverSwatches } from "@/lib/cover-palette" +import { clearCachePrefix } from "@/lib/persistent-cache" export { COVER_PALETTE_WAIT_MS } from "@/hooks/cover-palette-source" @@ -21,6 +23,16 @@ const region = createCoverPaletteSource({ derive: (swatches) => deriveCoverPalette(swatches), }) +/** + * Forget every extracted palette, in memory and on device, so covers are + * re-extracted. Both pipelines share the `palette:` cache prefix. + */ +export function clearCoverPaletteCache() { + region.clear() + clearLegacyCoverPaletteCache() + clearCachePrefix("palette") +} + /** Warm visible artwork colors before a detail page is pushed. */ export function prefetchCoverPalette(uri: string) { void loadArtworkColorsEnabled().then((enabled) => diff --git a/apps/mobile/hooks/use-legacy-cover-palette.ts b/apps/mobile/hooks/use-legacy-cover-palette.ts index 9ea720b..938ab75 100644 --- a/apps/mobile/hooks/use-legacy-cover-palette.ts +++ b/apps/mobile/hooks/use-legacy-cover-palette.ts @@ -65,3 +65,5 @@ export const prefetchLegacyCoverPalette = legacy.prefetch /** `react-native-image-colors` pipeline used when new artwork colors are off. */ export const useLegacyCoverPalette = legacy.usePalette + +export const clearLegacyCoverPaletteCache = legacy.clear diff --git a/apps/mobile/lib/navigation.ts b/apps/mobile/lib/navigation.ts index 3340a10..e5dd5f8 100644 --- a/apps/mobile/lib/navigation.ts +++ b/apps/mobile/lib/navigation.ts @@ -28,7 +28,7 @@ export function useStackScreenOptions( headerBackButtonDisplayMode: "minimal", ...(Platform.OS === "android" ? { - animation: "slide_from_right", + animation: "fade", headerLargeTitle: false, headerTransparent: false, headerStyle: { backgroundColor: theme.background }, diff --git a/apps/mobile/plugins/with-android-fast-fade.js b/apps/mobile/plugins/with-android-fast-fade.js new file mode 100644 index 0000000..6554c56 --- /dev/null +++ b/apps/mobile/plugins/with-android-fast-fade.js @@ -0,0 +1,36 @@ +const fs = require("fs") +const path = require("path") +const { withDangerousMod } = require("expo/config-plugins") + +// Overrides react-native-screens' 150ms crossfade for `animation: "fade"`. +// App resources win over library resources with the same name, so the stack +// fades through black in 100ms: the old page out, then the new page in. +const anims = { + "rns_fade_out.xml": ` +`, + "rns_fade_in.xml": ` +`, +} + +module.exports = (config) => + withDangerousMod(config, [ + "android", + (config) => { + const dir = path.join( + config.modRequest.platformProjectRoot, + "app/src/main/res/anim" + ) + fs.mkdirSync(dir, { recursive: true }) + for (const [name, xml] of Object.entries(anims)) { + fs.writeFileSync(path.join(dir, name), `\n${xml}`) + } + return config + }, + ]) diff --git a/packages/convex/client/persistent-cache.ts b/packages/convex/client/persistent-cache.ts index 15bebac..6704297 100644 --- a/packages/convex/client/persistent-cache.ts +++ b/packages/convex/client/persistent-cache.ts @@ -198,6 +198,15 @@ export async function clearPersistentCache() { } } +/** Deletes every entry whose key starts with `:`. */ +export function clearCachePrefix(prefix: string) { + for (const key of [...memory.keys()]) { + if (entryPrefix(key) !== prefix) continue + removeFromMemory(key) + scheduleWrite(key, null) + } +} + export function subscribeCache(key: string, listener: () => void) { let keyListeners = listeners.get(key) if (!keyListeners) {