|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 DuckDuckGo |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.duckduckgo.common.ui.experiments.visual.store |
| 18 | + |
| 19 | +import com.duckduckgo.app.di.AppCoroutineScope |
| 20 | +import com.duckduckgo.common.ui.experiments.visual.ExperimentalUIThemingFeature |
| 21 | +import com.duckduckgo.common.utils.DispatcherProvider |
| 22 | +import com.duckduckgo.di.scopes.AppScope |
| 23 | +import com.duckduckgo.feature.toggles.api.FeatureTogglesInventory |
| 24 | +import com.duckduckgo.privacy.config.api.PrivacyConfigCallbackPlugin |
| 25 | +import com.squareup.anvil.annotations.ContributesBinding |
| 26 | +import com.squareup.anvil.annotations.ContributesMultibinding |
| 27 | +import dagger.SingleInstanceIn |
| 28 | +import javax.inject.Inject |
| 29 | +import kotlinx.coroutines.CoroutineScope |
| 30 | +import kotlinx.coroutines.flow.StateFlow |
| 31 | +import kotlinx.coroutines.runBlocking |
| 32 | +import kotlinx.coroutines.sync.Mutex |
| 33 | +import kotlinx.coroutines.sync.withLock |
| 34 | +import kotlinx.coroutines.withContext |
| 35 | + |
| 36 | +/** |
| 37 | + * Original reason for creating this lazy provider is in https://app.asana.com/1/137249556945/task/1210355090527279/comment/1210422281011749?focus=true. |
| 38 | + * |
| 39 | + * Provides [VisualDesignExperimentDataStoreImpl] with an option to initialize it on a background thread |
| 40 | + * to avoid blocking the main thread during app startup. |
| 41 | + * |
| 42 | + * The data store performs synchronous checks of feature flags and experiments during initialization |
| 43 | + * to correctly set up its state flows. This is essential, as the state is used to determine the app’s main |
| 44 | + * theme and must be ready before any themeable activity launches to prevent flickering or incorrect |
| 45 | + * theming. |
| 46 | + * |
| 47 | + * Previously, the first accessor (e.g., Dagger initializer or theme manager) would block the main |
| 48 | + * thread while the store initialized. Offloading this work to a worker thread eliminates that risk. |
| 49 | + * |
| 50 | + * This provider is used in [com.duckduckgo.app.launch.LaunchBridgeActivity] to trigger initialization |
| 51 | + * while the splash screen is shown, ensuring the store is ready for synchronous access afterward. |
| 52 | + * |
| 53 | + * Access before the splash screen ends is not expected. A debug-only assertion guards |
| 54 | + * against premature access, allowing issues to be caught during development without impacting release builds |
| 55 | + * (in case there are flows we've not considered or tested yet but can happen in production). |
| 56 | + */ |
| 57 | +@ContributesBinding( |
| 58 | + scope = AppScope::class, |
| 59 | + boundType = VisualDesignExperimentDataStore::class, |
| 60 | +) |
| 61 | +@ContributesBinding( |
| 62 | + scope = AppScope::class, |
| 63 | + boundType = VisualDesignExperimentDataStoreInitializer::class, |
| 64 | +) |
| 65 | +@ContributesMultibinding( |
| 66 | + scope = AppScope::class, |
| 67 | + boundType = PrivacyConfigCallbackPlugin::class, |
| 68 | +) |
| 69 | +@SingleInstanceIn(scope = AppScope::class) |
| 70 | +class VisualDesignExperimentDataStoreLazyProvider @Inject constructor( |
| 71 | + @AppCoroutineScope private val appCoroutineScope: CoroutineScope, |
| 72 | + private val experimentalUIThemingFeature: ExperimentalUIThemingFeature, |
| 73 | + private val featureTogglesInventory: FeatureTogglesInventory, |
| 74 | + private val dispatcherProvider: DispatcherProvider, |
| 75 | + private val visualDesignExperimentDataStoreImplFactory: VisualDesignExperimentDataStoreImplFactory, |
| 76 | +) : VisualDesignExperimentDataStoreInitializer, VisualDesignExperimentDataStore, PrivacyConfigCallbackPlugin { |
| 77 | + |
| 78 | + private val initMutex = Mutex() |
| 79 | + |
| 80 | + private var _store: VisualDesignExperimentDataStoreImpl? = null |
| 81 | + |
| 82 | + private val store: VisualDesignExperimentDataStore |
| 83 | + get() { |
| 84 | + val ref = _store |
| 85 | + assert(ref != null) { "VisualDesignExperimentDataStore is not initialized." } |
| 86 | + return ref ?: runBlocking { initialize() } |
| 87 | + } |
| 88 | + |
| 89 | + override suspend fun initialize(): VisualDesignExperimentDataStore = initMutex.withLock { |
| 90 | + suspend fun createStore(): VisualDesignExperimentDataStoreImpl = withContext(dispatcherProvider.io()) { |
| 91 | + visualDesignExperimentDataStoreImplFactory.create( |
| 92 | + appCoroutineScope = appCoroutineScope, |
| 93 | + experimentalUIThemingFeature = experimentalUIThemingFeature, |
| 94 | + featureTogglesInventory = featureTogglesInventory, |
| 95 | + ) |
| 96 | + } |
| 97 | + |
| 98 | + _store ?: createStore().also { _store = it } |
| 99 | + } |
| 100 | + |
| 101 | + override fun onPrivacyConfigDownloaded() { |
| 102 | + // store fetches latest flags on initialization, so updates are only needed if the store has already been initialized before |
| 103 | + _store?.onPrivacyConfigDownloaded() |
| 104 | + } |
| 105 | + |
| 106 | + override val isExperimentEnabled: StateFlow<Boolean> |
| 107 | + get() = store.isExperimentEnabled |
| 108 | + override val isDuckAIPoCEnabled: StateFlow<Boolean> |
| 109 | + get() = store.isDuckAIPoCEnabled |
| 110 | + override val anyConflictingExperimentEnabled: StateFlow<Boolean> |
| 111 | + get() = store.anyConflictingExperimentEnabled |
| 112 | + override fun changeExperimentFlagPreference(enabled: Boolean) = store.changeExperimentFlagPreference(enabled) |
| 113 | + override fun changeDuckAIPoCFlagPreference(enabled: Boolean) = store.changeDuckAIPoCFlagPreference(enabled) |
| 114 | +} |
0 commit comments