LibreTube/app/src/main/java/com/github/libretube/database/DatabaseHelper.kt

63 lines
2.1 KiB
Kotlin
Raw Normal View History

2022-08-13 23:34:07 +05:30
package com.github.libretube.database
import com.github.libretube.obj.Streams
import com.github.libretube.obj.WatchHistoryItem
2022-08-14 01:33:11 +05:30
import com.github.libretube.obj.WatchPosition
2022-08-14 02:49:07 +05:30
import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.preferences.PreferenceKeys
2022-08-13 23:34:07 +05:30
import com.github.libretube.util.toID
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 {
DatabaseHolder.database.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
val watchHistory = DatabaseHolder.database.watchHistoryDao().getAll()
if (watchHistory.size > maxHistorySize.toInt()) {
DatabaseHolder.database.watchHistoryDao()
.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 {
DatabaseHolder.database.watchHistoryDao().delete(
DatabaseHolder.database.watchHistoryDao().getAll()[index]
)
}.start()
}
fun saveWatchPosition(videoId: String, position: Long) {
val watchPosition = WatchPosition(
videoId,
position
)
Thread {
DatabaseHolder.database.watchPositionDao().insertAll(watchPosition)
}.start()
}
fun removeWatchPosition(videoId: String) {
Thread {
DatabaseHolder.database.watchPositionDao().delete(
DatabaseHolder.database.watchPositionDao().findById(videoId)
)
}.start()
}
2022-08-13 23:34:07 +05:30
}