Afzal Najam
05/18/2022, 5:41 PMcalculateWindowSizeClass equivalent for non-activities? We’re using a fair bit of AbstractComposeViews, leveraging interop capabilities and so activity instance isn’t available without casting the Context 👀
Is this not enough? Or does rememberWidthSizeClass provide something else that this doesn’t?
@Composable
fun getWidthSizeClass(): WidthSizeClass {
val configuration = LocalConfiguration.current
val windowDpSize = configuration.screenWidthDp.dp
return when {
windowDpSize < 600.dp -> WidthSizeClass.COMPACT
windowDpSize < 840.dp -> WidthSizeClass.MEDIUM
else -> WidthSizeClass.EXPANDED
}
}Louis Pullen-Freilich [G]
05/18/2022, 8:59 PMIs there aA window exists at the root of the activity (and is tied to the activity), so you should calculate the size class at the activity level and provide the information down - it’s somewhat of an anti-pattern to try and figure out what the window size is from an arbitrary view, since the size of the window doesn’t mean that thisequivalent for non-activities?calculateWindowSizeClass
AbstractComposeView has the same size, or any other guarantees.
If you are using Views at the top level of your app, you can instead follow the View-based guidance for window size classes here: https://developer.android.com/guide/topics/large-screens/support-different-screen-sizes#kotlin
Define your own comparable size class, and use this at the top level, and then pass down some information to your Compose views.
Is this not enough? Or does rememberWidthSizeClass provide something else that this doesn’t?
screenWidthDp ignores system decorations (such as status / nav bars), so it isn’t really as accurate / consistent when we are specifically talking about window size classes, when it comes to things like going edge to edgeAfzal Najam
05/27/2022, 5:19 PMAbstractComposeView that’s a few levels away from the Activity (like inside a View within a Fragment ), would you say it’s okay to maybe hold the state somewhere globally?
Would it be okay to hold a Compose MutableState in a Kotlin object or equivalent and expose that to `Composable`s via LocalComposition?
Or would you recommend using a ViewModel instead?
Or is there a better way to put a MutableState in the global snapshot?Louis Pullen-Freilich [G]
05/27/2022, 5:22 PMAfzal Najam
05/27/2022, 5:27 PMWindowSizeClass (in the guide you linked) globally.
The use case is to access the WindowSizeClass in AbstractComposeView as well as pure Composable screens without passing them from the Activity directly (or a few Fragments down).Louis Pullen-Freilich [G]
05/27/2022, 5:28 PMLouis Pullen-Freilich [G]
05/27/2022, 5:29 PMAfzal Najam
05/27/2022, 5:31 PMthere is no guarantee that the sub section (view in this case) has the same size as the windowRight but in this case, I’m trying to match horizontal paddings of existing Android Views that specify it for
w1024dp-land and w1280dp . I could just use dimenResource but at the same time, I wanted to see if there was a Compose-only solution.Louis Pullen-Freilich [G]
05/27/2022, 6:59 PM