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

307 lines
8.0 KiB
TypeScript

import { SongSwipeRow } from "@/components/song-swipe-row"
import type { SwipeRowGroup } from "@/components/ios-swipe-to-remove-row"
import { MusicEntityView } from "@/components/music-entity-view"
import { collectionInset } from "@/components/collection-list"
import { CoverImage } from "@/components/cover-image"
import { Ionicons } from "@expo/vector-icons"
import {
useSongDownloadState,
useToggleSongDownload,
} from "@/contexts/downloads"
import type { DownloadSource } from "@otunes/convex/client/download-queue"
import { useServerConfig } from "@/contexts/server-config"
import type { BrowseSong } from "@otunes/convex/types"
import type { CoverPalette } from "@/lib/cover-palette"
import { formatDuration } from "@/lib/format"
import { albumCoverUrl } from "@/lib/urls"
import {
miniPlayerHeight,
tabletMiniPlayerHeight,
spacing,
useThemeColors,
} from "@/lib/theme"
import { memo, useCallback } from "react"
import { Platform, Pressable, StyleSheet, Text, View } from "react-native"
import {
MediaContextMenu,
type MediaAction,
type MediaMenuConfig,
} from "@/components/media-context-menu"
function consumeLongPress() {}
export const SongRow = memo(function SongRow({
song,
onPress,
contextMenu,
trailing,
palette,
inset = false,
swipe,
downloadFrom,
}: {
song: BrowseSong
onPress?: () => void
contextMenu?: Omit<MediaMenuConfig, "onAction"> & {
onAction: MediaMenuConfig["onAction"]
}
trailing?: React.ReactNode
palette?: Pick<CoverPalette, "foreground" | "secondaryForeground" | "divider">
swipe?: {
group: SwipeRowGroup
onPlayNext: () => void
onAddToQueue: () => void
onRemove?: () => void
backgroundColor: string
}
/** Album/playlist layout: the divider starts at the title, not the artwork. */
inset?: boolean
/** The download this row belongs to; removing only takes it out of that. */
downloadFrom?: DownloadSource
}) {
const { config } = useServerConfig()
const colors = useThemeColors()
const downloadState = useSongDownloadState(song.id, downloadFrom)
const downloaded = downloadState === "here"
const toggleDownload = useToggleSongDownload()
if (!config) return null
const foreground = palette?.foreground ?? colors.foreground
const secondaryForeground =
palette?.secondaryForeground ?? colors.mutedForeground
const divider = palette?.divider ?? colors.border
const content = (
<>
<View style={styles.rowMeta}>
<Text
style={[styles.rowTitle, { color: foreground }]}
numberOfLines={1}
>
{song.title}
</Text>
<Text
style={[styles.rowSubtitle, { color: secondaryForeground }]}
numberOfLines={1}
>
{song.artist.name} · {song.album.title}
</Text>
</View>
{downloaded || downloadState === "device" ? (
<Ionicons
name="arrow-down-circle"
size={14}
color={secondaryForeground}
accessibilityLabel="Downloaded"
/>
) : null}
<Text style={[styles.duration, { color: secondaryForeground }]}>
{formatDuration(song.durationSeconds)}
</Text>
{trailing}
</>
)
const rowContent = (
<>
<CoverImage
uri={albumCoverUrl(config.serverUrl, song.album.id)}
hasCover={song.album.cover != null}
style={styles.rowArtwork}
/>
{inset ? (
<View style={[styles.insetContent, { borderBottomColor: divider }]}>
{content}
</View>
) : (
content
)}
</>
)
const rowStyle = inset
? styles.insetRow
: [styles.row, { borderBottomColor: divider }]
const row =
swipe && Platform.OS !== "web" ? (
<SongSwipeRow
group={swipe.group}
onPlayNext={swipe.onPlayNext}
onAddToQueue={swipe.onAddToQueue}
onRemove={swipe.onRemove}
downloaded={downloaded}
onDownload={() => toggleDownload(song, downloaded, downloadFrom)}
backgroundColor={swipe.backgroundColor}
onPress={onPress}
style={rowStyle}
>
{rowContent}
</SongSwipeRow>
) : (
<Pressable
style={rowStyle}
onPress={onPress}
onLongPress={contextMenu ? consumeLongPress : undefined}
>
{rowContent}
</Pressable>
)
if (!contextMenu) {
return (
<MusicEntityView kind="song" id={song.id}>
{row}
</MusicEntityView>
)
}
return (
<MediaContextMenu {...contextMenu}>
<MusicEntityView kind="song" id={song.id}>
{row}
</MusicEntityView>
</MediaContextMenu>
)
})
/**
* A `SongRow` with the standard song context menu. Pass stable `onPlay` and
* `onAction` callbacks so rows in long lists only re-render when their own
* song or favorite state changes.
*/
export const SongListRow = memo(function SongListRow({
song,
favorited,
playlistLabel,
palette,
inset,
swipe,
removable,
downloadFrom,
onPlay,
onAction,
}: {
song: BrowseSong
favorited: boolean
playlistLabel?: string
palette?: Pick<CoverPalette, "foreground" | "secondaryForeground" | "divider">
inset?: boolean
swipe?: {
group: SwipeRowGroup
onPlayNext: () => void
onAddToQueue: () => void
onRemove?: () => void
backgroundColor: string
}
/** Shows "Remove from Playlist" in the context menu. */
removable?: boolean
/** The download this row belongs to; removing only takes it out of that. */
downloadFrom?: DownloadSource
onPlay: (song: BrowseSong) => void
onAction: (song: BrowseSong, action: MediaAction) => void
}) {
const { config } = useServerConfig()
const downloaded = useSongDownloadState(song.id, downloadFrom) === "here"
const handlePress = useCallback(() => onPlay(song), [onPlay, song])
return (
<SongRow
song={song}
palette={palette}
inset={inset}
swipe={swipe}
downloadFrom={downloadFrom}
onPress={handlePress}
contextMenu={{
preview: config
? {
kind: "song",
title: song.title,
artist: song.artist.name,
album: song.album.title,
artworkUrl: albumCoverUrl(config.serverUrl, song.album.id),
hasArtwork: song.album.cover != null,
}
: undefined,
stationArtistId: song.artist.id,
favorited,
downloaded,
playlistLabel,
showRemoveFromPlaylist: removable,
navigation: {
artist: { id: song.artist.id },
album: { id: song.album.id },
},
share: {
type: "song",
albumId: song.album.id,
songId: song.id,
title: `${song.title} - ${song.album.title}`,
},
onAction: (action) => onAction(song, action),
}}
/>
)
})
const tabletPlayerInset = tabletMiniPlayerHeight + spacing.sm + spacing.lg
export const tabContentBottomInset =
Platform.OS === "ios" && Platform.isPad ? tabletPlayerInset : spacing.lg
export const contentBottomInset =
Platform.OS === "ios" && Platform.isPad
? tabletPlayerInset
: Platform.OS === "android"
? spacing.lg
: miniPlayerHeight + spacing.lg
const styles = StyleSheet.create({
row: {
flexDirection: "row",
alignItems: "center",
gap: spacing.md,
minHeight: 64,
paddingVertical: 10,
paddingHorizontal: spacing.lg,
borderBottomWidth: StyleSheet.hairlineWidth,
},
insetRow: {
flexDirection: "row",
alignItems: "center",
gap: spacing.md,
paddingLeft: collectionInset,
},
insetContent: {
flex: 1,
minWidth: 0,
flexDirection: "row",
alignItems: "center",
alignSelf: "stretch",
gap: spacing.md,
minHeight: 64,
paddingVertical: 10,
marginRight: collectionInset,
borderBottomWidth: StyleSheet.hairlineWidth,
},
rowArtwork: {
width: 44,
height: 44,
borderRadius: 6,
},
rowMeta: {
flex: 1,
minWidth: 0,
},
rowTitle: {
fontSize: 16,
fontWeight: "500",
},
rowSubtitle: {
fontSize: 14,
marginTop: 3,
},
duration: {
fontSize: 12,
marginRight: spacing.xs,
},
})