-
Notifications
You must be signed in to change notification settings - Fork 136
/
DatabaseRepo.kt
182 lines (149 loc) · 6.75 KB
/
DatabaseRepo.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package com.yenaly.han1meviewer.logic
import com.yenaly.han1meviewer.Preferences
import com.yenaly.han1meviewer.logic.dao.DownloadDatabase
import com.yenaly.han1meviewer.logic.dao.HistoryDatabase
import com.yenaly.han1meviewer.logic.dao.MiscellanyDatabase
import com.yenaly.han1meviewer.logic.entity.HKeyframeEntity
import com.yenaly.han1meviewer.logic.entity.HKeyframeHeader
import com.yenaly.han1meviewer.logic.entity.HKeyframeType
import com.yenaly.han1meviewer.logic.entity.HanimeDownloadEntity
import com.yenaly.han1meviewer.logic.entity.SearchHistoryEntity
import com.yenaly.han1meviewer.logic.entity.WatchHistoryEntity
import com.yenaly.yenaly_libs.utils.applicationContext
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flow
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.decodeFromStream
/**
* @project Hanime1
* @author Yenaly Liew
* @time 2022/06/22 022 23:00
*/
object DatabaseRepo {
object HKeyframe {
private val hKeyframeDao = MiscellanyDatabase.instance.hKeyframeDao
fun loadAll(keyword: String? = null) =
if (keyword != null) hKeyframeDao.loadAll(keyword)
else hKeyframeDao.loadAll()
// #issue-106: 剧集分类
@OptIn(ExperimentalSerializationApi::class)
fun loadAllShared(): Flow<List<HKeyframeType>> = flow {
val res = applicationContext.assets.let { assets ->
assets.list("h_keyframes")?.asSequence()
?.filter { it.endsWith(".json") }
?.mapNotNull { fileName ->
try {
assets.open("h_keyframes/$fileName").use { inputStream ->
Json.decodeFromStream<HKeyframeEntity>(inputStream)
}
} catch (e: Exception) {
e.printStackTrace()
null
}
}
?.sortedWith(
compareBy<HKeyframeEntity> { it.group }.thenBy { it.episode }
)
?.groupBy { it.group ?: "???" }
?.flatMap { (group, entities) ->
listOf(HKeyframeHeader(title = group, attached = entities)) + entities
}
.orEmpty()
}
emit(res)
}
suspend fun findBy(videoCode: String) =
hKeyframeDao.findBy(videoCode)
@OptIn(ExperimentalSerializationApi::class)
fun observe(videoCode: String): Flow<HKeyframeEntity?> {
if (Preferences.sharedHKeyframesEnable) {
return flow t@{
val find = hKeyframeDao.findBy(videoCode)
if (find == null || Preferences.sharedHKeyframesUseFirst) {
applicationContext.assets
.open("h_keyframes/$videoCode.json")
.use { inputStream ->
val entity = Json.decodeFromStream<HKeyframeEntity>(inputStream)
[email protected](entity)
}
} else {
hKeyframeDao.observe(videoCode).collect {
}
}
}.catch t@{ e ->
e.printStackTrace()
hKeyframeDao.observe(videoCode).collect {
}
}
}
return hKeyframeDao.observe(videoCode)
}
suspend fun insert(entity: HKeyframeEntity) = hKeyframeDao.insert(entity)
suspend fun update(entity: HKeyframeEntity) = hKeyframeDao.update(entity)
suspend fun delete(entity: HKeyframeEntity) =
hKeyframeDao.delete(entity)
suspend fun modifyKeyframe(
videoCode: String,
oldKeyframe: HKeyframeEntity.Keyframe, keyframe: HKeyframeEntity.Keyframe,
) = hKeyframeDao.modifyKeyframe(videoCode, oldKeyframe, keyframe)
suspend fun appendKeyframe(
videoCode: String, title: String,
keyframe: HKeyframeEntity.Keyframe,
) = hKeyframeDao.appendKeyframe(videoCode, title, keyframe)
suspend fun removeKeyframe(
videoCode: String,
keyframe: HKeyframeEntity.Keyframe,
) = hKeyframeDao.removeKeyframe(videoCode, keyframe)
}
object SearchHistory {
private val searchHistoryDao = HistoryDatabase.instance.searchHistory
@JvmOverloads
fun loadAll(keyword: String? = null) =
if (keyword.isNullOrBlank()) searchHistoryDao.loadAll()
else searchHistoryDao.loadAll(keyword)
suspend fun delete(history: SearchHistoryEntity) =
searchHistoryDao.delete(history)
suspend fun insert(history: SearchHistoryEntity) =
searchHistoryDao.insertOrUpdate(history)
suspend fun deleteByKeyword(query: String) =
searchHistoryDao.deleteByKeyword(query)
}
object WatchHistory {
private val watchHistoryDao = HistoryDatabase.instance.watchHistory
fun loadAll() =
watchHistoryDao.loadAll()
suspend fun delete(history: WatchHistoryEntity) =
watchHistoryDao.delete(history)
suspend fun deleteAll() =
watchHistoryDao.deleteAll()
suspend fun insert(history: WatchHistoryEntity) =
watchHistoryDao.insertOrUpdate(history)
}
object HanimeDownload {
private val hanimeDownloadDao = DownloadDatabase.instance.hanimeDownloadDao
fun loadAllDownloadingHanime() =
hanimeDownloadDao.loadAllDownloadingHanime()
fun loadAllDownloadedHanime(
sortedBy: HanimeDownloadEntity.SortedBy,
ascending: Boolean,
) = hanimeDownloadDao.loadAllDownloadedHanime(sortedBy, ascending)
suspend fun deleteBy(videoCode: String, quality: String) =
hanimeDownloadDao.deleteBy(videoCode, quality)
suspend fun pauseAll() =
hanimeDownloadDao.pauseAll()
suspend fun delete(entity: HanimeDownloadEntity) =
hanimeDownloadDao.delete(entity)
suspend fun insert(entity: HanimeDownloadEntity) =
hanimeDownloadDao.insert(entity)
suspend fun update(entity: HanimeDownloadEntity) =
hanimeDownloadDao.update(entity)
suspend fun findBy(videoCode: String, quality: String) =
hanimeDownloadDao.findBy(videoCode, quality)
suspend fun isExist(videoCode: String, quality: String) =
hanimeDownloadDao.isExist(videoCode, quality)
}
}