-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Replace LoadingButtonAndroid with compose
- Loading branch information
Showing
9 changed files
with
228 additions
and
26 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
154 changes: 154 additions & 0 deletions
154
app/src/main/java/yokai/presentation/component/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,154 @@ | ||
package yokai.presentation.component | ||
|
||
import androidx.compose.animation.AnimatedVisibility | ||
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 | ||
import androidx.compose.ui.unit.sp | ||
|
||
// REF: https://gist.github.com/mmolosay/584ce5c47567cb66228b76ef98c3c4e4 | ||
|
||
private val SpringStiffness = Spring.StiffnessMediumLow | ||
|
||
@Composable | ||
fun LoadingButton( | ||
text: () -> String, | ||
loading: () -> Boolean, | ||
onClick: () -> Unit, | ||
modifier: Modifier = Modifier, | ||
) { | ||
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, | ||
) | ||
} | ||
} | ||
} | ||
|
||
@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, | ||
) | ||
} | ||
} | ||
|
||
@Composable | ||
private fun PrimaryContent( | ||
text: String, | ||
loadingStateTransition: Transition<Boolean>, | ||
) { | ||
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), | ||
fontSize = 16.sp, | ||
) | ||
} | ||
} | ||
|
||
@Preview | ||
@Composable | ||
private fun LoadingButtonPreview() { | ||
Box( | ||
modifier = Modifier.fillMaxWidth(), | ||
contentAlignment = Alignment.Center, | ||
) { | ||
LoadingButton( | ||
modifier = Modifier.fillMaxWidth(), | ||
text = { "Test" }, | ||
loading = { false }, | ||
onClick = {}, | ||
) | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
app/src/main/java/yokai/presentation/component/LoadingButtonComposeView.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,61 @@ | ||
package yokai.presentation.component | ||
|
||
import android.content.Context | ||
import android.util.AttributeSet | ||
import android.view.Gravity | ||
import android.widget.FrameLayout | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.AbstractComposeView | ||
import androidx.compose.ui.platform.ViewCompositionStrategy | ||
import eu.kanade.tachiyomi.R | ||
import yokai.presentation.theme.YokaiTheme | ||
|
||
class LoadingButtonComposeView @JvmOverloads constructor( | ||
context: Context, | ||
attrs: AttributeSet? = null, | ||
defStyleAttr: Int = 0, | ||
) : AbstractComposeView(context, attrs, defStyleAttr) { | ||
|
||
var text by mutableStateOf("placeholder") | ||
private var onClick: () -> Unit = {} | ||
private var isLoading by mutableStateOf(false) | ||
|
||
init { | ||
layoutParams = FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER) | ||
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnDetachedFromWindowOrReleasedFromPool) | ||
attrs?.let { | ||
val arr = context.obtainStyledAttributes(it, R.styleable.LoadingButtonComposeView) | ||
text = context.getString(arr.getResourceId(R.styleable.LoadingButtonComposeView_android_text, R.string.log_in)) | ||
} | ||
} | ||
|
||
fun setOnClick(onClick: () -> Unit) { | ||
this.onClick = onClick | ||
} | ||
|
||
fun startAnimation() { | ||
isLoading = true | ||
} | ||
|
||
fun revertAnimation(after: () -> Unit = {}) { | ||
isLoading = false | ||
after() | ||
} | ||
|
||
@Composable | ||
override fun Content() { | ||
YokaiTheme { | ||
LoadingButton( | ||
modifier = Modifier.fillMaxWidth(), | ||
text = { text }, | ||
loading = { isLoading }, | ||
onClick = onClick, | ||
) | ||
} | ||
} | ||
} |
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
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