Paste from history

This commit is contained in:
Sad Ellie 2023-02-22 23:46:38 +04:00
parent 6b45eb7bec
commit 1a6d6fdce4
2 changed files with 46 additions and 28 deletions

View File

@ -158,7 +158,8 @@ private fun CalculatorScreen(
.background(MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.5f)) .background(MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.5f))
.fillMaxSize(), .fillMaxSize(),
historyItems = uiState.history, historyItems = uiState.history,
historyItemHeightCallback = { historyItemHeight = it } historyItemHeightCallback = { historyItemHeight = it },
onTextClick = addSymbol
) )
}, },
textFields = { maxDragAmount -> textFields = { maxDragAmount ->

View File

@ -19,8 +19,10 @@
package com.sadellie.unitto.feature.calculator.components package com.sadellie.unitto.feature.calculator.components
import androidx.compose.foundation.background import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.horizontalScroll import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.Arrangement 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
@ -55,7 +57,8 @@ import java.util.*
internal fun HistoryList( internal fun HistoryList(
modifier: Modifier, modifier: Modifier,
historyItems: List<HistoryItem>, historyItems: List<HistoryItem>,
historyItemHeightCallback: (Int) -> Unit historyItemHeightCallback: (Int) -> Unit,
onTextClick: (String) -> Unit
) { ) {
val verticalArrangement by remember(historyItems) { val verticalArrangement by remember(historyItems) {
derivedStateOf { derivedStateOf {
@ -90,14 +93,16 @@ internal fun HistoryList(
// We do this so that callback for items height is called only once // We do this so that callback for items height is called only once
item { item {
HistoryListItem( HistoryListItem(
modifier = Modifier.onPlaced { historyItemHeightCallback(it.size.height) }.padding(horizontal = 8.dp), modifier = Modifier.onPlaced { historyItemHeightCallback(it.size.height) },
historyItem = historyItems.first() historyItem = historyItems.first(),
onTextClick = onTextClick
) )
} }
items(historyItems.drop(1)) { historyItem -> items(historyItems.drop(1)) { historyItem ->
HistoryListItem( HistoryListItem(
modifier = Modifier.padding(horizontal = 8.dp), modifier = Modifier,
historyItem = historyItem historyItem = historyItem,
onTextClick = onTextClick
) )
} }
} }
@ -107,30 +112,41 @@ internal fun HistoryList(
@Composable @Composable
private fun HistoryListItem( private fun HistoryListItem(
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
historyItem: HistoryItem historyItem: HistoryItem,
onTextClick: (String) -> Unit
) { ) {
Column(modifier = modifier) { Column(modifier = modifier) {
Box(
Modifier.clickable { onTextClick(historyItem.expression) }
) {
Text( Text(
text = historyItem.expression, text = historyItem.expression,
maxLines = 1, maxLines = 1,
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.padding(horizontal = 8.dp)
.horizontalScroll(rememberScrollState(), reverseScrolling = true), .horizontalScroll(rememberScrollState(), reverseScrolling = true),
style = NumbersTextStyleDisplayMedium, style = NumbersTextStyleDisplayMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant, color = MaterialTheme.colorScheme.onSurfaceVariant,
textAlign = TextAlign.End textAlign = TextAlign.End
) )
}
Box(
Modifier.clickable { onTextClick(historyItem.result) }
) {
Text( Text(
text = Formatter.format(historyItem.result), text = Formatter.format(historyItem.result),
maxLines = 1, maxLines = 1,
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.padding(horizontal = 8.dp)
.horizontalScroll(rememberScrollState(), reverseScrolling = true), .horizontalScroll(rememberScrollState(), reverseScrolling = true),
style = NumbersTextStyleDisplayMedium, style = NumbersTextStyleDisplayMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.5f), color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.5f),
textAlign = TextAlign.End textAlign = TextAlign.End
) )
} }
}
} }
@Preview @Preview
@ -160,6 +176,7 @@ private fun PreviewHistoryList() {
.background(MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.5f)) .background(MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.5f))
.fillMaxSize(), .fillMaxSize(),
historyItems = historyItems, historyItems = historyItems,
historyItemHeightCallback = {} historyItemHeightCallback = {},
onTextClick = {}
) )
} }