Repository: android/compose-samples
File: Jetcaster/app/src/main/java/com/example/jetcaster/ui/MainActivity.kt (Kotlin)

Code before:
import com.example.jetcaster.util.DevicePosture
import com.example.jetcaster.util.isBookPosture
import com.example.jetcaster.util.isTableTopPosture
import com.google.accompanist.insets.ProvideWindowInsets
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // This app draws behind the system bars, so we want to handle fitting system windows
        WindowCompat.setDecorFitsSystemWindows(window, false)

        /**
         * Flow of [DevicePosture] that emits every time there's a change in the windowLayoutInfo
         */
        val devicePosture = windowInfoRepository().windowLayoutInfo
            .flowWithLifecycle(this.lifecycle)
            .map { layoutInfo ->
                val foldingFeature =
                    layoutInfo.displayFeatures.filterIsInstance<FoldingFeature>().firstOrNull()
                when {
                    isTableTopPosture(foldingFeature) ->
                        DevicePosture.TableTopPosture(foldingFeature?.bounds!!)
                    isBookPosture(foldingFeature) -> DevicePosture.BookPosture
                    else -> DevicePosture.NormalPosture
                }
            }
            .stateIn(
                scope = lifecycleScope,
                started = SharingStarted.Eagerly,
                initialValue = DevicePosture.NormalPosture
            )

        setContent {
            JetcasterTheme {
                ProvideWindowInsets {
                    JetcasterApp(devicePosture)
                }
            }
        }
    }
}


Review comment (suggestion):
This might be too advanced, but a contract would avoid the need for a potentially unsafe `!!` here:

```
@OptIn(ExperimentalContracts::class)
fun isTableTopPosture(foldFeature: FoldingFeature?): Boolean {
    contract { returns(true) implies (foldFeature != null) }
    return foldFeature?.state == FoldingFeature.State.HALF_OPENED &&
        foldFeature.orientation == FoldingFeature.Orientation.HORIZONTAL
}
```

Code after:
import com.example.jetcaster.util.DevicePosture
import com.example.jetcaster.util.isBookPosture
import com.example.jetcaster.util.isTableTopPosture
import com.google.accompanist.insets.ProvideWindowInsets
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // This app draws behind the system bars, so we want to handle fitting system windows
        WindowCompat.setDecorFitsSystemWindows(window, false)

        /**
         * Flow of [DevicePosture] that emits every time there's a change in the windowLayoutInfo
         */
        val devicePosture = windowInfoRepository().windowLayoutInfo
            .flowWithLifecycle(this.lifecycle)
            .map { layoutInfo ->
                val foldingFeature =
                    layoutInfo.displayFeatures.filterIsInstance<FoldingFeature>().firstOrNull()
                when {
                    isTableTopPosture(foldingFeature) ->
                        DevicePosture.TableTopPosture(foldingFeature.bounds)
                    isBookPosture(foldingFeature) -> DevicePosture.BookPosture
                    else -> DevicePosture.NormalPosture
                }
            }
            .stateIn(
                scope = lifecycleScope,
                started = SharingStarted.Eagerly,
                initialValue = DevicePosture.NormalPosture
            )

        setContent {
            JetcasterTheme {
                ProvideWindowInsets {
                    JetcasterApp(devicePosture)
                }
            }
        }
    }
}