|
| 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.app.browser.omnibar |
| 18 | + |
| 19 | +import androidx.lifecycle.LifecycleOwner |
| 20 | +import com.duckduckgo.app.lifecycle.MainProcessLifecycleObserver |
| 21 | +import com.duckduckgo.app.pixels.remoteconfig.AndroidBrowserConfigFeature |
| 22 | +import com.duckduckgo.app.settings.db.SettingsDataStore |
| 23 | +import com.duckduckgo.browser.ui.omnibar.OmnibarType |
| 24 | +import com.duckduckgo.di.scopes.AppScope |
| 25 | +import com.squareup.anvil.annotations.ContributesMultibinding |
| 26 | +import javax.inject.Inject |
| 27 | + |
| 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 | +@ContributesMultibinding( |
| 34 | + scope = AppScope::class, |
| 35 | + boundType = MainProcessLifecycleObserver::class, |
| 36 | +) |
| 37 | +class SplitOmnibarFallbackObserver @Inject constructor( |
| 38 | + private val settingsDataStore: SettingsDataStore, |
| 39 | + private val browserFeatures: AndroidBrowserConfigFeature, |
| 40 | +) : MainProcessLifecycleObserver { |
| 41 | + override fun onStart(owner: LifecycleOwner) { |
| 42 | + super.onStart(owner) |
| 43 | + |
| 44 | + if (settingsDataStore.omnibarType == OmnibarType.SPLIT && |
| 45 | + (!browserFeatures.useUnifiedOmnibarLayout().isEnabled() || !browserFeatures.splitOmnibar().isEnabled()) |
| 46 | + ) { |
| 47 | + settingsDataStore.isSplitOmnibarSelected = true |
| 48 | + settingsDataStore.omnibarType = OmnibarType.SINGLE_TOP |
| 49 | + } else if (settingsDataStore.isSplitOmnibarSelected && |
| 50 | + browserFeatures.useUnifiedOmnibarLayout().isEnabled() && |
| 51 | + browserFeatures.splitOmnibar().isEnabled() |
| 52 | + ) { |
| 53 | + // Restore user's choice if the feature is re-enabled |
| 54 | + settingsDataStore.omnibarType = OmnibarType.SPLIT |
| 55 | + settingsDataStore.isSplitOmnibarSelected = false |
| 56 | + } |
| 57 | + } |
| 58 | +} |
0 commit comments