Skip to content

Instantly share code, notes, and snippets.

@tahaak67
Created February 23, 2023 10:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tahaak67/1ea09e49676c18cf1072cc1a0aed08a2 to your computer and use it in GitHub Desktop.
Save tahaak67/1ea09e49676c18cf1072cc1a0aed08a2 to your computer and use it in GitHub Desktop.
A utility fun for compose to determin if current device is a phone (compact) or tablet (medium / expanded)
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
@Composable
fun rememberWindowInfo(): WindowInfo {
val configuration = LocalConfiguration.current
return WindowInfo(
screenWidthInfo = when {
configuration.screenWidthDp < 600 -> WindowInfo.WindowType.Compact
configuration.screenWidthDp < 840 -> WindowInfo.WindowType.Medium
else -> WindowInfo.WindowType.Expanded
},
screenHeightInfo = when {
configuration.screenHeightDp < 480 -> WindowInfo.WindowType.Compact
configuration.screenHeightDp < 900 -> WindowInfo.WindowType.Medium
else -> WindowInfo.WindowType.Expanded
},
screenWidth = configuration.screenWidthDp.dp,
screenHeight = configuration.screenHeightDp.dp
)
}
data class WindowInfo(
val screenWidthInfo: WindowType,
val screenHeightInfo: WindowType,
val screenWidth: Dp,
val screenHeight: Dp
) {
sealed class WindowType {
object Compact: WindowType()
object Medium: WindowType()
object Expanded: WindowType()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment