Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ class GlobalSearchFragment : BaseFragment(R.layout.fragment_global_search) {
})
}


private fun handleSearchTextChange(text: String) {
searchJob?.cancel()
val trimmedText = text.trim()
Expand All @@ -434,6 +435,7 @@ class GlobalSearchFragment : BaseFragment(R.layout.fragment_global_search) {
}
updateSearchViewVisibility(trimmedText.isBlank())
}

private fun updateSearchViewVisibility(isBlank: Boolean) {
with(bind) {
recyclerview.visibility = if (isBlank) View.GONE else View.VISIBLE
Expand All @@ -443,9 +445,11 @@ class GlobalSearchFragment : BaseFragment(R.layout.fragment_global_search) {
tabLayoutContainer.visibility = if (isBlank) View.GONE else View.VISIBLE
}
}

private fun highlightSearchResults(results: List<GlobalSearchEntity>): List<GlobalSearchEntity> {
val search = viewModel.searchQuery.value ?: ""
val searchWords = search.split("\\s+".toRegex())
val searchWords = search.split(Regex("[\\s.,]+"))
.filter { it.isNotEmpty() }

return results.map { item ->
val highlightedTextInBody = highlightText(item.textInBody, searchWords)
Expand All @@ -461,11 +465,17 @@ class GlobalSearchFragment : BaseFragment(R.layout.fragment_global_search) {
}

private fun highlightText(original: String, wordsToHighlight: List<String>): String {
if (wordsToHighlight.isEmpty() || original.isEmpty()) return original
var result = original
for (word in wordsToHighlight) {
val regex = Regex("(?i)${Regex.escape(word)}")
result = result.replace(regex) {
"<span style='background-color: yellow; color: black; font-weight: bold;'>${it.value}</span>"
if (word.isNotEmpty()) {
val pattern = word.replace(Regex("[\\s.,]+"), "")
if (pattern.isNotEmpty()) {
val regex = Regex("(?i)($pattern)")
result = result.replace(regex) {
"<span style='background-color: yellow; color: black; font-weight: bold;'>${it.value}</span>"
}
}
}
}
return result
Expand Down Expand Up @@ -521,7 +531,7 @@ class GlobalSearchFragment : BaseFragment(R.layout.fragment_global_search) {
val count = filteredResults.size

bind.visibleViewGroup.visibility = if (count > 0) View.VISIBLE else View.GONE
bind.noItemFound.visibility = if (count == 0) View.VISIBLE else View.GONE
bind.noItemFound.visibility = if (count == 0 && bind.suggestedContent.visibility == View.GONE) View.VISIBLE else View.GONE

bind.searchItemCount.text = when (currentTab) {
0 -> "${count} result${if (count == 1) "" else "s"} in"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import org.apphatchery.gatbreferenceguide.db.Database
import org.apphatchery.gatbreferenceguide.db.entities.GlobalSearchEntity
Expand All @@ -26,13 +27,26 @@ class FAGlobalSearchViewModel @Inject constructor(

val searchQuery = MutableStateFlow("")

private val taskFlow = searchQuery.flatMapLatest { query ->
val cleanedQuery = query.filter { it.isLetterOrDigit() || it.isWhitespace() }
val searchTerms = cleanedQuery.split(" ").map { it.trim() }.filter { it.isNotEmpty() }
val queryList = if (searchTerms.isEmpty()) listOf("e") else searchTerms
val formattedQuery = queryList.joinToString(separator = " OR ") { "*$it*" }
db.globalSearchDao().getGlobalSearchEntity(formattedQuery)
}

private val validSearchFlow = searchQuery
.map { query ->
query.split(Regex("[\\s.,]+"))
.filter { it.isNotEmpty() }
.joinToString(" ")
}


private val taskFlow = validSearchFlow
.flatMapLatest { query ->
if (query.isBlank()) {
flow { emit(emptyList()) }
} else {
val searchTerms = query.split(" ")
.filter { it.isNotEmpty() }
val formattedQuery = searchTerms.joinToString(separator = " OR ") { "*$it*" }
db.globalSearchDao().getGlobalSearchEntity(formattedQuery)
}
}

val getGlobalSearchEntity = taskFlow.asLiveData()
fun getSubChapterById(id: String) = db.subChapterDao().getSubChapterById(id).asLiveData()
Expand Down