diff --git a/app/src/main/java/net/leodesouza/blitz/ui/ChessClockScreen.kt b/app/src/main/java/net/leodesouza/blitz/ui/ChessClockScreen.kt index e60f69b..88fac52 100644 --- a/app/src/main/java/net/leodesouza/blitz/ui/ChessClockScreen.kt +++ b/app/src/main/java/net/leodesouza/blitz/ui/ChessClockScreen.kt @@ -23,10 +23,10 @@ import androidx.compose.foundation.interaction.MutableInteractionSource import androidx.compose.foundation.layout.Box import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.derivedStateOf import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableFloatStateOf import androidx.compose.runtime.mutableIntStateOf -import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier @@ -37,7 +37,7 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle import androidx.lifecycle.viewmodel.compose.viewModel import kotlinx.coroutines.delay import kotlinx.coroutines.flow.Flow -import net.leodesouza.blitz.ui.components.IsLeaningRightHandler +import net.leodesouza.blitz.ui.components.OrientationHandler /** * Minimalist Fischer chess clock. @@ -68,7 +68,6 @@ fun ChessClockScreen( val uiState by chessClockViewModel.uiState.collectAsStateWithLifecycle() val isLandscape = LocalConfiguration.current.orientation == Configuration.ORIENTATION_LANDSCAPE val isRtl = LocalLayoutDirection.current == LayoutDirection.Rtl - var isLeaningRight by remember { mutableStateOf(true) } var backEventProgress by remember { mutableFloatStateOf(0F) } var backEventSwipeEdge by remember { if (isRtl) { @@ -77,11 +76,11 @@ fun ChessClockScreen( mutableIntStateOf(BackEventCompat.EDGE_LEFT) } } + var orientation by remember { mutableIntStateOf(0) } - IsLeaningRightHandler( - isLeaningRightProvider = { isLeaningRight }, - onLeaningSideChanged = { isLeaningRight = !isLeaningRight }, - ) + OrientationHandler(onOrientationChanged = { orientation = it }) + + val isLeaningRight by remember { derivedStateOf { orientation < 180 } } ChessClockTickingEffect( currentTimeProvider = { uiState.currentTime }, diff --git a/app/src/main/java/net/leodesouza/blitz/ui/components/IsLeaningRightHandler.kt b/app/src/main/java/net/leodesouza/blitz/ui/components/OrientationHandler.kt similarity index 69% rename from app/src/main/java/net/leodesouza/blitz/ui/components/IsLeaningRightHandler.kt rename to app/src/main/java/net/leodesouza/blitz/ui/components/OrientationHandler.kt index 5d1d240..dd80f23 100644 --- a/app/src/main/java/net/leodesouza/blitz/ui/components/IsLeaningRightHandler.kt +++ b/app/src/main/java/net/leodesouza/blitz/ui/components/OrientationHandler.kt @@ -27,17 +27,15 @@ import androidx.lifecycle.DefaultLifecycleObserver import androidx.lifecycle.LifecycleOwner /** - * Effect to handle whether the device is currently leaning towards its right side. - * - * @param[isLeaningRightProvider] Lambda for whether the device is currently leaning right. - * @param[onLeaningSideChanged] Callback called when the leaning side of the device changes. + * Observe the orientation of the device in a lifecycle-aware manner and call [onOrientationChanged] + * when the orientation of the device changes, with the new orientation in degrees as an argument. + * Take into account whether the screen is in its "natural" orientation. */ @Composable -fun IsLeaningRightHandler( - isLeaningRightProvider: () -> Boolean, onLeaningSideChanged: () -> Unit, -) { +fun OrientationHandler(onOrientationChanged: (Int) -> Unit) { val lifecycleOwner = LocalLifecycleOwner.current val context = LocalContext.current + val display = ContextCompat.getDisplayOrDefault(context) DisposableEffect(lifecycleOwner) { val lifecycleObserver = object : DefaultLifecycleObserver { @@ -45,22 +43,13 @@ fun IsLeaningRightHandler( object : OrientationEventListener(context) { override fun onOrientationChanged(orientation: Int) { if (orientation == ORIENTATION_UNKNOWN) return - - val rotation = when (ContextCompat.getDisplayOrDefault(context).rotation) { + val rotation = when (display.rotation) { Surface.ROTATION_0 -> 0 Surface.ROTATION_90 -> 90 Surface.ROTATION_180 -> 180 else -> 270 } - - val correctedOrientation = (orientation + rotation) % 360 - val isLeaningRight = isLeaningRightProvider() - - if (!isLeaningRight && correctedOrientation in 10 until 170) { - onLeaningSideChanged() - } else if (isLeaningRight && correctedOrientation in 190 until 350) { - onLeaningSideChanged() - } + onOrientationChanged((orientation + rotation) % 360) } } }