Coverage Summary for Class: PushTokenKt (cloud.mindbox.mobile_sdk.pushes)
| Class |
Method, %
|
Branch, %
|
Line, %
|
Instruction, %
|
| PushTokenKt |
100%
(3/3)
|
75%
(3/4)
|
100%
(8/8)
|
97.8%
(90/92)
|
| PushTokenKt$getPushTokens$2 |
100%
(1/1)
|
83.3%
(5/6)
|
93.3%
(14/15)
|
98.9%
(92/93)
|
| PushTokenKt$getPushTokens$2$1$1 |
100%
(1/1)
|
50%
(1/2)
|
71.4%
(5/7)
|
87.8%
(65/74)
|
| PushTokenKt$toTokensMap$1$pushTokenMapType$1 |
100%
(1/1)
|
|
100%
(1/1)
|
100%
(2/2)
|
| Total |
100%
(6/6)
|
75%
(9/12)
|
90.3%
(28/31)
|
95.4%
(249/261)
|
package cloud.mindbox.mobile_sdk.pushes
import android.content.Context
import cloud.mindbox.mobile_sdk.Mindbox
import cloud.mindbox.mobile_sdk.logger.mindboxLogI
import cloud.mindbox.mobile_sdk.utils.MindboxUtils.Stopwatch
import cloud.mindbox.mobile_sdk.utils.awaitAllWithTimeout
import com.google.gson.Gson
import com.google.gson.annotations.SerializedName
import com.google.gson.reflect.TypeToken
import kotlinx.coroutines.async
import kotlinx.coroutines.withContext
private const val GET_PUSH_TOKEN_TIMEOUT = 5000L
internal data class PushToken(
val provider: String,
val token: String,
)
internal data class PrefPushToken(
@SerializedName("token") val token: String,
@SerializedName("updateDate") val updateDate: Long,
)
internal typealias PushTokenMap = Map<String, String>
internal typealias PrefPushTokenMap = Map<String, PrefPushToken>
internal fun <K, V> Map<K, V>.toPreferences(): String =
runCatching {
Gson().toJson(this)
}.getOrDefault("")
internal fun String?.toTokensMap(): PrefPushTokenMap =
runCatching {
val pushTokenMapType = object : TypeToken<PrefPushTokenMap>() {}.type
Gson().fromJson(this, pushTokenMapType) as PrefPushTokenMap
}.getOrDefault(emptyMap())
internal suspend fun getPushTokens(context: Context, previousToken: PushTokenMap): PushTokenMap =
withContext(Mindbox.mindboxScope.coroutineContext) {
val handlers = Mindbox.pushServiceHandlers
if (handlers.isEmpty()) {
return@withContext emptyMap()
}
Stopwatch.start(Stopwatch.GET_PUSH_TOKENS)
handlers
.map { handler ->
async {
runCatching {
val provider = handler.notificationProvider
val token = handler.registerToken(context, previousToken[provider])
handler.notificationProvider to token
}.getOrElse {
Mindbox.logE("Failed to get push token from provider", it)
handler.notificationProvider to null
}
}
}
.awaitAllWithTimeout(GET_PUSH_TOKEN_TIMEOUT)
.mapNotNull { (provider, token) ->
if (!token.isNullOrEmpty()) provider to token else null
}
.toMap()
.also { tokens ->
val duration = Stopwatch.stop(Stopwatch.GET_PUSH_TOKENS)
mindboxLogI("Push tokens $tokens received in $duration")
}
}