From 2f10b7ba2324af45d29c939b30417c960bb98834 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 16 Jan 2023 18:25:12 +0100 Subject: [PATCH] Option to add newly opened links to queue --- app/src/main/AndroidManifest.xml | 17 +++++++- .../ui/activities/AddToQueueActivity.kt | 39 +++++++++++++++++++ .../libretube/ui/activities/RouterActivity.kt | 6 +-- .../com/github/libretube/util/PlayingQueue.kt | 23 +++++++---- 4 files changed, 73 insertions(+), 12 deletions(-) create mode 100644 app/src/main/java/com/github/libretube/ui/activities/AddToQueueActivity.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 06d4a6c8f..279de389d 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -47,6 +47,21 @@ android:name=".ui.activities.CommunityActivity" android:label="@string/settings" /> + + + + + + + + + + - + diff --git a/app/src/main/java/com/github/libretube/ui/activities/AddToQueueActivity.kt b/app/src/main/java/com/github/libretube/ui/activities/AddToQueueActivity.kt new file mode 100644 index 000000000..e1f1ca436 --- /dev/null +++ b/app/src/main/java/com/github/libretube/ui/activities/AddToQueueActivity.kt @@ -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() + } +} diff --git a/app/src/main/java/com/github/libretube/ui/activities/RouterActivity.kt b/app/src/main/java/com/github/libretube/ui/activities/RouterActivity.kt index a0ff62adf..898e35557 100644 --- a/app/src/main/java/com/github/libretube/ui/activities/RouterActivity.kt +++ b/app/src/main/java/com/github/libretube/ui/activities/RouterActivity.kt @@ -84,10 +84,8 @@ class RouterActivity : BaseActivity() { val pm: PackageManager = this.packageManager val intent = pm.getLaunchIntentForPackage(this.packageName) intent?.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK - this.startActivity( - resolveType(intent!!, uri) - ) - this.finishAndRemoveTask() + startActivity(resolveType(intent!!, uri)) + finishAndRemoveTask() } private fun parseTimestamp(t: String): Long? { diff --git a/app/src/main/java/com/github/libretube/util/PlayingQueue.kt b/app/src/main/java/com/github/libretube/util/PlayingQueue.kt index 995f45bfd..35f3c3436 100644 --- a/app/src/main/java/com/github/libretube/util/PlayingQueue.kt +++ b/app/src/main/java/com/github/libretube/util/PlayingQueue.kt @@ -6,6 +6,7 @@ import com.github.libretube.api.RetrofitInstance import com.github.libretube.api.obj.StreamItem import com.github.libretube.extensions.move import com.github.libretube.extensions.toID +import com.github.libretube.extensions.toStreamItem import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -13,6 +14,7 @@ import kotlinx.coroutines.launch object PlayingQueue { private val queue = mutableListOf() private var currentStream: StreamItem? = null + private val scope = CoroutineScope(Dispatchers.IO) /** * 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?) { var playlistNextPage: String? = nextPage - CoroutineScope(Dispatchers.IO).launch { + scope.launch { while (playlistNextPage != null) { RetrofitInstance.authApi.getPlaylistNextPage( playlistId, @@ -127,7 +129,7 @@ object PlayingQueue { } fun insertPlaylist(playlistId: String, newCurrentStream: StreamItem) { - CoroutineScope(Dispatchers.IO).launch { + scope.launch { try { val playlist = PlaylistsHelper.getPlaylist(playlistId) add(*playlist.relatedStreams.orEmpty().toTypedArray()) @@ -142,7 +144,7 @@ object PlayingQueue { private fun fetchMoreFromChannel(channelId: String, nextPage: String?) { var channelNextPage: String? = nextPage - CoroutineScope(Dispatchers.IO).launch { + scope.launch { while (channelNextPage != null) { RetrofitInstance.api.getChannelNextPage(channelId, nextPage!!).apply { add(*relatedStreams.orEmpty().toTypedArray()) @@ -153,15 +155,22 @@ object PlayingQueue { } fun insertChannel(channelId: String, newCurrentStream: StreamItem) { - CoroutineScope(Dispatchers.IO).launch { - try { + scope.launch { + runCatching { val channel = RetrofitInstance.api.getChannel(channelId) add(*channel.relatedStreams.orEmpty().toTypedArray()) updateCurrent(newCurrentStream) if (channel.nextpage == null) return@launch 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)) } } }