Created
November 9, 2022 14:27
-
-
Save parthdesai1208/87517861b9a4ae6e314a470745499d66 to your computer and use it in GitHub Desktop.
Compose for every screen
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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