2022-08-14 13:29:05 +05:30
|
|
|
package com.github.libretube.db
|
2022-08-13 23:34:07 +05:30
|
|
|
|
2022-09-20 23:30:51 +05:30
|
|
|
import com.github.libretube.api.obj.Streams
|
2022-09-08 21:59:00 +05:30
|
|
|
import com.github.libretube.constants.PreferenceKeys
|
2022-09-18 22:54:31 +05:30
|
|
|
import com.github.libretube.db.DatabaseHolder.Companion.Database
|
2022-08-15 13:46:53 +05:30
|
|
|
import com.github.libretube.db.obj.SearchHistoryItem
|
2022-08-14 13:29:05 +05:30
|
|
|
import com.github.libretube.db.obj.WatchHistoryItem
|
2022-09-22 21:22:00 +05:30
|
|
|
import com.github.libretube.extensions.query
|
2022-08-27 18:45:07 +05:30
|
|
|
import com.github.libretube.extensions.toID
|
2023-01-31 21:13:39 +05:30
|
|
|
import com.github.libretube.helpers.PreferenceHelper
|
2023-02-09 05:48:28 +05:30
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.withContext
|
2022-08-13 23:34:07 +05:30
|
|
|
|
|
|
|
object DatabaseHelper {
|
2022-11-18 23:12:59 +05:30
|
|
|
private const val MAX_SEARCH_HISTORY_SIZE = 20
|
|
|
|
|
2023-02-09 05:48:28 +05:30
|
|
|
suspend fun addToWatchHistory(videoId: String, streams: Streams) = withContext(Dispatchers.IO) {
|
2022-08-13 23:34:07 +05:30
|
|
|
val watchHistoryItem = WatchHistoryItem(
|
|
|
|
videoId,
|
|
|
|
streams.title,
|
2023-01-18 07:01:06 +05:30
|
|
|
streams.uploadDate.toString(),
|
2022-08-13 23:34:07 +05:30
|
|
|
streams.uploader,
|
2023-01-18 07:01:06 +05:30
|
|
|
streams.uploaderUrl.toID(),
|
2022-08-13 23:34:07 +05:30
|
|
|
streams.uploaderAvatar,
|
|
|
|
streams.thumbnailUrl,
|
|
|
|
streams.duration
|
|
|
|
)
|
2023-02-09 05:48:28 +05:30
|
|
|
Database.watchHistoryDao().insertAll(listOf(watchHistoryItem))
|
|
|
|
val maxHistorySize = PreferenceHelper.getString(PreferenceKeys.WATCH_HISTORY_SIZE, "100")
|
|
|
|
if (maxHistorySize == "unlimited") {
|
|
|
|
return@withContext
|
|
|
|
}
|
2022-08-14 02:49:07 +05:30
|
|
|
|
2023-02-09 05:48:28 +05:30
|
|
|
// delete the first watch history entry if the limit is reached
|
|
|
|
val watchHistory = Database.watchHistoryDao().getAll()
|
|
|
|
if (watchHistory.size > maxHistorySize.toInt()) {
|
|
|
|
Database.watchHistoryDao().delete(watchHistory.first())
|
2022-09-22 21:22:00 +05:30
|
|
|
}
|
2022-08-13 23:34:07 +05:30
|
|
|
}
|
2022-08-14 01:33:11 +05:30
|
|
|
|
2022-08-15 13:46:53 +05:30
|
|
|
fun addToSearchHistory(searchHistoryItem: SearchHistoryItem) {
|
2022-09-22 21:22:00 +05:30
|
|
|
query {
|
2022-09-18 22:54:31 +05:30
|
|
|
Database.searchHistoryDao().insertAll(searchHistoryItem)
|
2022-08-15 13:46:53 +05:30
|
|
|
|
|
|
|
// delete the first watch history entry if the limit is reached
|
2022-09-18 22:54:31 +05:30
|
|
|
val searchHistory = Database.searchHistoryDao().getAll()
|
2022-11-18 23:12:59 +05:30
|
|
|
if (searchHistory.size > MAX_SEARCH_HISTORY_SIZE) {
|
2022-09-18 22:54:31 +05:30
|
|
|
Database.searchHistoryDao()
|
2022-08-15 13:46:53 +05:30
|
|
|
.delete(searchHistory.first())
|
|
|
|
}
|
2022-09-22 21:22:00 +05:30
|
|
|
}
|
2022-08-15 13:46:53 +05:30
|
|
|
}
|
2022-08-13 23:34:07 +05:30
|
|
|
}
|