|
@@ -13,8 +13,10 @@ import com.tencent.liteav.demo.superplayer.database.entity.History
|
|
|
import com.tencent.liteav.demo.superplayer.database.repo.PlayerRepository
|
|
|
import com.tencent.liteav.demo.superplayer.util.CountDownUtil
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
+import kotlinx.coroutines.async
|
|
|
import kotlinx.coroutines.flow.first
|
|
|
import kotlinx.coroutines.launch
|
|
|
+import kotlinx.coroutines.withContext
|
|
|
import java.text.SimpleDateFormat
|
|
|
import java.util.*
|
|
|
|
|
@@ -37,12 +39,15 @@ class PlayerViewModel(
|
|
|
|
|
|
fun loadConfig(context: Context) = viewModelScope.launch {
|
|
|
val common = context.getSharedPreferences("player_timer", Context.MODE_PRIVATE)
|
|
|
- val defaultType = common.getInt("type", 0);
|
|
|
- val defaultValue = common.getInt("value", 0);
|
|
|
+ val defaultType = common.getInt("type", 0)
|
|
|
+ val defaultValue = common.getInt("value", 0)
|
|
|
+ val defaultDate = common.getString("datetime", "20220831000000")!!
|
|
|
+
|
|
|
val default = CountDown(
|
|
|
courseId = courseId,
|
|
|
type = defaultType,
|
|
|
value = defaultValue,
|
|
|
+ datetime = defaultDate,
|
|
|
)
|
|
|
val special = repository.findHasCountDown(courseId).first()
|
|
|
|
|
@@ -55,50 +60,60 @@ class PlayerViewModel(
|
|
|
setCountDown(countDown)
|
|
|
}
|
|
|
|
|
|
- /* 保存一条观看时间段的记录 */
|
|
|
- fun saveHistory(history: History) = viewModelScope.launch(Dispatchers.IO) {
|
|
|
- repository.insertHistory(history)
|
|
|
- }
|
|
|
-
|
|
|
/* 保存对该门课程单独设置的定时 */
|
|
|
private fun saveCountDown(countDown: CountDown) = viewModelScope.launch(Dispatchers.IO) {
|
|
|
+ repository.deleteCountDown(courseId)
|
|
|
repository.insertCountDown(countDown)
|
|
|
}
|
|
|
|
|
|
/* 开启定时器 */
|
|
|
private fun setCountDown(countDown: CountDown) {
|
|
|
- checkTimeout()
|
|
|
+ countDownUtil?.cancel()
|
|
|
+ countDownUtil = null
|
|
|
|
|
|
- if (countDown.type == CountDown.TYPE_EPISODE) {
|
|
|
+ checkTimeout(countDown)
|
|
|
+ }
|
|
|
|
|
|
- } else if (countDown.type == CountDown.TYPE_DURATION) {
|
|
|
- countDownUtil?.cancel()
|
|
|
- countDownUtil = CountDownUtil.build(1000, countDown.value, this)
|
|
|
- countDownUtil?.start()
|
|
|
- } else {
|
|
|
- countDownUtil?.cancel()
|
|
|
- }
|
|
|
+ fun resumeTimer() {
|
|
|
+ countDownUtil?.startOrResume()
|
|
|
}
|
|
|
|
|
|
- fun changeModel(model: SuperPlayerModel) {
|
|
|
+ fun pauseTimer() {
|
|
|
+ countDownUtil?.pause()
|
|
|
+ }
|
|
|
+
|
|
|
+ /* 保存一条观看时间段的记录 */
|
|
|
+ fun saveHistory(start: Long, end: Long, sectionId: String) {
|
|
|
val history = History(
|
|
|
- start = 0,
|
|
|
- end = 10,
|
|
|
- courseId = "0",
|
|
|
- sectionId = model.title!!,
|
|
|
- date = SimpleDateFormat("yyyy-MM-dd").format(Date())
|
|
|
+ start = start,
|
|
|
+ end = end,
|
|
|
+ courseId = courseId,
|
|
|
+ sectionId = sectionId,
|
|
|
+ date = SimpleDateFormat("yyyy-MM-dd").format(Date()),
|
|
|
+ countDown = countDown.datetime,
|
|
|
)
|
|
|
|
|
|
- saveHistory(history)
|
|
|
+ viewModelScope.launch(Dispatchers.IO) {
|
|
|
+ repository.insertHistory(history)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ suspend fun checkCanPlay(whenCan: () -> Unit) = viewModelScope.launch(Dispatchers.IO) {
|
|
|
+ checkTimeout(countDown)
|
|
|
+
|
|
|
+ withContext(Dispatchers.Main) {
|
|
|
+ whenCan.invoke()
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
override fun onListen(type: Int, value: Int) {
|
|
|
Log.d("PVM", "onListen: $type , $value")
|
|
|
|
|
|
- countDown = countDown.copyWith(
|
|
|
+ countDown = CountDown(
|
|
|
courseId = courseId,
|
|
|
type = type,
|
|
|
- value = value
|
|
|
+ value = value,
|
|
|
+ datetime = SimpleDateFormat("yyyyMMddhhmmss").format(Date())
|
|
|
)
|
|
|
|
|
|
countDown.let {
|
|
@@ -112,22 +127,18 @@ class PlayerViewModel(
|
|
|
PlayerTimerUtil.removeListener(this)
|
|
|
}
|
|
|
|
|
|
- override fun onPause(count: Int, period: Long) {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- override fun onTimeout(count: Int, period: Long) {
|
|
|
- countDownUtil?.cancel()
|
|
|
+ override fun onTimeout(util: CountDownUtil?) {
|
|
|
+ util?.cancel()
|
|
|
sendTimeout(true)
|
|
|
}
|
|
|
|
|
|
- private fun checkTimeout() {
|
|
|
+ private fun checkTimeout(countDown: CountDown) {
|
|
|
countDown.also {
|
|
|
viewModelScope.launch {
|
|
|
if (it.type == CountDown.TYPE_DURATION) {
|
|
|
checkDurationTimeout()
|
|
|
} else if (it.type == CountDown.TYPE_EPISODE) {
|
|
|
- checkEpisodeTimeout();
|
|
|
+ checkEpisodeTimeout()
|
|
|
} else {
|
|
|
sendTimeout(false)
|
|
|
}
|
|
@@ -136,14 +147,35 @@ class PlayerViewModel(
|
|
|
}
|
|
|
|
|
|
private suspend fun checkEpisodeTimeout() = viewModelScope.launch(Dispatchers.IO) {
|
|
|
+ val history = repository.queryTodayEpisodeHistory(courseId, countDown.datetime!!)
|
|
|
|
|
|
+ if (history >= countDown.value) {
|
|
|
+ sendTimeout(true)
|
|
|
+ } else {
|
|
|
+ sendTimeout(false)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private suspend fun checkDurationTimeout() = viewModelScope.launch(Dispatchers.IO) {
|
|
|
- var history = repository.queryTodayDurationHistory(courseId)
|
|
|
+ val history = repository.queryTodayDurationHistory(courseId, countDown.datetime!!)
|
|
|
+
|
|
|
+ val result = history.fold(0L) { r, h ->
|
|
|
+ r + (h.end - h.start)
|
|
|
+ }
|
|
|
|
|
|
+ if (result >= countDown.value) {
|
|
|
+ sendTimeout(true)
|
|
|
+ } else {
|
|
|
+ sendTimeout(false)
|
|
|
+ setupTimer(countDown.value - result)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+ private fun setupTimer(count: Long) {
|
|
|
+ if (countDown.type == CountDown.TYPE_DURATION) {
|
|
|
+ countDownUtil = CountDownUtil.build(1000, count, this)
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
private fun sendTimeout(state: Boolean) {
|
|
|
viewModelScope.launch(Dispatchers.Main) {
|