diff --git a/apps/mobile/app/(app)/(tabs)/home/album/[id].tsx b/apps/mobile/app/(app)/(tabs)/home/album/[id].tsx index d4ba20d..343d139 100644 --- a/apps/mobile/app/(app)/(tabs)/home/album/[id].tsx +++ b/apps/mobile/app/(app)/(tabs)/home/album/[id].tsx @@ -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 ? ( [] @@ -477,6 +488,7 @@ const AlbumTrackRow = memo(function AlbumTrackRow({ action: MediaAction ) => Promise }) { + const Row = Platform.OS === "ios" ? SongSwipeRow : Pressable return ( void onAction(track, album, action)} > - 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)} - + ) diff --git a/apps/mobile/app/(app)/(tabs)/home/playlist/[id].tsx b/apps/mobile/app/(app)/(tabs)/home/playlist/[id].tsx index 8cbe59c..f11a0d1 100644 --- a/apps/mobile/app/(app)/(tabs)/home/playlist/[id].tsx +++ b/apps/mobile/app/(app)/(tabs)/home/playlist/[id].tsx @@ -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 ( - - {row} - - ) - } - return ( void @@ -39,6 +42,11 @@ export const SongRow = memo(function SongRow({ } trailing?: React.ReactNode palette?: Pick + 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 = ( - + const rowContent = ( + <> + ) + const rowStyle = inset + ? styles.insetRow + : [styles.row, { borderBottomColor: divider }] + const row = + swipe && Platform.OS === "ios" ? ( + + {rowContent} + + ) : ( + + {rowContent} + + ) 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 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, diff --git a/apps/mobile/components/song-swipe-row.tsx b/apps/mobile/components/song-swipe-row.tsx new file mode 100644 index 0000000..3e2f044 --- /dev/null +++ b/apps/mobile/components/song-swipe-row.tsx @@ -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 + 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 ( + { + width.value = event.nativeEvent.layout.width + height.value = event.nativeEvent.layout.height + }} + > + activate(1)} + /> + activate(-1)} + /> + + { + if (event.nativeEvent.actionName === "playNext") activate(1) + }} + style={[styles.row, { backgroundColor }, rowStyle]} + > + + {children} + + + + ) +} + +function SwipeAction({ + side, + x, + height, + progress, + open, + onPress, +}: { + side: 1 | -1 + x: SharedValue + height: SharedValue + progress: SharedValue + 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 ( + 0 ? styles.leading : styles.trailing, + actionStyle, + ]} + > + 0 ? "Play Next" : "Download"} + onPress={onPress} + style={styles.actionButton} + > + 0 ? "#6b5bff" : "#008dff" }, + pillStyle, + ]} + > + + 0 + ? "text.line.first.and.arrowtriangle.forward" + : "arrow.down" + } + size={22} + tintColor="white" + /> + + + + + ) +} + +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" }, +})