Improved calculator design

- Added background for input fields,
- Added background for history
- Added drag handle
- Fixed padding
This commit is contained in:
Sad Ellie 2023-02-21 13:01:43 +04:00
parent 39f4997c73
commit 5be0d7d5e3
5 changed files with 55 additions and 10 deletions

View File

@ -23,6 +23,8 @@ import androidx.compose.foundation.layout.RowScope
import androidx.compose.material3.Scaffold import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarColors
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
@ -38,6 +40,7 @@ fun UnittoTopAppBar(
title: String, title: String,
navigateUpAction: () -> Unit, navigateUpAction: () -> Unit,
actions: @Composable RowScope.() -> Unit = {}, actions: @Composable RowScope.() -> Unit = {},
colors: TopAppBarColors = TopAppBarDefaults.topAppBarColors(),
content: @Composable (PaddingValues) -> Unit content: @Composable (PaddingValues) -> Unit
) { ) {
Scaffold( Scaffold(
@ -50,7 +53,8 @@ fun UnittoTopAppBar(
navigationIcon = { navigationIcon = {
NavigateUpButton { navigateUpAction() } NavigateUpButton { navigateUpAction() }
}, },
actions = actions actions = actions,
colors = colors
) )
}, },
content = content content = content

View File

@ -19,13 +19,18 @@
package com.sadellie.unitto.feature.calculator package com.sadellie.unitto.feature.calculator
import androidx.compose.animation.core.animateFloatAsState import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.Orientation import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.draggable import androidx.compose.foundation.gestures.draggable
import androidx.compose.foundation.gestures.rememberDraggableState import androidx.compose.foundation.gestures.rememberDraggableState
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.sizeIn
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Delete import androidx.compose.material.icons.filled.Delete
import androidx.compose.material3.AlertDialog import androidx.compose.material3.AlertDialog
@ -34,12 +39,14 @@ import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.material3.TextButton import androidx.compose.material3.TextButton
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.onPlaced import androidx.compose.ui.layout.onPlaced
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
@ -47,6 +54,7 @@ import androidx.compose.ui.text.TextRange
import androidx.compose.ui.text.input.TextFieldValue import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.sadellie.unitto.core.ui.common.UnittoTopAppBar import com.sadellie.unitto.core.ui.common.UnittoTopAppBar
@ -103,20 +111,28 @@ private fun CalculatorScreen(
UnittoTopAppBar( UnittoTopAppBar(
title = stringResource(R.string.calculator), title = stringResource(R.string.calculator),
navigateUpAction = navigateUpAction, navigateUpAction = navigateUpAction,
colors = TopAppBarDefaults.topAppBarColors(MaterialTheme.colorScheme.surfaceVariant),
actions = { actions = {
IconButton( IconButton(
onClick = { showClearHistoryDialog = true }, onClick = { showClearHistoryDialog = true },
content = { Icon(Icons.Default.Delete, stringResource(R.string.calculator_clear_history_title)) } content = {
Icon(
Icons.Default.Delete,
stringResource(R.string.calculator_clear_history_title)
)
}
) )
} }
) { paddingValues -> ) { paddingValues ->
DragDownView( DragDownView(
modifier = Modifier.padding(paddingValues), modifier = Modifier.padding(paddingValues),
drag = dragAmountAnimated.toInt(), drag = dragAmountAnimated.roundToInt(),
historyItemHeight = historyItemHeight, historyItemHeight = historyItemHeight,
historyList = { historyList = {
HistoryList( HistoryList(
modifier = Modifier.fillMaxSize(), modifier = Modifier
.background(MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.5f))
.fillMaxSize(),
historyItems = uiState.history, historyItems = uiState.history,
historyItemHeightCallback = { historyItemHeight = it } historyItemHeightCallback = { historyItemHeight = it }
) )
@ -125,6 +141,13 @@ private fun CalculatorScreen(
Column( Column(
Modifier Modifier
.onPlaced { textThingyHeight = it.size.height } .onPlaced { textThingyHeight = it.size.height }
.background(
MaterialTheme.colorScheme.surfaceVariant,
RoundedCornerShape(
topStart = 0.dp, topEnd = 0.dp,
bottomStart = 32.dp, bottomEnd = 32.dp
)
)
.draggable( .draggable(
orientation = Orientation.Vertical, orientation = Orientation.Vertical,
state = rememberDraggableState { delta -> state = rememberDraggableState { delta ->
@ -136,7 +159,9 @@ private fun CalculatorScreen(
.minBy { abs(draggedAmount.roundToInt() - it) } .minBy { abs(draggedAmount.roundToInt() - it) }
.toFloat() .toFloat()
} }
) ),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(8.dp)
) { ) {
InputTextField( InputTextField(
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),
@ -155,11 +180,21 @@ private fun CalculatorScreen(
color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.6f), color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.6f),
style = NumbersTextStyleDisplayMedium, style = NumbersTextStyleDisplayMedium,
) )
// Handle
Box(
Modifier
.padding(16.dp)
.background(
MaterialTheme.colorScheme.onSurfaceVariant,
RoundedCornerShape(100)
)
.sizeIn(36.dp, 4.dp)
)
} }
}, },
numPad = { numPad = {
CalculatorKeyboard( CalculatorKeyboard(
modifier = Modifier, modifier = Modifier.padding(horizontal = 8.dp, vertical = 4.dp),
addSymbol = addSymbol, addSymbol = addSymbol,
clearSymbols = clearSymbols, clearSymbols = clearSymbols,
deleteSymbol = deleteSymbol, deleteSymbol = deleteSymbol,
@ -186,7 +221,9 @@ private fun CalculatorScreen(
TextButton(onClick = clearHistory) { Text(stringResource(R.string.calculator_clear_history_label)) } TextButton(onClick = clearHistory) { Text(stringResource(R.string.calculator_clear_history_label)) }
}, },
dismissButton = { dismissButton = {
TextButton(onClick = { showClearHistoryDialog = false }) { Text(stringResource(R.string.cancel_label)) } TextButton(onClick = {
showClearHistoryDialog = false
}) { Text(stringResource(R.string.cancel_label)) }
}, },
onDismissRequest = { showClearHistoryDialog = false } onDismissRequest = { showClearHistoryDialog = false }
) )

View File

@ -127,7 +127,10 @@ internal fun CalculatorKeyboard(
.weight(1f) .weight(1f)
.padding(4.dp) .padding(4.dp)
Row(horizontalArrangement = Arrangement.spacedBy(2.dp)) { Row(
modifier = Modifier.padding(vertical = 8.dp),
horizontalArrangement = Arrangement.spacedBy(2.dp)
) {
// Additional buttons // Additional buttons
Column(modifier = weightModifier) { Column(modifier = weightModifier) {
Row(Modifier, horizontalArrangement = Arrangement.spacedBy(2.dp)) { Row(Modifier, horizontalArrangement = Arrangement.spacedBy(2.dp)) {

View File

@ -57,6 +57,7 @@ internal fun HistoryList(
.fillMaxWidth() .fillMaxWidth()
.horizontalScroll(rememberScrollState(), reverseScrolling = true), .horizontalScroll(rememberScrollState(), reverseScrolling = true),
style = NumbersTextStyleDisplayMedium, style = NumbersTextStyleDisplayMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant,
textAlign = TextAlign.End textAlign = TextAlign.End
) )
Text( Text(
@ -66,7 +67,7 @@ internal fun HistoryList(
.fillMaxWidth() .fillMaxWidth()
.horizontalScroll(rememberScrollState(), reverseScrolling = true), .horizontalScroll(rememberScrollState(), reverseScrolling = true),
style = NumbersTextStyleDisplayMedium, style = NumbersTextStyleDisplayMedium,
color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.5f), color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.5f),
textAlign = TextAlign.End textAlign = TextAlign.End
) )
} }

View File

@ -55,7 +55,7 @@ internal fun InputTextField(
}, },
textStyle = NumbersTextStyleDisplayLarge.copy( textStyle = NumbersTextStyleDisplayLarge.copy(
textAlign = TextAlign.End, textAlign = TextAlign.End,
color = MaterialTheme.colorScheme.onBackground color = MaterialTheme.colorScheme.onSurfaceVariant
), ),
minLines = 1, minLines = 1,
maxLines = 1, maxLines = 1,