diff --git a/autoconsent/autoconsent-impl/src/main/java/com/duckduckgo/autoconsent/impl/RealAutoconsent.kt b/autoconsent/autoconsent-impl/src/main/java/com/duckduckgo/autoconsent/impl/RealAutoconsent.kt index 772565dd45ef..4e8457910f77 100644 --- a/autoconsent/autoconsent-impl/src/main/java/com/duckduckgo/autoconsent/impl/RealAutoconsent.kt +++ b/autoconsent/autoconsent-impl/src/main/java/com/duckduckgo/autoconsent/impl/RealAutoconsent.kt @@ -28,11 +28,20 @@ import com.duckduckgo.autoconsent.impl.remoteconfig.AutoconsentFeature import com.duckduckgo.autoconsent.impl.store.AutoconsentSettingsRepository import com.duckduckgo.common.utils.plugins.PluginPoint import com.duckduckgo.di.scopes.AppScope +import com.duckduckgo.privacy.config.api.PrivacyConfigCallbackPlugin import com.duckduckgo.privacy.config.api.UnprotectedTemporary import com.squareup.anvil.annotations.ContributesBinding +import com.squareup.anvil.annotations.ContributesMultibinding import javax.inject.Inject -@ContributesBinding(AppScope::class) +@ContributesBinding( + scope = AppScope::class, + boundType = Autoconsent::class, +) +@ContributesMultibinding( + scope = AppScope::class, + boundType = PrivacyConfigCallbackPlugin::class, +) class RealAutoconsent @Inject constructor( private val messageHandlerPlugins: PluginPoint, private val settingsRepository: AutoconsentSettingsRepository, @@ -40,7 +49,7 @@ class RealAutoconsent @Inject constructor( private val autoconsent: AutoconsentFeature, private val userAllowlistRepository: UserAllowListRepository, private val unprotectedTemporary: UnprotectedTemporary, -) : Autoconsent { +) : Autoconsent, PrivacyConfigCallbackPlugin { private lateinit var autoconsentJs: String @@ -108,4 +117,8 @@ class RealAutoconsent @Inject constructor( } return autoconsentJs } + + override fun onPrivacyConfigDownloaded() { + settingsRepository.invalidateCache() + } } diff --git a/autoconsent/autoconsent-impl/src/main/java/com/duckduckgo/autoconsent/impl/store/AutoconsentSettingsDataStore.kt b/autoconsent/autoconsent-impl/src/main/java/com/duckduckgo/autoconsent/impl/store/AutoconsentSettingsDataStore.kt index a58f5aa57a3d..8b1a49145ae6 100644 --- a/autoconsent/autoconsent-impl/src/main/java/com/duckduckgo/autoconsent/impl/store/AutoconsentSettingsDataStore.kt +++ b/autoconsent/autoconsent-impl/src/main/java/com/duckduckgo/autoconsent/impl/store/AutoconsentSettingsDataStore.kt @@ -27,6 +27,7 @@ import kotlinx.coroutines.launch interface AutoconsentSettingsDataStore { var userSetting: Boolean var firstPopupHandled: Boolean + fun invalidateCache() } class RealAutoconsentSettingsDataStore constructor( @@ -38,7 +39,15 @@ class RealAutoconsentSettingsDataStore constructor( private val preferences: SharedPreferences by lazy { context.getSharedPreferences(FILENAME, Context.MODE_PRIVATE) } private var cachedInternalUserSetting: Boolean? = null - private val defaultValue: Boolean by lazy { autoconsentFeature.onByDefault().isEnabled() } + + private var _defaultValue: Boolean? = null + private val defaultValue: Boolean + get() { + if (_defaultValue == null) { + _defaultValue = autoconsentFeature.onByDefault().isEnabled() + } + return _defaultValue!! + } init { appCoroutineScope.launch(dispatcherProvider.io()) { @@ -68,6 +77,13 @@ class RealAutoconsentSettingsDataStore constructor( } } + override fun invalidateCache() { + appCoroutineScope.launch(dispatcherProvider.io()) { + _defaultValue = autoconsentFeature.onByDefault().isEnabled() + cachedInternalUserSetting = null // invalidate cache + } + } + companion object { private const val FILENAME = "com.duckduckgo.autoconsent.store.settings" private const val AUTOCONSENT_USER_SETTING = "AutoconsentUserSetting" diff --git a/autoconsent/autoconsent-impl/src/test/java/com/duckduckgo/autoconsent/impl/Fakes.kt b/autoconsent/autoconsent-impl/src/test/java/com/duckduckgo/autoconsent/impl/Fakes.kt index 93e4a7bccdf8..b5d556b7628d 100644 --- a/autoconsent/autoconsent-impl/src/test/java/com/duckduckgo/autoconsent/impl/Fakes.kt +++ b/autoconsent/autoconsent-impl/src/test/java/com/duckduckgo/autoconsent/impl/Fakes.kt @@ -54,6 +54,7 @@ class FakeMessageHandlerPlugin : MessageHandlerPlugin { class FakeSettingsRepository : AutoconsentSettingsRepository { override var userSetting: Boolean = false override var firstPopupHandled: Boolean = false + override fun invalidateCache() {} } class FakeUnprotected(private val exceptionList: List) : UnprotectedTemporary {