Skip to content

Commit

Permalink
Restore threshold of 10 degrees before changing display orientation
Browse files Browse the repository at this point in the history
  • Loading branch information
ldeso committed Mar 26, 2024
1 parent 5029cc7 commit e4a1451
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions app/src/main/java/net/leodesouza/blitz/ui/ChessClockScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -68,6 +68,8 @@ fun ChessClockScreen(
val uiState by chessClockViewModel.uiState.collectAsStateWithLifecycle()
val isLandscape = LocalConfiguration.current.orientation == Configuration.ORIENTATION_LANDSCAPE
val isRtl = LocalLayoutDirection.current == LayoutDirection.Rtl
var orientation by remember { mutableIntStateOf(0) }
var isLeaningRight by remember { mutableStateOf(true) }
var backEventProgress by remember { mutableFloatStateOf(0F) }
var backEventSwipeEdge by remember {
if (isRtl) {
Expand All @@ -76,11 +78,14 @@ fun ChessClockScreen(
mutableIntStateOf(BackEventCompat.EDGE_LEFT)
}
}
var orientation by remember { mutableIntStateOf(0) }

OrientationHandler(onOrientationChanged = { orientation = it })

val isLeaningRight by remember { derivedStateOf { orientation < 180 } }
IsLeaningRightHandler(
orientationProvider = { orientation },
isLeaningRightProvider = { isLeaningRight },
onLeaningSideChanged = { isLeaningRight = !isLeaningRight },
)

ChessClockTickingEffect(
currentTimeProvider = { uiState.currentTime },
Expand Down Expand Up @@ -145,7 +150,30 @@ fun ChessClockScreen(
}

/**
* Effect taking care of repeatedly waiting until next tick or pausing the clock when it has
* Handle whether the device is currently leaning towards its right side.
*
* @param[orientationProvider] Lambda for the orientation of the device in degrees.
* @param[isLeaningRightProvider] Lambda for whether the device is currently leaning right.
* @param[onLeaningSideChanged] Callback called when the leaning side of the device changes.
*/
@Composable
private fun IsLeaningRightHandler(
orientationProvider: () -> Int,
isLeaningRightProvider: () -> Boolean,
onLeaningSideChanged: () -> Unit,
) {
val orientation = orientationProvider()
val isLeaningRight = isLeaningRightProvider()

val isChangingFromRightToLeft = isLeaningRight && orientation in 10 until 170
val isChangingFromLeftToRight = !isLeaningRight && orientation in 190 until 350
if (isChangingFromRightToLeft || isChangingFromLeftToRight) {
onLeaningSideChanged()
}
}

/**
* Effect taking care of repeatedly waiting until the next tick or pausing the clock when it has
* finished ticking.
*
* @param[currentTimeProvider] Lambda for the time of the current player.
Expand Down

0 comments on commit e4a1451

Please sign in to comment.