Skip to content

Commit

Permalink
resources: smoother search (fixes #5164) (#5175)
Browse files Browse the repository at this point in the history
Co-authored-by: Gideon Okuro <[email protected]>
Co-authored-by: dogi <[email protected]>
  • Loading branch information
3 people authored Jan 31, 2025
1 parent ca8e41d commit f3b8222
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
applicationId "org.ole.planet.myplanet"
minSdkVersion 26
targetSdkVersion 34
versionCode 2262
versionName "0.22.62"
versionCode 2263
versionName "0.22.63"
ndkVersion '21.3.6528147'
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import org.ole.planet.myplanet.model.RealmTag
import org.ole.planet.myplanet.service.UserProfileDbHandler
import org.ole.planet.myplanet.utilities.Constants.PREFS_NAME
import org.ole.planet.myplanet.utilities.Utilities.toast
import java.text.Normalizer
import java.util.Locale

abstract class BaseRecyclerFragment<LI> : BaseRecyclerParentFragment<Any?>(), OnRatingChangeListener {
Expand Down Expand Up @@ -188,24 +189,31 @@ abstract class BaseRecyclerFragment<LI> : BaseRecyclerParentFragment<Any?>(), On
searchAndMatch(item, c, queryParts)
}
} else {
mRealm.where(c)
.contains(fieldName, s, Case.INSENSITIVE)
.findAll()
mRealm.where(c).findAll().filter { item ->
val title = when {
c.isAssignableFrom(RealmMyLibrary::class.java) -> (item as RealmMyLibrary).title
else -> (item as RealmMyCourse).courseTitle
}
title?.let {
normalizeText(it).contains(normalizeText(s))
} ?: false
}
}
}

private fun <LI : RealmModel> searchAndMatch(item: LI, c: Class<out RealmModel>, queryParts: List<String>): Boolean {
val title = when {
c.isAssignableFrom(RealmMyLibrary::class.java) -> (item as RealmMyLibrary).title
else -> (item as RealmMyCourse).courseTitle
}?.lowercase(Locale.getDefault()) ?: return false
}?.let { normalizeText(it) } ?: return false

return queryParts.all { queryPart ->
title.contains(queryPart.lowercase(Locale.getDefault()))
title.contains(normalizeText(queryPart))
}
}

fun filterLibraryByTag(s: String, tags: List<RealmTag>): List<RealmMyLibrary> {
val normalizedSearchTerm = normalizeText(s)
var list = getData(s, RealmMyLibrary::class.java)
list = if (isMyCourseLib) {
getMyLibraryByUserId(model?.id, list)
Expand All @@ -224,12 +232,18 @@ abstract class BaseRecyclerFragment<LI> : BaseRecyclerParentFragment<Any?>(), On
}

return libraries.sortedWith(compareBy<RealmMyLibrary> { library ->
!library.title?.lowercase()?.startsWith(s.lowercase())!! ?: true
val normalizedTitle = normalizeText(library.title ?: "")
!normalizedTitle.contains(normalizedSearchTerm)
}.thenBy { library ->
library.title?.lowercase() ?: ""
normalizeText(library.title ?: "")
})
}

fun normalizeText(str: String): String {
return Normalizer.normalize(str.lowercase(Locale.getDefault()), Normalizer.Form.NFD)
.replace(Regex("\\p{InCombiningDiacriticalMarks}+"), "")
}

fun filterCourseByTag(s: String, tags: List<RealmTag>): List<RealmMyCourse> {
if (tags.isEmpty() && s.isEmpty()) {
return applyCourseFilter(filterRealmMyCourseList(getList(RealmMyCourse::class.java)))
Expand Down

0 comments on commit f3b8222

Please sign in to comment.