177 lines
5.5 KiB
TypeScript
177 lines
5.5 KiB
TypeScript
import { useMemo } from "react"
|
|
import {
|
|
FlatList,
|
|
Pressable,
|
|
StyleSheet,
|
|
Text,
|
|
View,
|
|
type ListRenderItemInfo,
|
|
} from "react-native"
|
|
import { Stack } from "expo-router"
|
|
|
|
import { CoverImage } from "@/components/cover-image"
|
|
import { tabContentBottomInset } from "@/components/player"
|
|
import {
|
|
FAVORITES_DOWNLOAD_ID,
|
|
useDownloads,
|
|
useDownloadsVersion,
|
|
} from "@/contexts/downloads"
|
|
import { useServerConfig } from "@/contexts/server-config"
|
|
import { useMediaGridLayout } from "@/hooks/use-media-grid-layout"
|
|
import { useAppNavigation } from "@/lib/app-navigation"
|
|
import {
|
|
FROM_DOWNLOADS_PARAM,
|
|
OTHER_DOWNLOADS_TITLE,
|
|
} from "@/lib/downloads-navigation"
|
|
import { radius, spacing, useThemeColors } from "@/lib/theme"
|
|
import { albumCoverUrl, playlistCoverUrl } from "@/lib/urls"
|
|
import type { DownloadedCollection } from "@otunes/convex/client/download-queue"
|
|
import { formatBytes } from "@otunes/convex/download-quality"
|
|
|
|
type Card =
|
|
| { kind: "other"; songCount: number; bytes: number }
|
|
| { kind: "collection"; collection: DownloadedCollection; bytes: number }
|
|
|
|
function songCount(count: number) {
|
|
return count === 1 ? "1 song" : `${count} songs`
|
|
}
|
|
|
|
function collectionPath({ kind, id }: DownloadedCollection) {
|
|
if (kind === "album") return `/album/${id}?${FROM_DOWNLOADS_PARAM}`
|
|
if (id === FAVORITES_DOWNLOAD_ID) {
|
|
return `/playlist/favorites?${FROM_DOWNLOADS_PARAM}`
|
|
}
|
|
return `/playlist/${id}?${FROM_DOWNLOADS_PARAM}`
|
|
}
|
|
|
|
/** Downloaded albums and playlists, newest first, after Other Downloaded Songs. */
|
|
export default function DownloadsScreen() {
|
|
const colors = useThemeColors()
|
|
const { config } = useServerConfig()
|
|
const { queue } = useDownloads()
|
|
const version = useDownloadsVersion()
|
|
const { push } = useAppNavigation()
|
|
const { columns, cardWidth, onLayout, horizontalPadding, gap } =
|
|
useMediaGridLayout()
|
|
|
|
const cards = useMemo<Card[]>(() => {
|
|
if (!queue) return []
|
|
const breakdown = queue.breakdown()
|
|
const bytesByCollection = new Map(
|
|
breakdown.collections.map((row) => [row.collection, row.bytes])
|
|
)
|
|
const other: Card[] =
|
|
breakdown.otherSongs.songIds.length > 0
|
|
? [
|
|
{
|
|
kind: "other",
|
|
songCount: breakdown.otherSongs.songIds.length,
|
|
bytes: breakdown.otherSongs.bytes,
|
|
},
|
|
]
|
|
: []
|
|
return [
|
|
...other,
|
|
...queue
|
|
.collections()
|
|
.reverse()
|
|
.map((collection): Card => ({
|
|
kind: "collection",
|
|
collection,
|
|
bytes: bytesByCollection.get(collection) ?? 0,
|
|
})),
|
|
]
|
|
// version invalidates the lookup when downloads change.
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [queue, version])
|
|
|
|
function renderItem({ item }: ListRenderItemInfo<Card>) {
|
|
if (!config) return null
|
|
const isOther = item.kind === "other"
|
|
const collection = isOther ? null : item.collection
|
|
const isFavorites = collection?.id === FAVORITES_DOWNLOAD_ID
|
|
const title = collection?.title ?? OTHER_DOWNLOADS_TITLE
|
|
const subtitle = isOther
|
|
? `${songCount(item.songCount)} · ${formatBytes(item.bytes)}`
|
|
: `${collection!.kind === "album" ? "Album" : "Playlist"} · ${formatBytes(item.bytes)}`
|
|
const coverUri = !collection
|
|
? ""
|
|
: collection.kind === "album"
|
|
? albumCoverUrl(config.serverUrl, collection.id)
|
|
: playlistCoverUrl(config.serverUrl, collection.id, null)
|
|
return (
|
|
<Pressable
|
|
accessibilityRole="button"
|
|
style={[styles.card, { width: cardWidth }]}
|
|
onPress={() =>
|
|
push(collection ? collectionPath(collection) : "/downloads/other")
|
|
}
|
|
>
|
|
<CoverImage
|
|
uri={coverUri}
|
|
hasCover={collection !== null && !isFavorites}
|
|
placeholderIcon={isOther ? "arrow-down" : "star"}
|
|
style={{
|
|
width: cardWidth,
|
|
height: cardWidth,
|
|
borderRadius: radius.md,
|
|
}}
|
|
/>
|
|
<Text
|
|
style={[styles.title, { color: colors.foreground }]}
|
|
numberOfLines={1}
|
|
>
|
|
{title}
|
|
</Text>
|
|
<Text
|
|
style={[styles.subtitle, { color: colors.mutedForeground }]}
|
|
numberOfLines={1}
|
|
>
|
|
{subtitle}
|
|
</Text>
|
|
</Pressable>
|
|
)
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<Stack.Screen options={{ title: "Downloads", headerLargeTitle: true }} />
|
|
<FlatList
|
|
key={columns}
|
|
onLayout={onLayout}
|
|
data={cards}
|
|
keyExtractor={(card) =>
|
|
card.kind === "other"
|
|
? "other"
|
|
: `${card.collection.kind}:${card.collection.id}`
|
|
}
|
|
renderItem={renderItem}
|
|
numColumns={columns}
|
|
style={{ flex: 1, backgroundColor: colors.background }}
|
|
contentContainerStyle={{
|
|
paddingTop: spacing.lg,
|
|
paddingHorizontal: horizontalPadding,
|
|
paddingBottom: tabContentBottomInset,
|
|
gap,
|
|
}}
|
|
columnWrapperStyle={columns > 1 ? { gap } : undefined}
|
|
contentInsetAdjustmentBehavior="automatic"
|
|
ListEmptyComponent={
|
|
<View style={styles.empty}>
|
|
<Text style={{ color: colors.mutedForeground }}>
|
|
Nothing downloaded on this device.
|
|
</Text>
|
|
</View>
|
|
}
|
|
/>
|
|
</>
|
|
)
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
card: { gap: spacing.xs },
|
|
title: { fontSize: 14, fontWeight: "600" },
|
|
subtitle: { fontSize: 12, fontVariant: ["tabular-nums"] },
|
|
empty: { alignItems: "center", paddingTop: spacing.xl },
|
|
})
|