Skip to content

Commit 571c354

Browse files
committed
Add OmnibarDataStore
1 parent b9e3eac commit 571c354

File tree

3 files changed

+113
-2
lines changed

3 files changed

+113
-2
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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.datastore
18+
19+
import androidx.datastore.core.DataStore
20+
import androidx.datastore.preferences.core.Preferences
21+
import androidx.datastore.preferences.core.edit
22+
import androidx.datastore.preferences.core.stringPreferencesKey
23+
import com.duckduckgo.app.browser.omnibar.datastore.SharedPreferencesOmnibarDataStore.Keys.KEY_OMNIBAR_POSITION
24+
import com.duckduckgo.app.di.Omnibar
25+
import com.duckduckgo.browser.ui.omnibar.OmnibarPosition
26+
import com.duckduckgo.di.scopes.AppScope
27+
import com.squareup.anvil.annotations.ContributesBinding
28+
import dagger.SingleInstanceIn
29+
import kotlinx.coroutines.flow.Flow
30+
import kotlinx.coroutines.flow.first
31+
import kotlinx.coroutines.flow.map
32+
import kotlinx.coroutines.runBlocking
33+
import javax.inject.Inject
34+
35+
interface OmnibarDataStore {
36+
suspend fun setOmnibarPosition(omnibarPosition: OmnibarPosition)
37+
val omnibarPositionFlow: Flow<OmnibarPosition>
38+
39+
val omnibarPosition: OmnibarPosition
40+
get() = runBlocking {
41+
omnibarPositionFlow.first()
42+
}
43+
}
44+
45+
@ContributesBinding(AppScope::class)
46+
@SingleInstanceIn(AppScope::class)
47+
class SharedPreferencesOmnibarDataStore @Inject constructor(
48+
@Omnibar private val store: DataStore<Preferences>,
49+
) : OmnibarDataStore {
50+
51+
private object Keys {
52+
val KEY_OMNIBAR_POSITION = stringPreferencesKey(name = "KEY_OMNIBAR_POSITION")
53+
}
54+
55+
override suspend fun setOmnibarPosition(omnibarPosition: OmnibarPosition) {
56+
store.edit { it[KEY_OMNIBAR_POSITION] = omnibarPosition.name }
57+
}
58+
59+
override val omnibarPositionFlow: Flow<OmnibarPosition> = store.data.map { preferences ->
60+
val positionName = preferences[KEY_OMNIBAR_POSITION] ?: OmnibarPosition.TOP.name
61+
OmnibarPosition.valueOf(positionName)
62+
}
63+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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.di
18+
19+
import android.content.Context
20+
import androidx.datastore.core.DataStore
21+
import androidx.datastore.preferences.core.Preferences
22+
import androidx.datastore.preferences.preferencesDataStore
23+
import com.duckduckgo.di.scopes.AppScope
24+
import com.squareup.anvil.annotations.ContributesTo
25+
import dagger.Module
26+
import dagger.Provides
27+
import javax.inject.Qualifier
28+
29+
@ContributesTo(AppScope::class)
30+
@Module
31+
object OmnibarDataStoreModule {
32+
33+
private val Context.omnibarDataStore: DataStore<Preferences> by preferencesDataStore(
34+
name = "omnibar_feature",
35+
)
36+
37+
@Provides
38+
@Omnibar
39+
fun provideOmnibarDataStore(context: Context): DataStore<Preferences> = context.omnibarDataStore
40+
}
41+
42+
@Qualifier
43+
internal annotation class Omnibar

app/src/main/java/com/duckduckgo/app/settings/db/SettingsDataStore.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package com.duckduckgo.app.settings.db
1919
import android.content.Context
2020
import android.content.SharedPreferences
2121
import androidx.core.content.edit
22+
import com.duckduckgo.app.browser.omnibar.datastore.OmnibarDataStore
2223
import com.duckduckgo.app.fire.fireproofwebsite.ui.AutomaticFireproofSetting
2324
import com.duckduckgo.app.fire.fireproofwebsite.ui.AutomaticFireproofSetting.ASK_EVERY_TIME
2425
import com.duckduckgo.app.fire.fireproofwebsite.ui.AutomaticFireproofSetting.NEVER
@@ -32,6 +33,7 @@ import com.duckduckgo.browser.ui.omnibar.OmnibarPosition
3233
import com.duckduckgo.di.scopes.AppScope
3334
import com.squareup.anvil.annotations.ContributesBinding
3435
import dagger.SingleInstanceIn
36+
import kotlinx.coroutines.runBlocking
3537
import javax.inject.Inject
3638

3739
interface SettingsDataStore {
@@ -108,6 +110,7 @@ interface SettingsDataStore {
108110
class SettingsSharedPreferences @Inject constructor(
109111
private val context: Context,
110112
private val appBuildConfig: AppBuildConfig,
113+
private val omnibarDataStore: OmnibarDataStore,
111114
) : SettingsDataStore,
112115
AutoCompleteSettings {
113116
private val fireAnimationMapper = FireAnimationPrefsMapper()
@@ -214,8 +217,10 @@ class SettingsSharedPreferences @Inject constructor(
214217
set(enabled) = preferences.edit { putBoolean(SHOW_AUTOMATIC_FIREPROOF_DIALOG, enabled) }
215218

216219
override var omnibarPosition: OmnibarPosition
217-
get() = OmnibarPosition.valueOf(preferences.getString(KEY_OMNIBAR_POSITION, OmnibarPosition.TOP.name) ?: OmnibarPosition.TOP.name)
218-
set(value) = preferences.edit { putString(KEY_OMNIBAR_POSITION, value.name) }
220+
get() = omnibarDataStore.omnibarPosition
221+
set(value) = runBlocking {
222+
omnibarDataStore.setOmnibarPosition(value)
223+
}
219224

220225
override var isFullUrlEnabled: Boolean
221226
get() = preferences.getBoolean(KEY_IS_FULL_URL_ENABLED, true)

0 commit comments

Comments
 (0)