feat(mobile): swipe to remove songs (#9)
Co-authored-by: Rem Helper <remhelper@abunchofknowitalls.com> Reviewed-on: https://git.abunchofknowitalls.com/joshuafhiggins/otunes/pulls/9
This commit was merged in pull request #9.
This commit is contained in:
1 parent
d86a448a2b
commit
fe07c2b52f
1 file changed
+348
-97
@@ -1,6 +1,26 @@
|
||||
import { useMutation, usePaginatedQuery, useQuery } from "convex/react"
|
||||
import {
|
||||
Button as SwiftUIButton,
|
||||
ContextMenu,
|
||||
Host,
|
||||
List,
|
||||
RNHostView,
|
||||
SwipeActions,
|
||||
} from "@expo/ui/swift-ui"
|
||||
import {
|
||||
frame,
|
||||
listRowBackground,
|
||||
listRowInsets,
|
||||
listRowSeparator,
|
||||
listSectionSpacing,
|
||||
listRowSpacing,
|
||||
listStyle,
|
||||
onAppear,
|
||||
scrollContentBackground,
|
||||
} from "@expo/ui/swift-ui/modifiers"
|
||||
import { Stack, useLocalSearchParams, useRouter } from "expo-router"
|
||||
import { FlatList, Pressable, StyleSheet, Text, View } from "react-native"
|
||||
import { FlatList, Platform, Pressable, StyleSheet, Text, View } from "react-native"
|
||||
import ReanimatedSwipeable from "react-native-gesture-handler/ReanimatedSwipeable"
|
||||
import { Ionicons } from "@expo/vector-icons"
|
||||
|
||||
import { CoverImage } from "@/components/cover-image"
|
||||
@@ -18,11 +38,14 @@ import { useCoverBackgroundColor } from "@/hooks/use-cover-background-color"
|
||||
import { useMediaMenuActions } from "@/hooks/use-media-menu-actions"
|
||||
import { api } from "@otunes/convex/_generated/api"
|
||||
import type { Id } from "@otunes/convex/_generated/dataModel"
|
||||
import type { BrowseSong } from "@otunes/convex/types"
|
||||
import { shareMediaLink } from "@/lib/share-media-link"
|
||||
import { showError, showSuccess } from "@/lib/toast"
|
||||
import { playlistCoverUrl } from "@/lib/urls"
|
||||
import { radius, spacing, useThemeColors } from "@/lib/theme"
|
||||
|
||||
const PAGE_SIZE = 50
|
||||
const NATIVE_LOAD_MORE_THRESHOLD = 10
|
||||
|
||||
export default function PlaylistScreen() {
|
||||
const { id } = useLocalSearchParams<{ id: string }>()
|
||||
@@ -88,6 +111,11 @@ export default function PlaylistScreen() {
|
||||
}
|
||||
|
||||
const songIds = allSongIds
|
||||
const serverUrl = config.serverUrl
|
||||
|
||||
function maybeLoadMore() {
|
||||
if (status === "CanLoadMore") loadMore(PAGE_SIZE)
|
||||
}
|
||||
|
||||
async function handleSubscribeToggle() {
|
||||
if (!playlist?.id) return
|
||||
@@ -124,6 +152,109 @@ export default function PlaylistScreen() {
|
||||
void handlePlaylistDetailAction(songIds, action)
|
||||
}
|
||||
|
||||
async function handleRemoveSong(songId: Id<"songs">) {
|
||||
if (!playlist?.id) return
|
||||
try {
|
||||
await removeSongs({ playlistId: playlist.id, songIds: [songId] })
|
||||
showSuccess("Removed from playlist")
|
||||
} catch (error) {
|
||||
showError(error instanceof Error ? error.message : "Failed to remove from playlist")
|
||||
}
|
||||
}
|
||||
|
||||
function renderSongRow(song: BrowseSong, includeContextMenu = true) {
|
||||
const row = (
|
||||
<SongRow
|
||||
song={song}
|
||||
onPress={() => void playFromSong(song.id, songIds)}
|
||||
contextMenu={
|
||||
includeContextMenu
|
||||
? {
|
||||
favorited: isSongFavorite(song.id),
|
||||
playlistLabel: "Add to Another Playlist",
|
||||
navigation: {
|
||||
artist: { id: song.artist.id },
|
||||
album: { id: song.album.id },
|
||||
},
|
||||
share: {
|
||||
type: "song",
|
||||
albumId: song.album.id,
|
||||
songId: song.id,
|
||||
},
|
||||
onAction: (action) => void handleSongAction(song, action),
|
||||
}
|
||||
: undefined
|
||||
}
|
||||
/>
|
||||
)
|
||||
|
||||
return row
|
||||
}
|
||||
|
||||
function renderNativeSongContextMenu(song: BrowseSong) {
|
||||
return (
|
||||
<ContextMenu.Items>
|
||||
<SwiftUIButton
|
||||
label="Share"
|
||||
systemImage="square.and.arrow.up"
|
||||
onPress={() =>
|
||||
void shareMediaLink(serverUrl, {
|
||||
type: "song",
|
||||
albumId: song.album.id,
|
||||
songId: song.id,
|
||||
})
|
||||
}
|
||||
/>
|
||||
<SwiftUIButton
|
||||
label="Add to Another Playlist"
|
||||
systemImage="text.badge.plus"
|
||||
onPress={() => void handleSongAction(song, "playlist")}
|
||||
/>
|
||||
<SwiftUIButton
|
||||
label="Play Next"
|
||||
systemImage="text.line.first.and.arrowtriangle.forward"
|
||||
onPress={() => void handleSongAction(song, "play-next")}
|
||||
/>
|
||||
<SwiftUIButton
|
||||
label="Play Last"
|
||||
systemImage="text.line.last.and.arrowtriangle.forward"
|
||||
onPress={() => void handleSongAction(song, "play-last")}
|
||||
/>
|
||||
<SwiftUIButton
|
||||
label="Shuffle"
|
||||
systemImage="shuffle"
|
||||
onPress={() => void handleSongAction(song, "shuffle")}
|
||||
/>
|
||||
<SwiftUIButton
|
||||
label="Go to Artist"
|
||||
systemImage="person"
|
||||
onPress={() => void handleSongAction(song, "go-artist")}
|
||||
/>
|
||||
<SwiftUIButton
|
||||
label="Go to Album"
|
||||
systemImage="opticaldisc"
|
||||
onPress={() => void handleSongAction(song, "go-album")}
|
||||
/>
|
||||
<SwiftUIButton
|
||||
label={isSongFavorite(song.id) ? "Unfavorite" : "Favorite"}
|
||||
systemImage={isSongFavorite(song.id) ? "star.fill" : "star"}
|
||||
onPress={() => void handleSongAction(song, "favorite")}
|
||||
/>
|
||||
</ContextMenu.Items>
|
||||
)
|
||||
}
|
||||
|
||||
const nativeListRowModifiers = [
|
||||
listRowBackground(backgroundColor),
|
||||
listRowInsets({ top: 0, leading: 0, bottom: 0, trailing: 0 }),
|
||||
listRowSeparator("hidden"),
|
||||
]
|
||||
const nativeSongRowModifiers = [
|
||||
frame({ height: 32 }),
|
||||
]
|
||||
const shouldUseNativeSwipeList =
|
||||
Platform.OS === "ios" && playlist.isOwner && playlist.id
|
||||
|
||||
return (
|
||||
<>
|
||||
<Stack.Screen
|
||||
@@ -160,109 +291,210 @@ export default function PlaylistScreen() {
|
||||
),
|
||||
}}
|
||||
/>
|
||||
<FlatList
|
||||
data={songs}
|
||||
keyExtractor={(song) => song.id}
|
||||
style={{ flex: 1, backgroundColor }}
|
||||
contentContainerStyle={{ paddingBottom: contentBottomInset }}
|
||||
contentInsetAdjustmentBehavior="automatic"
|
||||
onEndReached={() => {
|
||||
if (status === "CanLoadMore") loadMore(PAGE_SIZE)
|
||||
}}
|
||||
onEndReachedThreshold={0.5}
|
||||
ListHeaderComponent={
|
||||
<View style={styles.hero}>
|
||||
<CoverImage
|
||||
uri={coverUri}
|
||||
hasCover={playlist.cover != null}
|
||||
style={styles.cover}
|
||||
/>
|
||||
<Text style={[styles.title, { color: colors.foreground }]}>{playlist.title}</Text>
|
||||
<Text style={{ color: colors.mutedForeground }}>{playlist.owner.name}</Text>
|
||||
<View style={styles.actions}>
|
||||
{!playlist.isOwner && playlist.id ? (
|
||||
<Pressable
|
||||
accessibilityRole="button"
|
||||
accessibilityLabel={playlist.isSubscribed ? "Subscribed" : "Subscribe"}
|
||||
accessibilityState={{ selected: playlist.isSubscribed }}
|
||||
hitSlop={8}
|
||||
onPress={() => void handleSubscribeToggle()}
|
||||
style={[
|
||||
styles.iconAction,
|
||||
{
|
||||
backgroundColor: playlist.isSubscribed
|
||||
? colors.muted
|
||||
: colors.primary,
|
||||
},
|
||||
{shouldUseNativeSwipeList ? (
|
||||
<Host style={[styles.iosSwipeListHost, { backgroundColor }]}>
|
||||
<List
|
||||
modifiers={[
|
||||
listStyle("plain"),
|
||||
listSectionSpacing(0),
|
||||
listRowSpacing(0),
|
||||
scrollContentBackground("hidden"),
|
||||
]}
|
||||
>
|
||||
<SwipeActions modifiers={nativeListRowModifiers}>
|
||||
<RNHostView matchContents>
|
||||
<PlaylistHero
|
||||
coverUri={coverUri}
|
||||
hasCover={playlist.cover != null}
|
||||
title={playlist.title}
|
||||
ownerName={playlist.owner.name}
|
||||
colors={colors}
|
||||
onPlay={() => playlist.id && void playPlaylist(playlist.id)}
|
||||
onShuffle={() => playlist.id && void playPlaylistShuffled(playlist.id)}
|
||||
/>
|
||||
</RNHostView>
|
||||
</SwipeActions>
|
||||
{songs.map((song, index) => {
|
||||
const nearEnd = index >= songs.length - NATIVE_LOAD_MORE_THRESHOLD
|
||||
return (
|
||||
<SwipeActions
|
||||
key={song.id}
|
||||
modifiers={[
|
||||
...nativeSongRowModifiers,
|
||||
...(nearEnd ? [onAppear(maybeLoadMore)] : []),
|
||||
]}
|
||||
>
|
||||
<Ionicons
|
||||
name={playlist.isSubscribed ? "checkmark" : "add"}
|
||||
size={24}
|
||||
color={
|
||||
playlist.isSubscribed
|
||||
? colors.foreground
|
||||
: colors.primaryForeground
|
||||
}
|
||||
/>
|
||||
</Pressable>
|
||||
) : null}
|
||||
<Button
|
||||
title="Play"
|
||||
icon="play"
|
||||
onPress={() => playlist.id && void playPlaylist(playlist.id)}
|
||||
style={styles.playButton}
|
||||
/>
|
||||
<Pressable
|
||||
accessibilityRole="button"
|
||||
accessibilityLabel="Shuffle"
|
||||
hitSlop={8}
|
||||
onPress={() => playlist.id && void playPlaylistShuffled(playlist.id)}
|
||||
style={[styles.iconAction, { backgroundColor: colors.muted }]}
|
||||
>
|
||||
<Ionicons name="shuffle" size={24} color={colors.foreground} />
|
||||
</Pressable>
|
||||
</View>
|
||||
</View>
|
||||
}
|
||||
renderItem={({ item: song }) => (
|
||||
<SongRow
|
||||
song={song}
|
||||
onPress={() => void playFromSong(song.id, songIds)}
|
||||
contextMenu={{
|
||||
favorited: isSongFavorite(song.id),
|
||||
playlistLabel: "Add to Another Playlist",
|
||||
navigation: {
|
||||
artist: { id: song.artist.id },
|
||||
album: { id: song.album.id },
|
||||
},
|
||||
share: {
|
||||
type: "song",
|
||||
albumId: song.album.id,
|
||||
songId: song.id,
|
||||
},
|
||||
onAction: (action) => void handleSongAction(song, action),
|
||||
}}
|
||||
trailing={
|
||||
playlist.isOwner && playlist.id ? (
|
||||
<Pressable
|
||||
onPress={() =>
|
||||
void removeSongs({ playlistId: playlist.id!, songIds: [song.id] }).then(() =>
|
||||
showSuccess("Removed from playlist")
|
||||
)
|
||||
}
|
||||
>
|
||||
<Ionicons name="trash-outline" size={18} color={colors.destructive} />
|
||||
</Pressable>
|
||||
) : null
|
||||
<ContextMenu>
|
||||
<ContextMenu.Trigger>
|
||||
<RNHostView matchContents>
|
||||
<View style={styles.nativeSongRow}>
|
||||
{renderSongRow(song, false)}
|
||||
</View>
|
||||
</RNHostView>
|
||||
</ContextMenu.Trigger>
|
||||
{renderNativeSongContextMenu(song)}
|
||||
</ContextMenu>
|
||||
<SwipeActions.Actions edge="trailing">
|
||||
<SwiftUIButton
|
||||
label=""
|
||||
systemImage="minus.circle.fill"
|
||||
role="destructive"
|
||||
onPress={() => void handleRemoveSong(song.id)}
|
||||
/>
|
||||
</SwipeActions.Actions>
|
||||
</SwipeActions>
|
||||
)
|
||||
})}
|
||||
</List>
|
||||
</Host>
|
||||
) : (
|
||||
<FlatList
|
||||
data={songs}
|
||||
keyExtractor={(song) => song.id}
|
||||
style={{ flex: 1, backgroundColor }}
|
||||
contentContainerStyle={{ paddingBottom: contentBottomInset }}
|
||||
contentInsetAdjustmentBehavior="automatic"
|
||||
onEndReached={maybeLoadMore}
|
||||
onEndReachedThreshold={0.5}
|
||||
ListHeaderComponent={
|
||||
<PlaylistHero
|
||||
coverUri={coverUri}
|
||||
hasCover={playlist.cover != null}
|
||||
title={playlist.title}
|
||||
ownerName={playlist.owner.name}
|
||||
colors={colors}
|
||||
isSubscribed={playlist.isSubscribed}
|
||||
showSubscribe={!playlist.isOwner && !!playlist.id}
|
||||
onSubscribeToggle={() => void handleSubscribeToggle()}
|
||||
onPlay={() => playlist.id && void playPlaylist(playlist.id)}
|
||||
onShuffle={() => playlist.id && void playPlaylistShuffled(playlist.id)}
|
||||
/>
|
||||
}
|
||||
renderItem={({ item: song }) => {
|
||||
const row = renderSongRow(song)
|
||||
|
||||
if (!playlist.isOwner || !playlist.id) {
|
||||
return <View>{row}</View>
|
||||
}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
|
||||
return (
|
||||
<ReanimatedSwipeable
|
||||
friction={2}
|
||||
rightThreshold={56}
|
||||
overshootRight={false}
|
||||
enableTrackpadTwoFingerGesture
|
||||
containerStyle={{ backgroundColor: colors.destructive }}
|
||||
childrenContainerStyle={{ backgroundColor }}
|
||||
renderRightActions={(_progress, _translation, swipeable) => (
|
||||
<Pressable
|
||||
accessibilityRole="button"
|
||||
accessibilityLabel={`Remove ${song.title} from playlist`}
|
||||
onPress={() => {
|
||||
swipeable.close()
|
||||
void handleRemoveSong(song.id)
|
||||
}}
|
||||
style={[
|
||||
styles.removeAction,
|
||||
{ backgroundColor: colors.destructive },
|
||||
]}
|
||||
>
|
||||
<Ionicons name="trash" size={22} color={colors.primaryForeground} />
|
||||
<Text style={[styles.removeActionText, { color: colors.primaryForeground }]}>
|
||||
Remove
|
||||
</Text>
|
||||
</Pressable>
|
||||
)}
|
||||
>
|
||||
{row}
|
||||
</ReanimatedSwipeable>
|
||||
)
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
function PlaylistHero({
|
||||
coverUri,
|
||||
hasCover,
|
||||
title,
|
||||
ownerName,
|
||||
colors,
|
||||
isSubscribed,
|
||||
showSubscribe = false,
|
||||
onSubscribeToggle,
|
||||
onPlay,
|
||||
onShuffle,
|
||||
}: {
|
||||
coverUri: string
|
||||
hasCover: boolean
|
||||
title: string
|
||||
ownerName: string
|
||||
colors: ReturnType<typeof useThemeColors>
|
||||
isSubscribed?: boolean
|
||||
showSubscribe?: boolean
|
||||
onSubscribeToggle?: () => void
|
||||
onPlay: () => void
|
||||
onShuffle: () => void
|
||||
}) {
|
||||
return (
|
||||
<View style={styles.hero}>
|
||||
<CoverImage
|
||||
uri={coverUri}
|
||||
hasCover={hasCover}
|
||||
style={styles.cover}
|
||||
/>
|
||||
<Text style={[styles.title, { color: colors.foreground }]}>{title}</Text>
|
||||
<Text style={{ color: colors.mutedForeground }}>{ownerName}</Text>
|
||||
<View style={styles.actions}>
|
||||
{showSubscribe ? (
|
||||
<Pressable
|
||||
accessibilityRole="button"
|
||||
accessibilityLabel={isSubscribed ? "Subscribed" : "Subscribe"}
|
||||
accessibilityState={{ selected: isSubscribed }}
|
||||
hitSlop={8}
|
||||
onPress={onSubscribeToggle}
|
||||
style={[
|
||||
styles.iconAction,
|
||||
{
|
||||
backgroundColor: isSubscribed
|
||||
? colors.muted
|
||||
: colors.primary,
|
||||
},
|
||||
]}
|
||||
>
|
||||
<Ionicons
|
||||
name={isSubscribed ? "checkmark" : "add"}
|
||||
size={24}
|
||||
color={
|
||||
isSubscribed
|
||||
? colors.foreground
|
||||
: colors.primaryForeground
|
||||
}
|
||||
/>
|
||||
</Pressable>
|
||||
) : null}
|
||||
<Button
|
||||
title="Play"
|
||||
icon="play"
|
||||
onPress={onPlay}
|
||||
style={styles.playButton}
|
||||
/>
|
||||
<Pressable
|
||||
accessibilityRole="button"
|
||||
accessibilityLabel="Shuffle"
|
||||
hitSlop={8}
|
||||
onPress={onShuffle}
|
||||
style={[styles.iconAction, { backgroundColor: colors.muted }]}
|
||||
>
|
||||
<Ionicons name="shuffle" size={24} color={colors.foreground} />
|
||||
</Pressable>
|
||||
</View>
|
||||
</View>
|
||||
)
|
||||
}
|
||||
|
||||
const styles = StyleSheet.create({
|
||||
empty: { flex: 1, alignItems: "center", justifyContent: "center" },
|
||||
hero: { alignItems: "center", padding: spacing.lg, gap: spacing.sm },
|
||||
@@ -287,4 +519,23 @@ const styles = StyleSheet.create({
|
||||
alignItems: "center",
|
||||
justifyContent: "center",
|
||||
},
|
||||
iosSwipeListHost: {
|
||||
flex: 1,
|
||||
alignSelf: "stretch",
|
||||
width: "100%",
|
||||
},
|
||||
nativeSongRow: {
|
||||
height: 64,
|
||||
overflow: "hidden",
|
||||
},
|
||||
removeAction: {
|
||||
width: 96,
|
||||
alignItems: "center",
|
||||
justifyContent: "center",
|
||||
gap: spacing.xs,
|
||||
},
|
||||
removeActionText: {
|
||||
fontSize: 12,
|
||||
fontWeight: "600",
|
||||
},
|
||||
})
|
||||
Reference in new issue
Block a user