diff --git a/app/src/main/java/com/github/libretube/db/DatabaseHelper.kt b/app/src/main/java/com/github/libretube/db/DatabaseHelper.kt index 3ea8b521b..9ff1669d5 100644 --- a/app/src/main/java/com/github/libretube/db/DatabaseHelper.kt +++ b/app/src/main/java/com/github/libretube/db/DatabaseHelper.kt @@ -5,7 +5,6 @@ import com.github.libretube.constants.PreferenceKeys import com.github.libretube.db.DatabaseHolder.Database import com.github.libretube.db.obj.SearchHistoryItem import com.github.libretube.db.obj.WatchHistoryItem -import com.github.libretube.extensions.query import com.github.libretube.extensions.toID import com.github.libretube.helpers.PreferenceHelper import kotlinx.coroutines.Dispatchers @@ -38,16 +37,13 @@ object DatabaseHelper { } } - fun addToSearchHistory(searchHistoryItem: SearchHistoryItem) { - query { - Database.searchHistoryDao().insertAll(searchHistoryItem) + suspend fun addToSearchHistory(searchHistoryItem: SearchHistoryItem) { + Database.searchHistoryDao().insertAll(listOf(searchHistoryItem)) - // delete the first watch history entry if the limit is reached - val searchHistory = Database.searchHistoryDao().getAll() - if (searchHistory.size > MAX_SEARCH_HISTORY_SIZE) { - Database.searchHistoryDao() - .delete(searchHistory.first()) - } + // delete the first watch history entry if the limit is reached + val searchHistory = Database.searchHistoryDao().getAll() + if (searchHistory.size > MAX_SEARCH_HISTORY_SIZE) { + Database.searchHistoryDao().delete(searchHistory.first()) } } } diff --git a/app/src/main/java/com/github/libretube/db/dao/SearchHistoryDao.kt b/app/src/main/java/com/github/libretube/db/dao/SearchHistoryDao.kt index 4556d4a13..209aa49ce 100644 --- a/app/src/main/java/com/github/libretube/db/dao/SearchHistoryDao.kt +++ b/app/src/main/java/com/github/libretube/db/dao/SearchHistoryDao.kt @@ -10,14 +10,14 @@ import com.github.libretube.db.obj.SearchHistoryItem @Dao interface SearchHistoryDao { @Query("SELECT * FROM searchHistoryItem") - fun getAll(): List + suspend fun getAll(): List @Insert(onConflict = OnConflictStrategy.REPLACE) - fun insertAll(vararg searchHistoryItem: SearchHistoryItem) + suspend fun insertAll(searchHistoryItems: List) @Delete - fun delete(searchHistoryItem: SearchHistoryItem) + suspend fun delete(searchHistoryItem: SearchHistoryItem) @Query("DELETE FROM searchHistoryItem") - fun deleteAll() + suspend fun deleteAll() } diff --git a/app/src/main/java/com/github/libretube/helpers/BackupHelper.kt b/app/src/main/java/com/github/libretube/helpers/BackupHelper.kt index 733381e25..5ce185fe8 100644 --- a/app/src/main/java/com/github/libretube/helpers/BackupHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/BackupHelper.kt @@ -47,9 +47,7 @@ object BackupHelper { } ?: return Database.watchHistoryDao().insertAll(backupFile.watchHistory.orEmpty()) - Database.searchHistoryDao().insertAll( - *backupFile.searchHistory.orEmpty().toTypedArray() - ) + Database.searchHistoryDao().insertAll(backupFile.searchHistory.orEmpty()) Database.watchPositionDao().insertAll( *backupFile.watchPositions.orEmpty().toTypedArray() ) diff --git a/app/src/main/java/com/github/libretube/ui/adapters/SearchHistoryAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/SearchHistoryAdapter.kt index af357a3e7..1fe0ac59a 100644 --- a/app/src/main/java/com/github/libretube/ui/adapters/SearchHistoryAdapter.kt +++ b/app/src/main/java/com/github/libretube/ui/adapters/SearchHistoryAdapter.kt @@ -8,8 +8,9 @@ import androidx.recyclerview.widget.RecyclerView import com.github.libretube.databinding.SuggestionRowBinding import com.github.libretube.db.DatabaseHolder.Database import com.github.libretube.db.obj.SearchHistoryItem -import com.github.libretube.extensions.query import com.github.libretube.ui.viewholders.SuggestionsViewHolder +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.runBlocking class SearchHistoryAdapter( private var historyList: List, @@ -36,10 +37,8 @@ class SearchHistoryAdapter( deleteHistory.setOnClickListener { historyList -= historyQuery - query { - Database.searchHistoryDao().delete( - SearchHistoryItem(query = historyQuery) - ) + runBlocking(Dispatchers.IO) { + Database.searchHistoryDao().delete(SearchHistoryItem(historyQuery)) } notifyItemRemoved(position) notifyItemRangeChanged(position, itemCount) diff --git a/app/src/main/java/com/github/libretube/ui/fragments/SearchFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/SearchFragment.kt index 0197e7e7c..6ad9b5b2e 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/SearchFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/SearchFragment.kt @@ -13,11 +13,13 @@ import com.github.libretube.api.RetrofitInstance import com.github.libretube.databinding.FragmentSearchBinding import com.github.libretube.db.DatabaseHolder.Database import com.github.libretube.extensions.TAG -import com.github.libretube.extensions.awaitQuery import com.github.libretube.ui.activities.MainActivity import com.github.libretube.ui.adapters.SearchHistoryAdapter import com.github.libretube.ui.adapters.SearchSuggestionsAdapter import com.github.libretube.ui.models.SearchViewModel +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext class SearchFragment : Fragment(R.layout.fragment_search) { private val binding by viewBinding(FragmentSearchBinding::bind) @@ -76,18 +78,19 @@ class SearchFragment : Fragment(R.layout.fragment_search) { } private fun showHistory() { - val historyList = awaitQuery { - Database.searchHistoryDao().getAll().map { it.query } - } - if (historyList.isNotEmpty()) { - binding.suggestionsRecycler.adapter = - SearchHistoryAdapter( + lifecycleScope.launch { + val historyList = withContext(Dispatchers.IO) { + Database.searchHistoryDao().getAll().map { it.query } + } + if (historyList.isNotEmpty()) { + binding.suggestionsRecycler.adapter = SearchHistoryAdapter( historyList, (activity as MainActivity).searchView ) - } else { - binding.suggestionsRecycler.visibility = View.GONE - binding.historyEmpty.visibility = View.VISIBLE + } else { + binding.suggestionsRecycler.visibility = View.GONE + binding.historyEmpty.visibility = View.VISIBLE + } } } } diff --git a/app/src/main/java/com/github/libretube/ui/fragments/SearchResultFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/SearchResultFragment.kt index b4b9265ce..2b2c84e3e 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/SearchResultFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/SearchResultFragment.kt @@ -20,6 +20,7 @@ import com.github.libretube.helpers.PreferenceHelper import com.github.libretube.ui.adapters.SearchAdapter import java.io.IOException import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import retrofit2.HttpException @@ -125,12 +126,10 @@ class SearchResultFragment : Fragment(R.layout.fragment_search_result) { private fun addToHistory(query: String) { val searchHistoryEnabled = PreferenceHelper.getBoolean(PreferenceKeys.SEARCH_HISTORY_TOGGLE, true) - if (searchHistoryEnabled && query != "") { - DatabaseHelper.addToSearchHistory( - SearchHistoryItem( - query = query - ) - ) + if (searchHistoryEnabled && query.isNotEmpty()) { + lifecycleScope.launch(Dispatchers.IO) { + DatabaseHelper.addToSearchHistory(SearchHistoryItem(query)) + } } } }