generated from hossain-khan/android-compose-app-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #172 from hossain-khan/126-dynamic-dimension
[ADDED] Support for dynamic padding using `WindowSizeClass`.
- Loading branch information
Showing
9 changed files
with
127 additions
and
16 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
86 changes: 86 additions & 0 deletions
86
app/src/main/java/dev/hossain/weatheralert/ui/theme/Dimension.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package dev.hossain.weatheralert.ui.theme | ||
|
||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.staticCompositionLocalOf | ||
import androidx.compose.ui.unit.Dp | ||
import androidx.compose.ui.unit.dp | ||
import androidx.window.core.layout.WindowWidthSizeClass | ||
|
||
// For background, see: | ||
// - https://github.com/hossain-khan/android-weather-alert/issues/126 | ||
// - https://bsky.app/profile/hossain.dev/post/3lflhafgn622p | ||
|
||
/** | ||
* Data class to hold dimension values for padding and screen spacing. | ||
*/ | ||
data class Dimensions( | ||
val smallPadding: Dp, | ||
val mediumPadding: Dp, | ||
val largePadding: Dp, | ||
val horizontalScreenPadding: Dp, | ||
val verticalScreenPadding: Dp, | ||
) | ||
|
||
/** | ||
* CompositionLocal to provide the current Dimensions instance. | ||
*/ | ||
val LocalDimensions = | ||
staticCompositionLocalOf { | ||
Dimensions( | ||
smallPadding = 16.dp, | ||
mediumPadding = 24.dp, | ||
largePadding = 32.dp, | ||
horizontalScreenPadding = 16.dp, | ||
verticalScreenPadding = 16.dp, | ||
) | ||
} | ||
|
||
/** | ||
* Extension property to access the current Dimensions instance from MaterialTheme. | ||
*/ | ||
val MaterialTheme.dimensions: Dimensions | ||
@Composable | ||
get() = LocalDimensions.current | ||
|
||
/** | ||
* Extension function to get Dimensions based on the WindowWidthSizeClass. | ||
* | ||
* @receiver WindowWidthSizeClass The current window size class. | ||
* @return Dimensions The corresponding Dimensions instance. | ||
*/ | ||
internal fun WindowWidthSizeClass.dimensions(): Dimensions = | ||
when (this) { | ||
WindowWidthSizeClass.COMPACT -> | ||
Dimensions( | ||
smallPadding = 8.dp, | ||
mediumPadding = 16.dp, | ||
largePadding = 24.dp, | ||
horizontalScreenPadding = 16.dp, | ||
verticalScreenPadding = 16.dp, | ||
) | ||
WindowWidthSizeClass.MEDIUM -> | ||
Dimensions( | ||
smallPadding = 16.dp, | ||
mediumPadding = 24.dp, | ||
largePadding = 32.dp, | ||
horizontalScreenPadding = 48.dp, | ||
verticalScreenPadding = 24.dp, | ||
) | ||
WindowWidthSizeClass.EXPANDED -> | ||
Dimensions( | ||
smallPadding = 24.dp, | ||
mediumPadding = 32.dp, | ||
largePadding = 40.dp, | ||
horizontalScreenPadding = 64.dp, | ||
verticalScreenPadding = 24.dp, | ||
) | ||
else -> | ||
Dimensions( | ||
smallPadding = 16.dp, | ||
mediumPadding = 24.dp, | ||
largePadding = 32.dp, | ||
horizontalScreenPadding = 16.dp, | ||
verticalScreenPadding = 16.dp, | ||
) | ||
} |
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