Coverage Summary for Class: MindboxEmbeddedBlockKt (cloud.mindbox.mobile_sdk.embedded.compose)

Class Method, % Branch, % Line, % Instruction, %
MindboxEmbeddedBlockKt 42.9% (6/14) 60% (18/30) 97.9% (47/48) 95.5% (858/898)
MindboxEmbeddedBlockKt$MindboxEmbeddedBlock$1 0% (0/1) 0% (0/1) 0% (0/1)
MindboxEmbeddedBlockKt$MindboxEmbeddedBlock$2 0% (0/1) 0% (0/1) 0% (0/1)
MindboxEmbeddedBlockKt$MindboxEmbeddedBlock$3 0% (0/1) 0% (0/1)
MindboxEmbeddedBlockKt$MindboxEmbeddedBlock$4 100% (1/1) 25% (2/8) 37.5% (3/8) 36.6% (26/71)
MindboxEmbeddedBlockKt$MindboxEmbeddedBlock$5 100% (1/1) 100% (1/1) 100% (4/4)
MindboxEmbeddedBlockKt$MindboxEmbeddedBlock$6$1 100% (1/1) 100% (12/12) 100% (55/55)
MindboxEmbeddedBlockKt$MindboxEmbeddedBlock$6$1$1$1 100% (1/1) 100% (1/1) 100% (10/10)
MindboxEmbeddedBlockKt$MindboxEmbeddedBlock$6$1$1$2 75% (3/4) 75% (3/4) 72.2% (13/18)
MindboxEmbeddedBlockKt$MindboxEmbeddedBlock$7 100% (1/1) 100% (1/1) 100% (2/2)
MindboxEmbeddedBlockKt$MindboxEmbeddedBlock$8 100% (1/1) 100% (4/4) 100% (3/3) 100% (47/47)
MindboxEmbeddedBlockKt$MindboxEmbeddedBlock$9
MindboxEmbeddedBlockKt$MindboxEmbeddedBlock$errorHost$1$1 0% (0/1) 0% (0/1) 0% (0/3)
MindboxEmbeddedBlockKt$MindboxEmbeddedBlock$placeholderHost$1$1 100% (1/1) 100% (1/1) 100% (3/3)
MindboxEmbeddedBlockKt$rememberFixedAtCreation$1 100% (1/1) 100% (2/2) 100% (5/5) 100% (20/20)
MindboxEmbeddedBlockKt$rememberLazySlotHost$1$1 100% (1/1) 100% (1/1) 100% (26/26)
MindboxEmbeddedBlockKt$rememberLazySlotHost$1$1$1$1 100% (1/1) 50% (1/2) 100% (1/1) 94.1% (32/34)
MindboxEmbeddedBlockKt$revealHeight$1 100% (1/1) 100% (3/3) 100% (28/28)
MindboxEmbeddedBlockKt$revealHeight$1$1 100% (1/1) 100% (1/1) 100% (9/9)
Total 61.8% (21/34) 58.7% (27/46) 88.3% (83/94) 92.1% (1133/1230)


 package cloud.mindbox.mobile_sdk.embedded.compose
 
 import androidx.compose.animation.core.Animatable
 import androidx.compose.animation.core.CubicBezierEasing
 import androidx.compose.animation.core.tween
 import androidx.compose.foundation.layout.fillMaxWidth
 import androidx.compose.foundation.layout.height
 import androidx.compose.runtime.Composable
 import androidx.compose.runtime.LaunchedEffect
 import androidx.compose.runtime.getValue
 import androidx.compose.runtime.key
 import androidx.compose.runtime.mutableStateOf
 import androidx.compose.runtime.remember
 import androidx.compose.runtime.rememberUpdatedState
 import androidx.compose.runtime.setValue
 import androidx.compose.ui.Modifier
 import androidx.compose.ui.draw.clipToBounds
 import androidx.compose.ui.layout.layout
 import androidx.compose.ui.platform.ComposeView
 import androidx.compose.ui.platform.LocalContext
 import androidx.compose.ui.platform.LocalLifecycleOwner
 import androidx.compose.ui.unit.dp
 import androidx.compose.ui.viewinterop.AndroidView
 import cloud.mindbox.mobile_sdk.Mindbox
 import cloud.mindbox.mobile_sdk.annotations.InternalMindboxApi
 import cloud.mindbox.mobile_sdk.embedded.MindboxEmbeddedBlockAppearance
 import cloud.mindbox.mobile_sdk.embedded.MindboxEmbeddedBlockFailReason
 import cloud.mindbox.mobile_sdk.embedded.MindboxEmbeddedBlockListener
 import cloud.mindbox.mobile_sdk.embedded.MindboxEmbeddedBlockLoadingStrategy
 import cloud.mindbox.mobile_sdk.embedded.MindboxEmbeddedBlockView
 import cloud.mindbox.mobile_sdk.logger.Level
 import kotlin.math.roundToInt
 
 /**
  * An embedded Mindbox block as a composable.
  *
  * The caller names a place and gives the block an explicit height; the SDK decides what the place
  * shows. The block takes the whole available width, and zero height when there is nothing to
  * show. The behavior is that of [MindboxEmbeddedBlockView]. Keep the block in the composition rather
  * than removing it in [onEmpty]: a collapsed block still shows content the place gets later.
  *
  * ```kotlin
  * MindboxEmbeddedBlock(
  *     placeSystemName = "main-screen-top",
  *     modifier = Modifier.height(120.dp),
  * )
  * ```
  *
  * @param placeSystemName The place the block fills. Whitespace around it is trimmed, letter case
  * is ignored. A new name builds a new block.
  * @param timeoutMs How long the block waits for the SDK's answer before failing with
  * [MindboxEmbeddedBlockFailReason.NETWORK_ERROR]. `null` takes the
  * `mindbox_embedded_block_timeout_ms` integer resource, 30 s by default. Counted while the screen
  * with the block is shown. Fixed when the block is created.
  * @param loadingStrategy What the block shows until the SDK answers.
  * [MindboxEmbeddedBlockLoadingStrategy.AUTOMATIC] by default. Fixed when the block is created.
  * @param animatesReveal Whether the content is revealed with an animation. `true` by default.
  * Fixed when the block is created.
  * @param onLoad The content is shown. Main thread.
  * @param onEmpty The place has nothing to show; the block collapsed. Main thread.
  * @param onFail The block could not get or show its content; the block collapsed or shows
  * [error]. The reason is one of the [MindboxEmbeddedBlockFailReason] constants. Main thread.
  * @param placeholder Replaces the SDK's shimmer while the content is loading. Fills the block.
  * @param error Shown instead of collapsing the block when it fails. Failures only. Set it before
  * the block fails: a slot set after the block has collapsed, for example in [onFail], does not
  * expand it. A block that waits hidden shows no error view. Fills the block.
  */
 @OptIn(InternalMindboxApi::class)
 @Composable
 public fun MindboxEmbeddedBlock(
     placeSystemName: String,
     modifier: Modifier = Modifier,
     timeoutMs: Long? = null,
     loadingStrategy: MindboxEmbeddedBlockLoadingStrategy = MindboxEmbeddedBlockLoadingStrategy.AUTOMATIC,
     animatesReveal: Boolean = true,
     onLoad: () -> Unit = {},
     onEmpty: () -> Unit = {},
     onFail: (MindboxEmbeddedBlockFailReason) -> Unit = {},
     placeholder: (@Composable () -> Unit)? = null,
     error: (@Composable () -> Unit)? = null,
 ) {
     val currentOnLoad by rememberUpdatedState(onLoad)
     val currentOnEmpty by rememberUpdatedState(onEmpty)
     val currentOnFail by rememberUpdatedState(onFail)
     val currentPlaceholder by rememberUpdatedState(placeholder)
     val currentError by rememberUpdatedState(error)
 
     val context = LocalContext.current
     val screenOwner by rememberUpdatedState(LocalLifecycleOwner.current)
 
     key(placeSystemName) {
         val creationTimeoutMs = rememberFixedAtCreation(placeSystemName, "timeoutMs", timeoutMs)
         val creationLoadingStrategy = rememberFixedAtCreation(placeSystemName, "loadingStrategy", loadingStrategy)
         val creationAnimatesReveal = rememberFixedAtCreation(placeSystemName, "animatesReveal", animatesReveal)
         var look by remember {
             mutableStateOf(
                 AppearanceChange(
                     MindboxEmbeddedBlockView.initialAppearance(context, placeSystemName, creationLoadingStrategy),
                     animated = false,
                 ),
             )
         }
 
         val placeholderHost = rememberLazySlotHost { currentPlaceholder }
         val errorHost = rememberLazySlotHost { currentError }
 
         val revealFraction = remember { Animatable(look.appearance.targetFraction) }
         LaunchedEffect(look.appearance) {
             val reveals = look.appearance == MindboxEmbeddedBlockAppearance.CONTENT && revealFraction.value < 1f
             if (reveals && look.animated) {
                 revealFraction.animateTo(
                     targetValue = 1f,
                     animationSpec = tween(
                         durationMillis = MindboxEmbeddedBlockView.REVEAL_ANIMATION_DURATION_MS.toInt(),
                         easing = EaseInOutCurve,
                     ),
                 )
             } else {
                 revealFraction.snapTo(look.appearance.targetFraction)
             }
         }
 
         AndroidView(
             modifier = Modifier
                 .revealHeight(look.appearance) { revealFraction.value }
                 .then(modifier)
                 .fillMaxWidth(),
             factory = { viewContext ->
                 MindboxEmbeddedBlockView(
                     context = viewContext,
                     placeSystemName = placeSystemName,
                     timeoutMs = creationTimeoutMs,
                     loadingStrategy = creationLoadingStrategy,
                     animatesReveal = creationAnimatesReveal,
                 ).apply {
                     setScreenOwner(screenOwner)
                     setAppearanceObserver { shown -> look = AppearanceChange(shown, isRevealAnimated) }
                     setListener(
                         object : MindboxEmbeddedBlockListener {
                             override fun onLoad(view: MindboxEmbeddedBlockView) {
                                 currentOnLoad()
                             }
 
                             override fun onEmpty(view: MindboxEmbeddedBlockView) {
                                 currentOnEmpty()
                             }
 
                             override fun onFail(view: MindboxEmbeddedBlockView, reason: MindboxEmbeddedBlockFailReason) {
                                 currentOnFail(reason)
                             }
                         },
                     )
                 }
             },
             update = { view ->
                 view.setScreenOwner(screenOwner)
                 view.setPlaceholderView(placeholder?.let { placeholderHost.value })
                 view.setErrorView(error?.let { errorHost.value })
             },
             onRelease = { view -> view.releaseOrRetain() },
         )
     }
 }
 
 private val EaseInOutCurve = CubicBezierEasing(0.42f, 0f, 0.58f, 1f)
 
 private data class AppearanceChange(
     val appearance: MindboxEmbeddedBlockAppearance,
     val animated: Boolean,
 )
 
 private val MindboxEmbeddedBlockAppearance.targetFraction: Float
     get() = if (this == MindboxEmbeddedBlockAppearance.COLLAPSED) 0f else 1f
 
 @Composable
 private fun <T> rememberFixedAtCreation(placeSystemName: String, name: String, value: T): T {
     val creationValue = remember { value }
     LaunchedEffect(value) {
         if (value != creationValue) {
             Mindbox.writeLog(
                 "[EmbeddedBlock] Block '$placeSystemName' was given $name=$value after creation " +
                     "and keeps $creationValue: $name is fixed when the block is created. Wrap the " +
                     "block in a key() of your own to build one anew.",
                 Level.WARN,
             )
         }
     }
     return creationValue
 }
 
 @Composable
 private fun rememberLazySlotHost(slot: () -> (@Composable () -> Unit)?): Lazy<ComposeView> {
     val context = LocalContext.current
     return remember(context) {
         lazy(LazyThreadSafetyMode.NONE) {
             ComposeView(context).apply { setContent { slot()?.invoke() } }
         }
     }
 }
 
 private fun Modifier.revealHeight(
     appearance: MindboxEmbeddedBlockAppearance,
     revealFraction: () -> Float,
 ): Modifier =
     if (appearance == MindboxEmbeddedBlockAppearance.COLLAPSED) {
         height(0.dp)
     } else {
         clipToBounds().layout { measurable, constraints ->
             val placeable = measurable.measure(constraints)
             val revealedHeight = (placeable.height * revealFraction()).roundToInt()
             layout(placeable.width, revealedHeight) { placeable.placeRelative(0, 0) }
         }
     }