2020-05-10 11:15:25 -04:00
|
|
|
package eu.kanade.tachiyomi.util
|
|
|
|
|
2022-06-18 10:01:03 -04:00
|
|
|
import android.content.Context
|
|
|
|
import eu.kanade.domain.manga.interactor.UpdateManga
|
2022-07-03 13:21:48 -04:00
|
|
|
import eu.kanade.domain.manga.model.hasCustomCover
|
2022-06-18 10:01:03 -04:00
|
|
|
import eu.kanade.domain.manga.model.isLocal
|
|
|
|
import eu.kanade.domain.manga.model.toDbManga
|
2020-05-10 11:15:25 -04:00
|
|
|
import eu.kanade.tachiyomi.data.cache.CoverCache
|
|
|
|
import eu.kanade.tachiyomi.data.database.models.Manga
|
2022-07-03 13:21:48 -04:00
|
|
|
import eu.kanade.tachiyomi.data.database.models.toDomainManga
|
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
|
2022-06-10 09:33:59 -04:00
|
|
|
import uy.kohesive.injekt.Injekt
|
|
|
|
import uy.kohesive.injekt.api.get
|
2022-06-18 10:01:03 -04:00
|
|
|
import java.io.InputStream
|
2020-05-10 11:15:25 -04:00
|
|
|
import java.util.Date
|
2022-06-18 10:01:03 -04:00
|
|
|
import eu.kanade.domain.manga.model.Manga as DomainManga
|
2020-05-10 11:15:25 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 {
|
2022-07-03 13:21:48 -04:00
|
|
|
toDomainManga()!!.isLocal() -> {
|
2020-05-10 23:10:31 -04:00
|
|
|
cover_last_modified = Date().time
|
|
|
|
}
|
2022-07-03 13:21:48 -04:00
|
|
|
toDomainManga()!!.hasCustomCover(coverCache) -> {
|
2020-05-10 23:10:31 -04:00
|
|
|
coverCache.deleteFromCache(this, false)
|
|
|
|
}
|
|
|
|
else -> {
|
|
|
|
cover_last_modified = Date().time
|
|
|
|
coverCache.deleteFromCache(this, false)
|
|
|
|
}
|
2020-05-10 11:15:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-25 11:03:48 -04:00
|
|
|
fun Manga.removeCovers(coverCache: CoverCache = Injekt.get()): Int {
|
2022-07-03 13:21:48 -04:00
|
|
|
if (toDomainManga()!!.isLocal()) return 0
|
2020-05-10 11:15:25 -04:00
|
|
|
|
|
|
|
cover_last_modified = Date().time
|
2022-06-25 11:03:48 -04:00
|
|
|
return coverCache.deleteFromCache(this, true)
|
2020-05-10 11:15:25 -04:00
|
|
|
}
|
|
|
|
|
2022-07-02 16:12:06 -04:00
|
|
|
fun DomainManga.shouldDownloadNewChapters(categories: List<Long>, 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.
|
2022-04-24 16:36:14 -04:00
|
|
|
val downloadNewChapter = prefs.downloadNewChapter().get()
|
|
|
|
if (!downloadNewChapter) return false
|
2020-05-17 17:33:26 -04:00
|
|
|
|
2022-07-02 16:12:06 -04:00
|
|
|
val includedCategories = prefs.downloadNewChapterCategories().get().map { it.toLong() }
|
|
|
|
val excludedCategories = prefs.downloadNewChapterCategoriesExclude().get().map { it.toLong() }
|
2021-11-11 16:25:38 -05:00
|
|
|
|
2022-04-24 16:36:14 -04:00
|
|
|
// Default: Download from all categories
|
|
|
|
if (includedCategories.isEmpty() && excludedCategories.isEmpty()) return true
|
2020-05-17 17:33:26 -04:00
|
|
|
|
2021-11-11 16:25:38 -05:00
|
|
|
// In excluded category
|
2022-07-02 16:12:06 -04:00
|
|
|
if (categories.any { it in excludedCategories }) return false
|
2022-04-24 16:36:14 -04:00
|
|
|
|
|
|
|
// Included category not selected
|
|
|
|
if (includedCategories.isEmpty()) return true
|
2021-04-04 17:15:06 -04:00
|
|
|
|
2021-11-11 16:25:38 -05:00
|
|
|
// In included category
|
2022-07-02 16:12:06 -04:00
|
|
|
return categories.any { it in includedCategories }
|
2020-05-17 17:33:26 -04:00
|
|
|
}
|
2022-06-18 10:01:03 -04:00
|
|
|
|
|
|
|
suspend fun DomainManga.editCover(
|
|
|
|
context: Context,
|
|
|
|
stream: InputStream,
|
|
|
|
updateManga: UpdateManga = Injekt.get(),
|
|
|
|
coverCache: CoverCache = Injekt.get(),
|
|
|
|
): Boolean {
|
|
|
|
return if (isLocal()) {
|
|
|
|
LocalSource.updateCover(context, toDbManga(), stream)
|
|
|
|
updateManga.awaitUpdateCoverLastModified(id)
|
|
|
|
} else if (favorite) {
|
|
|
|
coverCache.setCustomCoverToCache(toDbManga(), stream)
|
|
|
|
updateManga.awaitUpdateCoverLastModified(id)
|
|
|
|
} else {
|
|
|
|
// We should never reach this block
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|