Skip to content

Commit 17004d4

Browse files
authored
Store initial Duck.ai address bar setting (#5972)
Task/Issue URL: https://app.asana.com/1/137249556945/project/488551667048375/task/1210096722341711?focus=true ### Description - If the Duck.ai menu setting was initially disabled and the user updates, then the address bar setting is also disabled. - However, if the the menu setting is toggled, then both settings toggle because there is no initial stored value for the address bar setting. - This fixes that use-case. ### Steps to test this PR - [ ] Checkout 5.230.1 - [ ] Disable the Duck.ai menu setting - [ ] Update to this branch - [ ] Verify that both settings are disabled - [ ] Toggle the menu setting - [ ] Verify that only the menu setting is toggled
1 parent b563594 commit 17004d4

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

duckchat/duckchat-impl/src/main/java/com/duckduckgo/duckchat/impl/store/DuckChatDataStore.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import androidx.datastore.preferences.core.booleanPreferencesKey
2222
import androidx.datastore.preferences.core.edit
2323
import androidx.datastore.preferences.core.stringPreferencesKey
2424
import com.duckduckgo.app.di.AppCoroutineScope
25+
import com.duckduckgo.app.di.IsMainProcess
26+
import com.duckduckgo.common.utils.DispatcherProvider
2527
import com.duckduckgo.di.scopes.AppScope
2628
import com.duckduckgo.duckchat.impl.di.DuckChat
2729
import com.duckduckgo.duckchat.impl.store.SharedPreferencesDuckChatDataStore.Keys.DUCK_CHAT_OPENED
@@ -38,6 +40,7 @@ import kotlinx.coroutines.flow.distinctUntilChanged
3840
import kotlinx.coroutines.flow.firstOrNull
3941
import kotlinx.coroutines.flow.map
4042
import kotlinx.coroutines.flow.stateIn
43+
import kotlinx.coroutines.launch
4144

4245
interface DuckChatDataStore {
4346
suspend fun setShowInBrowserMenu(showDuckChat: Boolean)
@@ -56,6 +59,8 @@ interface DuckChatDataStore {
5659
@SingleInstanceIn(AppScope::class)
5760
class SharedPreferencesDuckChatDataStore @Inject constructor(
5861
@DuckChat private val store: DataStore<Preferences>,
62+
private val dispatchers: DispatcherProvider,
63+
@IsMainProcess private val isMainProcess: Boolean,
5964
@AppCoroutineScope private val appCoroutineScope: CoroutineScope,
6065
) : DuckChatDataStore {
6166

@@ -72,6 +77,21 @@ class SharedPreferencesDuckChatDataStore @Inject constructor(
7277
?: true
7378
}
7479

80+
init {
81+
if (isMainProcess) {
82+
storeDerivedValues()
83+
}
84+
}
85+
86+
private fun storeDerivedValues() = appCoroutineScope.launch(dispatchers.io()) {
87+
store.data.firstOrNull()?.let { prefs ->
88+
if (prefs[DUCK_CHAT_SHOW_IN_ADDRESS_BAR] == null) {
89+
val default = prefs[DUCK_CHAT_SHOW_IN_MENU] ?: true
90+
store.edit { it[DUCK_CHAT_SHOW_IN_ADDRESS_BAR] = default }
91+
}
92+
}
93+
}
94+
7595
private val duckChatShowInBrowserMenu: StateFlow<Boolean> = store.data
7696
.map { prefs -> prefs[DUCK_CHAT_SHOW_IN_MENU] ?: true }
7797
.distinctUntilChanged()

duckchat/duckchat-impl/src/test/kotlin/com/duckduckgo/duckchat/impl/store/SharedPreferencesDuckChatDataStoreTest.kt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@ class SharedPreferencesDuckChatDataStoreTest {
5050
)
5151

5252
private val testee: DuckChatDataStore =
53-
SharedPreferencesDuckChatDataStore(testDataStore, coroutineRule.testScope)
53+
SharedPreferencesDuckChatDataStore(
54+
testDataStore,
55+
coroutineRule.testDispatcherProvider,
56+
true,
57+
coroutineRule.testScope,
58+
)
5459

5560
companion object {
5661
val DUCK_CHAT_USER_PREFERENCES = stringPreferencesKey("DUCK_CHAT_USER_PREFERENCES")
@@ -107,9 +112,14 @@ class SharedPreferencesDuckChatDataStoreTest {
107112
}
108113

109114
@Test
110-
fun whenMenuFlagChangesThenAddressBarDefaultsToMenuValue() = runTest {
115+
fun whenMenuFlagChangesLaterThenAddressBarRemainsUnchanged() = runTest {
116+
assertTrue(testee.getShowInBrowserMenu())
117+
assertTrue(testee.getShowInAddressBar())
118+
111119
testee.setShowInBrowserMenu(false)
112-
assertFalse(testee.getShowInAddressBar())
120+
121+
assertFalse(testee.getShowInBrowserMenu())
122+
assertTrue(testee.getShowInAddressBar())
113123
}
114124

115125
@Test

0 commit comments

Comments
 (0)