计算已看视频个数

This commit is contained in:
renhaoting 2025-11-28 19:06:09 +08:00
parent f5162cb3a8
commit e00bf1da76
5 changed files with 104 additions and 16 deletions

View File

@ -8,5 +8,6 @@ object VididinEvents {
const val Event_HOME_WATCH_Time_TICK = 700 const val Event_HOME_WATCH_Time_TICK = 700
const val Event_Finish_Video = 701
} }

View File

@ -22,6 +22,7 @@ import com.gamedog.vididin.main.fragments.home.fragment.HomeItemFragment
import com.gamedog.vididin.main.interfaces.OnSwitchTabListener import com.gamedog.vididin.main.interfaces.OnSwitchTabListener
import com.gamedog.vididin.main.interfaces.OnTabStyleListener import com.gamedog.vididin.main.interfaces.OnTabStyleListener
import com.gamedog.vididin.youtubestatistic.RewardConst import com.gamedog.vididin.youtubestatistic.RewardConst
import com.gamedog.vididin.youtubestatistic.RewardConst.Companion.Check_Interval_MS
import com.gamedog.vididin.youtubestatistic.TickerTimer import com.gamedog.vididin.youtubestatistic.TickerTimer
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -66,7 +67,8 @@ class HomeFragment : AppViewsFragment<ViewBinding, UiState, ViewModel>(), OnSwit
} }
private fun handleWatchTimeTick() { private fun handleWatchTimeTick() {
mTotalMs += TickerTimer.Notify_Interval_MS mTotalMs += Check_Interval_MS
if (mTotalMs < RewardConst.HOME_WATCH_DURATION) { if (mTotalMs < RewardConst.HOME_WATCH_DURATION) {
binding?.dragIconView?.setProgress(mTotalMs * 100/RewardConst.HOME_WATCH_DURATION) binding?.dragIconView?.setProgress(mTotalMs * 100/RewardConst.HOME_WATCH_DURATION)
} else { } else {

View File

@ -34,7 +34,7 @@ class HomeItemFragment : AppViewsEmptyViewModelFragment<ViewBinding>() {
private var mVideoData: YoutubeVideo? = null private var mVideoData: YoutubeVideo? = null
private var mIsPlaying: Boolean = false private var mIsPlaying: Boolean = false
private var mCurPlayedSecond: Float = 0F private var mCurPlayedSecond: Float = 0F
private var mTotalDuration: Float = -1F private var mTotalDuration: Float = 0F
private val mTickerTimer = TickerTimer() private val mTickerTimer = TickerTimer()
@ -128,6 +128,7 @@ class HomeItemFragment : AppViewsEmptyViewModelFragment<ViewBinding>() {
override fun onVideoDuration(youTubePlayer: YouTubePlayer, duration: Float) { override fun onVideoDuration(youTubePlayer: YouTubePlayer, duration: Float) {
super.onVideoDuration(youTubePlayer, duration) super.onVideoDuration(youTubePlayer, duration)
mTotalDuration = duration mTotalDuration = duration
mTickerTimer.setVideoInfo(mVideoData!!.id, (1000L * mTotalDuration).toLong())
} }
override fun onStateChange( override fun onStateChange(

View File

@ -4,5 +4,7 @@ class RewardConst {
companion object { companion object {
const val HOME_WATCH_DURATION = 5 * 1000L const val HOME_WATCH_DURATION = 5 * 1000L
const val HOME_WATCH_REWARD_NUM = 28 const val HOME_WATCH_REWARD_NUM = 28
const val FINISH_RATIO = 0.1F // TODO - temp value, should be 0.8F
const val Check_Interval_MS = 50L
} }
} }

View File

@ -1,16 +1,21 @@
package com.gamedog.vididin.youtubestatistic package com.gamedog.vididin.youtubestatistic
import android.os.Handler import android.os.Handler
import android.os.HandlerThread
import android.os.Looper import android.os.Looper
import android.os.SystemClock
import com.ama.core.architecture.util.eventbus.NotifyMan import com.ama.core.architecture.util.eventbus.NotifyMan
import com.gamedog.vididin.VididinEvents import com.gamedog.vididin.VididinEvents
import com.gamedog.vididin.youtubestatistic.RewardConst.Companion.Check_Interval_MS
class TickerTimer() { class TickerTimer() {
private var mTotalMs = 0L
private var mHasFinishCurVideo = false
private var mVideoId: String? = null
private var mVideoDurationMs: Long = 0
private val mCountHandler: Handler by lazy { private val mCountHandler: Handler by lazy {
Handler(Looper.getMainLooper()) Handler(Looper.getMainLooper())
} }
@ -19,7 +24,7 @@ class TickerTimer() {
override fun run() { override fun run() {
if (isRunning) { if (isRunning) {
handleNewTick() handleNewTick()
mCountHandler.postDelayed(this, Notify_Interval_MS) mCountHandler.postDelayed(this, Check_Interval_MS)
} }
} }
} }
@ -30,30 +35,34 @@ class TickerTimer() {
fun setVideoInfo(videoId: String, videoDurationMs: Long) {
companion object { mVideoId = videoId
const val Notify_Interval_MS = 50L mVideoDurationMs = videoDurationMs
} }
init {
}
@Synchronized
fun start() { fun start() {
isRunning = true isRunning = true
mCountHandler.postDelayed(mCounterRunnable, Notify_Interval_MS) mCountHandler.postDelayed(mCounterRunnable, Check_Interval_MS)
} }
private fun handleNewTick() { private fun handleNewTick() {
NotifyMan.instance().sendEvent(VididinEvents.Event_HOME_WATCH_Time_TICK, null) NotifyMan.instance().sendEvent(VididinEvents.Event_HOME_WATCH_Time_TICK, null)
if (!mHasFinishCurVideo && mVideoDurationMs > 0L) {
mTotalMs += Check_Interval_MS
if (mTotalMs >= RewardConst.FINISH_RATIO * mVideoDurationMs) {
mHasFinishCurVideo = true
NotifyMan.instance().sendEvent(VididinEvents.Event_Finish_Video,
NotifyMan.NotifyData(Pair(mVideoId, System.currentTimeMillis())))
}
}
} }
fun pause() { fun pause() {
if (!isRunning) return if (!isRunning) return
isRunning = false isRunning = false
mCountHandler.removeCallbacks(mCounterRunnable)
} }
@ -62,6 +71,79 @@ class TickerTimer() {
mCounterRunnable.let { mCountHandler.removeCallbacks(it) } mCounterRunnable.let { mCountHandler.removeCallbacks(it) }
} }
/*private var mStartMs = 0L
private var mTotalMs = 0L
private var mHasFinishCurVideo = false
private var mVideoId: String? = null
private var mVideoDurationMs: Long = 0
private val mCountHandler: Handler by lazy {
Handler(Looper.getMainLooper())
}
private val mCounterRunnable by lazy {
object : Runnable {
override fun run() {
if (isRunning) {
if (mVideoDurationMs > 0L && mStartMs <= 0) {
mStartMs = SystemClock.elapsedRealtime()
}
handleNewTick()
mCountHandler.postDelayed(this, Check_Interval_MS)
}
}
}
}
@Volatile
private var isRunning = false
fun setVideoInfo(videoId: String, videoDurationMs: Long) {
mVideoId = videoId
mVideoDurationMs = videoDurationMs
}
fun start() {
isRunning = true
if (mVideoDurationMs > 0L && mStartMs <= 0) {
mStartMs = SystemClock.elapsedRealtime()
}
mCountHandler.postDelayed(mCounterRunnable, Check_Interval_MS)
}
private fun handleNewTick() {
NotifyMan.instance().sendEvent(VididinEvents.Event_HOME_WATCH_Time_TICK, null)
if (!mHasFinishCurVideo && mStartMs > 0L && mVideoDurationMs > 0L) {
mTotalMs += (SystemClock.elapsedRealtime() - mStartMs)
if (mTotalMs >= RewardConst.FINISH_RATIO * mVideoDurationMs) {
mHasFinishCurVideo = true
NotifyMan.instance().sendEvent(VididinEvents.Event_Finish_Video,
NotifyMan.NotifyData(Pair(mVideoId, System.currentTimeMillis())))
}
}
}
fun pause() {
if (!isRunning) return
isRunning = false
mCountHandler.removeCallbacks(mCounterRunnable)
mStartMs = 0L
}
fun release() {
isRunning = false
mCounterRunnable.let { mCountHandler.removeCallbacks(it) }
}*/
/* /*
private val mHandlerThread = HandlerThread("HomeCountTimeThread") private val mHandlerThread = HandlerThread("HomeCountTimeThread")
private val mCountHandler: Handler by lazy { private val mCountHandler: Handler by lazy {