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>
This commit is contained in:
Amogh Lele 2021-04-07 11:02:41 +05:30 committed by GitHub
parent 474770c5e3
commit 7acbf0eda8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 6 additions and 25 deletions

View file

@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file.
- Allow doing a merge instead of a rebase when pulling or syncing
- Add support for manually providing TOTP parameters
- Parse extra content as individual fields
- Improve search result filtering logic
### Fixed

View file

@ -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)

View file

@ -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 }

View file

@ -52,6 +52,7 @@ object Dependencies {
object FirstParty {
const val sublime_fuzzy = "com.github.android-password-store:sublime-fuzzy:1.0.0-alpha01"
const val zxing_android_embedded = "com.github.android-password-store:zxing-android-embedded:4.1.0-aps"
}