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
|
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 {
|
|
|
|
progress = DatabaseHolder.database.watchPositionDao().findById(videoId).position
|
|
|
|
} 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
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|