Created
February 23, 2023 10:32
-
-
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)
This file contains 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
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