fix: crash when search view destroyed before suggestions fetched

This commit is contained in:
Bnyro 2024-08-10 13:22:50 +02:00
parent 52fb2a9861
commit bedcb0c616

View File

@ -92,14 +92,18 @@ class SearchSuggestionsFragment : Fragment() {
} }
private fun showHistory() { private fun showHistory() {
val searchView = runCatching {
(activity as MainActivity).searchView
}.getOrNull()
lifecycleScope.launch { lifecycleScope.launch {
val historyList = withContext(Dispatchers.IO) { val historyList = withContext(Dispatchers.IO) {
Database.searchHistoryDao().getAll().map { it.query } Database.searchHistoryDao().getAll().map { it.query }
} }
if (historyList.isNotEmpty()) { if (historyList.isNotEmpty() && searchView != null) {
binding.suggestionsRecycler.adapter = SearchHistoryAdapter( binding.suggestionsRecycler.adapter = SearchHistoryAdapter(
historyList, historyList,
(activity as MainActivity).searchView searchView
) )
} else { } else {
binding.suggestionsRecycler.isGone = true binding.suggestionsRecycler.isGone = true