Coverage Summary for Class: InAppGeoRepositoryImpl (cloud.mindbox.mobile_sdk.inapp.data.repositories)
| Class |
Method, %
|
Branch, %
|
Line, %
|
Instruction, %
|
| InAppGeoRepositoryImpl |
100%
(5/5)
|
100%
(4/4)
|
100%
(23/23)
|
100%
(212/212)
|
| InAppGeoRepositoryImpl$fetchGeo$1 |
|
| InAppGeoRepositoryImpl$fetchGeo$2$geoTargetingDto$1 |
100%
(1/1)
|
|
100%
(1/1)
|
100%
(14/14)
|
| InAppGeoRepositoryImpl$getGeo$1 |
100%
(1/1)
|
|
100%
(1/1)
|
100%
(6/6)
|
| InAppGeoRepositoryImpl$getGeoFetchedStatus$1 |
100%
(1/1)
|
|
100%
(1/1)
|
100%
(5/5)
|
| Total |
100%
(8/8)
|
100%
(4/4)
|
100%
(26/26)
|
100%
(237/237)
|
package cloud.mindbox.mobile_sdk.inapp.data.repositories
import android.content.Context
import cloud.mindbox.mobile_sdk.inapp.data.managers.SessionStorageManager
import cloud.mindbox.mobile_sdk.inapp.data.mapper.InAppMapper
import cloud.mindbox.mobile_sdk.inapp.domain.interfaces.managers.GeoSerializationManager
import cloud.mindbox.mobile_sdk.inapp.domain.interfaces.repositories.InAppGeoRepository
import cloud.mindbox.mobile_sdk.inapp.domain.models.GeoError
import cloud.mindbox.mobile_sdk.inapp.domain.models.GeoFetchStatus
import cloud.mindbox.mobile_sdk.inapp.domain.models.GeoTargeting
import cloud.mindbox.mobile_sdk.managers.DbManager
import cloud.mindbox.mobile_sdk.managers.GatewayManager
import cloud.mindbox.mobile_sdk.repository.MindboxPreferences
import cloud.mindbox.mobile_sdk.utils.LoggingExceptionHandler
import cloud.mindbox.mobile_sdk.utils.Constants
import com.android.volley.TimeoutError
import kotlinx.coroutines.withTimeoutOrNull
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
internal class InAppGeoRepositoryImpl(
private val context: Context,
private val inAppMapper: InAppMapper,
private val geoSerializationManager: GeoSerializationManager,
private val sessionStorageManager: SessionStorageManager,
private val gatewayManager: GatewayManager
) : InAppGeoRepository {
private val geoMutex = Mutex()
override suspend fun fetchGeo() = geoMutex.withLock {
val state = sessionStorageManager.state
if (state.geoFetchStatus != GeoFetchStatus.GEO_NOT_FETCHED) {
return@withLock
}
val configuration = DbManager.listenConfigurations().first()
val geoTargetingDto = try {
withTimeoutOrNull(Constants.targetingFetchWaitLimit.interval) {
gatewayManager.checkGeoTargeting(configuration = configuration)
} ?: throw GeoError(TimeoutError())
} catch (error: GeoError) {
state.geoFetchStatus = GeoFetchStatus.GEO_FETCH_ERROR
throw error
}
val geoTargeting = inAppMapper.mapGeoTargetingDtoToGeoTargeting(geoTargetingDto = geoTargetingDto)
MindboxPreferences.inAppGeo =
geoSerializationManager.serializeToGeoString(geoTargeting)
state.geoFetchStatus = GeoFetchStatus.GEO_FETCH_SUCCESS
}
override fun setGeoStatus(status: GeoFetchStatus) {
sessionStorageManager.state.geoFetchStatus = status
}
override fun getGeoFetchedStatus(): GeoFetchStatus {
return LoggingExceptionHandler.runCatching(GeoFetchStatus.GEO_FETCH_ERROR) {
sessionStorageManager.state.geoFetchStatus
}
}
override fun getGeo(): GeoTargeting {
return LoggingExceptionHandler.runCatching(GeoTargeting("", "", "")) {
geoSerializationManager.deserializeToGeoTargeting(MindboxPreferences.inAppGeo)
}
}
}