From 2bd56a7c3e2b350f2f41e7bf466b73e5551f8ab5 Mon Sep 17 00:00:00 2001 From: Sad Ellie Date: Fri, 6 Jan 2023 20:48:24 +0400 Subject: [PATCH] Fixed long click #15 --- .../unitto/screens/common/UnittoButton.kt | 86 +++++++++++++++++++ .../screens/main/components/KeyboardButton.kt | 26 +++--- 2 files changed, 98 insertions(+), 14 deletions(-) create mode 100644 app/src/main/java/com/sadellie/unitto/screens/common/UnittoButton.kt diff --git a/app/src/main/java/com/sadellie/unitto/screens/common/UnittoButton.kt b/app/src/main/java/com/sadellie/unitto/screens/common/UnittoButton.kt new file mode 100644 index 00000000..f95a624a --- /dev/null +++ b/app/src/main/java/com/sadellie/unitto/screens/common/UnittoButton.kt @@ -0,0 +1,86 @@ +/* + * Unitto is a unit converter for Android + * Copyright (c) 2023 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 . + */ + +package com.sadellie.unitto.screens.common + +import androidx.compose.foundation.BorderStroke +import androidx.compose.foundation.combinedClickable +import androidx.compose.foundation.interaction.MutableInteractionSource +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.PaddingValues +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.RowScope +import androidx.compose.foundation.layout.defaultMinSize +import androidx.compose.foundation.layout.padding +import androidx.compose.material.ripple.rememberRipple +import androidx.compose.material3.ButtonDefaults +import androidx.compose.material3.LocalContentColor +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.ProvideTextStyle +import androidx.compose.material3.Surface +import androidx.compose.runtime.Composable +import androidx.compose.runtime.CompositionLocalProvider +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.clip +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.graphics.Shape +import androidx.compose.ui.semantics.Role + +@Composable +fun UnittoButton( + onClick: () -> Unit, + onLongClick: (() -> Unit)?, + modifier: Modifier = Modifier, + shape: Shape, + containerColor: Color, + contentColor: Color, + border: BorderStroke? = null, + contentPadding: PaddingValues = ButtonDefaults.ContentPadding, + interactionSource: MutableInteractionSource, + content: @Composable RowScope.() -> Unit +) { + Surface( + modifier = modifier.clip(shape).combinedClickable( + onClick = onClick, + onLongClick = onLongClick, + interactionSource = interactionSource, + indication = rememberRipple(), + role = Role.Button, + ), + color = containerColor, + contentColor = contentColor, + border = border + ) { + CompositionLocalProvider(LocalContentColor provides contentColor) { + ProvideTextStyle(value = MaterialTheme.typography.labelLarge) { + Row( + Modifier + .defaultMinSize( + minWidth = ButtonDefaults.MinWidth, + minHeight = ButtonDefaults.MinHeight + ) + .padding(contentPadding), + horizontalArrangement = Arrangement.Center, + verticalAlignment = Alignment.CenterVertically, + content = content + ) + } + } + } +} diff --git a/app/src/main/java/com/sadellie/unitto/screens/main/components/KeyboardButton.kt b/app/src/main/java/com/sadellie/unitto/screens/main/components/KeyboardButton.kt index b63a2b08..5f19f3fb 100644 --- a/app/src/main/java/com/sadellie/unitto/screens/main/components/KeyboardButton.kt +++ b/app/src/main/java/com/sadellie/unitto/screens/main/components/KeyboardButton.kt @@ -26,7 +26,6 @@ import androidx.compose.foundation.interaction.collectIsPressedAsState import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material3.Button -import androidx.compose.material3.ButtonDefaults import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text import androidx.compose.runtime.Composable @@ -34,6 +33,7 @@ import androidx.compose.runtime.getValue import androidx.compose.runtime.remember import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp +import com.sadellie.unitto.screens.common.UnittoButton import com.sadellie.unitto.ui.theme.NumbersTextStyleTitleLarge /** @@ -51,7 +51,7 @@ fun KeyboardButton( modifier: Modifier = Modifier, digit: String, isPrimary: Boolean = true, - onLongClick: () -> Unit = {}, + onLongClick: (() -> Unit)? = null, onClick: (String) -> Unit = {} ) { val interactionSource = remember { MutableInteractionSource() } @@ -59,24 +59,22 @@ fun KeyboardButton( val cornerRadius: Int by animateIntAsState( targetValue = if (isPressed) 30 else 50, animationSpec = tween(easing = FastOutSlowInEasing), - finishedListener = { if (it == 30) onLongClick() }) + ) - Button( - modifier = modifier, - interactionSource = interactionSource, - shape = RoundedCornerShape(cornerRadius), - colors = ButtonDefaults.buttonColors( - containerColor = if (isPrimary) MaterialTheme.colorScheme.inverseOnSurface else MaterialTheme.colorScheme.primaryContainer, - contentColor = MaterialTheme.colorScheme.onSecondaryContainer, - disabledContentColor = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.3f) - ), + UnittoButton( onClick = { onClick(digit) }, - contentPadding = PaddingValues(0.dp) + onLongClick = onLongClick, + modifier = modifier, + shape = RoundedCornerShape(cornerRadius), + containerColor = if (isPrimary) MaterialTheme.colorScheme.inverseOnSurface else MaterialTheme.colorScheme.primaryContainer, + contentColor = MaterialTheme.colorScheme.onSecondaryContainer, + contentPadding = PaddingValues(0.dp), + interactionSource = interactionSource, ) { Text( text = digit, style = NumbersTextStyleTitleLarge, - color = if (isPrimary) MaterialTheme.colorScheme.onSurfaceVariant else MaterialTheme.colorScheme.onSecondaryContainer, + color = if (isPrimary) MaterialTheme.colorScheme.onSurfaceVariant else MaterialTheme.colorScheme.onSecondaryContainer ) } }