From f129080e36eb34c3b05ec9570f599a70f178b826 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wolf=20Montwe=CC=81?= Date: Mon, 20 Mar 2023 17:51:43 +0100 Subject: [PATCH] Add WindowSizeInfo to :core:ui:compose:common to determine the WindowSizeClasses and current height and width --- .../compose/common/window/WindowSizeClass.kt | 37 +++++++++ .../compose/common/window/WindowSizeInfo.kt | 30 +++++++ .../common/window/WindowSizeClassTest.kt | 80 +++++++++++++++++++ 3 files changed, 147 insertions(+) create mode 100644 core/ui/compose/common/src/main/kotlin/app/k9mail/core/ui/compose/common/window/WindowSizeClass.kt create mode 100644 core/ui/compose/common/src/main/kotlin/app/k9mail/core/ui/compose/common/window/WindowSizeInfo.kt create mode 100644 core/ui/compose/common/src/test/kotlin/app/k9mail/core/ui/compose/common/window/WindowSizeClassTest.kt diff --git a/core/ui/compose/common/src/main/kotlin/app/k9mail/core/ui/compose/common/window/WindowSizeClass.kt b/core/ui/compose/common/src/main/kotlin/app/k9mail/core/ui/compose/common/window/WindowSizeClass.kt new file mode 100644 index 000000000..0f6c10c2c --- /dev/null +++ b/core/ui/compose/common/src/main/kotlin/app/k9mail/core/ui/compose/common/window/WindowSizeClass.kt @@ -0,0 +1,37 @@ +package app.k9mail.core.ui.compose.common.window + +/** + * WindowSizeClass as defined by supporting different screen sizes. + * + * See: https://developer.android.com/guide/topics/large-screens/support-different-screen-sizes#window_size_classes + */ +enum class WindowSizeClass { + Compact, + Medium, + Expanded, + ; + + companion object { + private const val COMPACT_MAX_WIDTH = 600 + private const val COMPACT_MAX_HEIGHT = 480 + + private const val MEDIUM_MAX_WIDTH = 840 + private const val MEDIUM_MAX_HEIGHT = 900 + + fun fromWidth(width: Int): WindowSizeClass { + return when { + width < COMPACT_MAX_WIDTH -> Compact + width < MEDIUM_MAX_WIDTH -> Medium + else -> Expanded + } + } + + fun fromHeight(height: Int): WindowSizeClass { + return when { + height < COMPACT_MAX_HEIGHT -> Compact + height < MEDIUM_MAX_HEIGHT -> Medium + else -> Expanded + } + } + } +} diff --git a/core/ui/compose/common/src/main/kotlin/app/k9mail/core/ui/compose/common/window/WindowSizeInfo.kt b/core/ui/compose/common/src/main/kotlin/app/k9mail/core/ui/compose/common/window/WindowSizeInfo.kt new file mode 100644 index 000000000..6fd7d8c5d --- /dev/null +++ b/core/ui/compose/common/src/main/kotlin/app/k9mail/core/ui/compose/common/window/WindowSizeInfo.kt @@ -0,0 +1,30 @@ +package app.k9mail.core.ui.compose.common.window + +import androidx.compose.runtime.Composable +import androidx.compose.runtime.Immutable +import androidx.compose.ui.platform.LocalConfiguration +import androidx.compose.ui.unit.Dp +import androidx.compose.ui.unit.dp + +/** + * Returns the current window size info based on current Configuration. + */ +@Composable +fun getWindowSizeInfo(): WindowSizeInfo { + val configuration = LocalConfiguration.current + + return WindowSizeInfo( + screenWidthSizeClass = WindowSizeClass.fromWidth(configuration.screenWidthDp), + screenHeightSizeClass = WindowSizeClass.fromHeight(configuration.screenHeightDp), + screenWidth = configuration.screenWidthDp.dp, + screenHeight = configuration.screenHeightDp.dp, + ) +} + +@Immutable +data class WindowSizeInfo( + val screenWidthSizeClass: WindowSizeClass, + val screenHeightSizeClass: WindowSizeClass, + val screenWidth: Dp, + val screenHeight: Dp, +) diff --git a/core/ui/compose/common/src/test/kotlin/app/k9mail/core/ui/compose/common/window/WindowSizeClassTest.kt b/core/ui/compose/common/src/test/kotlin/app/k9mail/core/ui/compose/common/window/WindowSizeClassTest.kt new file mode 100644 index 000000000..ea67a76b8 --- /dev/null +++ b/core/ui/compose/common/src/test/kotlin/app/k9mail/core/ui/compose/common/window/WindowSizeClassTest.kt @@ -0,0 +1,80 @@ +package app.k9mail.core.ui.compose.common.window + +import assertk.assertThat +import assertk.assertions.isEqualTo +import org.junit.Test + +class WindowSizeClassTest { + + @Test + fun `should return compact when width is less than 600`() { + val width = 599 + + val windowSizeClass = WindowSizeClass.fromWidth(width) + + assertThat(windowSizeClass).isEqualTo(WindowSizeClass.Compact) + } + + @Test + fun `should return medium when width is 600`() { + val width = 600 + + val windowSizeClass = WindowSizeClass.fromWidth(width) + + assertThat(windowSizeClass).isEqualTo(WindowSizeClass.Medium) + } + + @Test + fun `should return medium when width is less than 840`() { + val width = 839 + + val windowSizeClass = WindowSizeClass.fromWidth(width) + + assertThat(windowSizeClass).isEqualTo(WindowSizeClass.Medium) + } + + @Test + fun `should return expanded when width is 840`() { + val width = 840 + + val windowSizeClass = WindowSizeClass.fromWidth(width) + + assertThat(windowSizeClass).isEqualTo(WindowSizeClass.Expanded) + } + + @Test + fun `should return compact when height is less than 480`() { + val height = 479 + + val windowSizeClass = WindowSizeClass.fromHeight(height) + + assertThat(windowSizeClass).isEqualTo(WindowSizeClass.Compact) + } + + @Test + fun `should return medium when height is 480`() { + val height = 480 + + val windowSizeClass = WindowSizeClass.fromHeight(height) + + assertThat(windowSizeClass).isEqualTo(WindowSizeClass.Medium) + } + + @Test + fun `should return medium when height is less than 900`() { + val height = 899 + + val windowSizeClass = WindowSizeClass.fromHeight(height) + + assertThat(windowSizeClass).isEqualTo(WindowSizeClass.Medium) + } + + @Test + fun `should return expanded when height is 900`() { + val height = 900 + + val windowSizeClass = WindowSizeClass.fromHeight(height) + + assertThat(windowSizeClass).isEqualTo(WindowSizeClass.Expanded) + } +}