feat(mobile): add song swipe actions to albums and playlists (#85)
This commit was merged in pull request #85.
This commit is contained in:
1 parent
8e85271895
commit
b598173137
4 files changed
+400
-28
No files matched your search
@@ -1,3 +1,8 @@
|
||||
import { SongSwipeRow } from "@/components/song-swipe-row"
|
||||
import {
|
||||
useSwipeRowGroup,
|
||||
type SwipeRowGroup,
|
||||
} from "@/components/ios-swipe-to-remove-row"
|
||||
import {
|
||||
CollectionHero,
|
||||
useCollectionHeroLayout,
|
||||
@@ -68,6 +73,7 @@ export default function AlbumScreen() {
|
||||
handleAlbumTrackAction,
|
||||
isAlbumFavorite: isAlbumFav,
|
||||
} = useMediaMenuActions()
|
||||
const swipeGroup = useSwipeRowGroup()
|
||||
const isFocused = useIsFocused()
|
||||
const coverUri =
|
||||
config && typeof id === "string" ? albumCoverUrl(config.serverUrl, id) : ""
|
||||
@@ -112,6 +118,7 @@ export default function AlbumScreen() {
|
||||
}
|
||||
return album ? (
|
||||
<AlbumTrackRow
|
||||
swipeGroup={swipeGroup}
|
||||
track={item.track}
|
||||
number={item.number}
|
||||
album={album}
|
||||
@@ -132,6 +139,7 @@ export default function AlbumScreen() {
|
||||
palette,
|
||||
playFromSong,
|
||||
songIds,
|
||||
swipeGroup,
|
||||
]
|
||||
)
|
||||
|
||||
@@ -235,6 +243,7 @@ export default function AlbumScreen() {
|
||||
}}
|
||||
/>
|
||||
<Animated.FlatList
|
||||
onScrollBeginDrag={swipeGroup.closeOpenRow}
|
||||
onLayout={onLayout}
|
||||
{...longListProps}
|
||||
data={listItems}
|
||||
@@ -456,6 +465,7 @@ function albumListItemKey(item: AlbumListItem) {
|
||||
|
||||
const AlbumTrackRow = memo(function AlbumTrackRow({
|
||||
track,
|
||||
swipeGroup,
|
||||
number,
|
||||
album,
|
||||
songIds,
|
||||
@@ -465,6 +475,7 @@ const AlbumTrackRow = memo(function AlbumTrackRow({
|
||||
onAction,
|
||||
}: {
|
||||
track: BrowseAlbumTrack
|
||||
swipeGroup: SwipeRowGroup
|
||||
number: number
|
||||
album: BrowseAlbumDetail
|
||||
songIds: Id<"songs">[]
|
||||
@@ -477,6 +488,7 @@ const AlbumTrackRow = memo(function AlbumTrackRow({
|
||||
action: MediaAction
|
||||
) => Promise<void>
|
||||
}) {
|
||||
const Row = Platform.OS === "ios" ? SongSwipeRow : Pressable
|
||||
return (
|
||||
<MediaContextMenu
|
||||
stationArtistId={album.artist.id}
|
||||
@@ -494,7 +506,10 @@ const AlbumTrackRow = memo(function AlbumTrackRow({
|
||||
onAction={(action) => void onAction(track, album, action)}
|
||||
>
|
||||
<MusicEntityView kind="song" id={track.id}>
|
||||
<Pressable
|
||||
<Row
|
||||
group={swipeGroup}
|
||||
backgroundColor={palette.background}
|
||||
onPlayNext={() => void onAction(track, album, "play-next")}
|
||||
style={styles.trackRow}
|
||||
onPress={() => void onPlay(track.id as Id<"songs">, songIds)}
|
||||
>
|
||||
@@ -535,7 +550,7 @@ const AlbumTrackRow = memo(function AlbumTrackRow({
|
||||
{formatDuration(track.durationSeconds)}
|
||||
</PaletteText>
|
||||
</View>
|
||||
</Pressable>
|
||||
</Row>
|
||||
</MusicEntityView>
|
||||
</MediaContextMenu>
|
||||
)
|
||||
|
||||
@@ -39,7 +39,6 @@ import ReanimatedSwipeable from "react-native-gesture-handler/ReanimatedSwipeabl
|
||||
import { CollectionDivider } from "@/components/collection-list"
|
||||
import { CoverImage } from "@/components/cover-image"
|
||||
import {
|
||||
SwipeToRemoveRow,
|
||||
useSwipeRowGroup,
|
||||
type SwipeRowGroup,
|
||||
} from "@/components/ios-swipe-to-remove-row"
|
||||
@@ -434,28 +433,24 @@ const PlaylistSongRow = memo(function PlaylistSongRow({
|
||||
playlistLabel={PLAYLIST_LABEL}
|
||||
palette={palette}
|
||||
inset
|
||||
swipe={
|
||||
Platform.OS === "ios"
|
||||
? {
|
||||
group: swipeGroup,
|
||||
backgroundColor: palette.background,
|
||||
onPlayNext: () => onAction(song, "play-next"),
|
||||
}
|
||||
: undefined
|
||||
}
|
||||
onPlay={onPlay}
|
||||
onAction={onAction}
|
||||
/>
|
||||
)
|
||||
|
||||
if (!removable) {
|
||||
if (Platform.OS === "ios" || !removable) {
|
||||
return row
|
||||
}
|
||||
|
||||
if (Platform.OS === "ios") {
|
||||
return (
|
||||
<SwipeToRemoveRow
|
||||
group={swipeGroup}
|
||||
backgroundColor={palette.background}
|
||||
actionAccessibilityLabel={`Remove ${song.title} from playlist`}
|
||||
onRemove={handleRemove}
|
||||
>
|
||||
{row}
|
||||
</SwipeToRemoveRow>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<ReanimatedSwipeable
|
||||
friction={2}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { SongSwipeRow } from "@/components/song-swipe-row"
|
||||
import type { SwipeRowGroup } from "@/components/ios-swipe-to-remove-row"
|
||||
import { PaletteText } from "@/components/palette-text"
|
||||
import { MusicEntityView } from "@/components/music-entity-view"
|
||||
import { collectionInset } from "@/components/collection-list"
|
||||
@@ -31,6 +33,7 @@ export const SongRow = memo(function SongRow({
|
||||
trailing,
|
||||
palette,
|
||||
inset = false,
|
||||
swipe,
|
||||
}: {
|
||||
song: BrowseSong
|
||||
onPress?: () => void
|
||||
@@ -39,6 +42,11 @@ export const SongRow = memo(function SongRow({
|
||||
}
|
||||
trailing?: React.ReactNode
|
||||
palette?: Pick<CoverPalette, "foreground" | "secondaryForeground" | "divider">
|
||||
swipe?: {
|
||||
group: SwipeRowGroup
|
||||
onPlayNext: () => void
|
||||
backgroundColor: string
|
||||
}
|
||||
/** Album/playlist layout: the divider starts at the title, not the artwork. */
|
||||
inset?: boolean
|
||||
}) {
|
||||
@@ -79,16 +87,8 @@ export const SongRow = memo(function SongRow({
|
||||
</>
|
||||
)
|
||||
|
||||
const row = (
|
||||
<Pressable
|
||||
style={
|
||||
inset ? styles.insetRow : [styles.row, { borderBottomColor: divider }]
|
||||
}
|
||||
onPress={onPress}
|
||||
// Consume the long press so releasing a native context menu cannot play
|
||||
// the row through React Native's still-active press responder.
|
||||
onLongPress={contextMenu ? consumeLongPress : undefined}
|
||||
>
|
||||
const rowContent = (
|
||||
<>
|
||||
<CoverImage
|
||||
uri={albumCoverUrl(config.serverUrl, song.album.id)}
|
||||
hasCover={song.album.cover != null}
|
||||
@@ -101,8 +101,31 @@ export const SongRow = memo(function SongRow({
|
||||
) : (
|
||||
content
|
||||
)}
|
||||
</Pressable>
|
||||
</>
|
||||
)
|
||||
const rowStyle = inset
|
||||
? styles.insetRow
|
||||
: [styles.row, { borderBottomColor: divider }]
|
||||
const row =
|
||||
swipe && Platform.OS === "ios" ? (
|
||||
<SongSwipeRow
|
||||
group={swipe.group}
|
||||
onPlayNext={swipe.onPlayNext}
|
||||
backgroundColor={swipe.backgroundColor}
|
||||
onPress={onPress}
|
||||
style={rowStyle}
|
||||
>
|
||||
{rowContent}
|
||||
</SongSwipeRow>
|
||||
) : (
|
||||
<Pressable
|
||||
style={rowStyle}
|
||||
onPress={onPress}
|
||||
onLongPress={contextMenu ? consumeLongPress : undefined}
|
||||
>
|
||||
{rowContent}
|
||||
</Pressable>
|
||||
)
|
||||
|
||||
if (!contextMenu) {
|
||||
return (
|
||||
@@ -132,6 +155,7 @@ export const SongListRow = memo(function SongListRow({
|
||||
playlistLabel,
|
||||
palette,
|
||||
inset,
|
||||
swipe,
|
||||
onPlay,
|
||||
onAction,
|
||||
}: {
|
||||
@@ -140,6 +164,11 @@ export const SongListRow = memo(function SongListRow({
|
||||
playlistLabel?: string
|
||||
palette?: Pick<CoverPalette, "foreground" | "secondaryForeground" | "divider">
|
||||
inset?: boolean
|
||||
swipe?: {
|
||||
group: SwipeRowGroup
|
||||
onPlayNext: () => void
|
||||
backgroundColor: string
|
||||
}
|
||||
onPlay: (song: BrowseSong) => void
|
||||
onAction: (song: BrowseSong, action: MediaAction) => void
|
||||
}) {
|
||||
@@ -150,6 +179,7 @@ export const SongListRow = memo(function SongListRow({
|
||||
song={song}
|
||||
palette={palette}
|
||||
inset={inset}
|
||||
swipe={swipe}
|
||||
onPress={handlePress}
|
||||
contextMenu={{
|
||||
stationArtistId: song.artist.id,
|
||||
|
||||
@@ -0,0 +1,332 @@
|
||||
import * as Haptics from "expo-haptics"
|
||||
import { SymbolView } from "expo-symbols"
|
||||
import {
|
||||
useCallback,
|
||||
useEffect,
|
||||
useMemo,
|
||||
useState,
|
||||
type ReactNode,
|
||||
} from "react"
|
||||
import {
|
||||
Pressable,
|
||||
StyleSheet,
|
||||
type StyleProp,
|
||||
type ViewStyle,
|
||||
} from "react-native"
|
||||
import { Gesture, GestureDetector } from "react-native-gesture-handler"
|
||||
import Animated, {
|
||||
type SharedValue,
|
||||
cancelAnimation,
|
||||
Extrapolation,
|
||||
interpolate,
|
||||
useAnimatedStyle,
|
||||
useSharedValue,
|
||||
withSpring,
|
||||
} from "react-native-reanimated"
|
||||
import { scheduleOnRN } from "react-native-worklets"
|
||||
import type { SwipeRowGroup } from "@/components/ios-swipe-to-remove-row"
|
||||
|
||||
const ACTION_WIDTH = 82
|
||||
const SPRING = { duration: 450, dampingRatio: 1 }
|
||||
|
||||
function impact() {
|
||||
void Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Medium)
|
||||
}
|
||||
|
||||
/** A tap competes with the swipe so dragging a moving row never starts playback. */
|
||||
export function SongSwipeRow({
|
||||
children,
|
||||
style,
|
||||
backgroundColor,
|
||||
group,
|
||||
onPress,
|
||||
onPlayNext,
|
||||
}: {
|
||||
children: ReactNode
|
||||
style?: StyleProp<ViewStyle>
|
||||
backgroundColor: string
|
||||
group: SwipeRowGroup
|
||||
onPress?: () => void
|
||||
onPlayNext: () => void
|
||||
}) {
|
||||
const x = useSharedValue(0)
|
||||
const startX = useSharedValue(0)
|
||||
const width = useSharedValue(0)
|
||||
const height = useSharedValue(0)
|
||||
const committed = useSharedValue(false)
|
||||
const commitProgress = useSharedValue(0)
|
||||
const [open, setOpen] = useState<-1 | 0 | 1>(0)
|
||||
|
||||
const close = useCallback(() => {
|
||||
x.value = withSpring(0, SPRING)
|
||||
committed.value = false
|
||||
commitProgress.value = withSpring(0, SPRING)
|
||||
setOpen(0)
|
||||
group.clear(close)
|
||||
}, [commitProgress, committed, group, x])
|
||||
|
||||
useEffect(() => () => group.clear(close), [close, group])
|
||||
|
||||
const begin = useCallback(() => group.setOpen(close), [close, group])
|
||||
const settle = useCallback(
|
||||
(side: -1 | 0 | 1) => {
|
||||
setOpen(side)
|
||||
if (!side) group.clear(close)
|
||||
},
|
||||
[close, group]
|
||||
)
|
||||
const activate = useCallback(
|
||||
(side: number) => {
|
||||
close()
|
||||
if (side > 0) onPlayNext()
|
||||
// Download is intentionally a placeholder.
|
||||
},
|
||||
[close, onPlayNext]
|
||||
)
|
||||
const tapRow = useCallback(() => {
|
||||
if (open) close()
|
||||
else onPress?.()
|
||||
}, [close, onPress, open])
|
||||
|
||||
const gesture = useMemo(() => {
|
||||
const pan = Gesture.Pan()
|
||||
// Leave the screen's leading edge available for native back navigation.
|
||||
.hitSlop({ left: -24 })
|
||||
.activeOffsetX([-10, 10])
|
||||
.failOffsetY([-10, 10])
|
||||
.onStart(() => {
|
||||
cancelAnimation(x)
|
||||
startX.value = x.value
|
||||
scheduleOnRN(begin)
|
||||
})
|
||||
.onUpdate((event) => {
|
||||
x.value = Math.max(
|
||||
-width.value,
|
||||
Math.min(width.value, startX.value + event.translationX)
|
||||
)
|
||||
const commit =
|
||||
Math.abs(x.value) > Math.max(ACTION_WIDTH, width.value * 0.55)
|
||||
if (commit !== committed.value) {
|
||||
committed.value = commit
|
||||
commitProgress.value = withSpring(commit ? 1 : 0, SPRING)
|
||||
scheduleOnRN(impact)
|
||||
}
|
||||
})
|
||||
.onEnd((event) => {
|
||||
const side = x.value > 0 ? 1 : -1
|
||||
if (committed.value) {
|
||||
x.value = withSpring(0, SPRING)
|
||||
scheduleOnRN(activate, side)
|
||||
return
|
||||
}
|
||||
const projected = x.value + event.velocityX * 0.12
|
||||
const target =
|
||||
Math.abs(x.value) > 10 && projected * side > ACTION_WIDTH / 2
|
||||
? side
|
||||
: 0
|
||||
x.value = withSpring(target * ACTION_WIDTH, {
|
||||
...SPRING,
|
||||
velocity: event.velocityX,
|
||||
})
|
||||
scheduleOnRN(settle, target)
|
||||
})
|
||||
.onFinalize((_event, success) => {
|
||||
if (!success) {
|
||||
x.value = withSpring(0, SPRING)
|
||||
scheduleOnRN(close)
|
||||
}
|
||||
})
|
||||
const tap = Gesture.Tap()
|
||||
.maxDistance(10)
|
||||
.maxDuration(500)
|
||||
.onEnd((_event, success) => {
|
||||
if (success) scheduleOnRN(tapRow)
|
||||
})
|
||||
return Gesture.Exclusive(pan, tap)
|
||||
}, [
|
||||
activate,
|
||||
begin,
|
||||
close,
|
||||
commitProgress,
|
||||
committed,
|
||||
settle,
|
||||
startX,
|
||||
tapRow,
|
||||
width,
|
||||
x,
|
||||
])
|
||||
|
||||
const rowStyle = useAnimatedStyle(() => ({
|
||||
transform: [{ translateX: x.value }],
|
||||
borderRadius: interpolate(
|
||||
Math.abs(x.value),
|
||||
[0, 40],
|
||||
[0, height.value / 2],
|
||||
Extrapolation.CLAMP
|
||||
),
|
||||
}))
|
||||
const fillStyle = useAnimatedStyle(() => ({
|
||||
opacity: interpolate(
|
||||
Math.abs(x.value),
|
||||
[0, 40],
|
||||
[0, 1],
|
||||
Extrapolation.CLAMP
|
||||
),
|
||||
}))
|
||||
|
||||
return (
|
||||
<Animated.View
|
||||
style={styles.container}
|
||||
onLayout={(event) => {
|
||||
width.value = event.nativeEvent.layout.width
|
||||
height.value = event.nativeEvent.layout.height
|
||||
}}
|
||||
>
|
||||
<SwipeAction
|
||||
side={1}
|
||||
x={x}
|
||||
height={height}
|
||||
progress={commitProgress}
|
||||
open={open === 1}
|
||||
onPress={() => activate(1)}
|
||||
/>
|
||||
<SwipeAction
|
||||
side={-1}
|
||||
x={x}
|
||||
height={height}
|
||||
progress={commitProgress}
|
||||
open={open === -1}
|
||||
onPress={() => activate(-1)}
|
||||
/>
|
||||
<GestureDetector gesture={gesture}>
|
||||
<Animated.View
|
||||
accessible
|
||||
accessibilityRole="button"
|
||||
onAccessibilityTap={tapRow}
|
||||
accessibilityActions={[
|
||||
{ name: "playNext", label: "Play Next" },
|
||||
{ name: "download", label: "Download" },
|
||||
]}
|
||||
onAccessibilityAction={(event) => {
|
||||
if (event.nativeEvent.actionName === "playNext") activate(1)
|
||||
}}
|
||||
style={[styles.row, { backgroundColor }, rowStyle]}
|
||||
>
|
||||
<Animated.View
|
||||
pointerEvents="none"
|
||||
style={[StyleSheet.absoluteFill, styles.fill, fillStyle]}
|
||||
/>
|
||||
<Animated.View style={style}>{children}</Animated.View>
|
||||
</Animated.View>
|
||||
</GestureDetector>
|
||||
</Animated.View>
|
||||
)
|
||||
}
|
||||
|
||||
function SwipeAction({
|
||||
side,
|
||||
x,
|
||||
height,
|
||||
progress,
|
||||
open,
|
||||
onPress,
|
||||
}: {
|
||||
side: 1 | -1
|
||||
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 revealed = Math.max(0, x.value * side)
|
||||
const resting = Math.max(
|
||||
0,
|
||||
Math.min(
|
||||
revealed - 20,
|
||||
interpolate(revealed, [24, ACTION_WIDTH, 220], [0, 62, 200])
|
||||
)
|
||||
)
|
||||
const pillWidth =
|
||||
resting + (Math.max(0, revealed - 12) - resting) * progress.value
|
||||
const restingHeight = interpolate(
|
||||
pillWidth,
|
||||
[0, 24, 62, 140, 200],
|
||||
[0, 0.3, 0.77, 0.82, 0.86].map((ratio) => ratio * height.value),
|
||||
Extrapolation.CLAMP
|
||||
)
|
||||
const pillHeight =
|
||||
restingHeight +
|
||||
(Math.max(0, height.value - 12) - restingHeight) * progress.value
|
||||
return {
|
||||
width: pillWidth,
|
||||
height: pillHeight,
|
||||
borderRadius: pillHeight / 2,
|
||||
}
|
||||
})
|
||||
const iconStyle = useAnimatedStyle(() => ({
|
||||
transform: [
|
||||
{
|
||||
scale: interpolate(
|
||||
x.value * side,
|
||||
[24, 64],
|
||||
[0, 1],
|
||||
Extrapolation.CLAMP
|
||||
),
|
||||
},
|
||||
],
|
||||
}))
|
||||
return (
|
||||
<Animated.View
|
||||
pointerEvents={open ? "auto" : "none"}
|
||||
accessibilityElementsHidden={!open}
|
||||
importantForAccessibility={open ? "auto" : "no-hide-descendants"}
|
||||
style={[
|
||||
styles.action,
|
||||
side > 0 ? styles.leading : styles.trailing,
|
||||
actionStyle,
|
||||
]}
|
||||
>
|
||||
<Pressable
|
||||
accessibilityRole="button"
|
||||
accessibilityLabel={side > 0 ? "Play Next" : "Download"}
|
||||
onPress={onPress}
|
||||
style={styles.actionButton}
|
||||
>
|
||||
<Animated.View
|
||||
style={[
|
||||
styles.pill,
|
||||
{ backgroundColor: side > 0 ? "#6b5bff" : "#008dff" },
|
||||
pillStyle,
|
||||
]}
|
||||
>
|
||||
<Animated.View style={iconStyle}>
|
||||
<SymbolView
|
||||
name={
|
||||
side > 0
|
||||
? "text.line.first.and.arrowtriangle.forward"
|
||||
: "arrow.down"
|
||||
}
|
||||
size={22}
|
||||
tintColor="white"
|
||||
/>
|
||||
</Animated.View>
|
||||
</Animated.View>
|
||||
</Pressable>
|
||||
</Animated.View>
|
||||
)
|
||||
}
|
||||
|
||||
const styles = StyleSheet.create({
|
||||
container: { overflow: "hidden" },
|
||||
row: { overflow: "hidden" },
|
||||
fill: { backgroundColor: "rgba(128,128,128,0.16)" },
|
||||
action: { position: "absolute", top: 0, bottom: 0 },
|
||||
leading: { left: 0 },
|
||||
trailing: { right: 0 },
|
||||
actionButton: { flex: 1, alignItems: "center", justifyContent: "center" },
|
||||
pill: { alignItems: "center", justifyContent: "center", overflow: "hidden" },
|
||||
})
|
||||
Reference in new issue
Block a user