diff --git a/source/settings/settings.ts b/source/settings/settings.ts index ce65c90..512d44c 100644 --- a/source/settings/settings.ts +++ b/source/settings/settings.ts @@ -68,19 +68,13 @@ export class Settings { throw new Error(`Failed to move item with ID: ${id}`); } - const previousIndex = targetItem.sortIndex; - let targetIndex = previousIndex; - if (direction === 'down') { - targetIndex += 1; - } else if (direction === 'up') { - targetIndex -= 1; - } - + const currentIndex = targetItem.sortIndex; + const targetIndex = this.nextQueueItemSortIndex(currentIndex, direction); const existingItem = this.queue.find( (item) => item.sortIndex === targetIndex, ); - if (existingItem !== undefined) { - existingItem.sortIndex = previousIndex; + if (existingItem !== undefined && targetIndex !== undefined) { + existingItem.sortIndex = currentIndex; targetItem.sortIndex = targetIndex; await this.save(); } @@ -95,6 +89,28 @@ export class Settings { return this.queue.sort((a, b) => a.sortIndex - b.sortIndex)[0]; } + public nextQueueItemSortIndex( + currentIndex: number, + direction: Queue.MoveDirection, + ): number | undefined { + this.queue.sort((a, b) => { + return direction === 'up' + ? b.sortIndex - a.sortIndex + : a.sortIndex - b.sortIndex; + }); + + let foundCurrent = false; + for (const item of this.queue) { + if (foundCurrent) { + return item.sortIndex; + } + + if (item.sortIndex === currentIndex) { + foundCurrent = true; + } + } + } + public async removeQueueItem(id: number): Promise { const itemIndex = this.queue.findIndex((item) => item.id === id); if (itemIndex === -1) {