forked from recloudstream/cloudstream
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackup_LibraryViewModelCache.kt
More file actions
256 lines (221 loc) · 9.7 KB
/
backup_LibraryViewModelCache.kt
File metadata and controls
256 lines (221 loc) · 9.7 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package com.lagradost.cloudstream3.ui.library
import androidx.annotation.StringRes
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.lagradost.cloudstream3.CloudStreamApp.Companion.getKey
import com.lagradost.cloudstream3.CloudStreamApp.Companion.setKey
import com.lagradost.cloudstream3.MainActivity
import com.lagradost.cloudstream3.R
import com.lagradost.cloudstream3.SearchResponse
import com.lagradost.cloudstream3.mvvm.Resource
import com.lagradost.cloudstream3.mvvm.throwAbleToResource
import com.lagradost.cloudstream3.syncproviders.AccountManager
import com.lagradost.cloudstream3.syncproviders.SyncAPI
import com.lagradost.cloudstream3.utils.Coroutines.ioSafe
import com.lagradost.cloudstream3.utils.DataStoreHelper
import com.lagradost.cloudstream3.utils.DataStoreHelper.currentAccount
import com.lagradost.cloudstream3.utils.LocalLibraryCache
import kotlinx.coroutines.launch
enum class ListSorting(@StringRes val stringRes: Int) {
Query(R.string.none),
RatingHigh(R.string.sort_rating_desc),
RatingLow(R.string.sort_rating_asc),
UpdatedNew(R.string.sort_updated_new),
UpdatedOld(R.string.sort_updated_old),
AlphabeticalA(R.string.sort_alphabetical_a),
AlphabeticalZ(R.string.sort_alphabetical_z),
ReleaseDateNew(R.string.sort_release_date_new),
ReleaseDateOld(R.string.sort_release_date_old),
CacheFirst(R.string.sort_cache_first), // New sorting option
}
const val LAST_SYNC_API_KEY = "last_sync_api"
const val PREFER_LOCAL_CACHE_KEY = "prefer_local_cache"
class LibraryViewModel : ViewModel() {
private val localLibraryCache = LocalLibraryCache(MainActivity.getInstance())
fun switchPage(page: Int) {
_currentPage.postValue(page)
}
private val _currentPage: MutableLiveData<Int> = MutableLiveData(0)
val currentPage: LiveData<Int> = _currentPage
private val _pages: MutableLiveData<Resource<List<SyncAPI.Page>>> = MutableLiveData(null)
val pages: LiveData<Resource<List<SyncAPI.Page>>> = _pages
private val _currentApiName: MutableLiveData<String> = MutableLiveData("")
val currentApiName: LiveData<String> = _currentApiName
private val availableSyncApis
get() = AccountManager.syncApis.filter { it.isAvailable }
var currentSyncApi = availableSyncApis.let { allApis ->
val lastSelection = getKey<String>("$currentAccount/$LAST_SYNC_API_KEY")
availableSyncApis.firstOrNull { it.name == lastSelection } ?: allApis.firstOrNull()
}
private set(value) {
field = value
setKey("$currentAccount/$LAST_SYNC_API_KEY", field?.name)
}
val availableApiNames: List<String>
get() = availableSyncApis.map { it.name }
var sortingMethods = emptyList<ListSorting>()
private set
var currentSortingMethod: ListSorting? = sortingMethods.firstOrNull()
private set
fun switchList(name: String) {
currentSyncApi = availableSyncApis[availableApiNames.indexOf(name)]
_currentApiName.postValue(currentSyncApi?.name)
reloadPages(true)
}
fun sort(method: ListSorting, query: String? = null) = ioSafe {
val value = _pages.value ?: return@ioSafe
if (value is Resource.Success) {
sort(method, query, value.value)
}
}
private fun sort(method: ListSorting, query: String? = null, items: List<SyncAPI.Page>) {
currentSortingMethod = method
DataStoreHelper.librarySortingMode = method.ordinal
when (method) {
ListSorting.CacheFirst -> {
// Load cached data first, then online data
val cachedPages = loadCachedPages()
val onlinePages = loadOnlinePages(query, items)
val allPages = mergeCachedAndOnlinePages(cachedPages, onlinePages)
_pages.postValue(Resource.Success(allPages))
}
else -> {
// Original behavior - load from online API
items.forEach { page ->
page.sort(method, query)
}
_pages.postValue(Resource.Success(items))
}
}
}
private fun loadCachedPages(): List<SyncAPI.Page> {
val cachedSeries = localLibraryCache.getAllCachedSeries()
return cachedSeries.map { series ->
val episodes = localLibraryCache.getCachedEpisodes(series.id)
SyncAPI.Page(
name = series.name,
items = episodes.map { episode ->
SearchResponse(
id = episode.id.toInt(),
name = episode.name,
poster = episode.posterPath,
url = "local://${episode.id}", // Special URL for local content
type = SearchResponse.Type.TVSHOW,
apiName = "Local Cache",
posterPath = episode.posterPath,
rating = null,
year = null,
episode = episode.episode,
season = episode.season,
date = null,
description = episode.description,
duration = episode.duration,
comesFrom = "Local Cache",
skipintro = if (episode.skipIntroStart != null && episode.skipIntroEnd != null) {
com.lagradost.cloudstream3.SkipIntroData(episode.skipIntroStart, episode.skipIntroEnd)
} else null,
parentId = series.id.toInt()
)
}
)
}
}
private fun loadOnlinePages(query: String?, cachedPages: List<SyncAPI.Page>): List<SyncAPI.Page> {
// Only load online data if not cache-first mode or if cache is empty
if (cachedPages.isNotEmpty() && currentSortingMethod == ListSorting.CacheFirst) {
return emptyList()
}
return ioSafe {
currentSyncApi?.let { repo ->
val libraryResource = repo.library()
val err = libraryResource.exceptionOrNull()
if (err != null) {
return@let emptyList()
}
val library = libraryResource.getOrNull()
if (library == null) {
return@let emptyList()
}
sortingMethods = library.supportedListSorting.toList()
repo.requireLibraryRefresh = false
library.allLibraryLists.map {
SyncAPI.Page(
it.name,
it.items
)
}
} ?: emptyList()
}
}
private fun mergeCachedAndOnlinePages(
cachedPages: List<SyncAPI.Page>,
onlinePages: List<SyncAPI.Page>
): List<SyncAPI.Page> {
val allSeries = mutableMapOf<String, SyncAPI.Page>()
// Add cached series first
cachedPages.forEach { page ->
allSeries[page.name] = page
}
// Add online series that aren't cached
onlinePages.forEach { page ->
if (!allSeries.containsKey(page.name)) {
allSeries[page.name] = page
}
}
return allSeries.values.toList()
}
// New method to cache series data
fun cacheSeriesData(seriesId: String, searchResponse: SearchResponse) = viewModelScope.launch {
try {
// Get episodes from the API
currentSyncApi?.let { repo ->
val episodesResource = repo.episodes(seriesId.toInt())
val episodes = episodesResource.getOrNull() ?: emptyList()
localLibraryCache.cacheSeriesData(
seriesId = seriesId,
name = searchResponse.name,
posterUrl = searchResponse.poster,
bannerUrl = null, // Could be added if available
description = searchResponse.description,
rating = searchResponse.rating,
year = searchResponse.year,
status = null, // Could be derived from API
episodes = episodes
)
}
} catch (e: Exception) {
// Handle error
}
}
// New method to check if series is cached
fun isSeriesCached(seriesId: String): Boolean {
return localLibraryCache.getCachedSeries(seriesId) != null
}
// New method to get cached episode with file path
fun getCachedEpisodeWithFile(seriesId: String, episodeId: String): com.lagradost.cloudstream3.database.CachedEpisodeEntity? {
val episodes = localLibraryCache.getCachedEpisodes(seriesId)
return episodes.find { it.id == episodeId && it.filePath != null }
}
// New method to update episode file path
fun updateEpisodeFilePath(seriesId: String, episodeId: String, filePath: String) = viewModelScope.launch {
localLibraryCache.updateEpisodeFilePath(seriesId, episodeId, filePath)
}
// New method to clear cache for a series
fun clearSeriesCache(seriesId: String) = viewModelScope.launch {
localLibraryCache.clearCache(seriesId)
reloadPages(true) // Reload to reflect cache clear
}
// New method to get total cache size
fun getTotalCacheSize(): Long {
return localLibraryCache.getTotalCacheSize()
}
init {
MainActivity.reloadLibraryEvent += ::reloadPages
}
override fun onCleared() {
MainActivity.reloadLibraryEvent -= ::reloadPages
super.onCleared()
}
}