LibreTube/app/src/main/java/com/github/libretube/extensions/SetWatchProgressLength.kt

36 lines
1.2 KiB
Kotlin
Raw Normal View History

2022-07-28 12:48:32 +05:30
package com.github.libretube.util
import android.view.View
import android.view.ViewTreeObserver
import com.github.libretube.preferences.PreferenceHelper
/**
* shows the already watched time under the video
*/
fun View?.setWatchProgressLength(videoId: String, duration: Long) {
val view = this!!
val positions = PreferenceHelper.getWatchPositions()
var newWidth: Long? = null
view.getViewTreeObserver()
.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
this@setWatchProgressLength.getViewTreeObserver().removeOnGlobalLayoutListener(this)
positions.forEach {
if (it.videoId == videoId) {
newWidth = (width * (it.position / (duration))) / 1000
return@forEach
}
}
if (newWidth != null) {
val lp = view.layoutParams
lp.apply {
width = newWidth!!.toInt()
}
view.layoutParams = lp
} else {
view.visibility = View.GONE
}
}
})
}