From 416b5241aa52fab606355e7e9960ed6118e13d67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaquim=20St=C3=A4hli?= Date: Thu, 14 Mar 2024 15:04:54 +0100 Subject: [PATCH] Use movable content to swap players --- .../ui/showcases/misc/MultiPlayerShowcase.kt | 47 ++++++++++--------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/showcases/misc/MultiPlayerShowcase.kt b/pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/showcases/misc/MultiPlayerShowcase.kt index 3ff7a431b..0ade6e737 100644 --- a/pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/showcases/misc/MultiPlayerShowcase.kt +++ b/pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/showcases/misc/MultiPlayerShowcase.kt @@ -7,7 +7,6 @@ package ch.srgssr.pillarbox.demo.ui.showcases.misc import android.content.res.Configuration import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row -import androidx.compose.foundation.layout.aspectRatio import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding import androidx.compose.material3.Button @@ -16,6 +15,7 @@ import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.DisposableEffect import androidx.compose.runtime.getValue +import androidx.compose.runtime.movableContentOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue @@ -67,43 +67,48 @@ fun MultiPlayerShowcase() { playerTwo.pause() } } - val leftPlayer = if (swapLeftRight) playerTwo else playerOne - val rightPlayer = if (swapLeftRight) playerOne else playerTwo Column(horizontalAlignment = Alignment.CenterHorizontally) { Button(onClick = { swapLeftRight = !swapLeftRight }) { Text(text = "Swap players") } - if (LocalConfiguration.current.orientation == Configuration.ORIENTATION_LANDSCAPE) { - Row(modifier = Modifier.fillMaxWidth()) { + val playerOneView = remember { + movableContentOf { PlayerView( modifier = Modifier .weight(1.0f) .padding(MaterialTheme.paddings.mini), - player = leftPlayer, + player = playerOne, ) + } + } + val playerTwoView = remember { + movableContentOf { PlayerView( modifier = Modifier .weight(1.0f) .padding(MaterialTheme.paddings.mini), - player = rightPlayer + player = playerTwo, ) } + } + val players = remember { + movableContentOf { + if (swapLeftRight) { + playerTwoView() + playerOneView() + } else { + playerOneView() + playerTwoView() + } + } + } + if (LocalConfiguration.current.orientation == Configuration.ORIENTATION_LANDSCAPE) { + Row(modifier = Modifier.fillMaxWidth()) { + players() + } } else { Column(modifier = Modifier.fillMaxWidth()) { - PlayerView( - modifier = Modifier - .weight(1.0f) - .aspectRatio(AspectRatio) - .padding(MaterialTheme.paddings.mini), - player = leftPlayer - ) - PlayerView( - modifier = Modifier - .weight(1.0f) - .aspectRatio(AspectRatio) - .padding(MaterialTheme.paddings.mini), - player = rightPlayer - ) + players() } } }