Coverage Summary for Class: InAppContentFetcherImpl (cloud.mindbox.mobile_sdk.inapp.data.managers)

Class Method, % Branch, % Line, % Instruction, %
InAppContentFetcherImpl 33.3% (1/3) 0% (0/14) 6.7% (2/30) 2.6% (5/191)
InAppContentFetcherImpl$fetchContent$1
InAppContentFetcherImpl$fetchContent$2$1 0% (0/1) 0% (0/1) 0% (0/21)
InAppContentFetcherImpl$fetchContent$2$1$1 0% (0/1) 0% (0/1) 0% (0/19)
InAppContentFetcherImpl$fetchContent$4$1 0% (0/1) 0% (0/1) 0% (0/21)
InAppContentFetcherImpl$fetchContent$4$1$1 0% (0/1) 0% (0/1) 0% (0/19)
Total 14.3% (1/7) 0% (0/14) 5.9% (2/34) 1.8% (5/271)


 package cloud.mindbox.mobile_sdk.inapp.data.managers
 
 import cloud.mindbox.mobile_sdk.inapp.domain.interfaces.InAppContentFetcher
 import cloud.mindbox.mobile_sdk.inapp.domain.interfaces.InAppImageLoader
 import cloud.mindbox.mobile_sdk.inapp.domain.models.InAppType
 import cloud.mindbox.mobile_sdk.inapp.domain.models.Layer
 import kotlinx.coroutines.Deferred
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.async
 import kotlinx.coroutines.withContext
 
 internal class InAppContentFetcherImpl(
     private val inAppImageLoader: InAppImageLoader,
 ) : InAppContentFetcher {
 
     override suspend fun fetchContent(inAppId: String, formVariant: InAppType): Boolean {
         val inAppImageStorage: MutableList<Deferred<Boolean>> = mutableListOf()
         when (formVariant) {
             is InAppType.WebView -> {
                 // do nothing
             }
 
             is InAppType.ModalWindow -> {
                 formVariant.layers.filterIsInstance<Layer.ImageLayer>()
                     .forEach { layer ->
                         when (layer.source) {
                             is Layer.ImageLayer.Source.UrlSource -> {
                                 withContext(Dispatchers.IO) {
                                     inAppImageStorage.add(async {
                                         inAppImageLoader.loadImage(inAppId, layer.source.url)
                                     })
                                 }
                             }
                         }
                     }
                 if (inAppImageStorage
                         .map { deferredResult -> deferredResult.await() }
                         .contains(false)) {
                     return false
                 }
             }
 
             is InAppType.Snackbar -> {
                 formVariant.layers
                     .filterIsInstance<Layer.ImageLayer>()
                     .forEach { layer ->
                         when (layer.source) {
                             is Layer.ImageLayer.Source.UrlSource -> {
                                 withContext(Dispatchers.IO) {
                                     inAppImageStorage.add(async {
                                         inAppImageLoader.loadImage(inAppId, layer.source.url)
                                     })
                                 }
                             }
                         }
                     }
                 if (inAppImageStorage
                         .map { deferredResult -> deferredResult.await() }
                         .contains(false)) {
                     return false
                 }
             }
         }
         return true
     }
 
     override fun cancelFetching(inAppId: String) {
         inAppImageLoader.cancelLoading(inAppId)
     }
 }