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 22, 2024
1 parent 34863e5 commit 50e97b3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,29 @@ 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 timber.log.Timber
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 +118,8 @@ class RealAutoconsent @Inject constructor(
}
return autoconsentJs
}

override fun onPrivacyConfigDownloaded() {
settingsRepository.refresh()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ import android.content.SharedPreferences
import androidx.core.content.edit
import com.duckduckgo.autoconsent.impl.remoteconfig.AutoconsentFeature
import com.duckduckgo.common.utils.DispatcherProvider
import com.duckduckgo.common.utils.checkMainThread
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import timber.log.Timber

interface AutoconsentSettingsDataStore {
var userSetting: Boolean
var firstPopupHandled: Boolean
fun refresh()
}

class RealAutoconsentSettingsDataStore constructor(
Expand All @@ -38,7 +41,16 @@ 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) {
checkMainThread()
_defaultValue = autoconsentFeature.onByDefault().isEnabled()
}
return _defaultValue!!
}

init {
appCoroutineScope.launch(dispatcherProvider.io()) {
Expand Down Expand Up @@ -68,6 +80,13 @@ class RealAutoconsentSettingsDataStore constructor(
}
}

override fun refresh() {
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

0 comments on commit 50e97b3

Please sign in to comment.