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

Code before:
import androidx.window.layout.FoldingFeature
import androidx.window.layout.WindowInfoRepository.Companion.windowInfoRepository
import com.example.jetcaster.ui.theme.JetcasterTheme
import com.example.jetcaster.util.WindowInfo
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 [WindowInfo] that emits every time there's a change in the windowLayoutInfo
         */
        val windowInfo = windowInfoRepository().windowLayoutInfo
            .flowWithLifecycle(this.lifecycle)
            .map { layoutInfo ->
                val foldingFeature: FoldingFeature? =
                    layoutInfo.displayFeatures.find { it is FoldingFeature } as? FoldingFeature
                WindowInfo(
                    isInTableTopPosture = isTableTopPosture(foldingFeature),
                    isInBookPosture = isBookPosture(foldingFeature),
                    hingePosition = foldingFeature?.bounds
                )
            }
            .stateIn(
                scope = lifecycleScope,
                started = SharingStarted.Eagerly,
                initialValue = WindowInfo()
            )

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


Review comment (suggestion):
Totally optional, but this avoids having `FoldingFeature` twice:

```suggestion
                    layoutInfo.displayFeatures.filterIsInstance<FoldingFeature>().firstOrNull()
```

Code after:
import androidx.window.layout.FoldingFeature
import androidx.window.layout.WindowInfoRepository.Companion.windowInfoRepository
import com.example.jetcaster.ui.theme.JetcasterTheme
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)
                }
            }
        }
    }
}