Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
154 lines
5.0 KiB
TypeScript
154 lines
5.0 KiB
TypeScript
import { useCallback, useMemo } from "react"
|
|
import {
|
|
FlatList,
|
|
Pressable,
|
|
StyleSheet,
|
|
Text,
|
|
View,
|
|
type ListRenderItemInfo,
|
|
} from "react-native"
|
|
import { Ionicons } from "@expo/vector-icons"
|
|
import { Stack } from "expo-router"
|
|
|
|
import { useSwipeRowGroup } from "@/components/ios-swipe-to-remove-row"
|
|
import type { MediaAction } from "@/components/media-context-menu"
|
|
import { contentBottomInset, SongListRow } from "@/components/player"
|
|
import { Button } from "@/components/ui/primitives"
|
|
import { useDownloads, useDownloadsVersion } from "@/contexts/downloads"
|
|
import { useFavorites } from "@/contexts/favorites"
|
|
import { usePlaybackActions } from "@/contexts/playback"
|
|
import { useMediaMenuActions } from "@/hooks/use-media-menu-actions"
|
|
import { OTHER_DOWNLOADS_TITLE } from "@/lib/downloads-navigation"
|
|
import { longListProps } from "@/lib/list"
|
|
import { useSongs } from "@/lib/song-cache"
|
|
import { spacing, useThemeColors } from "@/lib/theme"
|
|
import type { Id } from "@otunes/convex/_generated/dataModel"
|
|
import { downloadsQueueOrigin } from "@otunes/convex/download-quality"
|
|
import type { BrowseSong } from "@otunes/convex/types"
|
|
|
|
const PLAY_ORIGIN = downloadsQueueOrigin("other")
|
|
|
|
/** Songs downloaded on their own. Playback here always uses the downloads. */
|
|
export default function OtherDownloadsScreen() {
|
|
const colors = useThemeColors()
|
|
const { queue } = useDownloads()
|
|
const version = useDownloadsVersion()
|
|
const songIds = useMemo(
|
|
() =>
|
|
(queue?.otherSongIds() ?? [])
|
|
.filter((songId) => queue?.getDownloaded(songId))
|
|
.reverse() as Id<"songs">[],
|
|
// version invalidates the lookup when downloads change.
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
[queue, version]
|
|
)
|
|
const cachedSongs = useSongs(songIds)
|
|
const songs = useMemo(
|
|
() => cachedSongs.filter((song): song is BrowseSong => song != null),
|
|
[cachedSongs]
|
|
)
|
|
const playableIds = useMemo(() => songs.map((song) => song.id), [songs])
|
|
const { playFromSong, playInOrder, playShuffled } = usePlaybackActions()
|
|
const { isSongFavorite } = useFavorites()
|
|
const { handleSongAction } = useMediaMenuActions()
|
|
const swipeGroup = useSwipeRowGroup()
|
|
|
|
const handlePlaySong = useCallback(
|
|
(song: BrowseSong) => void playFromSong(song.id, playableIds, PLAY_ORIGIN),
|
|
[playFromSong, playableIds]
|
|
)
|
|
const handleAction = useCallback(
|
|
(song: BrowseSong, action: MediaAction) =>
|
|
void handleSongAction(song, action, "other"),
|
|
[handleSongAction]
|
|
)
|
|
|
|
const renderItem = useCallback(
|
|
({ item: song }: ListRenderItemInfo<BrowseSong>) => (
|
|
<SongListRow
|
|
song={song}
|
|
favorited={isSongFavorite(song.id)}
|
|
downloadFrom="other"
|
|
swipe={{
|
|
group: swipeGroup,
|
|
backgroundColor: colors.background,
|
|
onPlayNext: () => handleAction(song, "play-next"),
|
|
onAddToQueue: () => handleAction(song, "play-last"),
|
|
}}
|
|
onPlay={handlePlaySong}
|
|
onAction={handleAction}
|
|
/>
|
|
),
|
|
[
|
|
colors.background,
|
|
handleAction,
|
|
handlePlaySong,
|
|
isSongFavorite,
|
|
swipeGroup,
|
|
]
|
|
)
|
|
|
|
return (
|
|
<>
|
|
<Stack.Screen
|
|
options={{ title: OTHER_DOWNLOADS_TITLE, headerLargeTitle: false }}
|
|
/>
|
|
<FlatList
|
|
{...longListProps}
|
|
data={songs}
|
|
keyExtractor={(song) => song.id}
|
|
renderItem={renderItem}
|
|
style={{ flex: 1, backgroundColor: colors.background }}
|
|
contentContainerStyle={{ paddingBottom: contentBottomInset }}
|
|
contentInsetAdjustmentBehavior="automatic"
|
|
onScrollBeginDrag={swipeGroup.closeOpenRow}
|
|
ListHeaderComponent={
|
|
songs.length > 0 ? (
|
|
<View style={styles.actions}>
|
|
<Pressable
|
|
accessibilityRole="button"
|
|
accessibilityLabel="Shuffle"
|
|
hitSlop={8}
|
|
onPress={() => void playShuffled(playableIds, PLAY_ORIGIN)}
|
|
style={[styles.iconAction, { backgroundColor: colors.muted }]}
|
|
>
|
|
<Ionicons name="shuffle" size={24} color={colors.foreground} />
|
|
</Pressable>
|
|
<Button
|
|
title="Play"
|
|
icon="play"
|
|
onPress={() => void playInOrder(playableIds, PLAY_ORIGIN)}
|
|
style={styles.playButton}
|
|
/>
|
|
</View>
|
|
) : undefined
|
|
}
|
|
ListEmptyComponent={
|
|
<Text style={[styles.empty, { color: colors.mutedForeground }]}>
|
|
Songs you download on their own appear here.
|
|
</Text>
|
|
}
|
|
/>
|
|
</>
|
|
)
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
actions: {
|
|
flexDirection: "row",
|
|
alignItems: "center",
|
|
justifyContent: "center",
|
|
gap: spacing.sm,
|
|
padding: spacing.lg,
|
|
},
|
|
playButton: { borderRadius: 999, minWidth: 160 },
|
|
iconAction: {
|
|
width: 44,
|
|
height: 44,
|
|
borderRadius: 22,
|
|
alignItems: "center",
|
|
justifyContent: "center",
|
|
},
|
|
empty: { padding: spacing.lg, textAlign: "center" },
|
|
})
|