173 lines
4.2 KiB
TypeScript
173 lines
4.2 KiB
TypeScript
import { Ionicons } from "@expo/vector-icons"
|
|
import {
|
|
Alert,
|
|
Pressable,
|
|
StyleSheet,
|
|
Text,
|
|
View,
|
|
type StyleProp,
|
|
type ViewStyle,
|
|
} from "react-native"
|
|
import Svg, { Circle, Rect } from "react-native-svg"
|
|
|
|
import { AudioFormatBadge } from "@/components/audio-format-badge"
|
|
import {
|
|
useDownloads,
|
|
useDownloadsVersion,
|
|
useIsCollectionDownloaded,
|
|
type DownloadTarget,
|
|
} from "@/contexts/downloads"
|
|
import type { CollectionRef } from "@otunes/convex/client/download-queue"
|
|
import { DOWNLOAD_QUALITY_LABELS } from "@otunes/convex/download-quality"
|
|
|
|
const SIZE = 22
|
|
const RADIUS = 9
|
|
const RING_LENGTH = 2 * Math.PI * RADIUS
|
|
|
|
/** Static ring that fills as songs finish; the square means "tap to stop". */
|
|
function ProgressRing({
|
|
fraction,
|
|
color,
|
|
}: {
|
|
fraction: number
|
|
color: string
|
|
}) {
|
|
return (
|
|
<Svg width={SIZE} height={SIZE} viewBox="0 0 22 22">
|
|
<Circle
|
|
cx={11}
|
|
cy={11}
|
|
r={RADIUS}
|
|
fill="none"
|
|
stroke={color}
|
|
strokeOpacity={0.3}
|
|
strokeWidth={2}
|
|
/>
|
|
<Circle
|
|
cx={11}
|
|
cy={11}
|
|
r={RADIUS}
|
|
fill="none"
|
|
stroke={color}
|
|
strokeWidth={2}
|
|
strokeDasharray={RING_LENGTH}
|
|
strokeDashoffset={RING_LENGTH * (1 - fraction)}
|
|
transform="rotate(-90 11 11)"
|
|
/>
|
|
<Rect x={8} y={8} width={6} height={6} fill={color} />
|
|
</Svg>
|
|
)
|
|
}
|
|
|
|
export type CollectionDownloadTarget = Exclude<
|
|
DownloadTarget,
|
|
{ kind: "songs" }
|
|
>
|
|
|
|
/** Download, progress, and downloaded states for an album or playlist hero, beside Play. */
|
|
export function CollectionDownloadButton({
|
|
target,
|
|
color,
|
|
style,
|
|
}: {
|
|
target: CollectionDownloadTarget
|
|
color: string
|
|
style?: StyleProp<ViewStyle>
|
|
}) {
|
|
const { queue, requestDownload, removeDownload } = useDownloads()
|
|
useDownloadsVersion()
|
|
const collection = queue?.getCollection(target)
|
|
const status = queue?.status(target.songIds)
|
|
const inProgress = collection !== undefined && (status?.queued ?? 0) > 0
|
|
const done = collection !== undefined && !inProgress
|
|
|
|
function handlePress() {
|
|
if (inProgress) {
|
|
void removeDownload(target)
|
|
} else if (done) {
|
|
Alert.alert("Remove download?", undefined, [
|
|
{ text: "Cancel", style: "cancel" },
|
|
{
|
|
text: "Remove",
|
|
style: "destructive",
|
|
onPress: () => void removeDownload(target),
|
|
},
|
|
])
|
|
} else {
|
|
void requestDownload(target)
|
|
}
|
|
}
|
|
|
|
return (
|
|
<Pressable
|
|
hitSlop={8}
|
|
accessibilityRole="button"
|
|
accessibilityLabel={
|
|
inProgress
|
|
? "Stop downloading"
|
|
: done
|
|
? `Remove download of ${target.title}`
|
|
: `Download ${target.title}`
|
|
}
|
|
disabled={target.songIds.length === 0}
|
|
onPress={handlePress}
|
|
style={style}
|
|
>
|
|
{inProgress && status ? (
|
|
<ProgressRing
|
|
fraction={status.downloaded / status.total}
|
|
color={color}
|
|
/>
|
|
) : (
|
|
<Ionicons
|
|
name={done ? "checkmark" : "arrow-down"}
|
|
size={24}
|
|
color={color}
|
|
/>
|
|
)}
|
|
</Pressable>
|
|
)
|
|
}
|
|
|
|
/** The quality an album or playlist was downloaded in, once downloaded. */
|
|
export function DownloadedQuality({
|
|
collection,
|
|
color,
|
|
backgroundColor,
|
|
}: {
|
|
collection: CollectionRef
|
|
color: string
|
|
backgroundColor: string
|
|
}) {
|
|
const { queue } = useDownloads()
|
|
const downloaded = useIsCollectionDownloaded(collection)
|
|
const quality = downloaded
|
|
? queue?.getCollection(collection)?.quality
|
|
: undefined
|
|
if (!quality) return null
|
|
return (
|
|
<View
|
|
style={styles.quality}
|
|
accessible
|
|
accessibilityLabel={`Downloaded in ${DOWNLOAD_QUALITY_LABELS[quality]}`}
|
|
>
|
|
<Ionicons name="arrow-down-circle" size={14} color={color} />
|
|
{quality === "lossless" || quality === "atmos" ? (
|
|
<AudioFormatBadge
|
|
format={quality}
|
|
color={color}
|
|
backgroundColor={backgroundColor}
|
|
/>
|
|
) : (
|
|
<Text style={{ color, fontSize: 13 }}>
|
|
{DOWNLOAD_QUALITY_LABELS[quality]}
|
|
</Text>
|
|
)}
|
|
</View>
|
|
)
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
quality: { flexDirection: "row", alignItems: "center", gap: 4 },
|
|
})
|