Remember flows (#4533)

* Add Konsist test to ensure that the result of a function returning a flow is remembered.

* Remember flows before they are collected by state.

* Fix compilation issue

* Make isOnline a val.

* Make selectedUsers() a val.

* Make flow() a val.

* Make getUserConsent(), didAskUserConsent() and getAnalyticsId() some val.

* Remove Timeline.paginationStatus() and replace by direct access to the underlined flow.

* Simplify test

* userConsentFlow must be initialized before because it's used in observeUserConsent

* Fix test compilation
This commit is contained in:
Benoit Marty 2025-04-04 16:50:43 +02:00 committed by GitHub
parent e557ee2c77
commit a230b83e99
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
52 changed files with 221 additions and 172 deletions

View file

@ -0,0 +1,37 @@
/*
* Copyright 2025 New Vector Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
* Please see LICENSE files in the repository root for full details.
*/
package io.element.android.tests.konsist
import androidx.compose.runtime.Composable
import com.lemonappdev.konsist.api.Konsist
import com.lemonappdev.konsist.api.ext.list.withAnnotationOf
import com.lemonappdev.konsist.api.verify.assertFalse
import org.junit.Test
class KonsistFlowTest {
@Test
fun `flow must be remembered when it is collected as state`() {
// Match
// ```).collectAsState```
// and
// ```)
// .collectAsState```
val regex = "(.*)\\)(\n\\s*)*\\.collectAsState".toRegex()
Konsist
.scopeFromProject()
.functions()
.withAnnotationOf(Composable::class)
.assertFalse(
additionalMessage = "Please check that the flow is remembered when it is collected as state." +
" Only val flows can be not remembered.",
) { function ->
regex.matches(function.text)
}
}
}