Repository: corona-warn-app/cwa-app-android
File: Corona-Warn-App/src/main/java/de/rki/coronawarnapp/covidcertificate/test/core/TestCertificateRepository.kt (Kotlin)

Code before:
        certMap.values.forEach {
            when {
                it.isNotRecycled -> it.toTestCertificateWrapper(valueSets, dccValidityMeasures)
                    ?.let { tc -> certificates += tc }
                it.isRecycled -> it.toTestCertificateOrNull(valueSets, CwaCovidCertificate.State.Recycled)
                    ?.let { tc -> recycledCertificates += tc }
            }
        }

        TestCertificatesHolder(
            certificates = certificates,
            recycledCertificates = recycledCertificates.filterNotNull().toSet()
        )
    }
        .shareLatest(
            tag = TAG,
            scope = appScope
        )

    fun findCertificateDetails(containerId: TestCertificateContainerId): Flow<TestCertificate?> =
        internalData.data.map { map ->
            val container = map[containerId]
            if (container?.isNotRecycled == true) {
                container.toTestCertificateWrapper(
                    valueSetsRepository.latestTestCertificateValueSets.first(),
                    dccValidityMeasuresObserver.dccValidityMeasures.first()
                )?.testCertificate
            } else null
        }

    /**
     * Returns a flow with a set of [TestCertificate] matching the predicate [TestCertificate.isNotRecycled]
     */
    val certificates: Flow<Set<TestCertificateWrapper>> = allCertificates
        .map { it.certificates }
        .shareLatest(scope = appScope)

    /**
     * Returns a flow with a set of [TestCertificate] matching the predicate [TestCertificate.isRecycled]
     */
    val recycledCertificates: Flow<Set<TestCertificate>> = allCertificates
        .map { it.recycledCertificates }
        .shareLatest(scope = appScope)

    /**
     * Will create a new test certificate entry.
     * Automation via [de.rki.coronawarnapp.covidcertificate.test.core.execution.TestCertificateRetrievalScheduler] will kick in.
     *
     * Throws an exception if there already is a test certificate entry for this test
     * or this is not a valid test (no consent, not supported by PoC).
     */

Review comment (suggestion):
I think this can be simplified to 
```kotlin
            map[containerId].takeIf { it?.isNotRecycled == true }?.toVaccinationCertificateWrapper(
                valueSetsRepository.latestVaccinationValueSets.first(),
                dccValidityMeasuresObserver.dccValidityMeasures.first()
            )?.vaccinationCertificate
```

Code after:
        certMap.values.forEach {
            when {
                it.isNotRecycled -> it.toTestCertificateWrapper(valueSets, dccValidityMeasures)
                    ?.let { tc -> certificates += tc }
                it.isRecycled -> it.toTestCertificateOrNull(valueSets, CwaCovidCertificate.State.Recycled)
                    ?.let { tc -> recycledCertificates += tc }
            }
        }

        TestCertificatesHolder(
            certificates = certificates,
            recycledCertificates = recycledCertificates.filterNotNull().toSet()
        )
    }
        .shareLatest(
            tag = TAG,
            scope = appScope
        )

    fun findCertificateDetails(containerId: TestCertificateContainerId): Flow<TestCertificate?> =
        internalData.data.map { map ->
            map[containerId].takeIf { it?.isNotRecycled == true }?.toTestCertificateWrapper(
                valueSetsRepository.latestTestCertificateValueSets.first(),
                dccValidityMeasuresObserver.dccValidityMeasures.first()
            )?.testCertificate
        }

    /**
     * Returns a flow with a set of [TestCertificate] matching the predicate [TestCertificate.isNotRecycled]
     */
    val certificates: Flow<Set<TestCertificateWrapper>> = allCertificates
        .map { it.certificates }
        .shareLatest(scope = appScope)

    /**
     * Returns a flow with a set of [TestCertificate] matching the predicate [TestCertificate.isRecycled]
     */
    val recycledCertificates: Flow<Set<TestCertificate>> = allCertificates
        .map { it.recycledCertificates }
        .shareLatest(scope = appScope)

    /**
     * Will create a new test certificate entry.
     * Automation via [de.rki.coronawarnapp.covidcertificate.test.core.execution.TestCertificateRetrievalScheduler] will kick in.
     *
     * Throws an exception if there already is a test certificate entry for this test
     * or this is not a valid test (no consent, not supported by PoC).
     */
    suspend fun requestCertificate(test: BaseCoronaTest): TestCertificateContainer {
        Timber.tag(TAG).d("requestCertificate(test.identifier=%s)", test.identifier)