Skip to content

Commit

Permalink
Fixes remote config not refreshed until app restart
Browse files Browse the repository at this point in the history
  • Loading branch information
cmonfortep committed Nov 25, 2024
1 parent d81c9a7 commit eb8f367
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,28 @@ 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<MessageHandlerPlugin>,
private val settingsRepository: AutoconsentSettingsRepository,
private val autoconsentExceptionsRepository: AutoconsentExceptionsRepository,
private val autoconsent: AutoconsentFeature,
private val userAllowlistRepository: UserAllowListRepository,
private val unprotectedTemporary: UnprotectedTemporary,
) : Autoconsent {
) : Autoconsent, PrivacyConfigCallbackPlugin {

private lateinit var autoconsentJs: String

Expand Down Expand Up @@ -108,4 +117,8 @@ class RealAutoconsent @Inject constructor(
}
return autoconsentJs
}

override fun onPrivacyConfigDownloaded() {
settingsRepository.invalidateCache()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import kotlinx.coroutines.launch
interface AutoconsentSettingsDataStore {
var userSetting: Boolean
var firstPopupHandled: Boolean
fun invalidateCache()
}

class RealAutoconsentSettingsDataStore constructor(
Expand All @@ -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()) {
Expand Down Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>) : UnprotectedTemporary {
Expand Down

0 comments on commit eb8f367

Please sign in to comment.