Skip to content

Commit

Permalink
wip Animating button disabling
Browse files Browse the repository at this point in the history
  • Loading branch information
LunarX committed Oct 17, 2024
1 parent f86afeb commit 22736b1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ package com.infomaniak.swisstransfer.ui.components

import android.content.res.Configuration
import androidx.annotation.StringRes
import androidx.compose.animation.animateColorAsState
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
Expand All @@ -44,8 +46,9 @@ fun LargeButton(
enabled: () -> Boolean = { true },
onClick: () -> Unit,
imageVector: ImageVector? = null,
animatedEnabling: Boolean = false,
) {
CoreButton(titleRes, modifier, ButtonSize.LARGE, style, enabled, onClick, imageVector)
CoreButton(titleRes, modifier, ButtonSize.LARGE, style, enabled, onClick, imageVector, animatedEnabling)
}

@Composable
Expand All @@ -57,7 +60,7 @@ fun SmallButton(
onClick: () -> Unit,
imageVector: ImageVector? = null,
) {
CoreButton(titleRes, modifier, ButtonSize.SMALL, style, enabled, onClick, imageVector)
CoreButton(titleRes, modifier, ButtonSize.SMALL, style, enabled, onClick, imageVector, false)
}

@Composable
Expand All @@ -69,12 +72,28 @@ private fun CoreButton(
enabled: () -> Boolean,
onClick: () -> Unit,
imageVector: ImageVector?,
animatedEnabling: Boolean,
) {
val isEnabled = enabled()

val buttonColors = if (animatedEnabling) {
val defaultColors = style.buttonColors()
val animatedContainerColor by animateColorAsState(if (isEnabled) defaultColors.containerColor else defaultColors.disabledContainerColor)
val animatedContentColor by animateColorAsState(if (isEnabled) defaultColors.contentColor else defaultColors.disabledContentColor)

ButtonDefaults.buttonColors(
containerColor = animatedContainerColor,
contentColor = animatedContentColor,
disabledContainerColor = animatedContainerColor,
disabledContentColor = animatedContentColor,
)
} else style.buttonColors()

Button(
modifier = modifier.height(buttonSize.height),
colors = style.buttonColors(),
colors = buttonColors,
shape = Shapes.medium,
enabled = enabled(),
enabled = isEnabled,
onClick = onClick,
) {
imageVector?.let {
Expand Down Expand Up @@ -126,7 +145,7 @@ private fun LargeButtonPreview() {
Column {
ButtonType.entries.forEach {
Row {
LargeButton(titleRes = R.string.appName, style = it, imageVector = AppIcons.Add, onClick = {})
LargeButton(titleRes = R.string.appName, style = it, onClick = {}, imageVector = AppIcons.Add)
Spacer(modifier = Modifier.width(Margin.Small))
SmallButton(titleRes = R.string.appName, style = it, imageVector = AppIcons.Add, onClick = {})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,16 @@ private fun BottomSheetDefaultsPreview() {
},
topButton = {
LargeButton(
modifier = it,
titleRes = R.string.appName,
modifier = it,
style = ButtonType.ERROR,
onClick = {},
)
},
bottomButton = {
LargeButton(
modifier = it,
titleRes = R.string.appName,
modifier = it,
style = ButtonType.TERTIARY,
onClick = {},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ private fun ImportFilesScreen(
titleRes = R.string.transferSendButton,
style = ButtonType.PRIMARY,
enabled = { isSendButtonEnabled },
animatedEnabling = true,
onClick = { /*TODO*/ },
)
},
Expand Down

0 comments on commit 22736b1

Please sign in to comment.