Coverage Summary for Class: EmbeddedBlockPlaceMemory (cloud.mindbox.mobile_sdk.embedded)
| Class |
Method, %
|
Branch, %
|
Line, %
|
Instruction, %
|
| EmbeddedBlockPlaceMemory |
100%
(7/7)
|
87.5%
(7/8)
|
100%
(20/20)
|
100%
(101/101)
|
| EmbeddedBlockPlaceMemory$1 |
100%
(1/1)
|
|
100%
(1/1)
|
100%
(2/2)
|
| EmbeddedBlockPlaceMemory$2 |
100%
(1/1)
|
|
100%
(1/1)
|
100%
(3/3)
|
| EmbeddedBlockPlaceMemory$3 |
100%
(1/1)
|
|
100%
(1/1)
|
100%
(2/2)
|
| EmbeddedBlockPlaceMemory$Companion |
100%
(1/1)
|
|
100%
(1/1)
|
100%
(5/5)
|
| EmbeddedBlockPlaceMemory$Companion$gson$2 |
100%
(1/1)
|
|
100%
(1/1)
|
100%
(3/3)
|
| EmbeddedBlockPlaceMemory$records$1 |
100%
(1/1)
|
75%
(9/12)
|
100%
(4/4)
|
96.6%
(56/58)
|
| EmbeddedBlockPlaceMemory$records$1$invoke$$inlined$fromJsonTyped$1 |
0%
(0/1)
|
|
| EmbeddedBlockPlaceMemory$write$1 |
100%
(1/1)
|
|
100%
(4/4)
|
100%
(24/24)
|
| EmbeddedBlockPlaceMemory$write$1$invoke$$inlined$toJsonTyped$1 |
0%
(0/1)
|
|
| Total |
87.5%
(14/16)
|
80%
(16/20)
|
100%
(33/33)
|
99%
(196/198)
|
package cloud.mindbox.mobile_sdk.embedded
import cloud.mindbox.mobile_sdk.fromJsonTyped
import cloud.mindbox.mobile_sdk.logger.mindboxLogE
import cloud.mindbox.mobile_sdk.logger.mindboxLogI
import cloud.mindbox.mobile_sdk.models.PlaceKey
import cloud.mindbox.mobile_sdk.models.Timestamp
import cloud.mindbox.mobile_sdk.models.convertToIso8601String
import cloud.mindbox.mobile_sdk.repository.MindboxPreferences
import cloud.mindbox.mobile_sdk.toJsonTyped
import cloud.mindbox.mobile_sdk.utils.loggingRunCatching
import com.google.gson.Gson
import com.google.gson.annotations.SerializedName
internal data class EmbeddedBlockPlaceRecord(
@SerializedName("rememberedAt") val rememberedAt: String,
)
internal class EmbeddedBlockPlaceMemory(
private val readRecordsJson: () -> String = { MindboxPreferences.embeddedBlockPlaceRecords },
private val writeRecordsJson: (String) -> Unit = { json -> MindboxPreferences.embeddedBlockPlaceRecords = json },
private val now: () -> Timestamp = { Timestamp(System.currentTimeMillis()) },
) {
fun hasShownContent(place: PlaceKey): Boolean = place.value in records()
fun rememberShownContent(place: PlaceKey) {
val records = records()
if (place.value in records) return
val record = EmbeddedBlockPlaceRecord(rememberedAt = now().convertToIso8601String())
if (!write(records + (place.value to record))) {
mindboxLogE(
"[EmbeddedBlock] Place '$place': could not write its record — new automatic " +
"blocks here start hidden",
)
return
}
mindboxLogI(
"[EmbeddedBlock] Place '$place' showed content — remembered, new automatic blocks here " +
"start with a placeholder",
)
}
fun forgetPlace(place: PlaceKey) {
val records = records()
if (place.value !in records) return
if (!write(records - place.value)) return
mindboxLogI(
"[EmbeddedBlock] Place '$place' came out empty — forgotten, new automatic blocks " +
"here start hidden",
)
}
private fun records(): Map<String, EmbeddedBlockPlaceRecord> =
loggingRunCatching(defaultValue = emptyMap()) {
val json = readRecordsJson().ifEmpty { return@loggingRunCatching emptyMap() }
cache?.takeIf { (cachedJson, _) -> cachedJson == json }?.second
?: (gson.fromJsonTyped<Map<String, EmbeddedBlockPlaceRecord>>(json) ?: emptyMap())
.also { parsed -> cache = json to parsed }
}
private fun write(records: Map<String, EmbeddedBlockPlaceRecord>): Boolean =
loggingRunCatching(defaultValue = false) {
val json = gson.toJsonTyped(records)
writeRecordsJson(json)
cache = json to records
true
}
private companion object {
private val gson by lazy { Gson() }
private var cache: Pair<String, Map<String, EmbeddedBlockPlaceRecord>>? = null
}
}