Skip to content

Instantly share code, notes, and snippets.

@parthdesai1208
Created November 9, 2022 14:27
Show Gist options
  • Save parthdesai1208/87517861b9a4ae6e314a470745499d66 to your computer and use it in GitHub Desktop.
Save parthdesai1208/87517861b9a4ae6e314a470745499d66 to your computer and use it in GitHub Desktop.
Compose for every screen
//WindowStateUtils.kt
/**
* Information about the posture of the device
*/
sealed interface DevicePosture {
object NormalPosture : DevicePosture
data class BookPosture(
val hingePosition: Rect
) : DevicePosture
data class Separating(
val hingePosition: Rect,
var orientation: FoldingFeature.Orientation
) : DevicePosture
}
******************************************************************************************************************
//MainActivity.kt
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
/**
* Flow of [DevicePosture] that emits every time there's a change in the windowLayoutInfo
*/
val devicePostureFlow = WindowInfoTracker.getOrCreate(this).windowLayoutInfo(this)
.flowWithLifecycle(this.lifecycle)
.map { layoutInfo ->
val foldingFeature =
layoutInfo.displayFeatures
.filterIsInstance<FoldingFeature>()
.firstOrNull()
when {
isBookPosture(foldingFeature) ->
DevicePosture.BookPosture(foldingFeature.bounds)
isSeparating(foldingFeature) ->
DevicePosture.Separating(foldingFeature.bounds, foldingFeature.orientation)
else -> DevicePosture.NormalPosture
}
}
.stateIn(
scope = lifecycleScope,
started = SharingStarted.Eagerly,
initialValue = DevicePosture.NormalPosture
)
setContent {
val devicePosture = devicePostureFlow.collectAsState().value
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment