Skip to content

Commit

Permalink
Move calculation of rotation out of DisposableEffect
Browse files Browse the repository at this point in the history
  • Loading branch information
ldeso committed Mar 27, 2024
1 parent 24fc550 commit c0bba1b
Showing 1 changed file with 7 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,20 @@ fun OrientationHandler(onOrientationChanged: (Int) -> Unit) {
val context = LocalContext.current
val display = ContextCompat.getDisplayOrDefault(context)

val rotation = when (display.rotation) {
Surface.ROTATION_0 -> 0
Surface.ROTATION_90 -> 90
Surface.ROTATION_180 -> 180
else -> 270
}

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 (display.rotation) {
Surface.ROTATION_0 -> 0
Surface.ROTATION_90 -> 90
Surface.ROTATION_180 -> 180
else -> 270
}

onOrientationChanged((orientation + rotation) % 360)
}
}
Expand Down

0 comments on commit c0bba1b

Please sign in to comment.