2020-05-10 11:15:25 -04:00
|
|
|
package eu.kanade.tachiyomi.util
|
|
|
|
|
|
|
|
import eu.kanade.tachiyomi.data.cache.CoverCache
|
|
|
|
import eu.kanade.tachiyomi.data.database.DatabaseHelper
|
|
|
|
import eu.kanade.tachiyomi.data.database.models.Manga
|
2020-05-17 17:33:26 -04:00
|
|
|
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
|
2020-05-10 11:15:25 -04:00
|
|
|
import eu.kanade.tachiyomi.source.LocalSource
|
2020-05-10 23:10:31 -04:00
|
|
|
import eu.kanade.tachiyomi.source.model.SManga
|
2020-05-10 11:15:25 -04:00
|
|
|
import java.util.Date
|
|
|
|
|
|
|
|
fun Manga.isLocal() = source == LocalSource.ID
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Call before updating [Manga.thumbnail_url] to ensure old cover can be cleared from cache
|
|
|
|
*/
|
2020-05-10 23:10:31 -04:00
|
|
|
fun Manga.prepUpdateCover(coverCache: CoverCache, remoteManga: SManga, refreshSameUrl: Boolean) {
|
|
|
|
// Never refresh covers if the new url is null, as the current url has possibly become invalid
|
|
|
|
val newUrl = remoteManga.thumbnail_url ?: return
|
|
|
|
|
2020-05-23 15:19:17 -04:00
|
|
|
// Never refresh covers if the url is empty to avoid "losing" existing covers
|
|
|
|
if (newUrl.isEmpty()) return
|
|
|
|
|
2020-05-10 23:10:31 -04:00
|
|
|
if (!refreshSameUrl && thumbnail_url == newUrl) return
|
2020-05-10 11:15:25 -04:00
|
|
|
|
2020-05-10 23:10:31 -04:00
|
|
|
when {
|
|
|
|
isLocal() -> {
|
|
|
|
cover_last_modified = Date().time
|
|
|
|
}
|
|
|
|
hasCustomCover(coverCache) -> {
|
|
|
|
coverCache.deleteFromCache(this, false)
|
|
|
|
}
|
|
|
|
else -> {
|
|
|
|
cover_last_modified = Date().time
|
|
|
|
coverCache.deleteFromCache(this, false)
|
|
|
|
}
|
2020-05-10 11:15:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-10 23:10:31 -04:00
|
|
|
fun Manga.hasCustomCover(coverCache: CoverCache): Boolean {
|
|
|
|
return coverCache.getCustomCoverFile(this).exists()
|
|
|
|
}
|
|
|
|
|
2020-05-10 11:15:25 -04:00
|
|
|
fun Manga.removeCovers(coverCache: CoverCache) {
|
|
|
|
if (isLocal()) return
|
|
|
|
|
|
|
|
cover_last_modified = Date().time
|
|
|
|
coverCache.deleteFromCache(this, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun Manga.updateCoverLastModified(db: DatabaseHelper) {
|
|
|
|
cover_last_modified = Date().time
|
|
|
|
db.updateMangaCoverLastModified(this).executeAsBlocking()
|
|
|
|
}
|
2020-05-17 17:33:26 -04:00
|
|
|
|
|
|
|
fun Manga.shouldDownloadNewChapters(db: DatabaseHelper, prefs: PreferencesHelper): Boolean {
|
2020-05-30 09:17:27 -04:00
|
|
|
if (!favorite) return false
|
|
|
|
|
2020-05-17 17:33:26 -04:00
|
|
|
// Boolean to determine if user wants to automatically download new chapters.
|
|
|
|
val downloadNew = prefs.downloadNew().get()
|
|
|
|
if (!downloadNew) return false
|
|
|
|
|
|
|
|
val categoriesToDownload = prefs.downloadNewCategories().get().map(String::toInt)
|
2021-11-11 16:25:38 -05:00
|
|
|
val categoriesToExclude = prefs.downloadNewCategoriesExclude().get().map(String::toInt)
|
|
|
|
|
|
|
|
// Default: download from all categories
|
|
|
|
if (categoriesToDownload.isEmpty() && categoriesToExclude.isEmpty()) return true
|
2020-05-17 17:33:26 -04:00
|
|
|
|
2020-05-30 09:17:27 -04:00
|
|
|
// Get all categories, else default category (0)
|
|
|
|
val categoriesForManga =
|
|
|
|
db.getCategoriesForManga(this).executeAsBlocking()
|
|
|
|
.mapNotNull { it.id }
|
|
|
|
.takeUnless { it.isEmpty() } ?: listOf(0)
|
2020-05-17 17:33:26 -04:00
|
|
|
|
2021-11-11 16:25:38 -05:00
|
|
|
// In excluded category
|
2021-04-04 17:15:06 -04:00
|
|
|
if (categoriesForManga.intersect(categoriesToExclude).isNotEmpty()) return false
|
|
|
|
|
2021-11-11 16:25:38 -05:00
|
|
|
// In included category
|
2020-05-17 17:33:26 -04:00
|
|
|
return categoriesForManga.intersect(categoriesToDownload).isNotEmpty()
|
|
|
|
}
|