1 package com.airbnb.lottie.compose 2 3 import androidx.compose.runtime.Composable 4 import androidx.compose.runtime.Stable 5 import androidx.compose.runtime.getValue 6 import androidx.compose.runtime.mutableStateOf 7 import androidx.compose.runtime.remember 8 import androidx.compose.runtime.setValue 9 import kotlinx.coroutines.channels.BufferOverflow 10 import kotlinx.coroutines.channels.Channel 11 import kotlinx.coroutines.channels.trySendBlocking 12 13 /** 14 * @see LottieRetrySignal 15 * @see rememberLottieComposition 16 */ 17 @Composable rememberLottieRetrySignalnull18fun rememberLottieRetrySignal(): LottieRetrySignal { 19 return remember { LottieRetrySignal() } 20 } 21 22 /** 23 * Helper to retry compositions that fail to load. This will mostly happen for animations loaded via url. 24 * 25 * Call [retry] from an action that should trigger a retry such as a snackbar or retry button. 26 * 27 * Call [awaitRetry] from the onRetry lambda to [rememberLottieComposition] then return true. 28 * 29 * @see rememberLottieComposition 30 */ 31 @Stable 32 class LottieRetrySignal internal constructor() { 33 private val channel = Channel<Unit>(capacity = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST) 34 35 var isAwaitingRetry: Boolean by mutableStateOf(false) 36 private set 37 retrynull38 fun retry() { 39 channel.trySendBlocking(Unit) 40 } 41 awaitRetrynull42 suspend fun awaitRetry() { 43 try { 44 isAwaitingRetry = true 45 channel.receive() 46 } finally { 47 isAwaitingRetry = false 48 } 49 } 50 }