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 { awaitQuery {
DatabaseHolder.Database.localPlaylistsDao().removePlaylistVideo(transaction.videos[index]) 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 // remove thumbnail if playlist now empty
awaitQuery { awaitQuery {
transaction.playlist.thumbnailUrl = "" transaction.playlist.thumbnailUrl = ""

View File

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