mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 22:30:30 +05:30
Merge pull request #3123 from Isira-Seneviratne/SearchHistoryDao_suspend
Convert SearchHistoryDao methods to suspend functions.
This commit is contained in:
commit
44b671c12a
@ -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())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -10,14 +10,14 @@ import com.github.libretube.db.obj.SearchHistoryItem
|
||||
@Dao
|
||||
interface SearchHistoryDao {
|
||||
@Query("SELECT * FROM searchHistoryItem")
|
||||
fun getAll(): List<SearchHistoryItem>
|
||||
suspend fun getAll(): List<SearchHistoryItem>
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun insertAll(vararg searchHistoryItem: SearchHistoryItem)
|
||||
suspend fun insertAll(searchHistoryItems: List<SearchHistoryItem>)
|
||||
|
||||
@Delete
|
||||
fun delete(searchHistoryItem: SearchHistoryItem)
|
||||
suspend fun delete(searchHistoryItem: SearchHistoryItem)
|
||||
|
||||
@Query("DELETE FROM searchHistoryItem")
|
||||
fun deleteAll()
|
||||
suspend fun deleteAll()
|
||||
}
|
||||
|
@ -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()
|
||||
)
|
||||
|
@ -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<String>,
|
||||
@ -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)
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user