Fixed long click #15

This commit is contained in:
Sad Ellie 2023-01-06 20:48:24 +04:00
parent e27f8fb2f4
commit 2bd56a7c3e
2 changed files with 98 additions and 14 deletions

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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
)
}
}
}
}

View File

@ -26,7 +26,6 @@ import androidx.compose.foundation.interaction.collectIsPressedAsState
import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.MaterialTheme import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
@ -34,6 +33,7 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import com.sadellie.unitto.screens.common.UnittoButton
import com.sadellie.unitto.ui.theme.NumbersTextStyleTitleLarge import com.sadellie.unitto.ui.theme.NumbersTextStyleTitleLarge
/** /**
@ -51,7 +51,7 @@ fun KeyboardButton(
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
digit: String, digit: String,
isPrimary: Boolean = true, isPrimary: Boolean = true,
onLongClick: () -> Unit = {}, onLongClick: (() -> Unit)? = null,
onClick: (String) -> Unit = {} onClick: (String) -> Unit = {}
) { ) {
val interactionSource = remember { MutableInteractionSource() } val interactionSource = remember { MutableInteractionSource() }
@ -59,24 +59,22 @@ fun KeyboardButton(
val cornerRadius: Int by animateIntAsState( val cornerRadius: Int by animateIntAsState(
targetValue = if (isPressed) 30 else 50, targetValue = if (isPressed) 30 else 50,
animationSpec = tween(easing = FastOutSlowInEasing), animationSpec = tween(easing = FastOutSlowInEasing),
finishedListener = { if (it == 30) onLongClick() }) )
Button( UnittoButton(
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)
),
onClick = { onClick(digit) }, 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(
text = digit, text = digit,
style = NumbersTextStyleTitleLarge, style = NumbersTextStyleTitleLarge,
color = if (isPrimary) MaterialTheme.colorScheme.onSurfaceVariant else MaterialTheme.colorScheme.onSecondaryContainer, color = if (isPrimary) MaterialTheme.colorScheme.onSurfaceVariant else MaterialTheme.colorScheme.onSecondaryContainer
) )
} }
} }