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

@ -43,6 +43,10 @@ class DefaultAnalyticsService @Inject constructor(
// Cache for the properties to send
private var pendingUserProperties: UserProperties? = null
override val userConsentFlow: Flow<Boolean> = analyticsStore.userConsentFlow
override val didAskUserConsentFlow: Flow<Boolean> = analyticsStore.didAskUserConsentFlow
override val analyticsIdFlow: Flow<String> = analyticsStore.analyticsIdFlow
init {
observeUserConsent()
observeSessions()
@ -52,19 +56,11 @@ class DefaultAnalyticsService @Inject constructor(
return analyticsProviders
}
override fun getUserConsent(): Flow<Boolean> {
return analyticsStore.userConsentFlow
}
override suspend fun setUserConsent(userConsent: Boolean) {
Timber.tag(analyticsTag.value).d("setUserConsent($userConsent)")
analyticsStore.setUserConsent(userConsent)
}
override fun didAskUserConsent(): Flow<Boolean> {
return analyticsStore.didAskUserConsentFlow
}
override suspend fun setDidAskUserConsent() {
Timber.tag(analyticsTag.value).d("setDidAskUserConsent()")
analyticsStore.setDidAskUserConsent()
@ -74,10 +70,6 @@ class DefaultAnalyticsService @Inject constructor(
analyticsStore.setDidAskUserConsent(false)
}
override fun getAnalyticsId(): Flow<String> {
return analyticsStore.analyticsIdFlow
}
override suspend fun setAnalyticsId(analyticsId: String) {
Timber.tag(analyticsTag.value).d("setAnalyticsId($analyticsId)")
analyticsStore.setAnalyticsId(analyticsId)
@ -93,7 +85,7 @@ class DefaultAnalyticsService @Inject constructor(
}
private fun observeUserConsent() {
getUserConsent()
userConsentFlow
.onEach { consent ->
Timber.tag(analyticsTag.value).d("User consent updated to $consent")
userConsent.set(consent)

View file

@ -132,10 +132,10 @@ class DefaultAnalyticsServiceTest {
analyticsStore = store,
)
assertThat(store.userConsentFlow.first()).isFalse()
assertThat(sut.getUserConsent().first()).isFalse()
assertThat(sut.userConsentFlow.first()).isFalse()
sut.setUserConsent(true)
assertThat(store.userConsentFlow.first()).isTrue()
assertThat(sut.getUserConsent().first()).isTrue()
assertThat(sut.userConsentFlow.first()).isTrue()
}
@Test
@ -146,10 +146,10 @@ class DefaultAnalyticsServiceTest {
analyticsStore = store,
)
assertThat(store.analyticsIdFlow.first()).isEqualTo("")
assertThat(sut.getAnalyticsId().first()).isEqualTo("")
assertThat(sut.analyticsIdFlow.first()).isEqualTo("")
sut.setAnalyticsId(AN_ID)
assertThat(store.analyticsIdFlow.first()).isEqualTo(AN_ID)
assertThat(sut.getAnalyticsId().first()).isEqualTo(AN_ID)
assertThat(sut.analyticsIdFlow.first()).isEqualTo(AN_ID)
}
@Test
@ -160,10 +160,10 @@ class DefaultAnalyticsServiceTest {
analyticsStore = store,
)
assertThat(store.didAskUserConsentFlow.first()).isFalse()
assertThat(sut.didAskUserConsent().first()).isFalse()
assertThat(sut.didAskUserConsentFlow.first()).isFalse()
sut.setDidAskUserConsent()
assertThat(store.didAskUserConsentFlow.first()).isTrue()
assertThat(sut.didAskUserConsent().first()).isTrue()
assertThat(sut.didAskUserConsentFlow.first()).isTrue()
}
@Test