Hi all. I'm trying to calculate window size classe...
# compose-ios
x
Hi all. I'm trying to calculate window size classes based on screen size. Currently, im doing this
Copy code
fun Main(): UIViewController = ComposeUIViewController {
  BoxWithConstraints {
    val windowSizeClass: WindowSizeClass = WindowSizeClass.calculateFromSize(DpSize(maxWidth, maxHeight))
    MaterialTheme {
     // My app
    }
  }
}
This feels like a hack. Is there a better way to get these sizes? A quick glance at
ComposeWindow
(
ComposeUIViewController
) implementation shows me that
ComposeLayer
getting updated on
viewWillTransitionToSize
, but this seems to be private.
👍 1
s
How you are able to access WindowSizeClass in platform specific module like iosMain? i am not able to do that
x
I ported this myself fo. It was pretty straightforward with expect/actual
Copy code
// commonMain
 @Suppress("ConvertSecondaryConstructorToPrimary") // To mirror android api
 expect class WindowSizeClass {
   val widthSizeClass: WindowWidthSizeClass
   val heightSizeClass: WindowHeightSizeClass
 
   private constructor(
     widthSizeClass: WindowWidthSizeClass,
     heightSizeClass: WindowHeightSizeClass
   )
 }
 
 // androidMain
 import androidx.compose.material3.windowsizeclass.WindowSizeClass
 
 actual typealias WindowSizeClass = WindowSizeClass
 
 // nonAndroidMain
 actual typealias WindowSizeClass = CommonWindowSizeClass // basically copy-pasta of android implementation
Working sample here
s
Cool, thanx, i am checking this repo.