Option to add newly opened links to queue

This commit is contained in:
Bnyro 2023-01-16 18:25:12 +01:00
parent ab24d95b8d
commit 2f10b7ba23
4 changed files with 73 additions and 12 deletions

View File

@ -47,6 +47,21 @@
android:name=".ui.activities.CommunityActivity" android:name=".ui.activities.CommunityActivity"
android:label="@string/settings" /> android:label="@string/settings" />
<activity
android:name=".ui.activities.AddToQueueActivity"
android:enabled="true"
android:launchMode="singleTop"
android:exported="true"
android:label="@string/add_to_queue">
<intent-filter android:label="@string/add_to_queue">
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
<activity <activity
android:name=".ui.activities.OfflinePlayerActivity" android:name=".ui.activities.OfflinePlayerActivity"
android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation" android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"
@ -251,7 +266,7 @@
android:exported="true" android:exported="true"
android:launchMode="singleInstance"> android:launchMode="singleInstance">
<intent-filter> <intent-filter android:label="@string/open">
<action android:name="android.intent.action.SEND" /> <action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" /> <data android:mimeType="text/plain" />

View File

@ -0,0 +1,39 @@
package com.github.libretube.ui.activities
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import com.github.libretube.ui.base.BaseActivity
import com.github.libretube.util.PlayingQueue
/**
* Receives a text by the intent and attempts to add it to the playing queue
* If no video is playing currently, the queue will be left unchanged and the the main activity is being resumed
*/
class AddToQueueActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val uri = Uri.parse(intent.getStringExtra(Intent.EXTRA_TEXT)!!)
var videoId: String? = null
listOf("/shorts/", "/v/", "/embed/").forEach {
if (uri.path!!.contains(it)) {
videoId = uri.path!!.replace(it, "")
}
}
if (
uri.path!!.contains("/watch") && uri.query != null
) {
videoId = uri.getQueryParameter("v")
}
if (videoId == null) videoId = uri.path!!.replace("/", "")
// if playing a video currently, the playing queue is not empty
if (PlayingQueue.isNotEmpty()) PlayingQueue.insertByVideoId(videoId!!)
val intent = packageManager.getLaunchIntentForPackage(packageName)
startActivity(intent)
finishAndRemoveTask()
}
}

View File

@ -84,10 +84,8 @@ class RouterActivity : BaseActivity() {
val pm: PackageManager = this.packageManager val pm: PackageManager = this.packageManager
val intent = pm.getLaunchIntentForPackage(this.packageName) val intent = pm.getLaunchIntentForPackage(this.packageName)
intent?.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK intent?.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK
this.startActivity( startActivity(resolveType(intent!!, uri))
resolveType(intent!!, uri) finishAndRemoveTask()
)
this.finishAndRemoveTask()
} }
private fun parseTimestamp(t: String): Long? { private fun parseTimestamp(t: String): Long? {

View File

@ -6,6 +6,7 @@ import com.github.libretube.api.RetrofitInstance
import com.github.libretube.api.obj.StreamItem import com.github.libretube.api.obj.StreamItem
import com.github.libretube.extensions.move import com.github.libretube.extensions.move
import com.github.libretube.extensions.toID import com.github.libretube.extensions.toID
import com.github.libretube.extensions.toStreamItem
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -13,6 +14,7 @@ import kotlinx.coroutines.launch
object PlayingQueue { object PlayingQueue {
private val queue = mutableListOf<StreamItem>() private val queue = mutableListOf<StreamItem>()
private var currentStream: StreamItem? = null private var currentStream: StreamItem? = null
private val scope = CoroutineScope(Dispatchers.IO)
/** /**
* Listener that gets called when the user selects an item from the queue * Listener that gets called when the user selects an item from the queue
@ -111,7 +113,7 @@ object PlayingQueue {
private fun fetchMoreFromPlaylist(playlistId: String, nextPage: String?) { private fun fetchMoreFromPlaylist(playlistId: String, nextPage: String?) {
var playlistNextPage: String? = nextPage var playlistNextPage: String? = nextPage
CoroutineScope(Dispatchers.IO).launch { scope.launch {
while (playlistNextPage != null) { while (playlistNextPage != null) {
RetrofitInstance.authApi.getPlaylistNextPage( RetrofitInstance.authApi.getPlaylistNextPage(
playlistId, playlistId,
@ -127,7 +129,7 @@ object PlayingQueue {
} }
fun insertPlaylist(playlistId: String, newCurrentStream: StreamItem) { fun insertPlaylist(playlistId: String, newCurrentStream: StreamItem) {
CoroutineScope(Dispatchers.IO).launch { scope.launch {
try { try {
val playlist = PlaylistsHelper.getPlaylist(playlistId) val playlist = PlaylistsHelper.getPlaylist(playlistId)
add(*playlist.relatedStreams.orEmpty().toTypedArray()) add(*playlist.relatedStreams.orEmpty().toTypedArray())
@ -142,7 +144,7 @@ object PlayingQueue {
private fun fetchMoreFromChannel(channelId: String, nextPage: String?) { private fun fetchMoreFromChannel(channelId: String, nextPage: String?) {
var channelNextPage: String? = nextPage var channelNextPage: String? = nextPage
CoroutineScope(Dispatchers.IO).launch { scope.launch {
while (channelNextPage != null) { while (channelNextPage != null) {
RetrofitInstance.api.getChannelNextPage(channelId, nextPage!!).apply { RetrofitInstance.api.getChannelNextPage(channelId, nextPage!!).apply {
add(*relatedStreams.orEmpty().toTypedArray()) add(*relatedStreams.orEmpty().toTypedArray())
@ -153,15 +155,22 @@ object PlayingQueue {
} }
fun insertChannel(channelId: String, newCurrentStream: StreamItem) { fun insertChannel(channelId: String, newCurrentStream: StreamItem) {
CoroutineScope(Dispatchers.IO).launch { scope.launch {
try { runCatching {
val channel = RetrofitInstance.api.getChannel(channelId) val channel = RetrofitInstance.api.getChannel(channelId)
add(*channel.relatedStreams.orEmpty().toTypedArray()) add(*channel.relatedStreams.orEmpty().toTypedArray())
updateCurrent(newCurrentStream) updateCurrent(newCurrentStream)
if (channel.nextpage == null) return@launch if (channel.nextpage == null) return@launch
fetchMoreFromChannel(channelId, channel.nextpage) fetchMoreFromChannel(channelId, channel.nextpage)
} catch (e: Exception) { }
e.printStackTrace() }
}
fun insertByVideoId(videoId: String) {
scope.launch {
runCatching {
val streams = RetrofitInstance.api.getStreams(videoId.toID())
add(streams.toStreamItem(videoId))
} }
} }
} }