132 lines
3.3 KiB
TypeScript
132 lines
3.3 KiB
TypeScript
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,
|
|
View,
|
|
type ViewStyle,
|
|
} from "react-native"
|
|
|
|
import { prefetchCoverPalette } from "@/hooks/use-cover-palette"
|
|
import { downloadedCoverUri } from "@/lib/downloads"
|
|
|
|
import { useThemeColors } from "@/lib/theme"
|
|
import {
|
|
coverImageSizes,
|
|
type CoverImageSize,
|
|
withCoverImageSize,
|
|
} from "@/lib/urls"
|
|
|
|
const DEFAULT_SOURCE_SIZE: CoverImageSize = 1024
|
|
const PIXEL_RATIO = PixelRatio.get()
|
|
|
|
function sourceSize(style: ImageStyle): CoverImageSize {
|
|
const layoutSizes = [
|
|
style.width,
|
|
style.height,
|
|
style.maxWidth,
|
|
style.maxHeight,
|
|
].filter((value): value is number => typeof value === "number")
|
|
|
|
if (layoutSizes.length === 0) return DEFAULT_SOURCE_SIZE
|
|
|
|
const requiredPixels = Math.max(...layoutSizes) * PIXEL_RATIO
|
|
return (
|
|
coverImageSizes.find((size) => size >= requiredPixels) ??
|
|
DEFAULT_SOURCE_SIZE
|
|
)
|
|
}
|
|
|
|
function placeholderIconSize(style: ImageStyle): number {
|
|
const { width, height, maxWidth } = style
|
|
if (typeof width === "number" && typeof height === "number") {
|
|
return Math.min(width, height) * 0.38
|
|
}
|
|
if (typeof width === "number") {
|
|
return width * 0.38
|
|
}
|
|
if (typeof maxWidth === "number") {
|
|
return maxWidth * 0.38
|
|
}
|
|
return 48
|
|
}
|
|
|
|
const placeholderBaseStyle: ViewStyle = {
|
|
alignItems: "center",
|
|
justifyContent: "center",
|
|
overflow: "hidden",
|
|
}
|
|
|
|
export const CoverImage = memo(function CoverImage({
|
|
uri,
|
|
hasCover,
|
|
style,
|
|
contentFit = "cover",
|
|
blurRadius,
|
|
placeholderVariant = "icon",
|
|
placeholderIcon = "musical-note",
|
|
}: {
|
|
uri: string
|
|
hasCover: boolean
|
|
style?: StyleProp<ImageStyle>
|
|
contentFit?: ImageContentFit
|
|
blurRadius?: number
|
|
placeholderVariant?: "icon" | "solid"
|
|
placeholderIcon?: keyof typeof Ionicons.glyphMap
|
|
}) {
|
|
const colors = useThemeColors()
|
|
const flatStyle = useMemo(() => StyleSheet.flatten(style) ?? {}, [style])
|
|
const sourceUri = useMemo(
|
|
() => (hasCover ? withCoverImageSize(uri, sourceSize(flatStyle)) : ""),
|
|
[flatStyle, hasCover, uri]
|
|
)
|
|
const source = useMemo(() => ({ uri: sourceUri }), [sourceUri])
|
|
// A downloaded cover shows while the network copy loads, and offline.
|
|
const savedUri = hasCover ? downloadedCoverUri(uri) : null
|
|
const placeholder = useMemo(
|
|
() => (savedUri ? { uri: savedUri } : undefined),
|
|
[savedUri]
|
|
)
|
|
|
|
if (!hasCover) {
|
|
return (
|
|
<View
|
|
style={[
|
|
style,
|
|
placeholderBaseStyle,
|
|
{ backgroundColor: colors.coverPlaceholder },
|
|
]}
|
|
>
|
|
{placeholderVariant === "icon" ? (
|
|
<Ionicons
|
|
name={placeholderIcon}
|
|
size={placeholderIconSize(flatStyle)}
|
|
color={colors.coverPlaceholderIcon}
|
|
/>
|
|
) : null}
|
|
</View>
|
|
)
|
|
}
|
|
|
|
return (
|
|
<Image
|
|
source={source}
|
|
style={[style, { backgroundColor: colors.coverPlaceholder }]}
|
|
contentFit={contentFit}
|
|
placeholder={placeholder}
|
|
placeholderContentFit={contentFit}
|
|
transition={150}
|
|
blurRadius={blurRadius}
|
|
onLoad={
|
|
Platform.OS === "android" ? () => prefetchCoverPalette(uri) : undefined
|
|
}
|
|
cachePolicy="memory-disk"
|
|
recyclingKey={sourceUri}
|
|
enforceEarlyResizing
|
|
/>
|
|
)
|
|
})
|