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

48 lines
1.6 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 01:33:11 +05:30
import com.github.libretube.database.DatabaseHolder
import com.github.libretube.obj.WatchPosition
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 01:33:11 +05:30
var positions = listOf<WatchPosition>()
2022-07-28 12:48:32 +05:30
var newWidth: Long? = null
2022-08-14 01:33:11 +05:30
val thread = Thread {
positions = DatabaseHolder.database.watchPositionDao().getAll()
}
thread.start()
thread.join()
2022-07-28 12:48:32 +05:30
view.getViewTreeObserver()
.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
this@setWatchProgressLength.getViewTreeObserver().removeOnGlobalLayoutListener(this)
positions.forEach {
if (it.videoId == videoId) {
2022-07-29 15:10:36 +05:30
val fullWidth = (parent as LinearLayout).width
2022-08-01 12:47:36 +05:30
if (duration != 0L) newWidth =
(fullWidth * (it.position / (duration))) / 1000
2022-07-28 12:48:32 +05:30
return@forEach
}
}
if (newWidth != null) {
val lp = view.layoutParams
lp.apply {
width = newWidth!!.toInt()
}
view.layoutParams = lp
2022-07-29 15:10:36 +05:30
view.visibility = View.VISIBLE
2022-07-28 12:48:32 +05:30
} else {
view.visibility = View.GONE
}
}
})
}