limit search history size

This commit is contained in:
Bnyro 2022-08-15 10:16:53 +02:00
parent 0084da6ea5
commit 80a24e3774
4 changed files with 22 additions and 10 deletions

View File

@ -1,7 +1,7 @@
package com.github.libretube.util
import kotlin.reflect.KProperty
import java.util.*
import kotlin.reflect.KProperty
class ResettableLazyManager {
// we synchronize to make sure the timing of a reset() call and new inits do not collide

View File

@ -1,5 +1,6 @@
package com.github.libretube.db
import com.github.libretube.db.obj.SearchHistoryItem
import com.github.libretube.db.obj.WatchHistoryItem
import com.github.libretube.db.obj.WatchPosition
import com.github.libretube.obj.Streams
@ -59,4 +60,18 @@ object DatabaseHelper {
)
}.start()
}
fun addToSearchHistory(searchHistoryItem: SearchHistoryItem) {
Thread {
DatabaseHolder.db.searchHistoryDao().insertAll(searchHistoryItem)
val maxHistorySize = 20
// delete the first watch history entry if the limit is reached
val searchHistory = DatabaseHolder.db.searchHistoryDao().getAll()
if (searchHistory.size > maxHistorySize) {
DatabaseHolder.db.searchHistoryDao()
.delete(searchHistory.first())
}
}.start()
}
}

View File

@ -95,7 +95,6 @@ import com.google.android.exoplayer2.upstream.DefaultHttpDataSource
import com.google.android.exoplayer2.util.RepeatModeUtil
import com.google.android.exoplayer2.video.VideoSize
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlin.math.abs
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@ -103,6 +102,7 @@ import org.chromium.net.CronetEngine
import retrofit2.HttpException
import java.io.IOException
import java.util.concurrent.Executors
import kotlin.math.abs
class PlayerFragment : BaseFragment() {

View File

@ -11,11 +11,10 @@ import com.github.libretube.R
import com.github.libretube.adapters.SearchAdapter
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.databinding.FragmentSearchResultBinding
import com.github.libretube.db.DatabaseHolder
import com.github.libretube.db.DatabaseHelper
import com.github.libretube.db.obj.SearchHistoryItem
import com.github.libretube.extensions.BaseFragment
import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.await
import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.preferences.PreferenceKeys
import com.github.libretube.util.hideKeyboard
@ -135,13 +134,11 @@ class SearchResultFragment : BaseFragment() {
val searchHistoryEnabled =
PreferenceHelper.getBoolean(PreferenceKeys.SEARCH_HISTORY_TOGGLE, true)
if (searchHistoryEnabled && query != "") {
Thread {
DatabaseHolder.db.searchHistoryDao().insertAll(
SearchHistoryItem(
query = query
)
DatabaseHelper.addToSearchHistory(
SearchHistoryItem(
query = query
)
}.await()
)
}
}
}