CenterAlignedTopAppBar in screen template

This commit is contained in:
Sad Ellie 2023-02-21 19:40:41 +04:00
parent ef0db15f11
commit 185f8452a8
5 changed files with 76 additions and 27 deletions

View File

@ -1102,6 +1102,7 @@
<string name="enable_unit_group_description">Enable unit group</string> <string name="enable_unit_group_description">Enable unit group</string>
<string name="reorder_unit_group_description">Reorder unit group</string> <string name="reorder_unit_group_description">Reorder unit group</string>
<string name="disable_unit_group_description">Disable unit group</string> <string name="disable_unit_group_description">Disable unit group</string>
<string name="open_menu_description">Open menu</string>
<string name="app_version_name_setting">Version name</string> <string name="app_version_name_setting">Version name</string>
<string name="about_unitto">About Unitto</string> <string name="about_unitto">About Unitto</string>

View File

@ -0,0 +1,42 @@
/*
* Unitto is a unit converter for Android
* Copyright (c) 2022 Elshan Agaev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.sadellie.unitto.core.ui.common
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.MenuOpen
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.res.stringResource
import com.sadellie.unitto.core.ui.R
/**
* Button that is used in Top bars
*
* @param onClick Action to be called when button is clicked.
*/
@Composable
fun MenuButton(onClick: () -> Unit) {
IconButton(onClick = onClick) {
Icon(
Icons.Outlined.MenuOpen,
contentDescription = stringResource(R.string.open_menu_description)
)
}
}

View File

@ -20,39 +20,38 @@ package com.sadellie.unitto.core.ui.common
import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.RowScope import androidx.compose.foundation.layout.RowScope
import androidx.compose.material3.CenterAlignedTopAppBar
import androidx.compose.material3.Scaffold import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarColors import androidx.compose.material3.TopAppBarColors
import androidx.compose.material3.TopAppBarDefaults 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
/** /**
* Commonly used TopAppBar with scroll behavior. * Template screen. Uses [Scaffold] and [CenterAlignedTopAppBar]
* *
* @param title Text that is displayed in top bar. * @param modifier See [Scaffold]
* @param navigateUpAction Action when user click arrow button at the top. * @param title See [CenterAlignedTopAppBar]
* @param content Content that can be scrolled. Don't forget to use padding values. * @param navigationIcon See [CenterAlignedTopAppBar]
* @param actions See [CenterAlignedTopAppBar]
* @param colors See [CenterAlignedTopAppBar]
* @param content See [Scaffold]
*/ */
@Composable @Composable
fun UnittoTopAppBar( fun UnittoScreenWithTopBar(
title: String, modifier: Modifier = Modifier,
navigateUpAction: () -> Unit, title: @Composable () -> Unit,
navigationIcon: @Composable () -> Unit,
actions: @Composable RowScope.() -> Unit = {}, actions: @Composable RowScope.() -> Unit = {},
colors: TopAppBarColors = TopAppBarDefaults.topAppBarColors(), colors: TopAppBarColors = TopAppBarDefaults.topAppBarColors(),
content: @Composable (PaddingValues) -> Unit content: @Composable (PaddingValues) -> Unit
) { ) {
Scaffold( Scaffold(
modifier = Modifier, modifier = modifier,
topBar = { topBar = {
TopAppBar( CenterAlignedTopAppBar(
title = { title = title,
Text(text = title) navigationIcon = navigationIcon,
},
navigationIcon = {
NavigateUpButton { navigateUpAction() }
},
actions = actions, actions = actions,
colors = colors colors = colors
) )

View File

@ -60,7 +60,8 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp 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.MenuButton
import com.sadellie.unitto.core.ui.common.UnittoScreenWithTopBar
import com.sadellie.unitto.core.ui.theme.NumbersTextStyleDisplayMedium import com.sadellie.unitto.core.ui.theme.NumbersTextStyleDisplayMedium
import com.sadellie.unitto.data.model.HistoryItem import com.sadellie.unitto.data.model.HistoryItem
import com.sadellie.unitto.feature.calculator.components.CalculatorKeyboard import com.sadellie.unitto.feature.calculator.components.CalculatorKeyboard
@ -112,9 +113,9 @@ private fun CalculatorScreen(
var textThingyHeight by remember { mutableStateOf(0) } var textThingyHeight by remember { mutableStateOf(0) }
var historyItemHeight by remember { mutableStateOf(0) } var historyItemHeight by remember { mutableStateOf(0) }
UnittoTopAppBar( UnittoScreenWithTopBar(
title = stringResource(R.string.calculator), title = { Text(stringResource(R.string.calculator)) },
navigateUpAction = navigateUpAction, navigationIcon = { MenuButton { navigateUpAction() } },
colors = TopAppBarDefaults.topAppBarColors(MaterialTheme.colorScheme.surfaceVariant), colors = TopAppBarDefaults.topAppBarColors(MaterialTheme.colorScheme.surfaceVariant),
actions = { actions = {
AnimatedVisibility( AnimatedVisibility(
@ -179,7 +180,9 @@ private fun CalculatorScreen(
verticalArrangement = Arrangement.spacedBy(8.dp) verticalArrangement = Arrangement.spacedBy(8.dp)
) { ) {
InputTextField( InputTextField(
modifier = Modifier.fillMaxWidth().padding(horizontal = 8.dp), modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 8.dp),
value = TextFieldValue( value = TextFieldValue(
text = uiState.input, text = uiState.input,
selection = TextRange(uiState.selection.first, uiState.selection.last) selection = TextRange(uiState.selection.first, uiState.selection.last)
@ -188,7 +191,9 @@ private fun CalculatorScreen(
pasteCallback = addSymbol pasteCallback = addSymbol
) )
Text( Text(
modifier = Modifier.fillMaxWidth().padding(horizontal = 8.dp), modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 8.dp),
text = uiState.output, text = uiState.output,
textAlign = TextAlign.End, textAlign = TextAlign.End,
softWrap = false, softWrap = false,

View File

@ -19,6 +19,7 @@
package com.sadellie.unitto.feature.epoch package com.sadellie.unitto.feature.epoch
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
@ -26,8 +27,9 @@ import androidx.compose.ui.text.TextRange
import androidx.compose.ui.unit.dp 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.MenuButton
import com.sadellie.unitto.core.ui.common.PortraitLandscape import com.sadellie.unitto.core.ui.common.PortraitLandscape
import com.sadellie.unitto.core.ui.common.UnittoTopAppBar import com.sadellie.unitto.core.ui.common.UnittoScreenWithTopBar
import com.sadellie.unitto.feature.epoch.component.EpochKeyboard import com.sadellie.unitto.feature.epoch.component.EpochKeyboard
import com.sadellie.unitto.feature.epoch.component.TopPart import com.sadellie.unitto.feature.epoch.component.TopPart
@ -58,9 +60,9 @@ private fun EpochScreen(
swap: () -> Unit, swap: () -> Unit,
onCursorChange: (IntRange) -> Unit onCursorChange: (IntRange) -> Unit
) { ) {
UnittoTopAppBar( UnittoScreenWithTopBar(
title = stringResource(R.string.epoch_converter), title = { Text(stringResource(R.string.epoch_converter)) },
navigateUpAction = navigateUpAction navigationIcon = { MenuButton { navigateUpAction() } }
) { padding -> ) { padding ->
PortraitLandscape( PortraitLandscape(
modifier = Modifier.padding(padding), modifier = Modifier.padding(padding),