Open sourced a single `@Composable` function for C...
# feed
a
Open sourced a single
@Composable
function for CMP I needed a common way to access the window container size but it is not yet available on Android, so here is
currentWindowContainerSize
until it
LocalWindowInfo.current.containerSize
reaches common:
Copy code
@Composable
fun App() {
    val containerSize = currentWindowContainerSize()

    if (containerSize.width >= 480.dp) {
        TabletLayout()
    } else {
        PhoneLayout()
    }
}
https://github.com/alexstyl/cmp-windowContainerSize
m
I guess you mean TabletLayout and not TableLayout, right? 🧐😉
a
good catch. fixed
s
Why not using window size classes on Android? I'm fairly sure the logic in the snippet above is "wrong"
The breakpoint between phone (aka compact) and foldable inner screen/small tablet (aka medium) is 600 dp for the width
a
Why not using window size classes on Android
You mean why use the function instead of window size classes? because I needed a way to be able to calculate my own window size classes / breakpoints, that is not tied to a specific framework
I'm fairly sure the logic in the snippet above is "wrong"
you are right. it's a random value i put for this example
s
I meant why not using WSC in the Android impl, and/or an API in that style for other targets too
a
Because in order to have the WSC style api, I wanted to have a common way to get the container size (hence the function)
s
I see, got it 👍
👌 1
a
@seb WindowSizeClass is not customizable is it (as in i cant set my own breakpoints)? from what i see it is not, but maybe i am missing something
s
I think there is a way, but I don't remember for sure. That's a Roberto question
Copy code
// new
val sizeClass = WindowSizeClass.BREAKPOINTS_V1
                               .computeWindowSizeClass(widthDp, heightDp)

when {
  sizeClass.isWidthAtLeastBreakpoint(WIDTH_DP_EXPANDED_LOWER_BOUND) -> {
    doExpanded()
  }
  sizeClass.isWidthAtLeastBreakpoint(WIDTH_DP_MEDIUM_LOWER_BOUND) -> {
    doMedium()
  }
  else -> {
    doCompact()
  }
}
I dont get what i am reading. looking into it
ok it seems configurable. the API seems awkward but seems configurable. nice
a
LocalWindowInfo.current.containerSize
just became part of 1.8.0 on the Android side: https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/WindowInfo#containerSize()
👀 1
very nice 1
y
https://kotlinlang.slack.com/archives/C0346LWVBJ4/p1696616182202429?thread_ts=1696442432.065739&channel=C0346LWVBJ4&message_ts=1696616182.202429 Is this still valid for android? There’s a new lint check in 1.8 that suggests migrating from LocalConfiguration.current.screen*Dp to this new API but the result is different.
a
LocalWindowInfo.current.containerSize
’s implementation should match
WindowMetricsCalculator.computeCurrentWindowMetrics
- are you seeing a case where it doesn’t?
y
@Alex Vanyo No I’m getting a lint error on
val screenWidth = LocalConfiguration.current.screenWidth.dp
after upgrading to 1.8:
Copy code
Using Configuration.screenWidthDp instead of LocalWindowInfo.current.containerSize
is this a false positive?
a
That’s expected! That lines up with my comment you linked, to avoid
LocalConfiguration.current.screenWidth.dp
. Now there’s a better built-in way with
LocalWindowInfo.current.containerSize
y
ok so I’m seeing big differences in previews and paparazzi tests with this change, I’ll try to make a minimum repro
@Alex Vanyo sorry the issue was on my end,
LocalWindowInfo.current.containerSize
works as expected 👍
a
Ah good to hear! Curious if that issue is something someone else would run into - I wasn't going to be entirely surprised if previews and Paparazzi tests interact somewhat strangely on some versions
y
So the issue was due to converting from
screenWidthDp.dp
directly to
containerSize.width.dp
, forgetting that the unit has changed so it should be
with(LocalDensity.current) { LocalWindowInfo.current.containerSize.width.toDp() }
instead 😅
guess this could be another lint check
a
Ohhhh… that’d be a great lint check to add