DemandAttention.kt (2423B)
1 /* 2 * This file is part of GNU Taler 3 * (C) 2024 Taler Systems S.A. 4 * 5 * GNU Taler is free software; you can redistribute it and/or modify it under the 6 * terms of the GNU General Public License as published by the Free Software 7 * Foundation; either version 3, or (at your option) any later version. 8 * 9 * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY 10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 * A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License along with 14 * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 17 package net.taler.wallet.compose 18 19 import androidx.compose.animation.core.Animatable 20 import androidx.compose.animation.core.LinearEasing 21 import androidx.compose.animation.core.RepeatMode 22 import androidx.compose.animation.core.repeatable 23 import androidx.compose.animation.core.tween 24 import androidx.compose.foundation.layout.Box 25 import androidx.compose.foundation.layout.offset 26 import androidx.compose.runtime.Composable 27 import androidx.compose.runtime.LaunchedEffect 28 import androidx.compose.runtime.remember 29 import androidx.compose.ui.Modifier 30 import androidx.compose.ui.unit.dp 31 import kotlinx.coroutines.delay 32 33 @Composable 34 fun DemandAttention( 35 initialDelayMillis: Long = 400, 36 wiggleWidth: Float = 5f, 37 wiggleCount: Int = 2, 38 demandAttention: Boolean = true, 39 content: @Composable () -> Unit, 40 ) { 41 val offsetX = remember { Animatable(0f) } 42 43 suspend fun wiggle() { 44 offsetX.animateTo( 45 targetValue = -wiggleWidth, 46 animationSpec = tween(80, easing = LinearEasing), 47 ) 48 49 offsetX.animateTo( 50 targetValue = wiggleWidth, 51 animationSpec = repeatable( 52 iterations = 5, 53 animation = tween(90, easing = LinearEasing), 54 repeatMode = RepeatMode.Reverse, 55 ) 56 ) 57 58 offsetX.animateTo( 59 targetValue = 0f, 60 animationSpec = tween(80, easing = LinearEasing), 61 ) 62 } 63 64 LaunchedEffect(demandAttention) { 65 if (demandAttention) { 66 delay(initialDelayMillis) 67 repeat(wiggleCount) { 68 wiggle() 69 } 70 } 71 } 72 73 Box(Modifier.offset(offsetX.value.dp, 0.dp)) { 74 content() 75 } 76 }