1- import { changeToIndex , fetchQueue , moveToPosition , queueItems , removeByIndex } from "@/lib/queue" ;
1+ import { changeToIndex , fetchQueue , modifiedQueueItems , moveToPosition , queueItems , removeByIndex } from "@/lib/queue" ;
22import { QueueItem } from "@/types/musickit" ;
33import { useIsFocused } from "@react-navigation/native" ;
44import { useAtom } from "jotai" ;
@@ -9,7 +9,7 @@ import { Button, Dialog, IconButton, List, Portal, useTheme } from "react-native
99
1010export default function Queue ( ) {
1111 const isFocused = useIsFocused ( ) ;
12- const [ queue , setQueue ] = useAtom ( queueItems ) ;
12+ const [ queue , setQueue ] = useAtom ( modifiedQueueItems ) ;
1313 const theme = useTheme ( ) ;
1414 const [ draggedIndex , setDraggedIndex ] = useState < number | null > ( null ) ;
1515
@@ -58,7 +58,7 @@ type UIQueueItemProps = {
5858 onDragEnd ?: ( ) => void ;
5959} ;
6060
61- function UIQueueItem ( { item, idx, isDragged, onDragStart, onDragEnd } : UIQueueItemProps ) {
61+ export function UIQueueItem ( { item, idx, isDragged, onDragStart, onDragEnd } : UIQueueItemProps ) {
6262 const [ queue , setQueue ] = useAtom ( queueItems ) ;
6363 const [ showActions , setShowActions ] = useState ( false ) ;
6464 const [ isDragging , setIsDragging ] = useState ( false ) ;
@@ -85,14 +85,23 @@ function UIQueueItem({ item, idx, isDragged, onDragStart, onDragEnd }: UIQueueIt
8585 toValue : 1.05 ,
8686 useNativeDriver : true ,
8787 } ) . start ( ) ;
88- } else if ( event . nativeEvent . state === State . END || event . nativeEvent . state === State . CANCELLED ) {
88+ } else if ( event . nativeEvent . state === State . END || event . nativeEvent . state === State . CANCELLED ) {
8989 const { translationY } = event . nativeEvent ;
90- const itemHeight = 80 ; // Approximate height of each item
90+ const itemHeight = 80 ;
9191 const moveDistance = Math . round ( translationY / itemHeight ) ;
92- const newIndex = Math . max ( 0 , Math . min ( idx + moveDistance , queue . length - 1 ) ) ;
93-
94- if ( newIndex !== idx && event . nativeEvent . state === State . END ) {
95- moveToPosition ( idx , newIndex ) ;
92+ const originalIdx = item . originalIndex ;
93+ const newOriginalIndex =
94+ originalIdx !== undefined
95+ ? Math . max ( 0 , Math . min ( originalIdx + moveDistance , queue . length - 1 ) )
96+ : undefined ;
97+
98+ if (
99+ newOriginalIndex !== undefined &&
100+ originalIdx !== undefined &&
101+ newOriginalIndex !== originalIdx &&
102+ event . nativeEvent . state === State . END
103+ ) {
104+ moveToPosition ( originalIdx , newOriginalIndex ) ;
96105 }
97106
98107 Animated . parallel ( [
@@ -106,7 +115,6 @@ function UIQueueItem({ item, idx, isDragged, onDragStart, onDragEnd }: UIQueueIt
106115 } ) ,
107116 ] ) . start ( ) ;
108117
109- // Delay resetting isDragging to prevent menu from showing
110118 setTimeout ( ( ) => {
111119 setIsDragging ( false ) ;
112120 } , 100 ) ;
@@ -115,7 +123,6 @@ function UIQueueItem({ item, idx, isDragged, onDragStart, onDragEnd }: UIQueueIt
115123 }
116124 } ;
117125
118- // Move PanGestureHandler to wrap only the drag handle
119126 return (
120127 < >
121128 < Animated . View
@@ -132,7 +139,7 @@ function UIQueueItem({ item, idx, isDragged, onDragStart, onDragEnd }: UIQueueIt
132139 title = { item . attributes . name ?? "Untitled" }
133140 description = { item . attributes . artistName ?? "" }
134141 onPress = { ( ) => {
135- changeToIndex ( idx ) ;
142+ changeToIndex ( item . originalIndex ?? idx ) ;
136143 } }
137144 left = { ( props ) =>
138145 item . attributes . artwork ?. url ? (
@@ -179,7 +186,7 @@ function UIQueueItem({ item, idx, isDragged, onDragStart, onDragEnd }: UIQueueIt
179186 < Button
180187 icon = "close"
181188 onPress = { ( ) => {
182- removeByIndex ( idx ) ;
189+ removeByIndex ( item . originalIndex ?? idx ) ;
183190 setShowActions ( false ) ;
184191 } } > Remove From Queue</ Button >
185192 </ Dialog . Content >
0 commit comments