2022-08-14 13:29:05 +05:30
|
|
|
package com.github.libretube.db
|
2022-08-13 23:34:07 +05:30
|
|
|
|
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
|
|
|
|
import com.github.libretube.db.obj.WatchPosition
|
2022-08-13 23:34:07 +05:30
|
|
|
import com.github.libretube.obj.Streams
|
2022-08-14 02:49:07 +05:30
|
|
|
import com.github.libretube.preferences.PreferenceHelper
|
|
|
|
import com.github.libretube.preferences.PreferenceKeys
|
2022-08-27 18:43:24 +05:30
|
|
|
import com.github.libretube.extensions.toID
|
2022-08-13 23:34:07 +05:30
|
|
|
|
|
|
|
object DatabaseHelper {
|
|
|
|
fun addToWatchHistory(videoId: String, streams: Streams) {
|
|
|
|
val watchHistoryItem = WatchHistoryItem(
|
|
|
|
videoId,
|
|
|
|
streams.title,
|
|
|
|
streams.uploadDate,
|
|
|
|
streams.uploader,
|
|
|
|
streams.uploaderUrl.toID(),
|
|
|
|
streams.uploaderAvatar,
|
|
|
|
streams.thumbnailUrl,
|
|
|
|
streams.duration
|
|
|
|
)
|
2022-08-13 23:41:56 +05:30
|
|
|
Thread {
|
2022-08-14 13:29:05 +05:30
|
|
|
DatabaseHolder.db.watchHistoryDao().insertAll(watchHistoryItem)
|
2022-08-14 13:25:28 +05:30
|
|
|
val maxHistorySize =
|
|
|
|
PreferenceHelper.getString(PreferenceKeys.WATCH_HISTORY_SIZE, "unlimited")
|
2022-08-14 02:49:07 +05:30
|
|
|
if (maxHistorySize == "unlimited") return@Thread
|
|
|
|
|
|
|
|
// delete the first watch history entry if the limit is reached
|
2022-08-14 13:29:05 +05:30
|
|
|
val watchHistory = DatabaseHolder.db.watchHistoryDao().getAll()
|
2022-08-14 02:49:07 +05:30
|
|
|
if (watchHistory.size > maxHistorySize.toInt()) {
|
2022-08-14 13:29:05 +05:30
|
|
|
DatabaseHolder.db.watchHistoryDao()
|
2022-08-14 02:49:07 +05:30
|
|
|
.delete(watchHistory.first())
|
|
|
|
}
|
2022-08-13 23:41:56 +05:30
|
|
|
}.start()
|
2022-08-13 23:34:07 +05:30
|
|
|
}
|
2022-08-14 01:33:11 +05:30
|
|
|
|
|
|
|
fun removeFromWatchHistory(index: Int) {
|
|
|
|
Thread {
|
2022-08-14 13:29:05 +05:30
|
|
|
DatabaseHolder.db.watchHistoryDao().delete(
|
|
|
|
DatabaseHolder.db.watchHistoryDao().getAll()[index]
|
2022-08-14 01:33:11 +05:30
|
|
|
)
|
|
|
|
}.start()
|
|
|
|
}
|
|
|
|
|
|
|
|
fun saveWatchPosition(videoId: String, position: Long) {
|
|
|
|
val watchPosition = WatchPosition(
|
|
|
|
videoId,
|
|
|
|
position
|
|
|
|
)
|
|
|
|
Thread {
|
2022-08-14 13:29:05 +05:30
|
|
|
DatabaseHolder.db.watchPositionDao().insertAll(watchPosition)
|
2022-08-14 01:33:11 +05:30
|
|
|
}.start()
|
|
|
|
}
|
|
|
|
|
|
|
|
fun removeWatchPosition(videoId: String) {
|
|
|
|
Thread {
|
2022-08-14 13:29:05 +05:30
|
|
|
DatabaseHolder.db.watchPositionDao().delete(
|
|
|
|
DatabaseHolder.db.watchPositionDao().findById(videoId)
|
2022-08-14 01:33:11 +05:30
|
|
|
)
|
|
|
|
}.start()
|
|
|
|
}
|
2022-08-15 13:46:53 +05:30
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
2022-08-13 23:34:07 +05:30
|
|
|
}
|