Merge pull request #2320 from Kruna1Pate1/fix/playlist-thumbnail

Fix playlist thumbnail change when remove first video
This commit is contained in:
Krunal Patel 2022-12-09 22:28:13 +05:30 committed by GitHub
commit 5a2c38e524
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 6 deletions

View File

@ -175,7 +175,15 @@ object PlaylistsHelper {
awaitQuery {
DatabaseHolder.Database.localPlaylistsDao().removePlaylistVideo(transaction.videos[index])
}
if (transaction.videos.size > 1) return
if (transaction.videos.size > 1) {
if (index == 0) {
transaction.videos[1].thumbnailUrl?.let { transaction.playlist.thumbnailUrl = it }
awaitQuery {
DatabaseHolder.Database.localPlaylistsDao().updatePlaylist(transaction.playlist)
}
}
return
}
// remove thumbnail if playlist now empty
awaitQuery {
transaction.playlist.thumbnailUrl = ""

View File

@ -15,6 +15,7 @@ import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.R
import com.github.libretube.api.PlaylistsHelper
import com.github.libretube.api.RetrofitInstance
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
@ -46,6 +47,7 @@ class PlaylistFragment : BaseFragment() {
private var playlistAdapter: PlaylistAdapter? = null
private var isLoading = true
private var isBookmarked = false
private val playlistFeed = mutableListOf<StreamItem>()
private val playerViewModel: PlayerViewModel by activityViewModels()
@ -108,6 +110,7 @@ class PlaylistFragment : BaseFragment() {
Log.e(TAG(), "HttpException, unexpected response")
return@launchWhenCreated
}
playlistFeed.addAll(response.relatedStreams.orEmpty())
binding.playlistScrollview.visibility = View.VISIBLE
nextPage = response.nextpage
playlistName = response.name
@ -133,7 +136,7 @@ class PlaylistFragment : BaseFragment() {
}
binding.playAll.setOnClickListener {
if (response.relatedStreams.orEmpty().isEmpty()) return@setOnClickListener
if (playlistFeed.isEmpty()) return@setOnClickListener
NavigationHelper.navigateVideo(
requireContext(),
response.relatedStreams!!.first().url?.toID(),
@ -165,7 +168,7 @@ class PlaylistFragment : BaseFragment() {
}
playlistAdapter = PlaylistAdapter(
response.relatedStreams.orEmpty().toMutableList(),
playlistFeed,
playlistId!!,
playlistType
)
@ -173,12 +176,26 @@ class PlaylistFragment : BaseFragment() {
// listen for playlist items to become deleted
playlistAdapter!!.registerAdapterDataObserver(object :
RecyclerView.AdapterDataObserver() {
override fun onChanged() {
binding.playlistInfo.text =
binding.playlistInfo.text.split(TextUtils.SEPARATOR).first() + TextUtils.SEPARATOR + getString(
override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) {
if (positionStart == 0) {
ImageHelper.loadImage(
playlistFeed.firstOrNull()?.thumbnail ?: "",
binding.thumbnail
)
}
val info = binding.playlistInfo.text.split(TextUtils.SEPARATOR)
binding.playlistInfo.text = (
if (info.size == 2) {
info[0] + TextUtils.SEPARATOR
} else {
""
}
) + getString(
R.string.videoCount,
playlistAdapter!!.itemCount.toString()
)
super.onItemRangeRemoved(positionStart, itemCount)
}
})