Joseph Hawkes-Cates
02/08/2022, 5:58 PMLocalConfiguraiton.current
. If I remove the “orientation” entry from the “android:config” changes field in my manifest, then it works every time, but with that in there, it will not update the first couple times, then it’ll work after that. Code in 🧵@Composable
fun Activity.rememberWindowSizeClass(): WindowSize {
val configuration = LocalConfiguration.current
val windowMetrics = remember(configuration) {
Timber.i("Recalculating window metrics")
WindowMetricsCalculator.getOrCreate().computeCurrentWindowMetrics(this)
}
val windowDpSize = with(LocalDensity.current) {
windowMetrics.bounds.toComposeRect().size.toDpSize()
}
Timber.i("Screen size: w=${windowDpSize.width},h=${windowDpSize.height}")
return when {
windowDpSize.width < 600.dp -> WindowSize.SMALL
windowDpSize.width < 840.dp -> WindowSize.MEDIUM
else -> WindowSize.LARGE
}
}
Rick Regan
02/08/2022, 8:04 PM@Composable
fun Activity.SlackOrientation() {
val configuration = LocalConfiguration.current
val windowMetrics = remember(configuration) {
println("Recalculating window metrics")
WindowMetricsCalculator.getOrCreate().computeCurrentWindowMetrics(this)
}
val windowDpSize = with(LocalDensity.current) {
windowMetrics.bounds.toComposeRect().size.toDpSize()
}
println("Screen size: w=${windowDpSize.width},h=${windowDpSize.height}")
when {
windowDpSize.width < 600.dp -> println("SMALL")
windowDpSize.width < 840.dp -> println("MEDIUM")
else -> println("LARGE")
}
}
Here are the messages:
Startup
-------
Recalculating window metrics
Screen size: w=392.72726.dp,h=850.9091.dp
SMALL
Screen size: w=392.72726.dp,h=850.9091.dp
SMALL
Rotate
------
Recalculating window metrics
Screen size: w=850.9091.dp,h=392.72726.dp
LARGE
Rotate back
-----------
Recalculating window metrics
Screen size: w=392.72726.dp,h=850.9091.dp
SMALL
That's with android:configChanges="colorMode|density|fontScale|keyboard|keyboardHidden|layoutDirection|locale|mcc|mnc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|touchscreen|uiMode">
With that line deleted, it also works, but rotate and rotate back each repeat the last two lines, like for startup.Joseph Hawkes-Cates
02/08/2022, 8:56 PMRick Regan
02/08/2022, 8:59 PMJoseph Hawkes-Cates
02/08/2022, 9:55 PMRick Regan
02/09/2022, 1:02 PMStartup
-------
Recalculating window metrics
Screen size: w=411.42856.dp,h=891.4286.dp
SMALL
Screen size: w=411.42856.dp,h=891.4286.dp
SMALL
Rotate
------
Screen size: w=411.42856.dp,h=891.4286.dp
SMALL
Rotate back
-----------
Recalculating window metrics
Screen size: w=411.42856.dp,h=891.4286.dp
SMALL
Rotate
------
Recalculating window metrics
Screen size: w=891.4286.dp,h=411.42856.dp
LARGE
Rotate back
-----------
Recalculating window metrics
Screen size: w=411.42856.dp,h=891.4286.dp
SMALL
(without the line in the manifest it works.)
That would seem like a bug.configuration.orientation
it will change appropriately on the first rotate (i.e., it will be '2' (landscape) even though you detect it as SMALL).BoxWithConstraints
works, "swapping" maxWidth
and maxHeight
on the first rotation.