fix(mobile): prevent back swipes from playing songs
This commit is contained in:
3 files changed
+59
-5
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import { SongPressable } from "@/components/song-pressable"
|
||||
import {
|
||||
CollectionHero,
|
||||
useCollectionHeroLayout,
|
||||
@@ -494,7 +495,7 @@ const AlbumTrackRow = memo(function AlbumTrackRow({
|
||||
onAction={(action) => void onAction(track, album, action)}
|
||||
>
|
||||
<MusicEntityView kind="song" id={track.id}>
|
||||
<Pressable
|
||||
<SongPressable
|
||||
style={styles.trackRow}
|
||||
onPress={() => void onPlay(track.id as Id<"songs">, songIds)}
|
||||
>
|
||||
@@ -535,7 +536,7 @@ const AlbumTrackRow = memo(function AlbumTrackRow({
|
||||
{formatDuration(track.durationSeconds)}
|
||||
</PaletteText>
|
||||
</View>
|
||||
</Pressable>
|
||||
</SongPressable>
|
||||
</MusicEntityView>
|
||||
</MediaContextMenu>
|
||||
)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { SongPressable } from "@/components/song-pressable"
|
||||
import { PaletteText } from "@/components/palette-text"
|
||||
import { MusicEntityView } from "@/components/music-entity-view"
|
||||
import { collectionInset } from "@/components/collection-list"
|
||||
@@ -14,7 +15,7 @@ import {
|
||||
useThemeColors,
|
||||
} from "@/lib/theme"
|
||||
import { memo, useCallback } from "react"
|
||||
import { Platform, Pressable, StyleSheet, View } from "react-native"
|
||||
import { Platform, StyleSheet, View } from "react-native"
|
||||
|
||||
import {
|
||||
MediaContextMenu,
|
||||
@@ -80,7 +81,7 @@ export const SongRow = memo(function SongRow({
|
||||
)
|
||||
|
||||
const row = (
|
||||
<Pressable
|
||||
<SongPressable
|
||||
style={
|
||||
inset ? styles.insetRow : [styles.row, { borderBottomColor: divider }]
|
||||
}
|
||||
@@ -101,7 +102,7 @@ export const SongRow = memo(function SongRow({
|
||||
) : (
|
||||
content
|
||||
)}
|
||||
</Pressable>
|
||||
</SongPressable>
|
||||
)
|
||||
|
||||
if (!contextMenu) {
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
import { useRef } from "react"
|
||||
import {
|
||||
Pressable,
|
||||
type GestureResponderEvent,
|
||||
type PressableProps,
|
||||
} from "react-native"
|
||||
|
||||
const TAP_DISTANCE = 10
|
||||
|
||||
type SongPressableProps = Omit<PressableProps, "onPress"> & {
|
||||
onPress?: () => void
|
||||
}
|
||||
|
||||
/** Cancel playback after dragging, even when navigation moves the row with the finger. */
|
||||
export function SongPressable({ onPress, ...props }: SongPressableProps) {
|
||||
const touch = useRef<{ x: number; y: number; cancelled: boolean } | null>(
|
||||
null
|
||||
)
|
||||
|
||||
function checkMovement(event: GestureResponderEvent) {
|
||||
const start = touch.current
|
||||
if (!start) return
|
||||
const { pageX, pageY } = event.nativeEvent
|
||||
if (Math.hypot(pageX - start.x, pageY - start.y) > TAP_DISTANCE) {
|
||||
start.cancelled = true
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Pressable
|
||||
{...props}
|
||||
onAccessibilityTap={props.onAccessibilityTap ?? onPress}
|
||||
onTouchStart={(event) => {
|
||||
const { pageX, pageY } = event.nativeEvent
|
||||
touch.current = { x: pageX, y: pageY, cancelled: false }
|
||||
props.onTouchStart?.(event)
|
||||
}}
|
||||
onTouchMove={(event) => {
|
||||
checkMovement(event)
|
||||
props.onTouchMove?.(event)
|
||||
}}
|
||||
onTouchCancel={(event) => {
|
||||
if (touch.current) touch.current.cancelled = true
|
||||
props.onTouchCancel?.(event)
|
||||
}}
|
||||
onPress={(event) => {
|
||||
checkMovement(event)
|
||||
if (!touch.current?.cancelled) onPress?.()
|
||||
}}
|
||||
/>
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user