feat(mobile): add songs to queue with a full swipe (#89)
This commit was merged in pull request #89.
This commit is contained in:
1 parent
dce66b7697
commit
7ee1580a72
4 files changed
+178
-35
No files matched your search
@@ -488,7 +488,7 @@ const AlbumTrackRow = memo(function AlbumTrackRow({
|
||||
action: MediaAction
|
||||
) => Promise<void>
|
||||
}) {
|
||||
const Row = Platform.OS === "ios" ? SongSwipeRow : Pressable
|
||||
const Row = Platform.OS !== "web" ? SongSwipeRow : Pressable
|
||||
const { config } = useServerConfig()
|
||||
return (
|
||||
<MediaContextMenu
|
||||
@@ -523,6 +523,7 @@ const AlbumTrackRow = memo(function AlbumTrackRow({
|
||||
group={swipeGroup}
|
||||
backgroundColor={palette.background}
|
||||
onPlayNext={() => void onAction(track, album, "play-next")}
|
||||
onAddToQueue={() => void onAction(track, album, "play-last")}
|
||||
style={styles.trackRow}
|
||||
onPress={() => void onPlay(track.id as Id<"songs">, songIds)}
|
||||
>
|
||||
|
||||
@@ -434,11 +434,18 @@ const PlaylistSongRow = memo(function PlaylistSongRow({
|
||||
palette={palette}
|
||||
inset
|
||||
swipe={
|
||||
Platform.OS === "ios"
|
||||
Platform.OS !== "web"
|
||||
? {
|
||||
group: swipeGroup,
|
||||
backgroundColor: palette.background,
|
||||
onPlayNext: () => onAction(song, "play-next"),
|
||||
onAddToQueue: () => onAction(song, "play-last"),
|
||||
onRemove:
|
||||
Platform.OS === "android" && removable
|
||||
? () => {
|
||||
void onRemove(song.id)
|
||||
}
|
||||
: undefined,
|
||||
}
|
||||
: undefined
|
||||
}
|
||||
@@ -447,7 +454,7 @@ const PlaylistSongRow = memo(function PlaylistSongRow({
|
||||
/>
|
||||
)
|
||||
|
||||
if (Platform.OS === "ios" || !removable) {
|
||||
if (Platform.OS !== "web" || !removable) {
|
||||
return row
|
||||
}
|
||||
|
||||
|
||||
@@ -45,6 +45,8 @@ export const SongRow = memo(function SongRow({
|
||||
swipe?: {
|
||||
group: SwipeRowGroup
|
||||
onPlayNext: () => void
|
||||
onAddToQueue: () => void
|
||||
onRemove?: () => void
|
||||
backgroundColor: string
|
||||
}
|
||||
/** Album/playlist layout: the divider starts at the title, not the artwork. */
|
||||
@@ -107,10 +109,12 @@ export const SongRow = memo(function SongRow({
|
||||
? styles.insetRow
|
||||
: [styles.row, { borderBottomColor: divider }]
|
||||
const row =
|
||||
swipe && Platform.OS === "ios" ? (
|
||||
swipe && Platform.OS !== "web" ? (
|
||||
<SongSwipeRow
|
||||
group={swipe.group}
|
||||
onPlayNext={swipe.onPlayNext}
|
||||
onAddToQueue={swipe.onAddToQueue}
|
||||
onRemove={swipe.onRemove}
|
||||
backgroundColor={swipe.backgroundColor}
|
||||
onPress={onPress}
|
||||
style={rowStyle}
|
||||
@@ -167,6 +171,8 @@ export const SongListRow = memo(function SongListRow({
|
||||
swipe?: {
|
||||
group: SwipeRowGroup
|
||||
onPlayNext: () => void
|
||||
onAddToQueue: () => void
|
||||
onRemove?: () => void
|
||||
backgroundColor: string
|
||||
}
|
||||
onPlay: (song: BrowseSong) => void
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { Ionicons } from "@expo/vector-icons"
|
||||
import * as Haptics from "expo-haptics"
|
||||
import { SymbolView } from "expo-symbols"
|
||||
import {
|
||||
@@ -8,6 +9,7 @@ import {
|
||||
type ReactNode,
|
||||
} from "react"
|
||||
import {
|
||||
Platform,
|
||||
Pressable,
|
||||
StyleSheet,
|
||||
type StyleProp,
|
||||
@@ -26,7 +28,10 @@ import Animated, {
|
||||
import { scheduleOnRN } from "react-native-worklets"
|
||||
import type { SwipeRowGroup } from "@/components/ios-swipe-to-remove-row"
|
||||
|
||||
const IS_ANDROID = Platform.OS === "android"
|
||||
|
||||
const ACTION_WIDTH = 82
|
||||
const LEADING_ACTION_WIDTH = 72
|
||||
const SPRING = { duration: 450, dampingRatio: 1 }
|
||||
|
||||
function impact() {
|
||||
@@ -41,6 +46,8 @@ export function SongSwipeRow({
|
||||
group,
|
||||
onPress,
|
||||
onPlayNext,
|
||||
onAddToQueue,
|
||||
onRemove,
|
||||
}: {
|
||||
children: ReactNode
|
||||
style?: StyleProp<ViewStyle>
|
||||
@@ -48,7 +55,10 @@ export function SongSwipeRow({
|
||||
group: SwipeRowGroup
|
||||
onPress?: () => void
|
||||
onPlayNext: () => void
|
||||
onAddToQueue: () => void
|
||||
onRemove?: () => void
|
||||
}) {
|
||||
const hasRemove = IS_ANDROID && !!onRemove
|
||||
const x = useSharedValue(0)
|
||||
const startX = useSharedValue(0)
|
||||
const width = useSharedValue(0)
|
||||
@@ -78,10 +88,10 @@ export function SongSwipeRow({
|
||||
const activate = useCallback(
|
||||
(side: number) => {
|
||||
close()
|
||||
if (side > 0) onPlayNext()
|
||||
if (side > 0) onAddToQueue()
|
||||
// Download is intentionally a placeholder.
|
||||
},
|
||||
[close, onPlayNext]
|
||||
[close, onAddToQueue]
|
||||
)
|
||||
const tapRow = useCallback(() => {
|
||||
if (open) close()
|
||||
@@ -105,7 +115,13 @@ export function SongSwipeRow({
|
||||
Math.min(width.value, startX.value + event.translationX)
|
||||
)
|
||||
const commit =
|
||||
Math.abs(x.value) > Math.max(ACTION_WIDTH, width.value * 0.55)
|
||||
Math.abs(x.value) >
|
||||
Math.max(
|
||||
x.value > 0
|
||||
? LEADING_ACTION_WIDTH * 2
|
||||
: ACTION_WIDTH * (hasRemove ? 2 : 1),
|
||||
width.value * 0.55
|
||||
)
|
||||
if (commit !== committed.value) {
|
||||
committed.value = commit
|
||||
commitProgress.value = withSpring(commit ? 1 : 0, SPRING)
|
||||
@@ -124,10 +140,16 @@ export function SongSwipeRow({
|
||||
Math.abs(x.value) > 10 && projected * side > ACTION_WIDTH / 2
|
||||
? side
|
||||
: 0
|
||||
x.value = withSpring(target * ACTION_WIDTH, {
|
||||
...SPRING,
|
||||
velocity: event.velocityX,
|
||||
})
|
||||
x.value = withSpring(
|
||||
target *
|
||||
(target > 0
|
||||
? LEADING_ACTION_WIDTH * 2
|
||||
: ACTION_WIDTH * (hasRemove ? 2 : 1)),
|
||||
{
|
||||
...SPRING,
|
||||
velocity: event.velocityX,
|
||||
}
|
||||
)
|
||||
scheduleOnRN(settle, target)
|
||||
})
|
||||
.onFinalize((_event, success) => {
|
||||
@@ -145,6 +167,7 @@ export function SongSwipeRow({
|
||||
return Gesture.Exclusive(pan, tap)
|
||||
}, [
|
||||
activate,
|
||||
hasRemove,
|
||||
begin,
|
||||
close,
|
||||
commitProgress,
|
||||
@@ -158,12 +181,14 @@ export function SongSwipeRow({
|
||||
|
||||
const rowStyle = useAnimatedStyle(() => ({
|
||||
transform: [{ translateX: x.value }],
|
||||
borderRadius: interpolate(
|
||||
Math.abs(x.value),
|
||||
[0, 40],
|
||||
[0, height.value / 2],
|
||||
Extrapolation.CLAMP
|
||||
),
|
||||
borderRadius: IS_ANDROID
|
||||
? 0
|
||||
: interpolate(
|
||||
Math.abs(x.value),
|
||||
[0, 40],
|
||||
[0, height.value / 2],
|
||||
Extrapolation.CLAMP
|
||||
),
|
||||
}))
|
||||
const fillStyle = useAnimatedStyle(() => ({
|
||||
opacity: interpolate(
|
||||
@@ -190,8 +215,37 @@ export function SongSwipeRow({
|
||||
open={open === 1}
|
||||
onPress={() => activate(1)}
|
||||
/>
|
||||
<SwipeAction
|
||||
side={1}
|
||||
secondary
|
||||
x={x}
|
||||
height={height}
|
||||
progress={commitProgress}
|
||||
open={open === 1}
|
||||
onPress={() => {
|
||||
close()
|
||||
onPlayNext()
|
||||
}}
|
||||
/>
|
||||
{hasRemove && (
|
||||
<SwipeAction
|
||||
side={-1}
|
||||
secondary
|
||||
paired
|
||||
remove
|
||||
x={x}
|
||||
height={height}
|
||||
progress={commitProgress}
|
||||
open={open === -1}
|
||||
onPress={() => {
|
||||
close()
|
||||
onRemove?.()
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
<SwipeAction
|
||||
side={-1}
|
||||
paired={hasRemove}
|
||||
x={x}
|
||||
height={height}
|
||||
progress={commitProgress}
|
||||
@@ -204,11 +258,23 @@ export function SongSwipeRow({
|
||||
accessibilityRole="button"
|
||||
onAccessibilityTap={tapRow}
|
||||
accessibilityActions={[
|
||||
{ name: "addToQueue", label: "Add to Queue" },
|
||||
{ name: "playNext", label: "Play Next" },
|
||||
{ name: "download", label: "Download" },
|
||||
...(hasRemove
|
||||
? [{ name: "remove", label: "Remove from Playlist" }]
|
||||
: []),
|
||||
]}
|
||||
onAccessibilityAction={(event) => {
|
||||
if (event.nativeEvent.actionName === "playNext") activate(1)
|
||||
if (event.nativeEvent.actionName === "remove" && hasRemove) {
|
||||
close()
|
||||
onRemove?.()
|
||||
}
|
||||
if (event.nativeEvent.actionName === "addToQueue") activate(1)
|
||||
if (event.nativeEvent.actionName === "playNext") {
|
||||
close()
|
||||
onPlayNext()
|
||||
}
|
||||
}}
|
||||
style={[styles.row, { backgroundColor }, rowStyle]}
|
||||
>
|
||||
@@ -225,6 +291,9 @@ export function SongSwipeRow({
|
||||
|
||||
function SwipeAction({
|
||||
side,
|
||||
secondary = false,
|
||||
paired = side > 0,
|
||||
remove = false,
|
||||
x,
|
||||
height,
|
||||
progress,
|
||||
@@ -232,22 +301,48 @@ function SwipeAction({
|
||||
onPress,
|
||||
}: {
|
||||
side: 1 | -1
|
||||
secondary?: boolean
|
||||
paired?: boolean
|
||||
remove?: boolean
|
||||
x: SharedValue<number>
|
||||
height: SharedValue<number>
|
||||
progress: SharedValue<number>
|
||||
open: boolean
|
||||
onPress: () => void
|
||||
}) {
|
||||
const actionStyle = useAnimatedStyle(() => ({
|
||||
width: Math.max(0, x.value * side),
|
||||
}))
|
||||
const pillStyle = useAnimatedStyle(() => {
|
||||
const actionStyle = useAnimatedStyle(() => {
|
||||
const revealed = Math.max(0, x.value * side)
|
||||
const primaryWidth = (revealed * (1 + progress.value)) / 2
|
||||
return {
|
||||
width: !paired
|
||||
? revealed
|
||||
: secondary
|
||||
? revealed - primaryWidth
|
||||
: primaryWidth,
|
||||
...(secondary
|
||||
? side > 0
|
||||
? { left: primaryWidth }
|
||||
: { right: primaryWidth }
|
||||
: {}),
|
||||
}
|
||||
})
|
||||
const pillStyle = useAnimatedStyle(() => {
|
||||
const total = Math.max(0, x.value * side)
|
||||
const revealed = !paired
|
||||
? total
|
||||
: (total * (secondary ? 1 - progress.value : 1 + progress.value)) / 2
|
||||
if (IS_ANDROID) {
|
||||
return { width: revealed, height: height.value, borderRadius: 0 }
|
||||
}
|
||||
const resting = Math.max(
|
||||
0,
|
||||
Math.min(
|
||||
revealed - 20,
|
||||
interpolate(revealed, [24, ACTION_WIDTH, 220], [0, 62, 200])
|
||||
revealed - (side > 0 ? 10 : 20),
|
||||
interpolate(
|
||||
revealed,
|
||||
[24, side > 0 ? LEADING_ACTION_WIDTH : ACTION_WIDTH, 220],
|
||||
[0, 62, 200]
|
||||
)
|
||||
)
|
||||
)
|
||||
const pillWidth =
|
||||
@@ -292,27 +387,61 @@ function SwipeAction({
|
||||
>
|
||||
<Pressable
|
||||
accessibilityRole="button"
|
||||
accessibilityLabel={side > 0 ? "Play Next" : "Download"}
|
||||
accessibilityLabel={
|
||||
remove
|
||||
? "Remove from Playlist"
|
||||
: side > 0
|
||||
? secondary
|
||||
? "Play Next"
|
||||
: "Add to Queue"
|
||||
: "Download"
|
||||
}
|
||||
onPress={onPress}
|
||||
style={styles.actionButton}
|
||||
>
|
||||
<Animated.View
|
||||
style={[
|
||||
styles.pill,
|
||||
{ backgroundColor: side > 0 ? "#6b5bff" : "#008dff" },
|
||||
{
|
||||
backgroundColor: remove
|
||||
? "#d32f2f"
|
||||
: side > 0
|
||||
? secondary
|
||||
? "#6b5bff"
|
||||
: "#f29943"
|
||||
: "#008dff",
|
||||
},
|
||||
pillStyle,
|
||||
]}
|
||||
>
|
||||
<Animated.View style={iconStyle}>
|
||||
<SymbolView
|
||||
name={
|
||||
side > 0
|
||||
? "text.line.first.and.arrowtriangle.forward"
|
||||
: "arrow.down"
|
||||
}
|
||||
size={22}
|
||||
tintColor="white"
|
||||
/>
|
||||
{IS_ANDROID ? (
|
||||
<Ionicons
|
||||
name={
|
||||
remove
|
||||
? "trash-outline"
|
||||
: side < 0
|
||||
? "arrow-down"
|
||||
: secondary
|
||||
? "play-forward"
|
||||
: "list"
|
||||
}
|
||||
size={22}
|
||||
color="white"
|
||||
/>
|
||||
) : (
|
||||
<SymbolView
|
||||
name={
|
||||
side > 0
|
||||
? secondary
|
||||
? "text.line.first.and.arrowtriangle.forward"
|
||||
: "text.line.last.and.arrowtriangle.forward"
|
||||
: "arrow.down"
|
||||
}
|
||||
size={22}
|
||||
tintColor="white"
|
||||
/>
|
||||
)}
|
||||
</Animated.View>
|
||||
</Animated.View>
|
||||
</Pressable>
|
||||
|
||||
Reference in new issue
Block a user