package com.github.libretube.util import com.github.libretube.api.obj.StreamItem object PlayingQueue { private val queue = mutableListOf() private var currentVideoId: String? = null var streams: List = listOf() fun add(videoId: String) { if (currentVideoId == videoId) return if (queue.contains(videoId)) queue.remove(videoId) queue.add(videoId) } fun addAsNext(videoId: String) { if (currentVideoId == videoId) return if (queue.contains(videoId)) queue.remove(videoId) queue.add( queue.indexOf(currentVideoId) + 1, videoId ) } fun getNext(): String? { return try { queue[currentIndex() + 1] } catch (e: Exception) { null } } fun getPrev(): String? { val index = queue.indexOf(currentVideoId) return if (index > 0) queue[index - 1] else null } fun hasPrev(): Boolean { return queue.indexOf(currentVideoId) > 0 } fun updateCurrent(videoId: String) { currentVideoId = videoId queue.add(videoId) } fun isNotEmpty() = queue.isNotEmpty() fun clear() = queue.clear() fun currentIndex() = queue.indexOf(currentVideoId) fun contains(videoId: String) = queue.contains(videoId) fun containsBeforeCurrent(videoId: String): Boolean { return queue.contains(videoId) && queue.indexOf(videoId) < currentIndex() } }