Merge pull request #2638 from Bnyro/master

Improve the responsiveness of the timebar preview
This commit is contained in:
Bnyro 2023-01-08 17:40:03 +01:00 committed by GitHub
commit a0289c652a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 38 deletions

View File

@ -1,6 +1,5 @@
package com.github.libretube.ui.extensions
import android.util.Log
import com.github.libretube.R
import com.github.libretube.util.PreferenceHelper
import com.google.android.material.button.MaterialButton

View File

@ -185,29 +185,29 @@ class PlaylistFragment : BaseFragment() {
// listen for playlist items to become deleted
playlistAdapter!!.registerAdapterDataObserver(object :
RecyclerView.AdapterDataObserver() {
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()
RecyclerView.AdapterDataObserver() {
override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) {
if (positionStart == 0) {
ImageHelper.loadImage(
playlistFeed.firstOrNull()?.thumbnail ?: "",
binding.thumbnail
)
super.onItemRangeRemoved(positionStart, itemCount)
}
})
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)
}
})
binding.playlistRecView.adapter = playlistAdapter
binding.playlistScrollview.viewTreeObserver

View File

@ -17,29 +17,19 @@ class SeekbarPreviewListener(
) : TimeBar.OnScrubListener {
private var moving = false
override fun onScrubStart(timeBar: TimeBar, position: Long) {}
override fun onScrubStart(timeBar: TimeBar, position: Long) {
moving = true
processPreview(position)
}
/**
* Show a preview of the scrubber position
*/
override fun onScrubMove(timeBar: TimeBar, position: Long) {
moving = true
val previewFrame = getPreviewFrame(position) ?: return
// update the offset of the preview image view
updatePreviewX(position)
val request = ImageRequest.Builder(previewIv.context)
.data(previewFrame.previewUrl)
.target {
if (!moving) return@target
val frame = cutOutBitmap(it.toBitmap(), previewFrame)
previewIv.setImageBitmap(frame)
previewIv.visibility = View.VISIBLE
}
.build()
ImageHelper.imageLoader.enqueue(request)
processPreview(position)
}
/**
@ -60,6 +50,28 @@ class SeekbarPreviewListener(
.start()
}
/**
* Make a request to get the image frame and update its position
*/
private fun processPreview(position: Long) {
val previewFrame = getPreviewFrame(position) ?: return
// update the offset of the preview image view
updatePreviewX(position)
val request = ImageRequest.Builder(previewIv.context)
.data(previewFrame.previewUrl)
.target {
if (!moving) return@target
val frame = cutOutBitmap(it.toBitmap(), previewFrame)
previewIv.setImageBitmap(frame)
previewIv.visibility = View.VISIBLE
}
.build()
ImageHelper.imageLoader.enqueue(request)
}
/**
* Get the preview frame according to the current position
*/