Skip to content

Commit

Permalink
feat: add browsing preference category
Browse files Browse the repository at this point in the history
  • Loading branch information
criticalAY committed Feb 3, 2025
1 parent aa97f0d commit 959f4c9
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import anki.collection.OpChanges
import anki.collection.OpChangesWithCount
import anki.config.ConfigKey
import anki.generic.Bool
import anki.search.BrowserColumns
import anki.search.BrowserRow
import com.ichi2.anki.AnkiDroidApp
Expand Down Expand Up @@ -177,6 +179,10 @@ class CardBrowserViewModel(
MutableStateFlow(sharedPrefs().getBoolean("isTruncated", false))
val isTruncated get() = flowOfIsTruncated.value

var shouldIgnoreAccents: Boolean = false

var defaultBrowserSearch: String? = null

private val _selectedRows: MutableSet<CardOrNoteId> = Collections.synchronizedSet(LinkedHashSet())

// immutable accessor for _selectedRows
Expand Down Expand Up @@ -206,6 +212,18 @@ class CardBrowserViewModel(
val lastDeckId: DeckId?
get() = lastDeckIdRepository.lastDeckId

private fun getIgnoreAccent() =
viewModelScope.launch {
Timber.d("Viewmodel accent value:: ${withCol { config.getBool(ConfigKey.Bool.IGNORE_ACCENTS_IN_SEARCH) }}")
shouldIgnoreAccents = withCol { config.getBool(ConfigKey.Bool.IGNORE_ACCENTS_IN_SEARCH) }
}

private fun getDefaultSearchText() =
viewModelScope.launch {
val defaultText = withCol { config.getString(ConfigKey.String.DEFAULT_SEARCH_TEXT) }
defaultBrowserSearch = defaultText
}

suspend fun setDeckId(deckId: DeckId) {
Timber.i("setting deck: %d", deckId)
lastDeckIdRepository.lastDeckId = deckId
Expand Down Expand Up @@ -288,6 +306,9 @@ class CardBrowserViewModel(
init {
Timber.d("CardBrowserViewModel::init")

getIgnoreAccent()
getDefaultSearchText()

var selectAllDecks = false
when (options) {
is CardBrowserLaunchOptions.SystemContextMenu -> {
Expand All @@ -300,7 +321,8 @@ class CardBrowserViewModel(
is CardBrowserLaunchOptions.DeepLink -> {
searchTerms = options.search
}
null -> {}
null -> {
}
}

performSearchFlow
Expand Down Expand Up @@ -454,6 +476,24 @@ class CardBrowserViewModel(
}
}

fun setIgnoreAccents(value: Boolean) {
Timber.d("Setting ignore accent in search to: $value")
viewModelScope.launch {
withCol { config.setBool(ConfigKey.Bool.IGNORE_ACCENTS_IN_SEARCH, value) }
shouldIgnoreAccents = value
}
}

fun setDefaultSearchText(text: String) {
Timber.d("Setting default search text to: $text")
viewModelScope.launch {
defaultBrowserSearch = text
withCol {
config.setString(ConfigKey.String.DEFAULT_SEARCH_TEXT, text)
}
}
}

fun selectAll() {
if (_selectedRows.addAll(cards)) {
Timber.d("selecting all: %d item(s)", cards.size)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ import android.widget.CheckBox
import android.widget.LinearLayout
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.TextView
import androidx.annotation.IdRes
import androidx.appcompat.app.AppCompatDialogFragment
import androidx.core.os.bundleOf
import androidx.fragment.app.activityViewModels
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.textfield.TextInputEditText
import com.ichi2.anki.CollectionManager.TR
import com.ichi2.anki.R
import com.ichi2.anki.browser.BrowserColumnSelectionFragment
import com.ichi2.anki.browser.CardBrowserViewModel
Expand Down Expand Up @@ -61,6 +64,16 @@ class BrowserOptionsDialog : AppCompatDialogFragment() {
if (newTruncate != isTruncated) {
viewModel.setTruncated(newTruncate)
}

val newIgnoreAccent = dialogView.findViewById<CheckBox>(R.id.ignore_accents_checkbox).isChecked
if (newTruncate != viewModel.shouldIgnoreAccents) {
viewModel.setIgnoreAccents(newIgnoreAccent)
}

val newSearchValue = dialogView.findViewById<TextInputEditText>(R.id.default_search_text).text.toString()
if (newSearchValue != viewModel.defaultBrowserSearch) {
viewModel.setDefaultSearchText(newSearchValue)
}
}

private val cardsOrNotes by lazy {
Expand Down Expand Up @@ -105,6 +118,18 @@ class BrowserOptionsDialog : AppCompatDialogFragment() {
openColumnManager()
}

dialogView.findViewById<CheckBox>(R.id.ignore_accents_checkbox).apply {
text = TR.preferencesIgnoreAccentsInSearch()
isChecked = viewModel.shouldIgnoreAccents
}

dialogView.findViewById<TextView>(R.id.default_search_heading).text = TR.preferencesDefaultSearchText()

dialogView.findViewById<TextInputEditText>(R.id.default_search_text).apply {
hint = TR.preferencesDefaultSearchTextExample()
setText(viewModel.defaultBrowserSearch.toString())
}

return MaterialAlertDialogBuilder(requireContext()).run {
this.setView(dialogView)
this.setTitle(getString(R.string.browser_options_dialog_heading))
Expand Down
10 changes: 9 additions & 1 deletion AnkiDroid/src/main/java/com/ichi2/libanki/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import anki.config.ConfigKey
import com.google.protobuf.kotlin.toByteStringUtf8
import com.ichi2.libanki.utils.NotInLibAnki
import kotlinx.serialization.SerializationException
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import net.ankiweb.rsdroid.Backend
import net.ankiweb.rsdroid.exceptions.BackendNotFoundException
Expand Down Expand Up @@ -66,6 +65,15 @@ class Config(
backend.setConfigBool(key, value, false)
}

fun getString(key: ConfigKey.String): String = backend.getConfigString(key)

fun setString(
key: ConfigKey.String,
value: String,
) {
backend.setConfigString(key, value, false)
}

@NotInLibAnki
inline fun <reified T> get(
key: String,
Expand Down
47 changes: 46 additions & 1 deletion AnkiDroid/src/main/res/layout/browser_options_dialog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools">

<LinearLayout
android:layout_width="match_parent"
Expand Down Expand Up @@ -73,6 +74,50 @@

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:orientation="vertical">

<TextView
android:layout_marginBottom="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/pref_cat_browsing"
android:textStyle="bold"
android:layout_marginHorizontal="16dp" />

<TextView
android:id="@+id/default_search_heading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/pref_cat_browsing"
tools:text="Default search text"
android:layout_marginHorizontal="16dp" />

<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/default_search_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:hint="eg. 'deck:current'"
android:layout_marginHorizontal="16dp"
android:inputType="text" />
</com.google.android.material.textfield.TextInputLayout>

<CheckBox
android:id="@+id/ignore_accents_checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
tools:text="Ignore accents in search (slower)"/>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down
1 change: 1 addition & 0 deletions AnkiDroid/src/main/res/values/07-cardbrowser.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,5 @@
<string name="user_potential_columns">Available</string>
<string name="include_column">Include column</string>
<string name="exclude_column">Exclude column</string>
<string name="pref_cat_browsing">Browsing</string>
</resources>
1 change: 1 addition & 0 deletions AnkiDroid/src/main/res/xml/preferences_general.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app1="urn:oasis:names:tc:xliff:document:1.2"
android:title="@string/pref_cat_general"
android:key="@string/pref_general_screen_key">
<ListPreference
Expand Down

0 comments on commit 959f4c9

Please sign in to comment.