LibreTube/app/src/main/java/com/github/libretube/extensions/SetWatchProgressLength.kt
2022-08-27 15:15:07 +02:00

40 lines
1.3 KiB
Kotlin

package com.github.libretube.extensions
import android.view.View
import android.view.ViewTreeObserver
import android.widget.LinearLayout
import com.github.libretube.db.DatabaseHolder
/**
* shows the already watched time under the video
*/
fun View?.setWatchProgressLength(videoId: String, duration: Long) {
val view = this!!
var progress: Long? = null
Thread {
try {
progress = DatabaseHolder.db.watchPositionDao().findById(videoId).position
} catch (e: Exception) {
progress = null
}
}.await()
view.getViewTreeObserver()
.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
this@setWatchProgressLength.getViewTreeObserver().removeOnGlobalLayoutListener(this)
if (progress == null || duration == 0L) {
view.visibility = View.GONE
return
}
val fullWidth = (parent as LinearLayout).width
val newWidth = (fullWidth * (progress!! / (duration))) / 1000
val lp = view.layoutParams
lp.width = newWidth.toInt()
view.layoutParams = lp
view.visibility = View.VISIBLE
}
})
}