mirror of
https://github.com/austinried/subtracks.git
synced 2026-11-15 00:42:54 +01:00
ui: preserve pending older queue loads
This commit is contained in:
@@ -120,6 +120,7 @@ class QueueViewModel(
|
|||||||
private set
|
private set
|
||||||
|
|
||||||
private var reorderPending = false
|
private var reorderPending = false
|
||||||
|
private var olderLoadPending = false
|
||||||
private var dragRollback: ReorderRollback? = null
|
private var dragRollback: ReorderRollback? = null
|
||||||
|
|
||||||
private var nextId = 0L
|
private var nextId = 0L
|
||||||
@@ -189,7 +190,7 @@ class QueueViewModel(
|
|||||||
val scanned = loaded.map { rowFor(it, reused, snapshot.resetVersion) }
|
val scanned = loaded.map { rowFor(it, reused, snapshot.resetVersion) }
|
||||||
val firstRow = rows.minByOrNull { it.position }
|
val firstRow = rows.minByOrNull { it.position }
|
||||||
val windowStart =
|
val windowStart =
|
||||||
if (firstRow == null) {
|
if (firstRow == null || olderLoadPending) {
|
||||||
start
|
start
|
||||||
} else {
|
} else {
|
||||||
scanned.firstOrNull { it.id == firstRow.id && it.position < firstPosition }?.position ?: firstPosition
|
scanned.firstOrNull { it.id == firstRow.id && it.position < firstPosition }?.position ?: firstPosition
|
||||||
@@ -214,21 +215,26 @@ class QueueViewModel(
|
|||||||
fun loadOlder() {
|
fun loadOlder() {
|
||||||
val top = rows.minOfOrNull { it.position } ?: return
|
val top = rows.minOfOrNull { it.position } ?: return
|
||||||
if (top <= 0L) return
|
if (top <= 0L) return
|
||||||
|
olderLoadPending = true
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
mutex.withLock {
|
try {
|
||||||
val first = rows.minOfOrNull { it.position } ?: return@withLock
|
mutex.withLock {
|
||||||
if (first <= 0L) return@withLock
|
val first = rows.minOfOrNull { it.position } ?: return@withLock
|
||||||
val snapshot = queueRepository.snapshot()
|
if (first <= 0L) return@withLock
|
||||||
val collected = mutableListOf<QueueWindowItem>()
|
val snapshot = queueRepository.snapshot()
|
||||||
var end = first - 1
|
val collected = mutableListOf<QueueWindowItem>()
|
||||||
while (end >= 0L && collected.size < QUEUE_CHUNK) {
|
var end = first - 1
|
||||||
val start = (end - QUEUE_CHUNK + 1).coerceAtLeast(0)
|
while (end >= 0L && collected.size < QUEUE_CHUNK) {
|
||||||
collected.addAll(0, queueRepository.range(snapshot, start, end).filter(::visible))
|
val start = (end - QUEUE_CHUNK + 1).coerceAtLeast(0)
|
||||||
end = start - 1
|
collected.addAll(0, queueRepository.range(snapshot, start, end).filter(::visible))
|
||||||
|
end = start - 1
|
||||||
|
}
|
||||||
|
if (collected.isEmpty()) return@withLock
|
||||||
|
rows.addAll(0, collected.map { newRow(it, snapshot.resetVersion) })
|
||||||
|
while (rows.size > QUEUE_WINDOW_ROWS) rows.removeAt(rows.size - 1)
|
||||||
}
|
}
|
||||||
if (collected.isEmpty()) return@withLock
|
} finally {
|
||||||
rows.addAll(0, collected.map { newRow(it, snapshot.resetVersion) })
|
olderLoadPending = false
|
||||||
while (rows.size > QUEUE_WINDOW_ROWS) rows.removeAt(rows.size - 1)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user