Skip to content

Commit

Permalink
Add landscape orientation
Browse files Browse the repository at this point in the history
  • Loading branch information
joaomanaia committed Apr 13, 2022
1 parent bf63322 commit a859796
Show file tree
Hide file tree
Showing 22 changed files with 234 additions and 204 deletions.
4 changes: 3 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,5 @@ dependencies {
kapt(Hilt.androidXHiltCompiler)
implementation(Hilt.navigationCompose)
androidTestImplementation(Hilt.hiltAndroidTesting)
kaptAndroidTest(Hilt.hiltAndroidCompiler)}
kaptAndroidTest(Hilt.hiltAndroidCompiler)
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,55 @@ sealed class ButtonAction(
is ButtonClear -> colorTertiary
}

fun getAllPrimaryButtons(): List<ButtonAction> = listOf(
ButtonClear,
ButtonParentheses,
ButtonPercent,
ButtonDivide,
Button7,
Button8,
Button9,
ButtonMultiply,
Button4,
Button5,
Button6,
ButtonMinus,
Button1,
Button2,
Button3,
ButtonPlus,
Button0,
ButtonDot,
ButtonRemove,
ButtonEqual
)
fun getAllPrimaryButtons(
isPortrait: Boolean
): List<ButtonAction> = if (isPortrait) {
listOf(
ButtonClear,
ButtonParentheses,
ButtonPercent,
ButtonDivide,
Button7,
Button8,
Button9,
ButtonMultiply,
Button4,
Button5,
Button6,
ButtonMinus,
Button1,
Button2,
Button3,
ButtonPlus,
Button0,
ButtonDot,
ButtonRemove,
ButtonEqual
)
} else {
listOf(
Button7,
Button8,
Button9,
ButtonDivide,
ButtonClear,
Button4,
Button5,
Button6,
ButtonMultiply,
ButtonParentheses,
Button1,
Button2,
Button3,
ButtonMinus,
ButtonPercent,
Button0,
ButtonDot,
ButtonRemove,
ButtonPlus,
ButtonEqual
)
}

