Consent thread safe
This commit is contained in:
parent
09c2c3dea1
commit
efb132c14f
1 changed files with 14 additions and 16 deletions
|
|
@ -33,6 +33,7 @@ import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.coroutines.flow.launchIn
|
import kotlinx.coroutines.flow.launchIn
|
||||||
import kotlinx.coroutines.flow.onEach
|
import kotlinx.coroutines.flow.onEach
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@SingleIn(AppScope::class)
|
@SingleIn(AppScope::class)
|
||||||
|
|
@ -45,7 +46,7 @@ class DefaultAnalyticsService @Inject constructor(
|
||||||
private val sessionObserver: SessionObserver,
|
private val sessionObserver: SessionObserver,
|
||||||
) : AnalyticsService, SessionListener {
|
) : AnalyticsService, SessionListener {
|
||||||
// Cache for the store values
|
// Cache for the store values
|
||||||
private var userConsent: Boolean? = null
|
private val userConsent = AtomicBoolean(false)
|
||||||
|
|
||||||
// Cache for the properties to send
|
// Cache for the properties to send
|
||||||
private var pendingUserProperties: UserProperties? = null
|
private var pendingUserProperties: UserProperties? = null
|
||||||
|
|
@ -104,7 +105,7 @@ class DefaultAnalyticsService @Inject constructor(
|
||||||
getUserConsent()
|
getUserConsent()
|
||||||
.onEach { consent ->
|
.onEach { consent ->
|
||||||
Timber.tag(analyticsTag.value).d("User consent updated to $consent")
|
Timber.tag(analyticsTag.value).d("User consent updated to $consent")
|
||||||
userConsent = consent
|
userConsent.set(consent)
|
||||||
initOrStop()
|
initOrStop()
|
||||||
}
|
}
|
||||||
.launchIn(coroutineScope)
|
.launchIn(coroutineScope)
|
||||||
|
|
@ -115,36 +116,33 @@ class DefaultAnalyticsService @Inject constructor(
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun initOrStop() {
|
private fun initOrStop() {
|
||||||
userConsent?.let { userConsent ->
|
if (userConsent.get()) {
|
||||||
when (userConsent) {
|
analyticsProviders.onEach { it.init() }
|
||||||
true -> {
|
pendingUserProperties?.let {
|
||||||
analyticsProviders.onEach { it.init() }
|
analyticsProviders.onEach { provider -> provider.updateUserProperties(it) }
|
||||||
pendingUserProperties?.let {
|
pendingUserProperties = null
|
||||||
analyticsProviders.onEach { provider -> provider.updateUserProperties(it) }
|
|
||||||
pendingUserProperties = null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
false -> analyticsProviders.onEach { it.stop() }
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
analyticsProviders.onEach { it.stop() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun capture(event: VectorAnalyticsEvent) {
|
override fun capture(event: VectorAnalyticsEvent) {
|
||||||
Timber.tag(analyticsTag.value).d("capture($event)")
|
Timber.tag(analyticsTag.value).d("capture($event)")
|
||||||
if (userConsent == true) {
|
if (userConsent.get()) {
|
||||||
analyticsProviders.onEach { it.capture(event) }
|
analyticsProviders.onEach { it.capture(event) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun screen(screen: VectorAnalyticsScreen) {
|
override fun screen(screen: VectorAnalyticsScreen) {
|
||||||
Timber.tag(analyticsTag.value).d("screen($screen)")
|
Timber.tag(analyticsTag.value).d("screen($screen)")
|
||||||
if (userConsent == true) {
|
if (userConsent.get()) {
|
||||||
analyticsProviders.onEach { it.screen(screen) }
|
analyticsProviders.onEach { it.screen(screen) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun updateUserProperties(userProperties: UserProperties) {
|
override fun updateUserProperties(userProperties: UserProperties) {
|
||||||
if (userConsent == true) {
|
if (userConsent.get()) {
|
||||||
analyticsProviders.onEach { it.updateUserProperties(userProperties) }
|
analyticsProviders.onEach { it.updateUserProperties(userProperties) }
|
||||||
} else {
|
} else {
|
||||||
pendingUserProperties = userProperties
|
pendingUserProperties = userProperties
|
||||||
|
|
@ -152,7 +150,7 @@ class DefaultAnalyticsService @Inject constructor(
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun trackError(throwable: Throwable) {
|
override fun trackError(throwable: Throwable) {
|
||||||
if (userConsent == true) {
|
if (userConsent.get()) {
|
||||||
analyticsProviders.onEach { it.trackError(throwable) }
|
analyticsProviders.onEach { it.trackError(throwable) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue