Merge pull request #2535 from Bnyro/master

Add shuffle button to owned playlists
This commit is contained in:
Bnyro 2022-12-28 17:46:09 +01:00 committed by GitHub
commit cc2d035bd7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 62 additions and 31 deletions

View File

@ -4,12 +4,15 @@ import android.app.Dialog
import android.os.Bundle
import android.widget.Toast
import androidx.fragment.app.DialogFragment
import androidx.lifecycle.lifecycleScope
import com.github.libretube.R
import com.github.libretube.api.PlaylistsHelper
import com.github.libretube.databinding.DialogCreatePlaylistBinding
import com.github.libretube.util.TextUtils
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class CreatePlaylistDialog(
private val onSuccess: () -> Unit = {}
@ -26,6 +29,7 @@ class CreatePlaylistDialog(
return@setOnClickListener
}
PlaylistsHelper.clonePlaylist(requireContext().applicationContext, playlistUrl)
dismiss()
}
binding.cancelButton.setOnClickListener {
@ -37,11 +41,16 @@ class CreatePlaylistDialog(
binding.createNewPlaylist.setOnClickListener(null)
val listName = binding.playlistName.text.toString()
if (listName != "") {
lifecycleScope.launchWhenCreated {
PlaylistsHelper.createPlaylist(listName, requireContext().applicationContext)
onSuccess.invoke()
dismiss()
CoroutineScope(Dispatchers.IO).launch {
val playlistId = PlaylistsHelper.createPlaylist(
listName,
requireContext().applicationContext
)
withContext(Dispatchers.Main) {
if (playlistId != null) onSuccess.invoke()
}
}
dismiss()
} else {
Toast.makeText(context, R.string.emptyPlaylistName, Toast.LENGTH_LONG).show()
}

View File

@ -33,6 +33,7 @@ import com.github.libretube.ui.models.PlayerViewModel
import com.github.libretube.ui.sheets.PlaylistOptionsBottomSheet
import com.github.libretube.util.ImageHelper
import com.github.libretube.util.NavigationHelper
import com.github.libretube.util.PlayingQueue
import com.github.libretube.util.TextUtils
import java.io.IOException
import retrofit2.HttpException
@ -146,27 +147,43 @@ class PlaylistFragment : BaseFragment() {
)
}
if (playlistType != PlaylistType.PUBLIC) binding.bookmark.visibility = View.GONE
binding.bookmark.setOnClickListener {
isBookmarked = !isBookmarked
updateBookmarkRes()
query {
if (!isBookmarked) {
DatabaseHolder.Database.playlistBookmarkDao().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
if (playlistType == PlaylistType.PUBLIC) {
binding.bookmark.setOnClickListener {
isBookmarked = !isBookmarked
updateBookmarkRes()
query {
if (!isBookmarked) {
DatabaseHolder.Database.playlistBookmarkDao()
.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
)
)
)
}
}
}
} else {
// private playlist, means shuffle is possible because all videos are received at once
binding.bookmark.setIconResource(R.drawable.ic_shuffle)
binding.bookmark.text = getString(R.string.shuffle)
binding.bookmark.setOnClickListener {
val queue = playlistFeed.shuffled()
PlayingQueue.resetToDefaults()
PlayingQueue.add(*queue.toTypedArray())
NavigationHelper.navigateVideo(
requireContext(),
queue.first().url?.toID(),
playlistId = playlistId,
keepQueue = true
)
}
}
playlistAdapter = PlaylistAdapter(

View File

@ -89,7 +89,11 @@ class WatchHistoryFragment : BaseFragment() {
)
}.toTypedArray()
)
NavigationHelper.navigateVideo(requireContext(), watchHistory.last().videoId, keepQueue = true)
NavigationHelper.navigateVideo(
requireContext(),
watchHistory.last().videoId,
keepQueue = true
)
}
// reversed order
@ -128,14 +132,14 @@ class WatchHistoryFragment : BaseFragment() {
// observe changes
watchHistoryAdapter.registerAdapterDataObserver(object :
RecyclerView.AdapterDataObserver() {
override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) {
if (watchHistoryAdapter.itemCount == 0) {
binding.historyScrollView.visibility = View.GONE
binding.historyEmpty.visibility = View.VISIBLE
}
RecyclerView.AdapterDataObserver() {
override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) {
if (watchHistoryAdapter.itemCount == 0) {
binding.historyScrollView.visibility = View.GONE
binding.historyEmpty.visibility = View.VISIBLE
}
})
}
})
binding.watchHistoryRecView.adapter = watchHistoryAdapter
binding.historyEmpty.visibility = View.GONE

View File

@ -422,6 +422,7 @@
<string name="hide_watched_from_feed_summary">Don\'t show videos being watched more than 90% in the subscriptions tab.</string>
<string name="playlistUrl">Playlist URL</string>
<string name="pause_on_quit">Pause on quit</string>
<string name="shuffle">Shuffle</string>
<!-- Notification channel strings -->
<string name="download_channel_name">Download Service</string>