Skip to content

Commit

Permalink
Support customizing searx instance
Browse files Browse the repository at this point in the history
  • Loading branch information
lippfi committed May 24, 2024
1 parent 7fe5865 commit 1644690
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.duckduckgo.app.searchengine

import android.os.Bundle
import android.view.inputmethod.EditorInfo
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
Expand All @@ -30,6 +31,9 @@ import com.duckduckgo.app.statistics.pixels.Pixel
import com.duckduckgo.appbuildconfig.api.AppBuildConfig
import com.duckduckgo.common.ui.DuckDuckGoActivity
import com.duckduckgo.common.ui.view.dialog.RadioListAlertDialogBuilder
import com.duckduckgo.common.ui.view.hide
import com.duckduckgo.common.ui.view.hideKeyboard
import com.duckduckgo.common.ui.view.show
import com.duckduckgo.common.ui.viewbinding.viewBinding
import com.duckduckgo.di.scopes.ActivityScope
import kotlinx.coroutines.flow.launchIn
Expand All @@ -55,12 +59,20 @@ class SearchEngineActivity : DuckDuckGoActivity() {
setContentView(binding.root)
setupToolbar(binding.includeToolbar.toolbar)

configureUiEventHandlers()
configureUiEventHandlers()
observeViewModel()
}

private fun configureUiEventHandlers() {
binding.searchEngineSetting.setClickListener { viewModel.userRequestedToChangeSearchEngine() }
binding.searxInstanceSetting.setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
viewModel.onSearxInstanceUpdated(binding.searxInstanceSetting.text)
binding.searxInstanceSetting.hideKeyboard()
return@setOnEditorActionListener true
}
return@setOnEditorActionListener false
}
}

private fun observeViewModel() {
Expand All @@ -69,6 +81,7 @@ class SearchEngineActivity : DuckDuckGoActivity() {
.onEach { viewState ->
viewState.let {
updateSelectedSearchEngine(it.selectedSearchEngine)
updateSelectedSearxInstance(it.selectedSearchEngine, it.selectedSearxInstance)
}
}.launchIn(lifecycleScope)

Expand All @@ -83,6 +96,15 @@ class SearchEngineActivity : DuckDuckGoActivity() {
binding.searchEngineSetting.setSecondaryText(subtitle)
}

private fun updateSelectedSearxInstance(searchEngine: SearchEngine, searxInstance: String) {
if (searchEngine is SearxSearchEngine) {
binding.searxInstanceSetting.show()
} else {
binding.searxInstanceSetting.hide()
}
binding.searxInstanceSetting.text = searxInstance
}

private fun processCommand(it: Command) {
when (it) {
is Command.LaunchSearchEngineSettings -> launchSearchEngineSelector(it.searchEngine)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ package com.duckduckgo.app.searchengine
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.duckduckgo.anvil.annotations.ContributesViewModel
import com.duckduckgo.app.settings.clear.ClearWhatOption
import com.duckduckgo.app.settings.clear.ClearWhenOption
import com.duckduckgo.app.settings.db.SettingsDataStore
import com.duckduckgo.app.settings.db.SettingsSharedPreferences
import com.duckduckgo.di.scopes.ActivityScope
import kotlinx.coroutines.channels.BufferOverflow.DROP_OLDEST
import kotlinx.coroutines.channels.Channel
Expand All @@ -41,12 +40,7 @@ class SearchEngineViewModel @Inject constructor(

data class ViewState(
val selectedSearchEngine: SearchEngine = DuckDuckGoSearchEngine,
)

data class AutomaticallyClearData(
val clearWhatOption: ClearWhatOption,
val clearWhenOption: ClearWhenOption,
val clearWhenOptionEnabled: Boolean = true,
val selectedSearxInstance: String = SettingsSharedPreferences.DEFAULT_SEARX_INSTANCE,
)

sealed class Command {
Expand All @@ -61,6 +55,7 @@ class SearchEngineViewModel @Inject constructor(
viewState.emit(
ViewState(
selectedSearchEngine = settingsDataStore.searchEngine,
selectedSearxInstance = settingsDataStore.searxInstance,
),
)
}
Expand All @@ -85,8 +80,15 @@ class SearchEngineViewModel @Inject constructor(
}
}

private fun isAutomaticallyClearingDataWhenSettingEnabled(clearWhatOption: ClearWhatOption?): Boolean {
return clearWhatOption != null && clearWhatOption != ClearWhatOption.CLEAR_NONE
fun onSearxInstanceUpdated(searxInstance: String) {
if (settingsDataStore.searxInstance == searxInstance) {
Timber.v("User selected same thing they already have set: $searxInstance; no need to do anything else")
return
}
settingsDataStore.searxInstance = searxInstance
viewModelScope.launch {
viewState.emit(currentViewState().copy(selectedSearxInstance = searxInstance))
}
}

private fun currentViewState(): ViewState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ class SettingsSharedPreferences @Inject constructor(
}

override var searxInstance: String
get() = preferences.getString(SEARX_INSTANCE, null) ?: "https://searx.hu/"
get() = preferences.getString(SEARX_INSTANCE, null) ?: DEFAULT_SEARX_INSTANCE
set(value) = preferences.edit { putString(SEARX_INSTANCE, value) }

override fun hasBackgroundTimestampRecorded(): Boolean = preferences.contains(KEY_APP_BACKGROUNDED_TIMESTAMP)
Expand Down Expand Up @@ -249,6 +249,8 @@ class SettingsSharedPreferences @Inject constructor(
}

companion object {
const val DEFAULT_SEARX_INSTANCE = "https://searx.hu/"

const val FILENAME = "com.duckduckgo.app.settings_activity.settings"
const val KEY_BACKGROUND_JOB_ID = "BACKGROUND_JOB_ID"
const val KEY_AUTOCOMPLETE_ENABLED = "AUTOCOMPLETE_ENABLED"
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/layout/activity_search_engine.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@
app:primaryTextTruncated="false"
tools:secondaryText="test test test" />

<com.duckduckgo.common.ui.view.text.DaxTextInput
android:id="@+id/searxInstanceSetting"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/settingsSearxInstanceInputHint"
app:type="single_line"
/>
</LinearLayout>
</ScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
10 changes: 5 additions & 5 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@

<!-- Search Engine -->
<string name="searchEngineActivityTitle">Search engine</string>
<string name="settingsSearchEngine">Search Engine</string>
<string name="settingsSearchEngineDuckduckgo">DuckDuckGo</string>
<string name="settingsSearchEngineSearx">Searx</string>
<string name="settingsSelectSearchEngineDialogSave">Save</string>
<string name="settingsSearxInstanceInputHint">Searx instance</string>

<!-- Permissions -->
<string name="permissionsActivityTitle">Permissions</string>
Expand All @@ -203,11 +208,6 @@
<string name="settingsAutomaticallyClearWhenAppExit30Minutes">App exit, inactive for 30 minutes</string>
<string name="settingsAutomaticallyClearWhenAppExit60Minutes">App exit, inactive for 1 hour</string>

<string name="settingsSearchEngine">Search Engine</string>
<string name="settingsSearchEngineDuckduckgo">DuckDuckGo</string>
<string name="settingsSearchEngineSearx">Searx</string>
<string name="settingsSelectSearchEngineDialogSave">Save</string>

<!-- About Activity -->
<string name="aboutActivityTitle">About DuckDuckGo</string>
<string name="aboutHeader">Welcome to the Duck Side!</string>
Expand Down

0 comments on commit 1644690

Please sign in to comment.