mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-29 16:30:31 +05:30
Convert SearchHistoryDao methods to suspend functions.
This commit is contained in:
parent
98bce58a14
commit
7bb0c838ff
@ -5,7 +5,6 @@ import com.github.libretube.constants.PreferenceKeys
|
|||||||
import com.github.libretube.db.DatabaseHolder.Database
|
import com.github.libretube.db.DatabaseHolder.Database
|
||||||
import com.github.libretube.db.obj.SearchHistoryItem
|
import com.github.libretube.db.obj.SearchHistoryItem
|
||||||
import com.github.libretube.db.obj.WatchHistoryItem
|
import com.github.libretube.db.obj.WatchHistoryItem
|
||||||
import com.github.libretube.extensions.query
|
|
||||||
import com.github.libretube.extensions.toID
|
import com.github.libretube.extensions.toID
|
||||||
import com.github.libretube.helpers.PreferenceHelper
|
import com.github.libretube.helpers.PreferenceHelper
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
@ -38,16 +37,13 @@ object DatabaseHelper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun addToSearchHistory(searchHistoryItem: SearchHistoryItem) {
|
suspend fun addToSearchHistory(searchHistoryItem: SearchHistoryItem) {
|
||||||
query {
|
Database.searchHistoryDao().insertAll(listOf(searchHistoryItem))
|
||||||
Database.searchHistoryDao().insertAll(searchHistoryItem)
|
|
||||||
|
|
||||||
// delete the first watch history entry if the limit is reached
|
// delete the first watch history entry if the limit is reached
|
||||||
val searchHistory = Database.searchHistoryDao().getAll()
|
val searchHistory = Database.searchHistoryDao().getAll()
|
||||||
if (searchHistory.size > MAX_SEARCH_HISTORY_SIZE) {
|
if (searchHistory.size > MAX_SEARCH_HISTORY_SIZE) {
|
||||||
Database.searchHistoryDao()
|
Database.searchHistoryDao().delete(searchHistory.first())
|
||||||
.delete(searchHistory.first())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,14 +10,14 @@ import com.github.libretube.db.obj.SearchHistoryItem
|
|||||||
@Dao
|
@Dao
|
||||||
interface SearchHistoryDao {
|
interface SearchHistoryDao {
|
||||||
@Query("SELECT * FROM searchHistoryItem")
|
@Query("SELECT * FROM searchHistoryItem")
|
||||||
fun getAll(): List<SearchHistoryItem>
|
suspend fun getAll(): List<SearchHistoryItem>
|
||||||
|
|
||||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||||
fun insertAll(vararg searchHistoryItem: SearchHistoryItem)
|
suspend fun insertAll(searchHistoryItems: List<SearchHistoryItem>)
|
||||||
|
|
||||||
@Delete
|
@Delete
|
||||||
fun delete(searchHistoryItem: SearchHistoryItem)
|
suspend fun delete(searchHistoryItem: SearchHistoryItem)
|
||||||
|
|
||||||
@Query("DELETE FROM searchHistoryItem")
|
@Query("DELETE FROM searchHistoryItem")
|
||||||
fun deleteAll()
|
suspend fun deleteAll()
|
||||||
}
|
}
|
||||||
|
@ -47,9 +47,7 @@ object BackupHelper {
|
|||||||
} ?: return
|
} ?: return
|
||||||
|
|
||||||
Database.watchHistoryDao().insertAll(backupFile.watchHistory.orEmpty())
|
Database.watchHistoryDao().insertAll(backupFile.watchHistory.orEmpty())
|
||||||
Database.searchHistoryDao().insertAll(
|
Database.searchHistoryDao().insertAll(backupFile.searchHistory.orEmpty())
|
||||||
*backupFile.searchHistory.orEmpty().toTypedArray()
|
|
||||||
)
|
|
||||||
Database.watchPositionDao().insertAll(
|
Database.watchPositionDao().insertAll(
|
||||||
*backupFile.watchPositions.orEmpty().toTypedArray()
|
*backupFile.watchPositions.orEmpty().toTypedArray()
|
||||||
)
|
)
|
||||||
|
@ -8,8 +8,9 @@ import androidx.recyclerview.widget.RecyclerView
|
|||||||
import com.github.libretube.databinding.SuggestionRowBinding
|
import com.github.libretube.databinding.SuggestionRowBinding
|
||||||
import com.github.libretube.db.DatabaseHolder.Database
|
import com.github.libretube.db.DatabaseHolder.Database
|
||||||
import com.github.libretube.db.obj.SearchHistoryItem
|
import com.github.libretube.db.obj.SearchHistoryItem
|
||||||
import com.github.libretube.extensions.query
|
|
||||||
import com.github.libretube.ui.viewholders.SuggestionsViewHolder
|
import com.github.libretube.ui.viewholders.SuggestionsViewHolder
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.runBlocking
|
||||||
|
|
||||||
class SearchHistoryAdapter(
|
class SearchHistoryAdapter(
|
||||||
private var historyList: List<String>,
|
private var historyList: List<String>,
|
||||||
@ -36,10 +37,8 @@ class SearchHistoryAdapter(
|
|||||||
|
|
||||||
deleteHistory.setOnClickListener {
|
deleteHistory.setOnClickListener {
|
||||||
historyList -= historyQuery
|
historyList -= historyQuery
|
||||||
query {
|
runBlocking(Dispatchers.IO) {
|
||||||
Database.searchHistoryDao().delete(
|
Database.searchHistoryDao().delete(SearchHistoryItem(historyQuery))
|
||||||
SearchHistoryItem(query = historyQuery)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
notifyItemRemoved(position)
|
notifyItemRemoved(position)
|
||||||
notifyItemRangeChanged(position, itemCount)
|
notifyItemRangeChanged(position, itemCount)
|
||||||
|
@ -13,11 +13,13 @@ import com.github.libretube.api.RetrofitInstance
|
|||||||
import com.github.libretube.databinding.FragmentSearchBinding
|
import com.github.libretube.databinding.FragmentSearchBinding
|
||||||
import com.github.libretube.db.DatabaseHolder.Database
|
import com.github.libretube.db.DatabaseHolder.Database
|
||||||
import com.github.libretube.extensions.TAG
|
import com.github.libretube.extensions.TAG
|
||||||
import com.github.libretube.extensions.awaitQuery
|
|
||||||
import com.github.libretube.ui.activities.MainActivity
|
import com.github.libretube.ui.activities.MainActivity
|
||||||
import com.github.libretube.ui.adapters.SearchHistoryAdapter
|
import com.github.libretube.ui.adapters.SearchHistoryAdapter
|
||||||
import com.github.libretube.ui.adapters.SearchSuggestionsAdapter
|
import com.github.libretube.ui.adapters.SearchSuggestionsAdapter
|
||||||
import com.github.libretube.ui.models.SearchViewModel
|
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) {
|
class SearchFragment : Fragment(R.layout.fragment_search) {
|
||||||
private val binding by viewBinding(FragmentSearchBinding::bind)
|
private val binding by viewBinding(FragmentSearchBinding::bind)
|
||||||
@ -76,18 +78,19 @@ class SearchFragment : Fragment(R.layout.fragment_search) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun showHistory() {
|
private fun showHistory() {
|
||||||
val historyList = awaitQuery {
|
lifecycleScope.launch {
|
||||||
Database.searchHistoryDao().getAll().map { it.query }
|
val historyList = withContext(Dispatchers.IO) {
|
||||||
}
|
Database.searchHistoryDao().getAll().map { it.query }
|
||||||
if (historyList.isNotEmpty()) {
|
}
|
||||||
binding.suggestionsRecycler.adapter =
|
if (historyList.isNotEmpty()) {
|
||||||
SearchHistoryAdapter(
|
binding.suggestionsRecycler.adapter = SearchHistoryAdapter(
|
||||||
historyList,
|
historyList,
|
||||||
(activity as MainActivity).searchView
|
(activity as MainActivity).searchView
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
binding.suggestionsRecycler.visibility = View.GONE
|
binding.suggestionsRecycler.visibility = View.GONE
|
||||||
binding.historyEmpty.visibility = View.VISIBLE
|
binding.historyEmpty.visibility = View.VISIBLE
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,7 @@ import com.github.libretube.helpers.PreferenceHelper
|
|||||||
import com.github.libretube.ui.adapters.SearchAdapter
|
import com.github.libretube.ui.adapters.SearchAdapter
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import retrofit2.HttpException
|
import retrofit2.HttpException
|
||||||
|
|
||||||
@ -125,12 +126,10 @@ class SearchResultFragment : Fragment(R.layout.fragment_search_result) {
|
|||||||
private fun addToHistory(query: String) {
|
private fun addToHistory(query: String) {
|
||||||
val searchHistoryEnabled =
|
val searchHistoryEnabled =
|
||||||
PreferenceHelper.getBoolean(PreferenceKeys.SEARCH_HISTORY_TOGGLE, true)
|
PreferenceHelper.getBoolean(PreferenceKeys.SEARCH_HISTORY_TOGGLE, true)
|
||||||
if (searchHistoryEnabled && query != "") {
|
if (searchHistoryEnabled && query.isNotEmpty()) {
|
||||||
DatabaseHelper.addToSearchHistory(
|
lifecycleScope.launch(Dispatchers.IO) {
|
||||||
SearchHistoryItem(
|
DatabaseHelper.addToSearchHistory(SearchHistoryItem(query))
|
||||||
query = query
|
}
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user