aboutsummaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorAmogh Lele <amolele@gmail.com>2021-04-07 11:02:41 +0530
committerGitHub <noreply@github.com>2021-04-07 11:02:41 +0530
commit7acbf0eda85f44ee305825de5395f4537fce999b (patch)
tree6dc3671a209f13ae32059484358fa35f33bcc04d /app
parent474770c5e38ac6f6519ed2ac54f0d0c52addbb0f (diff)
Switch to sublime text's fuzzy matching (#1372)
* refactor(search): use sublime text's fuzzy matching algorithm Signed-off-by: SphericalKat <amolele@gmail.com> * chore(changelog): update Signed-off-by: SphericalKat <amolele@gmail.com> * build: fetch sublime-fuzzy from Maven Central Signed-off-by: Harsh Shandilya <me@msfjarvis.dev> * chore(changelog): update Co-authored-by: Harsh Shandilya <me@msfjarvis.dev> Co-authored-by: Harsh Shandilya <me@msfjarvis.dev>
Diffstat (limited to 'app')
-rw-r--r--app/build.gradle.kts1
-rw-r--r--app/src/main/java/dev/msfjarvis/aps/util/viewmodel/SearchableRepositoryViewModel.kt28
2 files changed, 4 insertions, 25 deletions
diff --git a/app/build.gradle.kts b/app/build.gradle.kts
index 2b271563..401b29a5 100644
--- a/app/build.gradle.kts
+++ b/app/build.gradle.kts
@@ -66,6 +66,7 @@ dependencies {
implementation(Dependencies.Kotlin.Coroutines.android)
implementation(Dependencies.Kotlin.Coroutines.core)
+ implementation(Dependencies.FirstParty.sublime_fuzzy)
implementation(Dependencies.FirstParty.zxing_android_embedded)
implementation(Dependencies.ThirdParty.bouncycastle)
diff --git a/app/src/main/java/dev/msfjarvis/aps/util/viewmodel/SearchableRepositoryViewModel.kt b/app/src/main/java/dev/msfjarvis/aps/util/viewmodel/SearchableRepositoryViewModel.kt
index c3998d2a..881f98f4 100644
--- a/app/src/main/java/dev/msfjarvis/aps/util/viewmodel/SearchableRepositoryViewModel.kt
+++ b/app/src/main/java/dev/msfjarvis/aps/util/viewmodel/SearchableRepositoryViewModel.kt
@@ -31,6 +31,7 @@ import dev.msfjarvis.aps.util.autofill.DirectoryStructure
import dev.msfjarvis.aps.util.extensions.sharedPrefs
import dev.msfjarvis.aps.util.settings.PasswordSortOrder
import dev.msfjarvis.aps.util.settings.PreferenceKeys
+import dev.sphericalkat.sublimefuzzy.Fuzzy
import java.io.File
import java.text.Collator
import java.util.Locale
@@ -55,31 +56,8 @@ private fun File.toPasswordItem() =
else PasswordItem.newCategory(name, this, PasswordRepository.getRepositoryDirectory())
private fun PasswordItem.fuzzyMatch(filter: String): Int {
- var i = 0
- var j = 0
- var score = 0
- var bonus = 0
- var bonusIncrement = 0
-
- val toMatch = longName
-
- while (i < filter.length && j < toMatch.length) {
- when {
- filter[i].isWhitespace() -> i++
- filter[i].toLowerCase() == toMatch[j].toLowerCase() -> {
- i++
- bonusIncrement += 1
- bonus += bonusIncrement
- score += bonus
- }
- else -> {
- bonus = 0
- bonusIncrement = 0
- }
- }
- j++
- }
- return if (i == filter.length) score else 0
+ val (_, score) = Fuzzy.fuzzyMatch(filter, longName)
+ return score
}
private val CaseInsensitiveComparator = Collator.getInstance().apply { strength = Collator.PRIMARY }