Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
225 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
presentation/src/main/java/com/desarrollodroide/pagekeeper/ui/components/LoadingButton.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package com.desarrollodroide.pagekeeper.ui.components | ||
|
||
import androidx.compose.animation.AnimatedVisibility | ||
import androidx.compose.animation.ExperimentalAnimationApi | ||
import androidx.compose.animation.core.Spring | ||
import androidx.compose.animation.core.Transition | ||
import androidx.compose.animation.core.VisibilityThreshold | ||
import androidx.compose.animation.core.animateDp | ||
import androidx.compose.animation.core.spring | ||
import androidx.compose.animation.core.updateTransition | ||
import androidx.compose.animation.expandHorizontally | ||
import androidx.compose.animation.fadeIn | ||
import androidx.compose.animation.fadeOut | ||
import androidx.compose.animation.shrinkHorizontally | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.PaddingValues | ||
import androidx.compose.foundation.layout.defaultMinSize | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.layout.size | ||
import androidx.compose.material3.Button | ||
import androidx.compose.material3.CircularProgressIndicator | ||
import androidx.compose.material3.LocalContentColor | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.StrokeCap | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.compose.ui.unit.IntSize | ||
import androidx.compose.ui.unit.dp | ||
|
||
@Composable | ||
fun LoadingButton( | ||
text: String, | ||
onClick: () -> Unit, | ||
loading: Boolean, | ||
) { | ||
val transition = updateTransition( | ||
targetState = loading, | ||
label = "master transition", | ||
) | ||
val horizontalContentPadding by transition.animateDp( | ||
transitionSpec = { | ||
spring( | ||
stiffness = SpringStiffness, | ||
) | ||
}, | ||
targetValueByState = { toLoading -> if (toLoading) 12.dp else 24.dp }, | ||
label = "button's content padding", | ||
) | ||
Button( | ||
onClick = onClick, | ||
modifier = Modifier.defaultMinSize(minWidth = 1.dp), | ||
contentPadding = PaddingValues( | ||
horizontal = horizontalContentPadding, | ||
vertical = 8.dp, | ||
), | ||
) { | ||
Box(contentAlignment = Alignment.Center) { | ||
LoadingContent( | ||
loadingStateTransition = transition, | ||
) | ||
PrimaryContent( | ||
text = text, | ||
loadingStateTransition = transition, | ||
) | ||
} | ||
} | ||
} | ||
|
||
@OptIn(ExperimentalAnimationApi::class) | ||
@Composable | ||
private fun LoadingContent( | ||
loadingStateTransition: Transition<Boolean>, | ||
) { | ||
loadingStateTransition.AnimatedVisibility( | ||
visible = { loading -> loading }, | ||
enter = fadeIn(), | ||
exit = fadeOut( | ||
animationSpec = spring( | ||
stiffness = SpringStiffness, | ||
visibilityThreshold = 0.10f, | ||
), | ||
), | ||
) { | ||
CircularProgressIndicator( | ||
modifier = Modifier.size(18.dp), | ||
color = LocalContentColor.current, | ||
strokeWidth = 1.5f.dp, | ||
strokeCap = StrokeCap.Round, | ||
) | ||
} | ||
} | ||
|
||
@OptIn(ExperimentalAnimationApi::class) | ||
@Composable | ||
private fun PrimaryContent( | ||
loadingStateTransition: Transition<Boolean>, | ||
text: String, | ||
) { | ||
loadingStateTransition.AnimatedVisibility( | ||
visible = { loading -> !loading }, | ||
enter = fadeIn() + expandHorizontally( | ||
animationSpec = spring( | ||
stiffness = SpringStiffness, | ||
dampingRatio = Spring.DampingRatioMediumBouncy, | ||
visibilityThreshold = IntSize.VisibilityThreshold, | ||
), | ||
expandFrom = Alignment.CenterHorizontally, | ||
), | ||
exit = fadeOut( | ||
animationSpec = spring( | ||
stiffness = SpringStiffness, | ||
visibilityThreshold = 0.10f, | ||
), | ||
) + shrinkHorizontally( | ||
animationSpec = spring( | ||
stiffness = SpringStiffness, | ||
// dampingRatio is not applicable here, size cannot become negative | ||
visibilityThreshold = IntSize.VisibilityThreshold, | ||
), | ||
shrinkTowards = Alignment.CenterHorizontally, | ||
), | ||
) { | ||
Text( | ||
text = text, | ||
modifier = Modifier | ||
// so that bouncing button's width doesn't cut first and last letters | ||
.padding(horizontal = 4.dp), | ||
) | ||
} | ||
} | ||
|
||
// use same spring stiffness so that all animations finish at about the same time | ||
private val SpringStiffness = Spring.StiffnessMediumLow | ||
|
||
@Preview | ||
@Composable | ||
private fun LoadingButtonPreview() { | ||
Box( | ||
modifier = Modifier.fillMaxWidth(), | ||
contentAlignment = Alignment.Center, | ||
) { | ||
LoadingButton( | ||
text = "Login", | ||
onClick = {}, | ||
loading = false, | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.