Merge pull request #3124 from Isira-Seneviratne/Scope_extensions

Use lifecycle coroutine scope extensions.
This commit is contained in:
Bnyro 2023-02-22 10:25:47 +01:00 committed by GitHub
commit a2ce37c8d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 131 additions and 132 deletions

View File

@ -18,8 +18,6 @@ import com.github.libretube.extensions.toastFromMainThread
import com.github.libretube.ui.models.PlaylistViewModel import com.github.libretube.ui.models.PlaylistViewModel
import com.github.libretube.util.PlayingQueue import com.github.libretube.util.PlayingQueue
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
/** /**
@ -76,34 +74,35 @@ class AddToPlaylistDialog(
binding.addToPlaylist.setOnClickListener { binding.addToPlaylist.setOnClickListener {
val index = binding.playlistsSpinner.selectedItemPosition val index = binding.playlistsSpinner.selectedItemPosition
viewModel.lastSelectedPlaylistId = response[index].id!! viewModel.lastSelectedPlaylistId = response[index].id!!
addToPlaylist(response[index].id!!) dialog?.hide()
dialog?.dismiss() lifecycleScope.launch {
addToPlaylist(response[index].id!!)
dialog?.dismiss()
}
} }
} }
} }
} }
private fun addToPlaylist(playlistId: String) { private suspend fun addToPlaylist(playlistId: String) {
val appContext = context?.applicationContext ?: return val appContext = context?.applicationContext ?: return
CoroutineScope(Dispatchers.IO).launch { val streams = when {
val streams = when { videoId != null -> listOf(
videoId != null -> listOf( RetrofitInstance.api.getStreams(videoId).toStreamItem(videoId)
RetrofitInstance.api.getStreams(videoId).toStreamItem(videoId)
)
else -> PlayingQueue.getStreams()
}
val success = try {
PlaylistsHelper.addToPlaylist(playlistId, *streams.toTypedArray())
} catch (e: Exception) {
Log.e(TAG(), e.toString())
appContext.toastFromMainThread(R.string.unknown_error)
return@launch
}
appContext.toastFromMainThread(
if (success) R.string.added_to_playlist else R.string.fail
) )
else -> PlayingQueue.getStreams()
} }
val success = try {
PlaylistsHelper.addToPlaylist(playlistId, *streams.toTypedArray())
} catch (e: Exception) {
Log.e(TAG(), e.toString())
appContext.toastFromMainThread(R.string.unknown_error)
return
}
appContext.toastFromMainThread(
if (success) R.string.added_to_playlist else R.string.fail
)
} }
private fun Fragment?.runOnUiThread(action: () -> Unit) { private fun Fragment?.runOnUiThread(action: () -> Unit) {

View File

@ -4,12 +4,12 @@ import android.app.Dialog
import android.os.Bundle import android.os.Bundle
import android.widget.Toast import android.widget.Toast
import androidx.fragment.app.DialogFragment import androidx.fragment.app.DialogFragment
import androidx.lifecycle.lifecycleScope
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.api.PlaylistsHelper import com.github.libretube.api.PlaylistsHelper
import com.github.libretube.databinding.DialogCreatePlaylistBinding import com.github.libretube.databinding.DialogCreatePlaylistBinding
import com.github.libretube.extensions.toastFromMainThread import com.github.libretube.extensions.toastFromMainThread
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
@ -28,17 +28,19 @@ class CreatePlaylistDialog(
val appContext = context?.applicationContext val appContext = context?.applicationContext
playlistUrl?.queryParameter("list")?.let { playlistUrl?.queryParameter("list")?.let {
CoroutineScope(Dispatchers.IO).launch { lifecycleScope.launch {
val playlistId = PlaylistsHelper.clonePlaylist(requireContext(), it)?.also { requireDialog().hide()
withContext(Dispatchers.Main) { val playlistId = withContext(Dispatchers.IO) {
onSuccess.invoke() PlaylistsHelper.clonePlaylist(requireContext(), it)
} }
if (playlistId != null) {
onSuccess()
} }
appContext?.toastFromMainThread( appContext?.toastFromMainThread(
if (playlistId != null) R.string.playlistCloned else R.string.server_error if (playlistId != null) R.string.playlistCloned else R.string.server_error
) )
dismiss()
} }
dismiss()
} ?: run { } ?: run {
Toast.makeText(context, R.string.invalid_url, Toast.LENGTH_SHORT).show() Toast.makeText(context, R.string.invalid_url, Toast.LENGTH_SHORT).show()
} }
@ -53,16 +55,16 @@ class CreatePlaylistDialog(
binding.createNewPlaylist.setOnClickListener(null) binding.createNewPlaylist.setOnClickListener(null)
val listName = binding.playlistName.text.toString() val listName = binding.playlistName.text.toString()
if (listName != "") { if (listName != "") {
CoroutineScope(Dispatchers.IO).launch { lifecycleScope.launch {
val playlistId = PlaylistsHelper.createPlaylist( requireDialog().hide()
listName, val playlistId = withContext(Dispatchers.IO) {
requireContext().applicationContext PlaylistsHelper.createPlaylist(listName, requireContext())
)
withContext(Dispatchers.Main) {
if (playlistId != null) onSuccess.invoke()
} }
if (playlistId != null) {
onSuccess()
}
dismiss()
} }
dismiss()
} else { } else {
Toast.makeText(context, R.string.emptyPlaylistName, Toast.LENGTH_LONG).show() Toast.makeText(context, R.string.emptyPlaylistName, Toast.LENGTH_LONG).show()
} }

View File

@ -1,20 +1,22 @@
package com.github.libretube.ui.dialogs package com.github.libretube.ui.dialogs
import android.app.Dialog import android.app.Dialog
import android.content.DialogInterface
import android.os.Bundle import android.os.Bundle
import android.text.InputType import android.text.InputType
import android.util.Log import android.util.Log
import android.widget.Toast import android.widget.Toast
import androidx.fragment.app.DialogFragment import androidx.fragment.app.DialogFragment
import androidx.lifecycle.lifecycleScope
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.api.PlaylistsHelper import com.github.libretube.api.PlaylistsHelper
import com.github.libretube.databinding.DialogTextPreferenceBinding import com.github.libretube.databinding.DialogTextPreferenceBinding
import com.github.libretube.extensions.TAG import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.toastFromMainThread import com.github.libretube.extensions.toastFromMainThread
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class RenamePlaylistDialog( class RenamePlaylistDialog(
private val playlistId: String, private val playlistId: String,
@ -29,34 +31,42 @@ class RenamePlaylistDialog(
return MaterialAlertDialogBuilder(requireContext()) return MaterialAlertDialogBuilder(requireContext())
.setTitle(R.string.renamePlaylist) .setTitle(R.string.renamePlaylist)
.setView(binding.root) .setView(binding.root)
.setPositiveButton(R.string.okay) { _, _ -> .setPositiveButton(R.string.okay, null)
val input = binding.input.text.toString() .setNegativeButton(R.string.cancel, null)
if (input == "") { .show()
Toast.makeText( .apply {
context, getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener {
R.string.emptyPlaylistName, val input = binding.input.text.toString()
Toast.LENGTH_SHORT if (input == "") {
).show() Toast.makeText(
return@setPositiveButton context,
} R.string.emptyPlaylistName,
if (input == currentPlaylistName) return@setPositiveButton Toast.LENGTH_SHORT
val appContext = requireContext().applicationContext ).show()
CoroutineScope(Dispatchers.IO).launch { return@setOnClickListener
val success = try {
PlaylistsHelper.renamePlaylist(playlistId, binding.input.text.toString())
} catch (e: Exception) {
Log.e(TAG(), e.toString())
e.localizedMessage?.let { appContext.toastFromMainThread(it) }
return@launch
} }
if (success) { if (input == currentPlaylistName) return@setOnClickListener
appContext.toastFromMainThread(R.string.success) val appContext = requireContext().applicationContext
} else {
appContext.toastFromMainThread(R.string.server_error) lifecycleScope.launch {
requireDialog().hide()
val success = try {
withContext(Dispatchers.IO) {
PlaylistsHelper.renamePlaylist(playlistId, input)
}
} catch (e: Exception) {
Log.e(TAG(), e.toString())
e.localizedMessage?.let { appContext.toastFromMainThread(it) }
return@launch
}
if (success) {
appContext.toastFromMainThread(R.string.success)
} else {
appContext.toastFromMainThread(R.string.server_error)
}
dismiss()
} }
} }
} }
.setNegativeButton(R.string.cancel, null)
.show()
} }
} }

View File

@ -8,6 +8,7 @@ import android.view.ViewGroup
import androidx.core.view.updatePadding import androidx.core.view.updatePadding
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import com.github.libretube.api.JsonHelper import com.github.libretube.api.JsonHelper
import com.github.libretube.api.RetrofitInstance import com.github.libretube.api.RetrofitInstance
@ -19,7 +20,6 @@ import com.github.libretube.extensions.TAG
import com.github.libretube.ui.adapters.CommentsAdapter import com.github.libretube.ui.adapters.CommentsAdapter
import com.github.libretube.ui.extensions.filterNonEmptyComments import com.github.libretube.ui.extensions.filterNonEmptyComments
import com.github.libretube.ui.models.CommentsViewModel import com.github.libretube.ui.models.CommentsViewModel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
@ -89,7 +89,7 @@ class CommentsRepliesFragment : Fragment() {
nextPage: String, nextPage: String,
onFinished: (CommentsPage) -> Unit onFinished: (CommentsPage) -> Unit
) { ) {
CoroutineScope(Dispatchers.IO).launch { lifecycleScope.launch(Dispatchers.IO) {
if (isLoading) return@launch if (isLoading) return@launch
isLoading = true isLoading = true
repliesPage = try { repliesPage = try {

View File

@ -109,7 +109,6 @@ import java.io.IOException
import java.util.* import java.util.*
import java.util.concurrent.Executors import java.util.concurrent.Executors
import kotlin.math.abs import kotlin.math.abs
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
@ -656,7 +655,7 @@ class PlayerFragment : Fragment(R.layout.fragment_player), OnlinePlayerOptions {
} }
if (PlayingQueue.isEmpty()) { if (PlayingQueue.isEmpty()) {
CoroutineScope(Dispatchers.IO).launch { lifecycleScope.launch(Dispatchers.IO) {
if (playlistId != null) { if (playlistId != null) {
PlayingQueue.insertPlaylist(playlistId!!, streams.toStreamItem(videoId!!)) PlayingQueue.insertPlaylist(playlistId!!, streams.toStreamItem(videoId!!))
} else if (channelId != null) { } else if (channelId != null) {
@ -721,7 +720,7 @@ class PlayerFragment : Fragment(R.layout.fragment_player), OnlinePlayerOptions {
* fetch the segments for SponsorBlock * fetch the segments for SponsorBlock
*/ */
private fun fetchSponsorBlockSegments() { private fun fetchSponsorBlockSegments() {
CoroutineScope(Dispatchers.IO).launch { lifecycleScope.launch(Dispatchers.IO) {
runCatching { runCatching {
val categories = PlayerHelper.getSponsorBlockCategories() val categories = PlayerHelper.getSponsorBlockCategories()
if (categories.isEmpty()) return@runCatching if (categories.isEmpty()) return@runCatching

View File

@ -3,16 +3,15 @@ package com.github.libretube.ui.models
import android.util.Log import android.util.Log
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.github.libretube.api.RetrofitInstance import com.github.libretube.api.RetrofitInstance
import com.github.libretube.api.obj.CommentsPage import com.github.libretube.api.obj.CommentsPage
import com.github.libretube.extensions.TAG import com.github.libretube.extensions.TAG
import com.github.libretube.ui.extensions.filterNonEmptyComments import com.github.libretube.ui.extensions.filterNonEmptyComments
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
class CommentsViewModel : ViewModel() { class CommentsViewModel : ViewModel() {
private val scope = CoroutineScope(Dispatchers.IO)
private var isLoading = false private var isLoading = false
val commentsPage = MutableLiveData<CommentsPage?>() val commentsPage = MutableLiveData<CommentsPage?>()
@ -25,7 +24,7 @@ class CommentsViewModel : ViewModel() {
fun fetchComments() { fun fetchComments() {
videoId ?: return videoId ?: return
scope.launch { viewModelScope.launch(Dispatchers.IO) {
isLoading = true isLoading = true
val response = try { val response = try {
RetrofitInstance.api.getComments(videoId!!) RetrofitInstance.api.getComments(videoId!!)
@ -42,7 +41,7 @@ class CommentsViewModel : ViewModel() {
fun fetchNextComments() { fun fetchNextComments() {
if (isLoading || nextPage == null || videoId == null) return if (isLoading || nextPage == null || videoId == null) return
scope.launch { viewModelScope.launch(Dispatchers.IO) {
isLoading = true isLoading = true
val response = try { val response = try {
RetrofitInstance.api.getCommentsNextPage(videoId!!, nextPage!!) RetrofitInstance.api.getCommentsNextPage(videoId!!, nextPage!!)

View File

@ -4,6 +4,7 @@ import android.os.Bundle
import androidx.annotation.StringRes import androidx.annotation.StringRes
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.commitNow import androidx.fragment.app.commitNow
import androidx.lifecycle.lifecycleScope
import androidx.preference.Preference import androidx.preference.Preference
import com.github.libretube.BuildConfig import com.github.libretube.BuildConfig
import com.github.libretube.R import com.github.libretube.R
@ -12,9 +13,9 @@ import com.github.libretube.ui.activities.SettingsActivity
import com.github.libretube.ui.base.BasePreferenceFragment import com.github.libretube.ui.base.BasePreferenceFragment
import com.github.libretube.ui.dialogs.UpdateAvailableDialog import com.github.libretube.ui.dialogs.UpdateAvailableDialog
import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class MainSettings : BasePreferenceFragment() { class MainSettings : BasePreferenceFragment() {
override val titleResourceId: Int = R.string.settings override val titleResourceId: Int = R.string.settings
@ -84,10 +85,12 @@ class MainSettings : BasePreferenceFragment() {
// checking for update: yes -> dialog, no -> snackBar // checking for update: yes -> dialog, no -> snackBar
update?.setOnPreferenceClickListener { update?.setOnPreferenceClickListener {
CoroutineScope(Dispatchers.IO).launch { lifecycleScope.launch {
// check for update // check for update
val updateInfo = try { val updateInfo = try {
RetrofitInstance.externalApi.getUpdateInfo() withContext(Dispatchers.IO) {
RetrofitInstance.externalApi.getUpdateInfo()
}
} catch (e: Exception) { } catch (e: Exception) {
showSnackBar(R.string.unknown_error) showSnackBar(R.string.unknown_error)
return@launch return@launch

View File

@ -4,10 +4,12 @@ import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import com.github.libretube.databinding.BottomSheetBinding import com.github.libretube.databinding.BottomSheetBinding
import com.github.libretube.obj.BottomSheetItem import com.github.libretube.obj.BottomSheetItem
import com.github.libretube.ui.adapters.BottomSheetAdapter import com.github.libretube.ui.adapters.BottomSheetAdapter
import kotlinx.coroutines.launch
open class BaseBottomSheet : ExpandedBottomSheet() { open class BaseBottomSheet : ExpandedBottomSheet() {
private lateinit var items: List<BottomSheetItem> private lateinit var items: List<BottomSheetItem>
@ -30,19 +32,17 @@ open class BaseBottomSheet : ExpandedBottomSheet() {
binding.optionsRecycler.adapter = BottomSheetAdapter(items, listener) binding.optionsRecycler.adapter = BottomSheetAdapter(items, listener)
} }
fun setItems(items: List<BottomSheetItem>, listener: ((index: Int) -> Unit)?) = apply { fun setItems(items: List<BottomSheetItem>, listener: (suspend (index: Int) -> Unit)?) = apply {
this.items = items this.items = items
this.listener = { index -> this.listener = { index ->
listener?.invoke(index) lifecycleScope.launch {
dialog?.dismiss() dialog?.hide()
listener?.invoke(index)
dismiss()
}
} }
} }
fun setSimpleItems(titles: List<String>, listener: ((index: Int) -> Unit)?) = apply { fun setSimpleItems(titles: List<String>, listener: (suspend (index: Int) -> Unit)?) =
this.items = titles.map { BottomSheetItem(it) } setItems(titles.map { BottomSheetItem(it) }, listener)
this.listener = { index ->
listener?.invoke(index)
dialog?.dismiss()
}
}
} }

View File

@ -11,7 +11,8 @@ import com.github.libretube.helpers.BackgroundHelper
import com.github.libretube.helpers.NavigationHelper import com.github.libretube.helpers.NavigationHelper
import com.github.libretube.obj.ShareData import com.github.libretube.obj.ShareData
import com.github.libretube.ui.dialogs.ShareDialog import com.github.libretube.ui.dialogs.ShareDialog
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
/** /**
* Dialog with different options for a selected video. * Dialog with different options for a selected video.
@ -41,7 +42,7 @@ class ChannelOptionsBottomSheet(
} }
getString(R.string.play_latest_videos) -> { getString(R.string.play_latest_videos) -> {
try { try {
val channel = runBlocking { val channel = withContext(Dispatchers.IO) {
RetrofitInstance.api.getChannel(channelId) RetrofitInstance.api.getChannel(channelId)
} }
channel.relatedStreams.firstOrNull()?.url?.toID()?.let { channel.relatedStreams.firstOrNull()?.url?.toID()?.let {
@ -57,7 +58,7 @@ class ChannelOptionsBottomSheet(
} }
getString(R.string.playOnBackground) -> { getString(R.string.playOnBackground) -> {
try { try {
val channel = runBlocking { val channel = withContext(Dispatchers.IO) {
RetrofitInstance.api.getChannel(channelId) RetrofitInstance.api.getChannel(channelId)
} }
channel.relatedStreams.firstOrNull()?.url?.toID()?.let { channel.relatedStreams.firstOrNull()?.url?.toID()?.let {

View File

@ -8,7 +8,6 @@ import com.github.libretube.db.DatabaseHolder
import com.github.libretube.enums.PlaylistType import com.github.libretube.enums.PlaylistType
import com.github.libretube.enums.ShareObjectType import com.github.libretube.enums.ShareObjectType
import com.github.libretube.extensions.awaitQuery import com.github.libretube.extensions.awaitQuery
import com.github.libretube.extensions.query
import com.github.libretube.extensions.toID import com.github.libretube.extensions.toID
import com.github.libretube.extensions.toastFromMainThread import com.github.libretube.extensions.toastFromMainThread
import com.github.libretube.helpers.BackgroundHelper import com.github.libretube.helpers.BackgroundHelper
@ -16,10 +15,8 @@ import com.github.libretube.obj.ShareData
import com.github.libretube.ui.dialogs.DeletePlaylistDialog import com.github.libretube.ui.dialogs.DeletePlaylistDialog
import com.github.libretube.ui.dialogs.RenamePlaylistDialog import com.github.libretube.ui.dialogs.RenamePlaylistDialog
import com.github.libretube.ui.dialogs.ShareDialog import com.github.libretube.ui.dialogs.ShareDialog
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.withContext
import kotlinx.coroutines.runBlocking
class PlaylistOptionsBottomSheet( class PlaylistOptionsBottomSheet(
private val playlistId: String, private val playlistId: String,
@ -55,28 +52,26 @@ class PlaylistOptionsBottomSheet(
when (optionsList[which]) { when (optionsList[which]) {
// play the playlist in the background // play the playlist in the background
getString(R.string.playOnBackground) -> { getString(R.string.playOnBackground) -> {
runBlocking { val playlist = withContext(Dispatchers.IO) {
val playlist = PlaylistsHelper.getPlaylist(playlistId) PlaylistsHelper.getPlaylist(playlistId)
if (playlist.relatedStreams.isEmpty()) return@runBlocking }
playlist.relatedStreams.firstOrNull()?.let {
BackgroundHelper.playOnBackground( BackgroundHelper.playOnBackground(
context = requireContext(), requireContext(),
videoId = playlist.relatedStreams[0].url!!.toID(), it.url!!.toID(),
playlistId = playlistId playlistId = playlistId
) )
} }
} }
// Clone the playlist to the users Piped account // Clone the playlist to the users Piped account
getString(R.string.clonePlaylist) -> { getString(R.string.clonePlaylist) -> {
val appContext = context?.applicationContext val context = requireContext()
CoroutineScope(Dispatchers.IO).launch { val playlistId = withContext(Dispatchers.IO) {
val playlistId = PlaylistsHelper.clonePlaylist( PlaylistsHelper.clonePlaylist(context, playlistId)
requireContext().applicationContext,
playlistId
)
appContext?.toastFromMainThread(
if (playlistId != null) R.string.playlistCloned else R.string.server_error
)
} }
context.toastFromMainThread(
if (playlistId != null) R.string.playlistCloned else R.string.server_error
)
} }
// share the playlist // share the playlist
getString(R.string.share) -> { getString(R.string.share) -> {
@ -87,7 +82,7 @@ class PlaylistOptionsBottomSheet(
getString(R.string.deletePlaylist) -> { getString(R.string.deletePlaylist) -> {
DeletePlaylistDialog(playlistId, playlistType) { DeletePlaylistDialog(playlistId, playlistType) {
// try to refresh the playlists in the library on deletion success // try to refresh the playlists in the library on deletion success
onDelete.invoke() onDelete()
}.show(parentFragmentManager, null) }.show(parentFragmentManager, null)
} }
getString(R.string.renamePlaylist) -> { getString(R.string.renamePlaylist) -> {
@ -95,22 +90,18 @@ class PlaylistOptionsBottomSheet(
.show(parentFragmentManager, null) .show(parentFragmentManager, null)
} }
else -> { else -> {
CoroutineScope(Dispatchers.IO).launch { withContext(Dispatchers.IO) {
if (isBookmarked) { if (isBookmarked) {
query { DatabaseHolder.Database.playlistBookmarkDao().deleteById(playlistId)
DatabaseHolder.Database.playlistBookmarkDao()
.deleteById(playlistId)
}
} else { } else {
val bookmark = try { val bookmark = try {
RetrofitInstance.api.getPlaylist(playlistId) RetrofitInstance.api.getPlaylist(playlistId)
} catch (e: Exception) { } catch (e: Exception) {
return@launch return@withContext
}.toPlaylistBookmark(playlistId) }.toPlaylistBookmark(playlistId)
DatabaseHolder.Database.playlistBookmarkDao().insertAll(bookmark) DatabaseHolder.Database.playlistBookmarkDao().insertAll(bookmark)
} }
} }
dismiss()
} }
} }
} }

View File

@ -18,9 +18,8 @@ import com.github.libretube.ui.dialogs.DownloadDialog
import com.github.libretube.ui.dialogs.ShareDialog import com.github.libretube.ui.dialogs.ShareDialog
import com.github.libretube.ui.fragments.SubscriptionsFragment import com.github.libretube.ui.fragments.SubscriptionsFragment
import com.github.libretube.util.PlayingQueue import com.github.libretube.util.PlayingQueue
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.withContext
/** /**
* Dialog with different options for a selected video. * Dialog with different options for a selected video.
@ -75,27 +74,23 @@ class VideoOptionsBottomSheet(
shareDialog.show(parentFragmentManager, ShareDialog::class.java.name) shareDialog.show(parentFragmentManager, ShareDialog::class.java.name)
} }
getString(R.string.play_next) -> { getString(R.string.play_next) -> {
CoroutineScope(Dispatchers.IO).launch { try {
try { val streamItem = withContext(Dispatchers.IO) {
PlayingQueue.addAsNext( RetrofitInstance.api.getStreams(videoId).toStreamItem(videoId)
RetrofitInstance.api.getStreams(videoId)
.toStreamItem(videoId)
)
} catch (e: Exception) {
e.printStackTrace()
} }
PlayingQueue.addAsNext(streamItem)
} catch (e: Exception) {
e.printStackTrace()
} }
} }
getString(R.string.add_to_queue) -> { getString(R.string.add_to_queue) -> {
CoroutineScope(Dispatchers.IO).launch { try {
try { val streamItem = withContext(Dispatchers.IO) {
PlayingQueue.add( RetrofitInstance.api.getStreams(videoId).toStreamItem(videoId)
RetrofitInstance.api.getStreams(videoId)
.toStreamItem(videoId)
)
} catch (e: Exception) {
e.printStackTrace()
} }
PlayingQueue.add(streamItem)
} catch (e: Exception) {
e.printStackTrace()
} }
} }
getString(R.string.mark_as_watched) -> { getString(R.string.mark_as_watched) -> {