Skip to content

Commit

Permalink
Improve base frame to support flexibly landscape layouts
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkeppeler committed Feb 5, 2023
1 parent a0c7bed commit ba5b059
Show file tree
Hide file tree
Showing 16 changed files with 80 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ fun CalendarView(

FrameBase(
header = header,
config = config,
content = {
CalendarTopComponent(
config = config,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ fun ClockView(

FrameBase(
header = header,
config = config,
content = {
PortraitTimeValueComponent(
modifier = Modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ fun ColorView(

FrameBase(
header = header,
config = config,
content = {
ColorSelectionModeComponent(
config = config,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
package com.maxkeppeker.sheets.core.models.base

import com.maxkeppeker.sheets.core.icons.LibIcons
import com.maxkeppeker.sheets.core.utils.BaseConstants.DEFAULT_ICON_STYLE
import com.maxkeppeker.sheets.core.utils.BaseConstants

/**
* Base configs for dialog-specific configs.
* @param icons The style of icons that are used for dialog/ view-specific icons.
* @param orientation The orientation of the view or null for auto orientation.
*/
abstract class BaseConfigs(
open val icons: LibIcons = DEFAULT_ICON_STYLE
open val icons: LibIcons = BaseConstants.DEFAULT_ICON_STYLE,
open val orientation: LibOrientation? = BaseConstants.DEFAULT_LIB_LAYOUT,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.maxkeppeker.sheets.core.models.base

/**
* Represents the orientations of the use-case views.
*
* @property PORTRAIT orientation with height greater than width
* @property LANDSCAPE orientation with width greater than height
*/
enum class LibOrientation {
PORTRAIT,
LANDSCAPE,
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
*/
package com.maxkeppeker.sheets.core.utils

import androidx.compose.ui.unit.dp
import com.maxkeppeker.sheets.core.icons.LibIcons
import com.maxkeppeker.sheets.core.models.base.LibOrientation

/**
* Defines module-wide constants.
Expand All @@ -27,4 +29,10 @@ object BaseConstants {
const val SUCCESS_DISMISS_DELAY = 600L

val DEFAULT_ICON_STYLE = LibIcons.Rounded
val DEFAULT_LIB_LAYOUT: LibOrientation? = null // Auto orientation

val KEYBOARD_HEIGHT_MAX = 300.dp
const val KEYBOARD_RATIO = 0.8f

val DYNAMIC_SIZE_MAX = 300.dp
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,18 @@
*/
package com.maxkeppeker.sheets.core.utils

import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalConfiguration


/**
* Joins multiple test tags together into a sequence.
* @param testTags The test tags you apply to the element.
*/
fun testSequenceTagOf(vararg testTags: Any): String = testTags.joinToString()
fun testSequenceTagOf(vararg testTags: Any): String = testTags.joinToString()

private const val TABLET_THRESHOLD = 800

@Composable
fun shouldUseLandscape(): Boolean =
LocalConfiguration.current.screenHeightDp < TABLET_THRESHOLD
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ fun ButtonsComponent(

Row(
modifier = Modifier
.fillMaxWidth()
.padding(vertical = dimensionResource(id = R.dimen.scd_small_100))
.padding(horizontal = dimensionResource(id = R.dimen.scd_normal_100)),
verticalAlignment = Alignment.CenterVertically,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.dimensionResource
import com.maxkeppeker.sheets.core.models.base.BaseConfigs
import com.maxkeppeker.sheets.core.models.base.Header
import com.maxkeppeker.sheets.core.models.base.LibOrientation
import com.maxkeppeker.sheets.core.utils.BaseValues
import com.maxkeppeker.sheets.core.utils.TestTags
import com.maxkeppeker.sheets.core.utils.shouldUseLandscape
import com.maxkeppeker.sheets.core.views.HeaderComponent
import com.maxkeppeler.sheets.core.R as RC

Expand All @@ -45,18 +48,37 @@ import com.maxkeppeler.sheets.core.R as RC
@Composable
fun FrameBase(
header: Header? = null,
config: BaseConfigs? = null,
contentHorizontalAlignment: Alignment.Horizontal = Alignment.Start,
contentLandscapeVerticalAlignment: Alignment.Vertical = Alignment.CenterVertically,
horizontalContentPadding: PaddingValues = BaseValues.CONTENT_DEFAULT_PADDING,
content: @Composable ColumnScope.() -> Unit,
contentLandscape: @Composable (RowScope.() -> Unit)? = null,
buttonsVisible: Boolean = true,
buttons: @Composable (ColumnScope.() -> Unit)? = null,
) {
val layoutDirection = LocalLayoutDirection.current
val configuration = LocalConfiguration.current
val shouldUseLandscape = shouldUseLandscape()
val orientation = when (config?.orientation) {
null -> {
val currentOrientation = LocalConfiguration.current.orientation
val isAutoLandscape = currentOrientation == Configuration.ORIENTATION_LANDSCAPE
when {
// Only if auto orientation is currently landscape, content for landscape exists
// and the device screen is not larger than a typical phone.
isAutoLandscape
&& contentLandscape != null
&& shouldUseLandscape -> LibOrientation.LANDSCAPE
else -> LibOrientation.PORTRAIT
}
}
LibOrientation.LANDSCAPE -> if (contentLandscape != null) LibOrientation.LANDSCAPE else LibOrientation.PORTRAIT
else -> config.orientation
}

Column(
modifier = Modifier.wrapContentHeight()
modifier = Modifier.wrapContentSize(),
horizontalAlignment = Alignment.End
) {

header?.let {
Expand Down Expand Up @@ -91,21 +113,22 @@ fun FrameBase(
top = dimensionResource(RC.dimen.scd_normal_100),
)
)
when {
configuration.orientation == Configuration.ORIENTATION_LANDSCAPE && contentLandscape != null -> {
Row(
modifier = contentModifier,
verticalAlignment = Alignment.CenterVertically,
content = contentLandscape
)
}
else -> {
when (orientation) {
LibOrientation.PORTRAIT -> {
Column(
modifier = contentModifier.fillMaxWidth(),
modifier = contentModifier,
horizontalAlignment = contentHorizontalAlignment,
content = content
)
}
LibOrientation.LANDSCAPE -> {
Row(
modifier = contentModifier,
verticalAlignment = contentLandscapeVerticalAlignment,
content = contentLandscape!!
)
}
else -> Unit
}

buttons?.let { buttons ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ fun DateTimeView(

FrameBase(
header = header,
config = config,
content = {
val datePicker = @Composable {
PickerComponent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ fun DurationView(

FrameBase(
header = header,
config = config,
content = {
TimeDisplayComponent(
orientation = Orientation.Vertical,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ fun EmojiView(

FrameBase(
header = header,
config = config,
content = {
EmojiHeaderComponent(
config = config,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ fun InputView(

FrameBase(
header = header,
config = config,
content = {
InputComponent(
modifier = Modifier.dynamicContentWrapOrMaxHeight(this),
Expand Down
1 change: 1 addition & 0 deletions list/src/main/java/com/maxkeppeler/sheets/list/ListView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ fun ListView(

FrameBase(
header = header,
config = config,
content = {
ListOptionBoundsComponent(
selection = selection,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ fun OptionView(

FrameBase(
header = header,
config = config,
// Override content padding, spacing is within the scrollable container for display mode GRID_HORIZONTAL
horizontalContentPadding = PaddingValues(horizontal = 0.dp),
content = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ fun StateView(
) {
FrameBase(
header = header,
config = config,
contentHorizontalAlignment = Alignment.CenterHorizontally,
horizontalContentPadding = PaddingValues(
horizontal = dimensionResource(id = RC.dimen.scd_normal_100),
Expand Down

0 comments on commit ba5b059

Please sign in to comment.