|
17 | 17 | package com.duckduckgo.app.browser.omnibar |
18 | 18 |
|
19 | 19 | import androidx.lifecycle.LifecycleOwner |
| 20 | +import com.duckduckgo.app.di.AppCoroutineScope |
20 | 21 | import com.duckduckgo.app.lifecycle.MainProcessLifecycleObserver |
21 | 22 | import com.duckduckgo.app.pixels.remoteconfig.AndroidBrowserConfigFeature |
22 | 23 | import com.duckduckgo.app.settings.db.SettingsDataStore |
23 | 24 | import com.duckduckgo.browser.ui.omnibar.OmnibarType |
| 25 | +import com.duckduckgo.common.utils.DispatcherProvider |
24 | 26 | import com.duckduckgo.di.scopes.AppScope |
25 | 27 | import com.squareup.anvil.annotations.ContributesMultibinding |
| 28 | +import dagger.SingleInstanceIn |
| 29 | +import kotlinx.coroutines.CoroutineScope |
| 30 | +import kotlinx.coroutines.launch |
26 | 31 | import javax.inject.Inject |
27 | 32 |
|
28 | | -/** |
29 | | - * Observes app lifecycle to ensure that if the Split Omnibar feature is disabled via remote config, |
30 | | - * and the user had selected the Split Omnibar, we revert to Single Top Omnibar. |
31 | | - * If the feature is re-enabled, we restore the user's choice. |
32 | | - */ |
33 | 33 | @ContributesMultibinding( |
34 | 34 | scope = AppScope::class, |
35 | 35 | boundType = MainProcessLifecycleObserver::class, |
36 | 36 | ) |
37 | | -class SplitOmnibarFallbackObserver @Inject constructor( |
| 37 | +@SingleInstanceIn(AppScope::class) |
| 38 | +class OmnibarFeatureRepository @Inject constructor( |
38 | 39 | private val settingsDataStore: SettingsDataStore, |
39 | 40 | private val browserFeatures: AndroidBrowserConfigFeature, |
| 41 | + private val dispatcherProvider: DispatcherProvider, |
| 42 | + @AppCoroutineScope private val coroutineScope: CoroutineScope, |
40 | 43 | ) : MainProcessLifecycleObserver { |
| 44 | + var isUnifiedOmnibarEnabled: Boolean = false |
| 45 | + private set |
| 46 | + |
| 47 | + var isSplitOmnibarEnabled: Boolean = false |
| 48 | + private set |
| 49 | + |
| 50 | + val isSplitOmnibarAvailable: Boolean |
| 51 | + get() = isUnifiedOmnibarEnabled && isSplitOmnibarEnabled |
| 52 | + |
41 | 53 | override fun onStart(owner: LifecycleOwner) { |
42 | | - super.onStart(owner) |
| 54 | + updateFeatureFlags() |
| 55 | + } |
| 56 | + |
| 57 | + fun updateFeatureFlags() { |
| 58 | + coroutineScope.launch(dispatcherProvider.io()) { |
| 59 | + isUnifiedOmnibarEnabled = browserFeatures.useUnifiedOmnibarLayout().isEnabled() |
| 60 | + isSplitOmnibarEnabled = browserFeatures.splitOmnibar().isEnabled() |
| 61 | + |
| 62 | + resetOmnibarTypeIfNecessary() |
| 63 | + } |
| 64 | + } |
43 | 65 |
|
44 | | - if (settingsDataStore.omnibarType == OmnibarType.SPLIT && |
45 | | - (!browserFeatures.useUnifiedOmnibarLayout().isEnabled() || !browserFeatures.splitOmnibar().isEnabled()) |
46 | | - ) { |
| 66 | + private fun resetOmnibarTypeIfNecessary() { |
| 67 | + if (settingsDataStore.omnibarType == OmnibarType.SPLIT && !isSplitOmnibarAvailable) { |
47 | 68 | settingsDataStore.isSplitOmnibarSelected = true |
48 | 69 | settingsDataStore.omnibarType = OmnibarType.SINGLE_TOP |
49 | | - } else if (settingsDataStore.isSplitOmnibarSelected && |
50 | | - browserFeatures.useUnifiedOmnibarLayout().isEnabled() && |
51 | | - browserFeatures.splitOmnibar().isEnabled() |
52 | | - ) { |
| 70 | + } else if (settingsDataStore.isSplitOmnibarSelected && isSplitOmnibarAvailable) { |
53 | 71 | // Restore user's choice if the feature is re-enabled |
54 | 72 | settingsDataStore.omnibarType = OmnibarType.SPLIT |
55 | 73 | settingsDataStore.isSplitOmnibarSelected = false |
|
0 commit comments