From ae6c7c7f4319f57a9f78d2119b3d9e920617ee06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaquim=20St=C3=A4hli?= Date: Tue, 23 Jan 2024 16:29:06 +0100 Subject: [PATCH] Remove depreciated call of lifecycleScope.launchWhenCreated --- .../demo/ui/player/SimplePlayerActivity.kt | 19 ++++++++++--------- .../integrations/MediaControllerActivity.kt | 18 +++++++++--------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/player/SimplePlayerActivity.kt b/pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/player/SimplePlayerActivity.kt index 2088c23f0..c4c7e8b30 100644 --- a/pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/player/SimplePlayerActivity.kt +++ b/pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/player/SimplePlayerActivity.kt @@ -25,8 +25,8 @@ import androidx.compose.runtime.getValue import androidx.compose.ui.Modifier import androidx.lifecycle.Lifecycle import androidx.lifecycle.ViewModelProvider +import androidx.lifecycle.flowWithLifecycle import androidx.lifecycle.lifecycleScope -import androidx.lifecycle.repeatOnLifecycle import androidx.media3.common.Player import ch.srgssr.pillarbox.analytics.SRGAnalytics import ch.srgssr.pillarbox.core.business.integrationlayer.service.IlHost @@ -72,14 +72,9 @@ class SimplePlayerActivity : ComponentActivity(), ServiceConnection { val ilHost = (intent.extras?.getSerializable(ARG_IL_HOST) as URL?) ?: IlHost.DEFAULT playerViewModel = ViewModelProvider(this, factory = SimplePlayerViewModel.Factory(application, ilHost))[SimplePlayerViewModel::class.java] readIntent(intent) - lifecycleScope.launch { - repeatOnLifecycle(Lifecycle.State.RESUMED) { - SRGAnalytics.trackPagView(DemoPageView("simple player", levels = listOf("app", "pillarbox"))) - } - } - lifecycleScope.launchWhenCreated { - playerViewModel.pictureInPictureRatio.collectLatest { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && playerViewModel.pictureInPictureEnabled.value) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + lifecycleScope.launch { + playerViewModel.pictureInPictureRatio.flowWithLifecycle(lifecycle, Lifecycle.State.CREATED).collectLatest { val params = PictureInPictureParams.Builder() .setAspectRatio(playerViewModel.pictureInPictureRatio.value) .build() @@ -87,6 +82,7 @@ class SimplePlayerActivity : ComponentActivity(), ServiceConnection { } } } + // Bind PlaybackService to allow background playback and MediaNotification. bindPlaybackService() setContent { @@ -166,6 +162,11 @@ class SimplePlayerActivity : ComponentActivity(), ServiceConnection { } } + override fun onResume() { + super.onResume() + SRGAnalytics.trackPagView(DemoPageView("simple player", levels = listOf("app", "pillarbox"))) + } + override fun onStart() { super.onStart() playerViewModel.player.play() diff --git a/pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/showcases/integrations/MediaControllerActivity.kt b/pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/showcases/integrations/MediaControllerActivity.kt index 6a6af2410..88811c242 100644 --- a/pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/showcases/integrations/MediaControllerActivity.kt +++ b/pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/showcases/integrations/MediaControllerActivity.kt @@ -20,8 +20,8 @@ import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.ui.Modifier import androidx.lifecycle.Lifecycle +import androidx.lifecycle.flowWithLifecycle import androidx.lifecycle.lifecycleScope -import androidx.lifecycle.repeatOnLifecycle import androidx.media3.common.Player import ch.srgssr.pillarbox.analytics.SRGAnalytics import ch.srgssr.pillarbox.demo.DemoPageView @@ -43,14 +43,9 @@ class MediaControllerActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - lifecycleScope.launch { - repeatOnLifecycle(Lifecycle.State.RESUMED) { - SRGAnalytics.trackPagView(DemoPageView("media controller player", levels = listOf("app", "pillarbox"))) - } - } - lifecycleScope.launchWhenCreated { - controllerViewModel.pictureInPictureRatio.collectLatest { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && controllerViewModel.pictureInPictureEnabled.value) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + lifecycleScope.launch { + controllerViewModel.pictureInPictureRatio.flowWithLifecycle(lifecycle, Lifecycle.State.CREATED).collectLatest { val params = PictureInPictureParams.Builder() .setAspectRatio(controllerViewModel.pictureInPictureRatio.value) .build() @@ -111,4 +106,9 @@ class MediaControllerActivity : ComponentActivity() { controllerViewModel.pictureInPictureEnabled.value = isInPictureInPictureMode } } + + override fun onResume() { + super.onResume() + SRGAnalytics.trackPagView(DemoPageView("media controller player", levels = listOf("app", "pillarbox"))) + } }