AquaStream/app/src/main/java/com/lagradost/cloudstream3/utils/DataStoreHelper.kt

170 lines
5.5 KiB
Kotlin
Raw Normal View History

2021-06-15 16:07:20 +00:00
package com.lagradost.cloudstream3.utils
import com.lagradost.cloudstream3.AcraApplication.Companion.getKey
import com.lagradost.cloudstream3.AcraApplication.Companion.getKeys
import com.lagradost.cloudstream3.AcraApplication.Companion.removeKey
import com.lagradost.cloudstream3.AcraApplication.Companion.setKey
import com.lagradost.cloudstream3.DubStatus
2021-07-30 14:09:57 +00:00
import com.lagradost.cloudstream3.SearchResponse
import com.lagradost.cloudstream3.TvType
2021-06-15 23:25:58 +00:00
import com.lagradost.cloudstream3.ui.WatchType
2021-06-15 16:07:20 +00:00
const val VIDEO_POS_DUR = "video_pos_dur"
2021-06-15 23:25:58 +00:00
const val RESULT_WATCH_STATE = "result_watch_state"
2021-07-30 14:09:57 +00:00
const val RESULT_WATCH_STATE_DATA = "result_watch_state_data"
2021-08-25 15:28:25 +00:00
const val RESULT_RESUME_WATCHING = "result_resume_watching"
2021-06-26 22:15:19 +00:00
const val RESULT_SEASON = "result_season"
const val RESULT_DUB = "result_dub"
2021-06-15 16:07:20 +00:00
object DataStoreHelper {
2021-07-18 23:57:04 +00:00
data class PosDur(val position: Long, val duration: Long)
2021-07-30 14:09:57 +00:00
2021-07-18 23:57:04 +00:00
fun PosDur.fixVisual(): PosDur {
if (duration <= 0) return PosDur(0, duration)
val percentage = position * 100 / duration
if (percentage <= 1) return PosDur(0, duration)
if (percentage <= 5) return PosDur(5 * duration / 100, duration)
if (percentage >= 95) return PosDur(duration, duration)
return this
}
2021-07-30 14:09:57 +00:00
data class BookmarkedData(
2021-07-30 23:41:54 +00:00
override val id: Int?,
val bookmarkedTime: Long,
val latestUpdatedTime: Long,
2021-07-30 14:09:57 +00:00
override val name: String,
override val url: String,
override val apiName: String,
override val type: TvType,
override val posterUrl: String?,
2021-08-25 15:28:25 +00:00
val year: Int?,
) : SearchResponse
data class ResumeWatchingResult(
override val name: String,
override val url: String,
override val apiName: String,
override val type: TvType,
override val posterUrl: String?,
val watchPos: PosDur?,
override val id: Int?,
val parentId: Int?,
val episode: Int?,
val season: Int?,
val isFromDownload: Boolean,
2021-07-30 14:09:57 +00:00
) : SearchResponse
2021-06-15 16:07:20 +00:00
var currentAccount: String = "0" //TODO ACCOUNT IMPLEMENTATION
fun getAllWatchStateIds(): List<Int>? {
2021-07-30 21:03:46 +00:00
val folder = "$currentAccount/$RESULT_WATCH_STATE"
return getKeys(folder)?.mapNotNull {
2021-07-30 23:41:54 +00:00
it.removePrefix("$folder/").toIntOrNull()
}
2021-07-30 21:03:46 +00:00
}
fun getAllResumeStateIds(): List<Int>? {
2021-08-25 15:28:25 +00:00
val folder = "$currentAccount/$RESULT_RESUME_WATCHING"
return getKeys(folder)?.mapNotNull {
2021-08-25 15:28:25 +00:00
it.removePrefix("$folder/").toIntOrNull()
}
}
fun setLastWatched(
2021-08-25 15:28:25 +00:00
parentId: Int?,
episodeId: Int?,
episode: Int?,
season: Int?,
isFromDownload: Boolean = false
) {
if (parentId == null || episodeId == null) return
setKey(
"$currentAccount/$RESULT_RESUME_WATCHING",
parentId.toString(),
VideoDownloadHelper.ResumeWatching(
parentId,
episodeId,
episode,
season,
System.currentTimeMillis(),
isFromDownload
)
)
}
fun removeLastWatched(parentId: Int?) {
2021-08-25 15:28:25 +00:00
if (parentId == null) return
removeKey("$currentAccount/$RESULT_RESUME_WATCHING", parentId.toString())
}
fun getLastWatched(id: Int?): VideoDownloadHelper.ResumeWatching? {
2021-08-25 15:28:25 +00:00
if (id == null) return null
return getKey(
"$currentAccount/$RESULT_RESUME_WATCHING",
id.toString(),
)
}
fun setBookmarkedData(id: Int?, data: BookmarkedData) {
2021-07-30 14:09:57 +00:00
if (id == null) return
2021-07-30 23:41:54 +00:00
setKey("$currentAccount/$RESULT_WATCH_STATE_DATA", id.toString(), data)
2021-07-30 14:09:57 +00:00
}
fun getBookmarkedData(id: Int?): BookmarkedData? {
2021-07-30 14:09:57 +00:00
if (id == null) return null
2021-07-30 23:41:54 +00:00
return getKey("$currentAccount/$RESULT_WATCH_STATE_DATA", id.toString())
2021-07-30 14:09:57 +00:00
}
fun setViewPos(id: Int?, pos: Long, dur: Long) {
2021-06-15 23:25:58 +00:00
if (id == null) return
2021-12-24 15:35:44 +00:00
if (dur < 30_000) return // too short
2021-06-15 16:07:20 +00:00
setKey("$currentAccount/$VIDEO_POS_DUR", id.toString(), PosDur(pos, dur))
}
fun getViewPos(id: Int): PosDur? {
2021-06-15 23:25:58 +00:00
return getKey("$currentAccount/$VIDEO_POS_DUR", id.toString(), null)
}
fun getDub(id: Int): DubStatus {
return DubStatus.values()[getKey("$currentAccount/$RESULT_DUB", id.toString()) ?: 0]
}
fun setDub(id: Int, status: DubStatus) {
setKey("$currentAccount/$RESULT_DUB", id.toString(), status.ordinal)
}
fun setResultWatchState(id: Int?, status: Int) {
2021-06-15 23:25:58 +00:00
if (id == null) return
2021-07-30 23:41:54 +00:00
val folder = "$currentAccount/$RESULT_WATCH_STATE"
if (status == WatchType.NONE.internalId) {
removeKey(folder, id.toString())
removeKey("$currentAccount/$RESULT_WATCH_STATE_DATA", id.toString())
} else {
setKey(folder, id.toString(), status)
}
2021-06-15 23:25:58 +00:00
}
fun getResultWatchState(id: Int): WatchType {
2021-06-15 23:25:58 +00:00
return WatchType.fromInternalId(getKey<Int>("$currentAccount/$RESULT_WATCH_STATE", id.toString(), null))
2021-06-15 16:07:20 +00:00
}
2021-06-26 22:15:19 +00:00
fun getResultSeason(id: Int): Int {
return getKey("$currentAccount/$RESULT_SEASON", id.toString()) ?: -1
2021-06-26 22:15:19 +00:00
}
2021-07-30 14:09:57 +00:00
fun setResultSeason(id: Int, value: Int?) {
2021-12-13 18:41:33 +00:00
setKey("$currentAccount/$RESULT_SEASON", id.toString(), value)
}
fun addSync(id: Int, idPrefix: String, url: String) {
2021-12-13 18:41:33 +00:00
setKey("${idPrefix}_sync", id.toString(), url)
}
fun getSync(id: Int, idPrefixes: List<String>): List<String?> {
2021-12-13 18:41:33 +00:00
return idPrefixes.map { idPrefix ->
getKey("${idPrefix}_sync", id.toString())
}
2021-06-26 22:15:19 +00:00
}
2021-06-15 16:07:20 +00:00
}