From f64fa4fb2352aaf2fcb53b33b914958975f20cab Mon Sep 17 00:00:00 2001 From: Naomi Nyama Date: Fri, 18 Oct 2024 13:59:04 +0200 Subject: [PATCH] - Remove special characters --- .../ui/fragments/GlobalSearchFragment.kt | 20 +++++++++---- .../ui/viewmodels/FAGlobalSearchViewModel.kt | 28 ++++++++++++++----- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/org/apphatchery/gatbreferenceguide/ui/fragments/GlobalSearchFragment.kt b/app/src/main/java/org/apphatchery/gatbreferenceguide/ui/fragments/GlobalSearchFragment.kt index c5df5af..696f81a 100644 --- a/app/src/main/java/org/apphatchery/gatbreferenceguide/ui/fragments/GlobalSearchFragment.kt +++ b/app/src/main/java/org/apphatchery/gatbreferenceguide/ui/fragments/GlobalSearchFragment.kt @@ -425,6 +425,7 @@ class GlobalSearchFragment : BaseFragment(R.layout.fragment_global_search) { }) } + private fun handleSearchTextChange(text: String) { searchJob?.cancel() val trimmedText = text.trim() @@ -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 @@ -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): List { 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) @@ -461,11 +465,17 @@ class GlobalSearchFragment : BaseFragment(R.layout.fragment_global_search) { } private fun highlightText(original: String, wordsToHighlight: List): 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) { - "${it.value}" + if (word.isNotEmpty()) { + val pattern = word.replace(Regex("[\\s.,]+"), "") + if (pattern.isNotEmpty()) { + val regex = Regex("(?i)($pattern)") + result = result.replace(regex) { + "${it.value}" + } + } } } return result @@ -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" diff --git a/app/src/main/java/org/apphatchery/gatbreferenceguide/ui/viewmodels/FAGlobalSearchViewModel.kt b/app/src/main/java/org/apphatchery/gatbreferenceguide/ui/viewmodels/FAGlobalSearchViewModel.kt index 6ba3bc5..201e1a7 100644 --- a/app/src/main/java/org/apphatchery/gatbreferenceguide/ui/viewmodels/FAGlobalSearchViewModel.kt +++ b/app/src/main/java/org/apphatchery/gatbreferenceguide/ui/viewmodels/FAGlobalSearchViewModel.kt @@ -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 @@ -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()