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
|
2022-09-21 17:45:07 -04:00
|
|
|
import eu.kanade.domain.download.service.DownloadPreferences
|
2022-06-18 10:01:03 -04:00
|
|
|
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-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
|
|
|
|
2022-08-18 14:07:13 -04:00
|
|
|
val domainManga = toDomainManga()!!
|
2020-05-10 23:10:31 -04:00
|
|
|
when {
|
2022-08-18 14:07:13 -04:00
|
|
|
domainManga.isLocal() -> {
|
2020-05-10 23:10:31 -04:00
|
|
|
cover_last_modified = Date().time
|
|
|
|
}
|
2022-08-18 14:07:13 -04:00
|
|
|
domainManga.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-08-31 14:41:35 -04:00
|
|
|
fun DomainManga.removeCovers(coverCache: CoverCache = Injekt.get()): DomainManga {
|
|
|
|
if (isLocal()) return this
|
|
|
|
coverCache.deleteFromCache(this, true)
|
|
|
|
return copy(coverLastModified = Date().time)
|
|
|
|
}
|
|
|
|
|
2022-09-21 17:45:07 -04:00
|
|
|
fun DomainManga.shouldDownloadNewChapters(dbCategories: List<Long>, preferences: DownloadPreferences): Boolean {
|
2020-05-30 09:17:27 -04:00
|
|
|
if (!favorite) return false
|
|
|
|
|
2022-07-13 09:18:17 -04:00
|
|
|
val categories = dbCategories.ifEmpty { listOf(0L) }
|
2022-07-07 08:01:15 -04:00
|
|
|
|
2020-05-17 17:33:26 -04:00
|
|
|
// Boolean to determine if user wants to automatically download new chapters.
|
2022-08-22 17:37:54 -04:00
|
|
|
val downloadNewChapters = preferences.downloadNewChapters().get()
|
|
|
|
if (!downloadNewChapters) return false
|
2020-05-17 17:33:26 -04:00
|
|
|
|
2022-08-06 15:27:25 -04:00
|
|
|
val includedCategories = preferences.downloadNewChapterCategories().get().map { it.toLong() }
|
|
|
|
val excludedCategories = preferences.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(),
|
2022-08-10 16:11:12 -04:00
|
|
|
) {
|
|
|
|
if (isLocal()) {
|
2022-06-18 10:01:03 -04:00
|
|
|
LocalSource.updateCover(context, toDbManga(), stream)
|
|
|
|
updateManga.awaitUpdateCoverLastModified(id)
|
|
|
|
} else if (favorite) {
|
|
|
|
coverCache.setCustomCoverToCache(toDbManga(), stream)
|
|
|
|
updateManga.awaitUpdateCoverLastModified(id)
|
|
|
|
}
|
|
|
|
}
|