[db] fix 'add next' when in queue shuffle mode (#1414)

* [db] fix 'add next' when in queue shuffle mode

* [db] review on 'add next' fix

Co-authored-by: whatdoineed2do/Ray <whatdoineed2do@nospam.gmail.com>
This commit is contained in:
whatdoineed2do 2022-02-06 19:23:06 +00:00 committed by GitHub
parent 26d7cf453c
commit a0ea8416f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5161,6 +5161,8 @@ db_queue_add_by_query(struct query_params *qp, char reshuffle, uint32_t item_id,
int queue_version; int queue_version;
uint32_t queue_count; uint32_t queue_count;
int pos; int pos;
int shuffle_pos;
bool append_to_queue;
int ret; int ret;
if (new_item_id) if (new_item_id)
@ -5190,24 +5192,34 @@ db_queue_add_by_query(struct query_params *qp, char reshuffle, uint32_t item_id,
return 0; return 0;
} }
if (position < 0 || position > queue_count) append_to_queue = (position < 0 || position > queue_count);
if (append_to_queue)
{ {
pos = queue_count; pos = queue_count;
shuffle_pos = queue_count;
} }
else else
{ {
pos = position; pos = position;
shuffle_pos = position;
// Update pos for all items from the given position (make room for the new items in the queue) // Update pos for all items from the given position (make room for the new items in the queue)
query = sqlite3_mprintf("UPDATE queue SET pos = pos + %d, queue_version = %d WHERE pos >= %d;", qp->results, queue_version, pos); query = sqlite3_mprintf("UPDATE queue SET pos = pos + %d, queue_version = %d WHERE pos >= %d;", qp->results, queue_version, pos);
ret = db_query_run(query, 1, 0); ret = db_query_run(query, 1, 0);
if (ret < 0) if (ret < 0)
goto end_transaction; goto end_transaction;
// and similary update on shuffle_pos
query = sqlite3_mprintf("UPDATE queue SET shuffle_pos = shuffle_pos + %d, queue_version = %d WHERE shuffle_pos >= %d;", qp->results, queue_version, pos);
ret = db_query_run(query, 1, 0);
if (ret < 0)
goto end_transaction;
} }
while ((ret = db_query_fetch_file(&dbmfi, qp)) == 0) while ((ret = db_query_fetch_file(&dbmfi, qp)) == 0)
{ {
ret = queue_item_add_from_file(&dbmfi, pos, queue_count, queue_version); ret = queue_item_add_from_file(&dbmfi, pos, shuffle_pos, queue_version);
if (ret < 0) if (ret < 0)
{ {
@ -5215,7 +5227,7 @@ db_queue_add_by_query(struct query_params *qp, char reshuffle, uint32_t item_id,
break; break;
} }
DPRINTF(E_DBG, L_DB, "Added song id %s (%s) to queue with item id %d\n", dbmfi.id, dbmfi.title, ret); DPRINTF(E_DBG, L_DB, "Added (pos=%d shuffle_pos=%d reshuffle=%d req position=%d) song id %s (%s) to queue with item id %d\n", pos, shuffle_pos, reshuffle, position, dbmfi.id, dbmfi.title, ret);
if (new_item_id && *new_item_id == 0) if (new_item_id && *new_item_id == 0)
*new_item_id = ret; *new_item_id = ret;
@ -5223,7 +5235,7 @@ db_queue_add_by_query(struct query_params *qp, char reshuffle, uint32_t item_id,
(*count)++; (*count)++;
pos++; pos++;
queue_count++; shuffle_pos++;
} }
if (ret > 0) if (ret > 0)
@ -5234,8 +5246,10 @@ db_queue_add_by_query(struct query_params *qp, char reshuffle, uint32_t item_id,
if (ret < 0) if (ret < 0)
goto end_transaction; goto end_transaction;
// Reshuffle after adding new items // Reshuffle after adding new items, if no queue position was specified - this
if (reshuffle) // case would indicate an 'add next' condition where if shuffling invalidates
// the tracks added to 'next'
if (append_to_queue && reshuffle)
{ {
ret = queue_reshuffle(item_id, queue_version); ret = queue_reshuffle(item_id, queue_version);
} }