Repository: corona-warn-app/cwa-app-android
File: Corona-Warn-App/src/main/java/de/rki/coronawarnapp/datadonation/analytics/modules/keysubmission/AnalyticsKeySubmissionCollector.kt (Kotlin)

Code before:
import de.rki.coronawarnapp.datadonation.analytics.storage.AnalyticsSettings
import de.rki.coronawarnapp.risk.RiskState
import de.rki.coronawarnapp.risk.storage.RiskLevelStorage
import de.rki.coronawarnapp.server.protocols.internal.ppdd.PpaData
import de.rki.coronawarnapp.util.TimeAndDateExtensions.toLocalDateUtc
import de.rki.coronawarnapp.util.TimeStamper
import kotlinx.coroutines.flow.first
import org.joda.time.Duration
import javax.inject.Inject

class AnalyticsKeySubmissionCollector @Inject constructor(
    private val timeStamper: TimeStamper,
    private val analyticsSettings: AnalyticsSettings,
    private val pcrStorage: AnalyticsPCRKeySubmissionStorage,
    private val raStorage: AnalyticsRAKeySubmissionStorage,
    private val riskLevelStorage: RiskLevelStorage,
) {

    fun reset(type: CoronaTest.Type) {
        type.storage.clear()
    }

    fun reportPositiveTestResultReceived(type: CoronaTest.Type) {
        if (disabled) return
        if (type.storage.testResultReceivedAt.value > 0) return
        type.storage.testResultReceivedAt.update { timeStamper.nowUTC.millis }
    }

    suspend fun reportTestRegistered(type: CoronaTest.Type) {
        if (disabled) return

        val testRegisteredAt = timeStamper.nowUTC
        type.storage.testRegisteredAt.update { testRegisteredAt.millis }

        val lastResult = riskLevelStorage
            .latestAndLastSuccessfulCombinedEwPtRiskLevelResult
            .first()
            .lastCalculated

        if (lastResult.ewRiskLevelResult.riskState == RiskState.INCREASED_RISK) {
            riskLevelStorage.allEwRiskLevelResults
                .first()
                .getLastChangeToHighEwRiskBefore(testRegisteredAt)
                ?.let {
                    val hours = Duration(
                        it,
                        testRegisteredAt
                    ).standardHours.toInt()
                    type.storage.ewHoursSinceHighRiskWarningAtTestRegistration.update {
                        hours
                    }

Review comment (suggestion):
Suggestion: Maybe do something like the code below to make it a bit more understandable? Or add a comment?

```kotlin
val alreadySet = type.storage.testResultReceivedAt.value != -1
if(!alreadySet){
    type.storage.testResultReceivedAt.update { timeStamper.nowUTC.millis }
}

```

Code after:
import de.rki.coronawarnapp.datadonation.analytics.storage.AnalyticsSettings
import de.rki.coronawarnapp.risk.RiskState
import de.rki.coronawarnapp.risk.storage.RiskLevelStorage
import de.rki.coronawarnapp.server.protocols.internal.ppdd.PpaData
import de.rki.coronawarnapp.util.TimeAndDateExtensions.toLocalDateUtc
import de.rki.coronawarnapp.util.TimeStamper
import kotlinx.coroutines.flow.first
import org.joda.time.Duration
import javax.inject.Inject

class AnalyticsKeySubmissionCollector @Inject constructor(
    private val timeStamper: TimeStamper,
    private val analyticsSettings: AnalyticsSettings,
    private val pcrStorage: AnalyticsPCRKeySubmissionStorage,
    private val raStorage: AnalyticsRAKeySubmissionStorage,
    private val riskLevelStorage: RiskLevelStorage,
) {

    fun reset(type: CoronaTest.Type) {
        type.storage.clear()
    }

    fun reportPositiveTestResultReceived(type: CoronaTest.Type) {
        if (disabled) return
        // do not overwrite once set
        if (type.storage.testResultReceivedAt.value > 0) return
        type.storage.testResultReceivedAt.update { timeStamper.nowUTC.millis }
    }

    suspend fun reportTestRegistered(type: CoronaTest.Type) {
        if (disabled) return

        val testRegisteredAt = timeStamper.nowUTC
        type.storage.testRegisteredAt.update { testRegisteredAt.millis }

        val lastResult = riskLevelStorage
            .latestAndLastSuccessfulCombinedEwPtRiskLevelResult
            .first()
            .lastCalculated

        if (lastResult.ewRiskLevelResult.riskState == RiskState.INCREASED_RISK) {
            riskLevelStorage.allEwRiskLevelResults
                .first()
                .getLastChangeToHighEwRiskBefore(testRegisteredAt)
                ?.let {
                    val hours = Duration(
                        it,
                        testRegisteredAt
                    ).standardHours.toInt()
                    type.storage.ewHoursSinceHighRiskWarningAtTestRegistration.update {
                        hours