Skip to content

Commit

Permalink
Fix: Fixed App Searching.
Browse files Browse the repository at this point in the history
also fixed the app list from being empty if the search is empty.
  • Loading branch information
CreativeCodeCat committed Nov 24, 2024
1 parent d18310d commit 1f0d4c8
Showing 1 changed file with 25 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class DrawFragment : Fragment(),
val searchQuery = trimmedQuery.substringAfter("!")
requireContext().searchCustomSearchEngine(preferenceHelper, searchQuery)
} else {
checkAppThenRun(trimmedQuery)
searchApp(trimmedQuery, false)
return true // Exit the function
}
}
Expand All @@ -152,7 +152,7 @@ class DrawFragment : Fragment(),
}

override fun onQueryTextChange(newText: String?): Boolean {
searchApp(newText.toString())
searchApp(newText.toString(), true)
return true
}
})
Expand Down Expand Up @@ -188,31 +188,7 @@ class DrawFragment : Fragment(),
}
}

private fun checkAppThenRun(query: String) {
val searchQuery = "%$query%"

// Launch a coroutine tied to the lifecycle of the view
viewLifecycleOwner.lifecycleScope.launch {
// Use repeatOnLifecycle to manage the lifecycle state
repeatOnLifecycle(Lifecycle.State.CREATED) {
val trimmedQuery = searchQuery.trim()
viewModel.searchAppInfo().collect { searchResults ->
val numberOfItemsLeft = searchResults.size
val appResults = searchResults.firstOrNull()
if (numberOfItemsLeft == 0 && !requireContext().searchOnPlayStore(trimmedQuery)) {
requireContext().openSearch(trimmedQuery)
} else {
appResults?.let { appInfo ->
observeBioAuthCheck(appInfo)
}
drawAdapter.submitList(searchResults)
}
}
}
}
}

private fun searchApp(query: String) {
private fun searchApp(query: String, isSearching: Boolean) {
// Launch a coroutine tied to the lifecycle of the view
viewLifecycleOwner.lifecycleScope.launch {
// Repeat the block when the lifecycle is at least CREATED
Expand Down Expand Up @@ -260,16 +236,30 @@ class DrawFragment : Fragment(),
val numberOfItemsLeft = finalResults.size
val appResults = finalResults.firstOrNull()

when (numberOfItemsLeft) {
1 -> {
appResults?.let { appInfo ->
if (preferenceHelper.automaticOpenApp) observeBioAuthCheck(appInfo)
if (isSearching) {
when (numberOfItemsLeft) {
1 -> {
appResults?.let { appInfo ->
if (preferenceHelper.automaticOpenApp) observeBioAuthCheck(appInfo)
}
drawAdapter.submitList(finalResults)
}
drawAdapter.submitList(finalResults)
}

else -> {
drawAdapter.submitList(finalResults)
else -> {
drawAdapter.submitList(finalResults)
}
}
if (trimmedQuery.isEmpty()) {
drawAdapter.submitList(searchResults)
}
} else {
if (numberOfItemsLeft == 0 && !requireContext().searchOnPlayStore(trimmedQuery)) {
requireContext().openSearch(trimmedQuery)
} else {
appResults?.let { appInfo ->
observeBioAuthCheck(appInfo)
}
drawAdapter.submitList(searchResults)
}
}
}
Expand Down

0 comments on commit 1f0d4c8

Please sign in to comment.