fun getAllSecondaryButtons(): List<ButtonAction> = listOf(
ButtonSquareRoot,
Expand Down Expand Up @@ -86,7 +113,7 @@ sealed class ButtonAction(
object ButtonPercent : ButtonAction(value = "%")

object ButtonClear : ButtonAction(value = "AC")
object ButtonRemove : ButtonAction(value = "")
object ButtonRemove : ButtonAction(value = "")
object ButtonParentheses : ButtonAction(value = "( )")

object ButtonSquareRoot : ButtonAction(value = "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import com.infinitepower.calculator.compose.ui.theme.spacing
@OptIn(ExperimentalMaterial3Api::class)
internal fun ButtonComponent(
modifier: Modifier = Modifier,
isPortrait: Boolean,
buttonGridExpanded: Boolean = true,
buttonAction: ButtonAction,
onClick: () -> Unit
Expand All @@ -38,6 +39,7 @@ internal fun ButtonComponent(

ButtonComponentImpl(
modifier = modifier,
isPortrait = isPortrait,
buttonGridExpanded = buttonGridExpanded,
actionText = buttonAction.value,
color = color,
Expand All @@ -49,6 +51,7 @@ internal fun ButtonComponent(
@ExperimentalMaterial3Api
private fun ButtonComponentImpl(
modifier: Modifier = Modifier,
isPortrait: Boolean,
buttonGridExpanded: Boolean,
actionText: String,
color: Color,
Expand All @@ -64,7 +67,7 @@ private fun ButtonComponentImpl(
animationSpec = tween(durationMillis = 250, easing = LinearEasing)
)

val textStyle = if (buttonGridExpanded) {
val textStyle = if (isPortrait && !buttonGridExpanded) {
MaterialTheme.typography.headlineMedium
} else MaterialTheme.typography.titleMedium

Expand All @@ -77,8 +80,7 @@ private fun ButtonComponentImpl(
interactionSource = interactionSource
) {
Box(
contentAlignment = Alignment.Center,
modifier = Modifier.padding(MaterialTheme.spacing.medium)
contentAlignment = Alignment.Center
) {
Text(
text = actionText,
Expand All @@ -101,6 +103,7 @@ private fun ButtonComponentPreview() {
.size(100.dp)
.padding(MaterialTheme.spacing.medium),
buttonAction = ButtonAction.Button1,
isPortrait = false,
onClick = {}
)
Spacer(modifier = Modifier.height(MaterialTheme.spacing.medium))
Expand All @@ -111,6 +114,7 @@ private fun ButtonComponentPreview() {
.padding(MaterialTheme.spacing.medium),
buttonAction = ButtonAction.Button1,
buttonGridExpanded = false,
isPortrait = false,
onClick = {}
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.infinitepower.calculator.compose.ui.components.button.primary

import android.content.res.Configuration
import android.content.res.Configuration.UI_MODE_NIGHT_YES
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.ExperimentalFoundationApi
Expand All @@ -15,7 +16,9 @@ import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.infinitepower.calculator.compose.ui.components.button.ButtonAction
import com.infinitepower.calculator.compose.ui.theme.CalculatorTheme
import com.infinitepower.calculator.compose.ui.theme.spacing
Expand All @@ -24,13 +27,15 @@ import com.infinitepower.calculator.compose.ui.theme.spacing
@OptIn(ExperimentalFoundationApi::class)
internal fun ButtonGrid(
modifier: Modifier = Modifier,
isPortrait: Boolean,
buttonGridExpanded: Boolean = true,
onActionClick: (action: ButtonAction) -> Unit
) {
val actions = ButtonAction.getAllPrimaryButtons()
val actions = ButtonAction.getAllPrimaryButtons(isPortrait = isPortrait)

ButtonGridImpl(
modifier = modifier,
isPortrait = isPortrait,
buttonGridExpanded = buttonGridExpanded,
actions = actions,
onActionClick = onActionClick
Expand All @@ -41,6 +46,7 @@ internal fun ButtonGrid(
@ExperimentalFoundationApi
private fun ButtonGridImpl(
modifier: Modifier = Modifier,
isPortrait: Boolean,
buttonGridExpanded: Boolean,
actions: List<ButtonAction>,
onActionClick: (action: ButtonAction) -> Unit
Expand All @@ -49,23 +55,32 @@ private fun ButtonGridImpl(
val spaceMedium = MaterialTheme.spacing.medium

val buttonAspectRatio by animateFloatAsState(
targetValue = if (buttonGridExpanded) 1f else 1f / 0.7f
targetValue = when {
isPortrait && !buttonGridExpanded -> 1f
isPortrait && buttonGridExpanded -> 1f / 0.7f
else -> 1f / 0.5f
}
)

LazyVerticalGrid(
modifier = modifier,
cells = GridCells.Fixed(count = 4),
verticalArrangement = Arrangement.spacedBy(spaceSmall),
horizontalArrangement = Arrangement.spacedBy(spaceSmall),
contentPadding = PaddingValues(
val gridPadding = if (isPortrait) {
PaddingValues(
start = spaceMedium,
end = spaceMedium,
bottom = spaceMedium
)
} else PaddingValues(0.dp)

LazyVerticalGrid(
modifier = modifier,
cells = GridCells.Fixed(count = if (isPortrait) 4 else 5),
verticalArrangement = Arrangement.spacedBy(spaceSmall),
horizontalArrangement = Arrangement.spacedBy(spaceSmall),
contentPadding = gridPadding
) {
items(items = actions) { action ->
ButtonComponent(
buttonAction = action,
isPortrait = isPortrait,
buttonGridExpanded = buttonGridExpanded,
modifier = Modifier
.fillParentMaxWidth()
Expand All @@ -77,13 +92,24 @@ private fun ButtonGridImpl(
}

@Composable
@Preview(showBackground = true)
@Preview(
showBackground = true,
group = "Portrait"
)
@Preview(
showBackground = true,
group = "Landscape",
device = "spec:shape=Normal,width=2340,height=1080,unit=px,dpi=440"
)
@Preview(showBackground = true, uiMode = UI_MODE_NIGHT_YES)
private fun ButtonGridPreview() {
val isPortrait = LocalConfiguration.current.orientation == Configuration.ORIENTATION_PORTRAIT

CalculatorTheme {
Surface {
ButtonGrid(
modifier = Modifier.fillMaxSize(),
isPortrait = isPortrait,
buttonGridExpanded = true,
onActionClick = {}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import com.infinitepower.calculator.compose.ui.theme.spacing
@OptIn(ExperimentalFoundationApi::class, ExperimentalMaterial3Api::class)
internal fun SecondaryButtonGrid(
modifier: Modifier = Modifier,
isPortrait: Boolean,
buttonGridExpanded: Boolean,
onActionClick: (action: ButtonAction) -> Unit,
onMoreActionsClick: (expanded: Boolean) -> Unit
Expand All @@ -34,6 +35,7 @@ internal fun SecondaryButtonGrid(

SecondaryButtonGridImpl(
modifier = modifier,
isPortrait = isPortrait,
actions = actions,
buttonGridExpanded = buttonGridExpanded,
onActionClick = onActionClick,
Expand All @@ -46,6 +48,7 @@ internal fun SecondaryButtonGrid(
@ExperimentalFoundationApi
private fun SecondaryButtonGridImpl(
modifier: Modifier = Modifier,
isPortrait: Boolean,
actions: List<ButtonAction>,
buttonGridExpanded: Boolean,
onActionClick: (action: ButtonAction) -> Unit,
Expand All @@ -58,15 +61,15 @@ private fun SecondaryButtonGridImpl(

Row(
verticalAlignment = Alignment.Top,
modifier = Modifier.padding(end = spaceMedium)
modifier = modifier.padding(end = spaceMedium)
) {
LazyVerticalGrid(
modifier = modifier.weight(1f),
cells = GridCells.Fixed(count = 4),
modifier = Modifier.weight(1f),
cells = GridCells.Fixed(count = if (isPortrait) 4 else 3),
verticalArrangement = Arrangement.spacedBy(spaceSmall),
horizontalArrangement = Arrangement.spacedBy(spaceSmall),
contentPadding = PaddingValues(
horizontal = spaceMedium,
horizontal = if (isPortrait) spaceMedium else 0.dp,
vertical = spaceSmall
)
) {
Expand All @@ -78,13 +81,15 @@ private fun SecondaryButtonGridImpl(
)
}
}
MoreSecondaryActionsItem(
modifier = Modifier.padding(top = spaceSmall),
buttonGridExpanded = buttonGridExpanded,
onClick = {
onMoreActionsClick(!buttonGridExpanded)
}
)
if (isPortrait) {
MoreSecondaryActionsItem(
modifier = Modifier.padding(top = spaceSmall),
buttonGridExpanded = buttonGridExpanded,
onClick = {
onMoreActionsClick(!buttonGridExpanded)
}
)
}
}
}

Expand Down Expand Up @@ -124,6 +129,7 @@ private fun ButtonGridPreview() {
Surface {
SecondaryButtonGrid(
modifier = Modifier.fillMaxWidth(),
isPortrait = true,
buttonGridExpanded = true,
onActionClick = {},
onMoreActionsClick = {}
Expand Down
Loading

0 comments on commit a859796

Please sign in to comment.