|
1 | 1 | package com.github.libretube.ui.fragments |
2 | 2 |
|
3 | 3 | import android.os.Bundle |
4 | | -import android.util.Log |
5 | 4 | import android.view.View |
6 | 5 | import androidx.core.view.isGone |
7 | 6 | import androidx.core.view.isVisible |
8 | 7 | import androidx.fragment.app.Fragment |
9 | 8 | import androidx.fragment.app.activityViewModels |
10 | | -import androidx.lifecycle.distinctUntilChanged |
| 9 | +import androidx.lifecycle.Lifecycle |
11 | 10 | import androidx.lifecycle.lifecycleScope |
12 | | -import androidx.lifecycle.map |
| 11 | +import androidx.lifecycle.repeatOnLifecycle |
13 | 12 | import androidx.navigation.fragment.findNavController |
14 | 13 | import com.github.libretube.R |
15 | | -import com.github.libretube.api.MediaServiceRepository |
16 | 14 | import com.github.libretube.constants.IntentData |
17 | | -import com.github.libretube.constants.PreferenceKeys |
18 | 15 | import com.github.libretube.databinding.FragmentSearchSuggestionsBinding |
19 | | -import com.github.libretube.db.DatabaseHolder.Database |
20 | | -import com.github.libretube.extensions.TAG |
21 | 16 | import com.github.libretube.extensions.anyChildFocused |
22 | | -import com.github.libretube.helpers.PreferenceHelper |
23 | 17 | import com.github.libretube.ui.activities.MainActivity |
24 | 18 | import com.github.libretube.ui.adapters.SearchHistoryAdapter |
25 | 19 | import com.github.libretube.ui.adapters.SearchSuggestionsAdapter |
@@ -65,70 +59,48 @@ class SearchSuggestionsFragment : Fragment(R.layout.fragment_search_suggestions) |
65 | 59 | _binding = FragmentSearchSuggestionsBinding.bind(view) |
66 | 60 | super.onViewCreated(view, savedInstanceState) |
67 | 61 |
|
68 | | - viewModel.searchQuery |
69 | | - .map { it.isNullOrEmpty() } |
70 | | - .distinctUntilChanged() |
71 | | - .observe(viewLifecycleOwner) { isQueryEmpty -> |
72 | | - if (isQueryEmpty) { |
73 | | - binding.suggestionsRecycler.adapter = historyAdapter |
74 | | - } else if (PreferenceHelper.getBoolean(PreferenceKeys.SEARCH_SUGGESTIONS, true)) { |
75 | | - binding.suggestionsRecycler.adapter = suggestionsAdapter |
76 | | - } |
77 | | - } |
78 | | - |
79 | | - // waiting for the query to change |
80 | 62 | viewModel.searchQuery.observe(viewLifecycleOwner) { |
81 | | - showData(it) |
82 | | - } |
| 63 | + val isEmpty = it.isNullOrEmpty() |
| 64 | + binding.suggestionsRecycler.adapter = if (isEmpty) historyAdapter else suggestionsAdapter |
83 | 65 |
|
84 | | - setOnBackPressed { |
85 | | - if (mainActivity.searchView.anyChildFocused()) mainActivity.searchView.clearFocus() |
86 | | - else findNavController().popBackStack() |
| 66 | + toggleHistoryVisibility() |
87 | 67 | } |
88 | | - } |
89 | 68 |
|
90 | | - private fun showData(query: String?) { |
91 | | - // fetch search suggestions if enabled or show the search history |
92 | | - binding.historyEmpty.isGone = true |
93 | | - binding.suggestionsRecycler.isVisible = true |
94 | | - if (query.isNullOrEmpty()) { |
95 | | - showHistory() |
96 | | - } else if (PreferenceHelper.getBoolean(PreferenceKeys.SEARCH_SUGGESTIONS, true)) { |
97 | | - fetchSuggestions(query) |
| 69 | + lifecycleScope.launch(Dispatchers.IO) { |
| 70 | + repeatOnLifecycle(Lifecycle.State.STARTED) { |
| 71 | + viewModel.searchSuggestions.collect { suggestions -> |
| 72 | + withContext(Dispatchers.Main) { |
| 73 | + suggestionsAdapter.submitList(suggestions.reversed()) |
| 74 | + toggleHistoryVisibility() |
| 75 | + } |
| 76 | + } |
| 77 | + } |
98 | 78 | } |
99 | | - } |
100 | 79 |
|
101 | | - private fun fetchSuggestions(query: String) { |
102 | | - lifecycleScope.launch { |
103 | | - val response = try { |
104 | | - withContext(Dispatchers.IO) { |
105 | | - MediaServiceRepository.instance.getSuggestions(query) |
| 80 | + lifecycleScope.launch(Dispatchers.IO) { |
| 81 | + repeatOnLifecycle(Lifecycle.State.STARTED) { |
| 82 | + viewModel.searchHistory.collect { historyList -> |
| 83 | + withContext(Dispatchers.Main) { |
| 84 | + historyAdapter.submitList(historyList.map { it.query }) |
| 85 | + toggleHistoryVisibility() |
| 86 | + } |
106 | 87 | } |
107 | | - } catch (e: Exception) { |
108 | | - Log.e(TAG(), e.toString()) |
109 | | - return@launch |
110 | | - } |
111 | | - // only load the suggestions if the input field didn't get cleared yet |
112 | | - if (!viewModel.searchQuery.value.isNullOrEmpty()) { |
113 | | - suggestionsAdapter.submitList(response.reversed()) |
114 | 88 | } |
115 | 89 | } |
116 | | - } |
117 | 90 |
|
118 | | - private fun showHistory() { |
119 | | - lifecycleScope.launch { |
120 | | - val historyList = withContext(Dispatchers.IO) { |
121 | | - Database.searchHistoryDao().getAll().map { it.query } |
122 | | - } |
123 | | - if (historyList.isNotEmpty()) { |
124 | | - historyAdapter.submitList(historyList) |
125 | | - } else { |
126 | | - binding.suggestionsRecycler.isGone = true |
127 | | - binding.historyEmpty.isVisible = true |
128 | | - } |
| 91 | + setOnBackPressed { |
| 92 | + if (mainActivity.searchView.anyChildFocused()) mainActivity.searchView.clearFocus() |
| 93 | + else findNavController().popBackStack() |
129 | 94 | } |
130 | 95 | } |
131 | 96 |
|
| 97 | + private fun toggleHistoryVisibility() { |
| 98 | + val isEmpty = viewModel.searchQuery.value.isNullOrEmpty() |
| 99 | + val showHistoryEmpty = isEmpty && historyAdapter.currentList.isEmpty() |
| 100 | + binding.historyEmpty.isVisible = showHistoryEmpty |
| 101 | + binding.suggestionsRecycler.isGone = showHistoryEmpty |
| 102 | + } |
| 103 | + |
132 | 104 | override fun onDestroy() { |
133 | 105 | super.onDestroy() |
134 | 106 |
|
|
0 commit comments