Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify playing queue logic #3324

Merged
merged 2 commits into from
Mar 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 7 additions & 22 deletions app/src/main/java/com/github/libretube/util/PlayingQueue.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,10 @@ object PlayingQueue {
)
}

fun getNext(): String? {
try {
return queue[currentIndex() + 1].url?.toID()
} catch (e: Exception) {
Log.e("queue ended", e.toString())
}
if (repeatQueue) return queue.firstOrNull()?.url?.toID()
return null
}
fun getNext(): String? = queue.getOrNull(currentIndex() + 1)?.url?.toID()
?: queue.firstOrNull()?.url?.toID()?.takeIf { repeatQueue }

fun getPrev(): String? {
return if (currentIndex() > 0) queue[currentIndex() - 1].url?.toID() else null
}
fun getPrev(): String? = queue.getOrNull(currentIndex() - 1)?.url?.toID()

fun hasPrev(): Boolean {
return currentIndex() > 0
Expand All @@ -84,15 +75,9 @@ object PlayingQueue {

fun size() = queue.size

fun currentIndex(): Int {
return try {
queue.indexOf(
queue.first { it.url?.toID() == currentStream?.url?.toID() }
)
} catch (e: Exception) {
0
}
}
fun currentIndex(): Int = queue.indexOfFirst {
it.url?.toID() == currentStream?.url?.toID()
}.takeIf { it >= 0 } ?: 0

fun getCurrent(): StreamItem? = currentStream

Expand Down Expand Up @@ -131,7 +116,7 @@ object PlayingQueue {
scope.launch {
try {
val playlist = PlaylistsHelper.getPlaylist(playlistId)
add(*playlist.relatedStreams.orEmpty().toTypedArray())
add(*playlist.relatedStreams.toTypedArray())
updateCurrent(newCurrentStream)
if (playlist.nextpage == null) return@launch
fetchMoreFromPlaylist(playlistId, playlist.nextpage)
Expand Down