Skip to content

Commit

Permalink
Use screensaverLock in PlaybackRewriteFragment
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsvanvelzen committed May 10, 2024
1 parent 2d0369a commit 54f575d
Showing 1 changed file with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import org.jellyfin.androidtv.ui.ScreensaverViewModel
import org.jellyfin.androidtv.ui.playback.VideoQueueManager
import org.jellyfin.playback.core.PlaybackManager
import org.jellyfin.playback.core.model.PlayState
import org.jellyfin.playback.core.ui.PlayerSurfaceView
import org.jellyfin.sdk.api.client.ApiClient
import org.koin.android.ext.android.inject
import org.koin.androidx.viewmodel.ext.android.activityViewModel
import timber.log.Timber

/**
Expand All @@ -27,6 +34,9 @@ class PlaybackRewriteFragment : Fragment() {
private val playbackManager by inject<PlaybackManager>()
private val api by inject<ApiClient>()

private val screensaverViewModel by activityViewModel<ScreensaverViewModel>()
private var screensaverLock: (() -> Unit)? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

Expand All @@ -47,6 +57,20 @@ class PlaybackRewriteFragment : Fragment() {

// Pause player until the initial resume
playbackManager.state.pause()

// Add lifecycle listeners
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.RESUMED) {
playbackManager.state.playState.onEach { playState ->
if (playState == PlayState.PLAYING && screensaverLock == null) {
screensaverLock = screensaverViewModel.addLifecycleLock(lifecycle)
} else if (playState != PlayState.PLAYING && screensaverLock != null) {
screensaverLock?.invoke()
screensaverLock = null
}
}.launchIn(this)
}
}
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
Expand Down

0 comments on commit 54f575d

Please sign in to comment.