From 0edbc63e14605966f907f08d864417d4635ea78d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaquim=20St=C3=A4hli?= Date: Fri, 15 Mar 2024 08:54:22 +0100 Subject: [PATCH] Use movable content to swap players (#472) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Gaƫtan Muller --- .../ui/showcases/misc/MultiPlayerShowcase.kt | 34 ++++++------------- 1 file changed, 11 insertions(+), 23 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..a4be645b2 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,46 +67,34 @@ 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 players = remember { + movableContentOf { PlayerView( modifier = Modifier .weight(1.0f) .padding(MaterialTheme.paddings.mini), - player = leftPlayer, + player = if (swapLeftRight) playerTwo else playerOne, ) PlayerView( modifier = Modifier .weight(1.0f) .padding(MaterialTheme.paddings.mini), - player = rightPlayer + player = if (swapLeftRight) playerOne else playerTwo, ) } + } + 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() } } } } - -private const val AspectRatio = 16 / 9f