package com.suno.android.ui.screens.hooks.overlay import androidx.compose.animation.core.Animatable import androidx.compose.animation.core.keyframes import androidx.compose.runtime.Composable import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.Modifier import androidx.compose.ui.draw.rotate import androidx.compose.ui.draw.scale import androidx.compose.ui.hapticfeedback.HapticFeedbackType import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalHapticFeedback import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.stringResource import com.suno.android.common_data.mappers.hooks.LocalHookData import com.suno.android.common_ui.R import com.suno.android.common_ui.components.player.PlayerActionButton import com.suno.android.common_ui.components.player.PlayerActionButtonColors import com.suno.android.common_ui.extensions.xAsFormattedCount import com.suno.android.common_ui.theme.SunoPink import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.launch import com.suno.android.common_res.R as CommonResR private const val ANIMATION_DURATION_MS = 400 private const val DEFAULT_SCALE_VALUE = 1f private const val ANIMATED_SCALE_VALUE = 1.4f private const val DEFAULT_ROTATION_VALUE = 0f private const val ANIMATED_ROTATION_VALUE = -15f @Composable fun HookLikeButton( hook: LocalHookData, onLikeButtonClick: () -> Unit, ) { val context = LocalContext.current val formattedUpvoteCount = remember(hook.likeCount) { if (hook.likeCount == 0) { context.getString(CommonResR.string.like) } else { hook.likeCount.xAsFormattedCount() } } val hapticFeedback = LocalHapticFeedback.current val scope = rememberCoroutineScope() val scaleAnimatable = remember { Animatable(DEFAULT_SCALE_VALUE) } val rotationAnimatable = remember { Animatable(DEFAULT_ROTATION_VALUE) } val colors = PlayerActionButtonColors.Default.copy(selectedContent = SunoPink) PlayerActionButton( icon = painterResource(R.drawable.heart_v2), contentDescription = stringResource(CommonResR.string.like), iconModifier = Modifier .scale(scaleAnimatable.value) .rotate(rotationAnimatable.value), text = formattedUpvoteCount, isSelected = hook.currentUserLiked, colors = colors, onClick = { if (!hook.currentUserLiked) { hapticFeedback.performHapticFeedback(HapticFeedbackType.Confirm) animateLike(scope, scaleAnimatable, rotationAnimatable) } onLikeButtonClick() }, ) } private fun animateLike( scope: CoroutineScope, scaleAnimatable: Animatable, rotationAnimatable: Animatable, ) { scope.launch { scaleAnimatable.animateTo( targetValue = DEFAULT_SCALE_VALUE, animationSpec = keyframes { durationMillis = ANIMATION_DURATION_MS DEFAULT_SCALE_VALUE at 0 ANIMATED_SCALE_VALUE at ANIMATION_DURATION_MS / 2 }, ) } scope.launch { rotationAnimatable.animateTo( targetValue = DEFAULT_ROTATION_VALUE, animationSpec = keyframes { durationMillis = ANIMATION_DURATION_MS DEFAULT_ROTATION_VALUE at 0 ANIMATED_ROTATION_VALUE at ANIMATION_DURATION_MS / 2 }, ) } }