Skip to content

Commit

Permalink
Add dismiss animation
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-radhika-s committed Dec 13, 2023
1 parent 0c8d6da commit 629e0be
Showing 1 changed file with 51 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.graphics.BlendMode
Expand All @@ -38,6 +39,7 @@ import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlin.math.absoluteValue
import kotlin.math.max
import kotlin.math.min
Expand Down Expand Up @@ -75,6 +77,8 @@ private fun TargetContent(
targetCords.positionInRoot().y.toDp()
}

var dismissShowcaseRequest by remember(target) { mutableStateOf(false) }

val isTargetInGutter = gutterArea > yOffset || yOffset > screenHeight.dp.minus(gutterArea)

val maxDimension =
Expand All @@ -86,20 +90,23 @@ private fun TargetContent(
repeatMode = RepeatMode.Restart,
)

var outerOffset by remember {
var outerOffset by remember(target) {
mutableStateOf(Offset(0f, 0f))
}

var outerRadius by remember {
var outerRadius by remember(target) {
mutableStateOf(0f)
}

val outerAnimatable = remember { Animatable(0.6f) }
val outerAlphaAnimatable = remember(target) { Animatable(0f) }

val animatables = listOf(
remember { Animatable(0f) },
remember { Animatable(0f) }
)
val animatables = remember(target) {
listOf(
Animatable(0f),
Animatable(0f)
)
}

LaunchedEffect(target) {
outerAnimatable.snapTo(0.6f)
Expand All @@ -113,6 +120,40 @@ private fun TargetContent(
)
}

LaunchedEffect(target) {
outerAlphaAnimatable.animateTo(
targetValue = target.style.backgroundAlpha,
animationSpec = tween(
durationMillis = 500,
easing = FastOutSlowInEasing,
),
)
}

LaunchedEffect(dismissShowcaseRequest) {
if (dismissShowcaseRequest) {
launch {
outerAlphaAnimatable.animateTo(
0f,
animationSpec = tween(
durationMillis = 200
)
)
}
launch {
outerAnimatable.animateTo(
targetValue = 0.6f,
animationSpec = tween(
durationMillis = 350,
easing = FastOutSlowInEasing,
)
)
}
delay(350)
onShowcaseCompleted()
}
}

animatables.forEachIndexed { index, animatable ->
LaunchedEffect(animatable) {
delay(index * 1000L)
Expand All @@ -123,14 +164,14 @@ private fun TargetContent(
}

val dys = animatables.map { it.value }
Box {
Box(modifier = Modifier.alpha(outerAlphaAnimatable.value)) {
Canvas(
modifier = Modifier
.fillMaxSize()
.pointerInput(target) {
detectTapGestures { tapOffeset ->
if (targetRect.contains(tapOffeset)) {
onShowcaseCompleted()
dismissShowcaseRequest = true
}
}
}
Expand All @@ -139,7 +180,7 @@ private fun TargetContent(
it.clickable(
interactionSource = remember { MutableInteractionSource() },
indication = null
) { onShowcaseCompleted() }
) { dismissShowcaseRequest = true }
} else it
}
.graphicsLayer(alpha = 0.99f)
Expand Down Expand Up @@ -193,7 +234,7 @@ private fun ShowCaseText(
updateContentCoordinates: (LayoutCoordinates) -> Unit
) {

var contentOffsetY by remember {
var contentOffsetY by remember(currentTarget) {
mutableStateOf(0f)
}

Expand Down

0 comments on commit 629e0be

Please sign in to comment.