lifting the global scope to a provide to allow for unit testing the analytics impl

This commit is contained in:
Adam Brown 2022-02-15 12:26:08 +00:00
parent 580ecc9c44
commit f1f8f51805
3 changed files with 37 additions and 6 deletions

View File

@ -0,0 +1,23 @@
/*
* Copyright (c) 2022 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.app.core.di
import javax.inject.Qualifier
@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class NamedGlobalScope

View File

@ -46,6 +46,7 @@ import im.vector.app.features.ui.SharedPreferencesUiStateRepository
import im.vector.app.features.ui.UiStateRepository
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.SupervisorJob
import org.matrix.android.sdk.api.Matrix
import org.matrix.android.sdk.api.auth.AuthenticationService
@ -147,4 +148,11 @@ object VectorStaticModule {
fun providesCoroutineDispatchers(): CoroutineDispatchers {
return CoroutineDispatchers(io = Dispatchers.IO, computation = Dispatchers.Default)
}
@Suppress("EXPERIMENTAL_API_USAGE")
@Provides
@NamedGlobalScope
fun providesGlobalScope(): CoroutineScope {
return GlobalScope
}
}

View File

@ -22,13 +22,14 @@ import com.posthog.android.PostHog
import com.posthog.android.Properties
import im.vector.app.BuildConfig
import im.vector.app.config.analyticsConfig
import im.vector.app.core.di.NamedGlobalScope
import im.vector.app.features.analytics.VectorAnalytics
import im.vector.app.features.analytics.itf.VectorAnalyticsEvent
import im.vector.app.features.analytics.itf.VectorAnalyticsScreen
import im.vector.app.features.analytics.log.analyticsTag
import im.vector.app.features.analytics.plan.UserProperties
import im.vector.app.features.analytics.store.AnalyticsStore
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
@ -42,7 +43,8 @@ private val IGNORED_OPTIONS: Options? = null
@Singleton
class DefaultVectorAnalytics @Inject constructor(
private val context: Context,
private val analyticsStore: AnalyticsStore
private val analyticsStore: AnalyticsStore,
@NamedGlobalScope private val globalScope: CoroutineScope
) : VectorAnalytics {
private var posthog: PostHog? = null
@ -88,7 +90,6 @@ class DefaultVectorAnalytics @Inject constructor(
createAnalyticsClient()
}
@Suppress("EXPERIMENTAL_API_USAGE")
private fun observeAnalyticsId() {
getAnalyticsId()
.onEach { id ->
@ -96,7 +97,7 @@ class DefaultVectorAnalytics @Inject constructor(
analyticsId = id
identifyPostHog()
}
.launchIn(GlobalScope)
.launchIn(globalScope)
}
private fun identifyPostHog() {
@ -110,7 +111,6 @@ class DefaultVectorAnalytics @Inject constructor(
}
}
@Suppress("EXPERIMENTAL_API_USAGE")
private fun observeUserConsent() {
getUserConsent()
.onEach { consent ->
@ -118,7 +118,7 @@ class DefaultVectorAnalytics @Inject constructor(
userConsent = consent
optOutPostHog()
}
.launchIn(GlobalScope)
.launchIn(globalScope)
}
private fun optOutPostHog() {