Use parameter wasLastSession

This commit is contained in:
Benoit Marty 2025-10-23 16:44:14 +02:00 committed by Benoit Marty
parent fdc64e9be7
commit dae44ccab7
2 changed files with 16 additions and 8 deletions

View file

@ -16,7 +16,6 @@ import im.vector.app.features.analytics.itf.VectorAnalyticsScreen
import im.vector.app.features.analytics.plan.SuperProperties import im.vector.app.features.analytics.plan.SuperProperties
import im.vector.app.features.analytics.plan.UserProperties import im.vector.app.features.analytics.plan.UserProperties
import io.element.android.libraries.di.annotations.AppCoroutineScope import io.element.android.libraries.di.annotations.AppCoroutineScope
import io.element.android.libraries.sessionstorage.api.SessionStore
import io.element.android.libraries.sessionstorage.api.observer.SessionListener import io.element.android.libraries.sessionstorage.api.observer.SessionListener
import io.element.android.libraries.sessionstorage.api.observer.SessionObserver import io.element.android.libraries.sessionstorage.api.observer.SessionObserver
import io.element.android.services.analytics.api.AnalyticsService import io.element.android.services.analytics.api.AnalyticsService
@ -39,7 +38,6 @@ class DefaultAnalyticsService(
@AppCoroutineScope @AppCoroutineScope
private val coroutineScope: CoroutineScope, private val coroutineScope: CoroutineScope,
private val sessionObserver: SessionObserver, private val sessionObserver: SessionObserver,
private val sessionStore: SessionStore,
) : AnalyticsService, SessionListener { ) : AnalyticsService, SessionListener {
// Cache for the store values // Cache for the store values
private val userConsent = AtomicBoolean(false) private val userConsent = AtomicBoolean(false)
@ -77,7 +75,7 @@ class DefaultAnalyticsService(
override suspend fun onSessionDeleted(userId: String, wasLastSession: Boolean) { override suspend fun onSessionDeleted(userId: String, wasLastSession: Boolean) {
// Delete the store when the last session is deleted // Delete the store when the last session is deleted
if (sessionStore.getAllSessions().isEmpty()) { if (wasLastSession) {
analyticsStore.reset() analyticsStore.reset()
} }
} }

View file

@ -16,9 +16,7 @@ import im.vector.app.features.analytics.plan.MobileScreen
import im.vector.app.features.analytics.plan.PollEnd import im.vector.app.features.analytics.plan.PollEnd
import im.vector.app.features.analytics.plan.SuperProperties import im.vector.app.features.analytics.plan.SuperProperties
import im.vector.app.features.analytics.plan.UserProperties import im.vector.app.features.analytics.plan.UserProperties
import io.element.android.libraries.sessionstorage.api.SessionStore
import io.element.android.libraries.sessionstorage.api.observer.SessionObserver import io.element.android.libraries.sessionstorage.api.observer.SessionObserver
import io.element.android.libraries.sessionstorage.test.InMemorySessionStore
import io.element.android.libraries.sessionstorage.test.observer.NoOpSessionObserver import io.element.android.libraries.sessionstorage.test.observer.NoOpSessionObserver
import io.element.android.services.analytics.impl.store.AnalyticsStore import io.element.android.services.analytics.impl.store.AnalyticsStore
import io.element.android.services.analytics.impl.store.FakeAnalyticsStore import io.element.android.services.analytics.impl.store.FakeAnalyticsStore
@ -178,10 +176,24 @@ class DefaultAnalyticsServiceTest {
coroutineScope = backgroundScope, coroutineScope = backgroundScope,
analyticsStore = store, analyticsStore = store,
) )
sut.onSessionDeleted("userId", false) sut.onSessionDeleted("userId", true)
resetLambda.assertions().isCalledOnce() resetLambda.assertions().isCalledOnce()
} }
@Test
fun `when a session is deleted, the store is not reset if it was not the last session`() = runTest {
val resetLambda = lambdaRecorder<Unit> { }
val store = FakeAnalyticsStore(
resetLambda = resetLambda,
)
val sut = createDefaultAnalyticsService(
coroutineScope = backgroundScope,
analyticsStore = store,
)
sut.onSessionDeleted("userId", false)
resetLambda.assertions().isNeverCalled()
}
@Test @Test
fun `when a session is added, nothing happen`() = runTest { fun `when a session is added, nothing happen`() = runTest {
val sut = createDefaultAnalyticsService( val sut = createDefaultAnalyticsService(
@ -260,13 +272,11 @@ class DefaultAnalyticsServiceTest {
), ),
analyticsStore: AnalyticsStore = FakeAnalyticsStore(), analyticsStore: AnalyticsStore = FakeAnalyticsStore(),
sessionObserver: SessionObserver = NoOpSessionObserver(), sessionObserver: SessionObserver = NoOpSessionObserver(),
sessionStore: SessionStore = InMemorySessionStore(),
) = DefaultAnalyticsService( ) = DefaultAnalyticsService(
analyticsProviders = analyticsProviders, analyticsProviders = analyticsProviders,
analyticsStore = analyticsStore, analyticsStore = analyticsStore,
coroutineScope = coroutineScope, coroutineScope = coroutineScope,
sessionObserver = sessionObserver, sessionObserver = sessionObserver,
sessionStore = sessionStore,
).also { ).also {
// Wait for the service to be ready // Wait for the service to be ready
delay(1) delay(1)