Merge pull request #3852 from Bnyro/master

Update watch positions every second to avoid outdated positions
This commit is contained in:
Bnyro 2023-05-30 12:17:11 +02:00 committed by GitHub
commit dfc0ae449e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -71,6 +71,7 @@ import com.github.libretube.enums.PlayerEvent
import com.github.libretube.enums.ShareObjectType
import com.github.libretube.extensions.formatShort
import com.github.libretube.extensions.hideKeyboard
import com.github.libretube.extensions.setMetadata
import com.github.libretube.extensions.toID
import com.github.libretube.extensions.toastFromMainDispatcher
import com.github.libretube.extensions.updateParameters
@ -110,6 +111,9 @@ import com.github.libretube.util.PlayingQueue
import com.github.libretube.util.TextUtils
import com.github.libretube.util.TextUtils.toTimeInSeconds
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import java.io.IOException
import java.util.*
import java.util.concurrent.Executors
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@ -117,12 +121,9 @@ import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import kotlinx.serialization.encodeToString
import retrofit2.HttpException
import java.io.IOException
import java.util.*
import java.util.concurrent.Executors
import com.github.libretube.extensions.setMetadata
import kotlin.math.abs
@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
class PlayerFragment : Fragment(), OnlinePlayerOptions {
private var _binding: FragmentPlayerBinding? = null
@ -248,6 +249,14 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
broadcastReceiver,
IntentFilter(PlayerHelper.getIntentActon(requireContext())),
)
// schedule task to save the watch position each second
Timer().scheduleAtFixedRate(object : TimerTask() {
override fun run() {
handler.post(this@PlayerFragment::saveWatchPosition)
}
}, 1000, 1000)
}
override fun onCreateView(
@ -616,7 +625,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
}
try {
if (exoPlayer.duration != C.TIME_UNSET) saveWatchPosition()
saveWatchPosition()
PlayingQueue.clear()
@ -637,7 +646,12 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
// save the watch position if video isn't finished and option enabled
private fun saveWatchPosition() {
if (!PlayerHelper.watchPositionsVideo || exoPlayer.currentPosition == 0L) return
if (!this::exoPlayer.isInitialized || !PlayerHelper.watchPositionsVideo
|| exoPlayer.duration == C.TIME_UNSET || exoPlayer.currentPosition in listOf(
0L,
C.TIME_UNSET
)
) return
val watchPosition = WatchPosition(videoId!!, exoPlayer.currentPosition)
CoroutineScope(Dispatchers.IO).launch {
Database.watchPositionDao().insert(watchPosition)
@ -949,11 +963,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
}
override fun onPlaybackStateChanged(playbackState: Int) {
// save the watch position to the database
// only called when the position is unequal to 0, otherwise it would become reset
// before the player can seek to the saved position from videos of the queue
// not called when the video has ended, since it then might save it to the next autoplay video
if (playbackState != Player.STATE_ENDED) saveWatchPosition()
saveWatchPosition()
// check if video has ended, next video is available and autoplay is enabled.
if (
@ -1567,7 +1577,9 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
}
private fun shouldStartPiP(): Boolean {
return usePiP() && exoPlayer.isPlaying && !BackgroundHelper.isBackgroundServiceRunning(requireContext())
return usePiP() && exoPlayer.isPlaying && !BackgroundHelper.isBackgroundServiceRunning(
requireContext()
)
}
private fun killPlayerFragment() {