Files
otunes/apps/mobile/components/mini-player.tsx
T

361 lines
8.5 KiB
TypeScript

import { useAndroidTabPalette } 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, Text, View } from "react-native"
import { useSafeAreaInsets } from "react-native-safe-area-context"
import { CoverImage } from "@/components/cover-image"
import { usePlaybackActions, usePlaybackQueue } from "@/contexts/playback"
import { useServerConfig } from "@/contexts/server-config"
import { albumCoverUrl } from "@/lib/urls"
import { miniPlayerHeight, tabBarHeight, useThemeColors } from "@/lib/theme"
function MiniPlayerContent({
placement,
variant = "bar",
onOpenPlayer,
}: {
placement: "regular" | "inline"
variant: "bar" | "accessory"
onOpenPlayer: () => void
}) {
const theme = useThemeColors()
const palette = useAndroidTabPalette()
const colors =
Platform.OS === "android"
? {
...theme,
foreground: palette.foreground,
mutedForeground: palette.secondaryForeground,
}
: theme
const { config } = useServerConfig()
const { currentSong, isPlaying } = usePlaybackQueue()
const { setIsPlaying, skipNext, skipPrevious } = usePlaybackActions()
if (!currentSong || !config) {
return null
}
const compact = placement === "inline"
const isAccessory = variant === "accessory"
const artworkStyle = isAccessory ? styles.accessoryArtwork : styles.artwork
const rowStyle = compact
? styles.inlineContent
: isAccessory
? styles.accessoryContent
: styles.content
if (compact) {
return (
<Pressable style={rowStyle} onPress={onOpenPlayer}>
<CoverImage
uri={albumCoverUrl(config.serverUrl, currentSong.album.id)}
hasCover={currentSong.album.cover != null}
style={
isAccessory ? styles.accessoryInlineArtwork : styles.inlineArtwork
}
/>
<View
style={isAccessory ? styles.accessoryInlineMeta : styles.inlineMeta}
>
<Text
style={[
isAccessory ? styles.accessoryTitle : styles.title,
{ color: colors.foreground },
]}
numberOfLines={1}
>
{currentSong.title}
</Text>
</View>
<Pressable
style={styles.controlButton}
onPress={(event) => {
event.stopPropagation()
setIsPlaying(!isPlaying)
}}
hitSlop={6}
>
<Ionicons
name={isPlaying ? "pause" : "play"}
size={20}
color={colors.foreground}
/>
</Pressable>
</Pressable>
)
}
return (
<Pressable style={rowStyle} onPress={onOpenPlayer}>
<CoverImage
uri={albumCoverUrl(config.serverUrl, currentSong.album.id)}
hasCover={currentSong.album.cover != null}
style={artworkStyle}
/>
<View style={isAccessory ? styles.accessoryMeta : styles.meta}>
<Text
style={[
isAccessory ? styles.accessoryTitle : styles.title,
{ color: colors.foreground },
]}
numberOfLines={1}
>
{currentSong.title}
</Text>
<Text
style={[
isAccessory ? styles.accessoryArtist : styles.artist,
{ color: colors.mutedForeground },
]}
numberOfLines={1}
>
{currentSong.artist.name}
</Text>
</View>
<View style={styles.controls}>
{Platform.OS === "android" && !isAccessory ? (
<Pressable
style={styles.controlButton}
accessibilityRole="button"
accessibilityLabel="Previous track"
onPress={(event) => {
event.stopPropagation()
void skipPrevious()
}}
hitSlop={6}
>
<Ionicons name="play-back" size={24} color={colors.foreground} />
</Pressable>
) : null}
<Pressable
style={styles.controlButton}
onPress={(event) => {
event.stopPropagation()
setIsPlaying(!isPlaying)
}}
hitSlop={6}
>
<Ionicons
name={isPlaying ? "pause" : "play"}
size={24}
color={colors.foreground}
/>
</Pressable>
<Pressable
style={styles.controlButton}
onPress={(event) => {
event.stopPropagation()
void skipNext()
}}
hitSlop={6}
>
<Ionicons name="play-forward" size={24} color={colors.foreground} />
</Pressable>
</View>
</Pressable>
)
}
function MiniPlayerChrome({ children }: { children: React.ReactNode }) {
const palette = useAndroidTabPalette()
return (
<View style={[styles.androidBar, { backgroundColor: palette.background }]}>
{children}
<View
style={[
styles.separator,
styles.bottomBorder,
{ backgroundColor: palette.secondaryForeground },
]}
/>
</View>
)
}
export function TabBottomAccessory() {
const router = useRouter()
const { currentSong, queueSongIds } = usePlaybackQueue()
const placement = NativeTabs.BottomAccessory.usePlacement()
if (!currentSong || queueSongIds.length === 0) {
return null
}
const content = (
<View style={styles.accessoryContainer}>
<MiniPlayerContent
placement={placement}
variant="accessory"
onOpenPlayer={() => router.push("/player")}
/>
</View>
)
if (Platform.OS === "android") {
return <MiniPlayerChrome>{content}</MiniPlayerChrome>
}
return content
}
/** NativeTabs.BottomAccessory only renders on iOS. */
export function AndroidMiniPlayer() {
const router = useRouter()
const insets = useSafeAreaInsets()
const { currentSong, queueSongIds } = usePlaybackQueue()
if (Platform.OS !== "android" || !currentSong || queueSongIds.length === 0) {
return null
}
return (
<View
style={[
styles.floatingContainer,
{ bottom: tabBarHeight + insets.bottom },
]}
>
<MiniPlayerChrome>
<MiniPlayerContent
placement="regular"
variant="bar"
onOpenPlayer={() => router.push("/player")}
/>
</MiniPlayerChrome>
</View>
)
}
const styles = StyleSheet.create({
floatingContainer: {
position: "absolute",
left: 0,
right: 0,
zIndex: 100,
},
accessoryContainer: {
flex: 1,
justifyContent: "center",
alignSelf: "stretch",
},
blur: {
overflow: "hidden",
},
androidBar: {
overflow: "hidden",
},
separator: {
position: "absolute",
left: 0,
right: 0,
height: 1,
zIndex: 1,
},
bottomBorder: { bottom: 0 },
content: {
flexDirection: "row",
alignItems: "center",
paddingHorizontal: 16,
paddingVertical: 10,
minHeight: miniPlayerHeight,
gap: 12,
},
accessoryContent: {
flex: 1,
flexDirection: "row",
alignItems: "center",
alignSelf: "stretch",
paddingHorizontal: 12,
paddingVertical: 8,
gap: 12,
},
inlineContent: {
flex: 1,
flexDirection: "row",
alignItems: "center",
alignSelf: "stretch",
paddingHorizontal: 12,
paddingVertical: 8,
gap: 10,
},
artwork: {
width: 36,
height: 36,
borderRadius: 5,
},
accessoryArtwork: {
width: 32,
height: 32,
borderRadius: 4,
},
inlineArtwork: {
width: 28,
height: 28,
borderRadius: 4,
},
accessoryInlineArtwork: {
width: 24,
height: 24,
borderRadius: 4,
},
meta: {
flex: 1,
minWidth: 0,
justifyContent: "center",
},
accessoryMeta: {
flex: 1,
minWidth: 0,
justifyContent: "center",
paddingTop: 3,
},
inlineMeta: {
flex: 1,
minWidth: 0,
justifyContent: "center",
},
accessoryInlineMeta: {
flex: 1,
minWidth: 0,
justifyContent: "center",
paddingTop: 2,
},
title: {
fontSize: 15,
fontWeight: "500",
lineHeight: 18,
},
accessoryTitle: {
fontSize: 14,
fontWeight: "500",
lineHeight: 17,
},
artist: {
fontSize: 13,
lineHeight: 16,
marginTop: 2,
},
accessoryArtist: {
fontSize: 12,
lineHeight: 15,
marginTop: 1,
},
controls: {
flexDirection: "row",
alignItems: "center",
gap: 4,
marginLeft: 4,
},
controlButton: {
width: 36,
height: 36,
alignItems: "center",
justifyContent: "center",
},
})
export { miniPlayerHeight, tabBarHeight }