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

41 lines
1.3 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
2022-07-29 15:10:36 +05:30
import android.widget.LinearLayout
2022-08-14 13:29:05 +05:30
import com.github.libretube.db.DatabaseHolder
2022-08-14 02:07:13 +05:30
import com.github.libretube.extensions.await
2022-07-28 12:48:32 +05:30
/**
* shows the already watched time under the video
*/
fun View?.setWatchProgressLength(videoId: String, duration: Long) {
val view = this!!
2022-08-14 02:36:02 +05:30
var progress: Long? = null
2022-08-14 01:33:11 +05:30
2022-08-14 02:07:13 +05:30
Thread {
2022-08-14 02:36:02 +05:30
try {
2022-08-14 13:29:05 +05:30
progress = DatabaseHolder.db.watchPositionDao().findById(videoId).position
2022-08-14 02:36:02 +05:30
} catch (e: Exception) {
progress = null
}
2022-08-14 02:07:13 +05:30
}.await()
2022-08-14 01:33:11 +05:30
2022-07-28 12:48:32 +05:30
view.getViewTreeObserver()
.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
this@setWatchProgressLength.getViewTreeObserver().removeOnGlobalLayoutListener(this)
2022-08-14 02:36:02 +05:30
if (progress == null) {
2022-07-28 12:48:32 +05:30
view.visibility = View.GONE
2022-08-14 02:36:02 +05:30
return
2022-07-28 12:48:32 +05:30
}
2022-08-14 02:36:02 +05:30
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
2022-07-28 12:48:32 +05:30
}
})
}