Skip to content

Commit

Permalink
Create component OrientationHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
ldeso committed Mar 26, 2024
1 parent 1b527bf commit 8bdcc8a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 25 deletions.
13 changes: 6 additions & 7 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 All @@ -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.
Expand Down Expand Up @@ -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) {
Expand All @@ -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 },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,40 +27,29 @@ 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 {
private val orientationEventListener by lazy {
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)
}
}
}
Expand Down

0 comments on commit 8bdcc8a

Please sign in to comment.