Add bookmark option to playlist options bottom sheet

This commit is contained in:
Bnyro 2023-01-04 19:27:36 +01:00
parent a6a0654f5b
commit 2db5852459
4 changed files with 62 additions and 17 deletions

View File

@ -0,0 +1,15 @@
package com.github.libretube.extensions
import com.github.libretube.api.obj.Playlist
import com.github.libretube.db.obj.PlaylistBookmark
fun Playlist.toPlaylistBookmark(playlistId: String): PlaylistBookmark {
return PlaylistBookmark(
playlistId = playlistId,
playlistName = name,
thumbnailUrl = thumbnailUrl,
uploader = uploader,
uploaderAvatar = uploaderAvatar,
uploaderUrl = uploaderUrl
)
}

View File

@ -19,13 +19,13 @@ import com.github.libretube.api.obj.StreamItem
import com.github.libretube.constants.IntentData
import com.github.libretube.databinding.FragmentPlaylistBinding
import com.github.libretube.db.DatabaseHolder
import com.github.libretube.db.obj.PlaylistBookmark
import com.github.libretube.enums.PlaylistType
import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.awaitQuery
import com.github.libretube.extensions.query
import com.github.libretube.extensions.toID
import com.github.libretube.extensions.toPixel
import com.github.libretube.extensions.toPlaylistBookmark
import com.github.libretube.ui.adapters.PlaylistAdapter
import com.github.libretube.ui.base.BaseFragment
import com.github.libretube.ui.extensions.serializable
@ -157,14 +157,7 @@ class PlaylistFragment : BaseFragment() {
.deleteById(playlistId!!)
} else {
DatabaseHolder.Database.playlistBookmarkDao().insertAll(
PlaylistBookmark(
playlistId = playlistId!!,
playlistName = response.name,
thumbnailUrl = response.thumbnailUrl,
uploader = response.uploader,
uploaderAvatar = response.uploaderAvatar,
uploaderUrl = response.uploaderUrl
)
response.toPlaylistBookmark(playlistId!!)
)
}
}

View File

@ -4,14 +4,21 @@ import android.os.Bundle
import com.github.libretube.R
import com.github.libretube.api.PlaylistsHelper
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.db.DatabaseHolder
import com.github.libretube.enums.PlaylistType
import com.github.libretube.enums.ShareObjectType
import com.github.libretube.extensions.awaitQuery
import com.github.libretube.extensions.query
import com.github.libretube.extensions.toID
import com.github.libretube.extensions.toPlaylistBookmark
import com.github.libretube.obj.ShareData
import com.github.libretube.ui.dialogs.DeletePlaylistDialog
import com.github.libretube.ui.dialogs.RenamePlaylistDialog
import com.github.libretube.ui.dialogs.ShareDialog
import com.github.libretube.util.BackgroundHelper
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
class PlaylistOptionsBottomSheet(
@ -24,12 +31,21 @@ class PlaylistOptionsBottomSheet(
override fun onCreate(savedInstanceState: Bundle?) {
// options for the dialog
val optionsList = mutableListOf(
context?.getString(R.string.playOnBackground)!!
getString(R.string.playOnBackground)
)
val isBookmarked = awaitQuery {
DatabaseHolder.Database.playlistBookmarkDao().includes(playlistId)
}
if (playlistType == PlaylistType.PUBLIC) {
optionsList.add(context?.getString(R.string.share)!!)
optionsList.add(context?.getString(R.string.clonePlaylist)!!)
optionsList.add(getString(R.string.share))
optionsList.add(getString(R.string.clonePlaylist))
// only add the bookmark option to the playlist if public
optionsList.add(
getString(if (isBookmarked) R.string.remove_bookmark else R.string.add_to_bookmarks)
)
} else {
optionsList.add(context?.getString(R.string.renamePlaylist)!!)
optionsList.add(context?.getString(R.string.deletePlaylist)!!)
@ -38,7 +54,7 @@ class PlaylistOptionsBottomSheet(
setSimpleItems(optionsList) { which ->
when (optionsList[which]) {
// play the playlist in the background
context?.getString(R.string.playOnBackground) -> {
getString(R.string.playOnBackground) -> {
runBlocking {
val playlist =
if (playlistType == PlaylistType.PRIVATE) {
@ -54,25 +70,43 @@ class PlaylistOptionsBottomSheet(
}
}
// Clone the playlist to the users Piped account
context?.getString(R.string.clonePlaylist) -> {
getString(R.string.clonePlaylist) -> {
PlaylistsHelper.clonePlaylist(requireContext(), playlistId)
}
// share the playlist
context?.getString(R.string.share) -> {
getString(R.string.share) -> {
val shareDialog = ShareDialog(playlistId, ShareObjectType.PLAYLIST, shareData)
// using parentFragmentManager, childFragmentManager doesn't work here
shareDialog.show(parentFragmentManager, ShareDialog::class.java.name)
}
context?.getString(R.string.deletePlaylist) -> {
getString(R.string.deletePlaylist) -> {
DeletePlaylistDialog(playlistId, playlistType) {
// try to refresh the playlists in the library on deletion success
onDelete.invoke()
}.show(parentFragmentManager, null)
}
context?.getString(R.string.renamePlaylist) -> {
getString(R.string.renamePlaylist) -> {
RenamePlaylistDialog(playlistId, playlistName)
.show(parentFragmentManager, null)
}
else -> {
CoroutineScope(Dispatchers.IO).launch {
if (isBookmarked) {
query {
DatabaseHolder.Database.playlistBookmarkDao()
.deleteById(playlistId)
}
} else {
val bookmark = try {
RetrofitInstance.api.getPlaylist(playlistId)
} catch (e: Exception) {
return@launch
}.toPlaylistBookmark(playlistId)
DatabaseHolder.Database.playlistBookmarkDao().insertAll(bookmark)
}
}
dismiss()
}
}
}
super.onCreate(savedInstanceState)

View File

@ -423,6 +423,9 @@
<string name="playlistUrl">Playlist URL</string>
<string name="pause_on_quit">Pause on quit</string>
<string name="shuffle">Shuffle</string>
<string name="add_to_bookmarks">Add to bookmarks</string>
<string name="remove_bookmark">Remove bookmark</string>
<!-- Notification channel strings -->
<string name="download_channel_name">Download Service</string>
<string name="download_channel_description">Shows a notification when downloading media.</string>