Skip to content

Commit

Permalink
feat: implement browsing preferences in Card browsing
Browse files Browse the repository at this point in the history
  • Loading branch information
criticalAY committed Feb 3, 2025
1 parent 959f4c9 commit 7dfb423
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
1 change: 1 addition & 0 deletions AnkiDroid/src/main/java/com/ichi2/anki/CardBrowser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ open class CardBrowser :
)

val launchOptions = intent?.toCardBrowserLaunchOptions() // must be called after super.onCreate()

// must be called once we have an accessible collection
viewModel = createViewModel(launchOptions)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ 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 @@ -86,7 +85,9 @@ import java.io.DataOutputStream
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.text.Normalizer
import java.util.Collections
import java.util.regex.Pattern
import kotlin.math.max
import kotlin.math.min

Expand Down Expand Up @@ -222,6 +223,8 @@ class CardBrowserViewModel(
viewModelScope.launch {
val defaultText = withCol { config.getString(ConfigKey.String.DEFAULT_SEARCH_TEXT) }
defaultBrowserSearch = defaultText
searchTerms = defaultText
setSearchTerms(defaultText)
}

suspend fun setDeckId(deckId: DeckId) {
Expand Down Expand Up @@ -303,6 +306,10 @@ class CardBrowserViewModel(
emit(Unit)
}

fun setSearchTerms(text: String) {
searchTerms = text
}

init {
Timber.d("CardBrowserViewModel::init")

Expand All @@ -321,7 +328,21 @@ class CardBrowserViewModel(
is CardBrowserLaunchOptions.DeepLink -> {
searchTerms = options.search
}
null -> {
null -> {}
}

viewModelScope.launch {
// Ensure intent-based search takes priority
if (searchTerms.isEmpty()) {
val searchText: String =
withCol {
config.getString(ConfigKey.String.DEFAULT_SEARCH_TEXT)
}

Timber.d("Default search term text: $searchText")
if (searchText.isNotEmpty()) {
setSearchTerms(searchText)
}
}
}

Expand Down Expand Up @@ -935,8 +956,22 @@ class CardBrowserViewModel(
Timber.d("skipping duplicate search: forceRefresh is false")
return
}
searchTerms = searchQuery
launchSearchForCards()
viewModelScope.launch {
searchTerms =
if (shouldIgnoreAccents()) {
searchQuery.normalizeForSearch()
} else {
searchQuery
}
launchSearchForCards()
}
}

private suspend fun shouldIgnoreAccents() = withCol { config.getBool(ConfigKey.Bool.IGNORE_ACCENTS_IN_SEARCH) }

private fun String.normalizeForSearch(): String {
val normalized = Normalizer.normalize(this, Normalizer.Form.NFD)
return Pattern.compile("\\p{InCombiningDiacriticalMarks}+").matcher(normalized).replaceAll("")
}

/**
Expand Down

0 comments on commit 7dfb423

Please sign in to comment.