Skip to content

Commit

Permalink
resources: smoother mylibrary search (fixes #5086) (#5087)
Browse files Browse the repository at this point in the history
Co-authored-by: dogi <[email protected]>
  • Loading branch information
Avinash-Codes and dogi authored Jan 22, 2025
1 parent da5c5f1 commit aacacd4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 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 2228
versionName "0.22.28"
versionCode 2229
versionName "0.22.29"
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 @@ -177,25 +177,31 @@ abstract class BaseRecyclerFragment<LI> : BaseRecyclerParentFragment<Any?>(), On
}

private fun <LI : RealmModel> getData(s: String, c: Class<LI>): List<LI> {
if (s.isEmpty()) return mRealm.where(c).findAll()

val queryParts = s.split(" ").filterNot { it.isEmpty() }
return if (s.contains(" ")) {
val fieldName = if (c == RealmMyLibrary::class.java) "title" else "courseTitle"

return if (queryParts.size > 1) {
val data: RealmResults<LI> = mRealm.where(c).findAll()
data.filter { item ->
searchAndMatch(item, c, queryParts)
}
} else {
mRealm.where(c).contains(if (c == RealmMyLibrary::class.java) "title" else "courseTitle", s, Case.INSENSITIVE).findAll()
mRealm.where(c)
.contains(fieldName, s, Case.INSENSITIVE)
.findAll()
}
}

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

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

Expand All @@ -206,14 +212,22 @@ abstract class BaseRecyclerFragment<LI> : BaseRecyclerParentFragment<Any?>(), On
} else {
getOurLibrary(model?.id, list)
}
if (tags.isEmpty()) {
return list
}
val libraries = mutableListOf<RealmMyLibrary>()
for (library in list) {
filter(tags, library, libraries)

val libraries = if (tags.isNotEmpty()) {
val filteredLibraries = mutableListOf<RealmMyLibrary>()
for (library in list) {
filter(tags, library, filteredLibraries)
}
filteredLibraries
} else {
list
}
return libraries

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

fun filterCourseByTag(s: String, tags: List<RealmTag>): List<RealmMyCourse> {
Expand Down

0 comments on commit aacacd4

Please sign in to comment.