From d5710bfd1096a5c82c69d622b22e7fc7c0469d2c Mon Sep 17 00:00:00 2001 From: Jays2Kings Date: Thu, 8 Apr 2021 15:40:41 -0400 Subject: [PATCH] Fixing logic for fetching updated manga covers And for shortcut images --- .../tachiyomi/data/image/coil/MangaFetcher.kt | 71 +++++++++++-------- .../data/library/LibraryUpdateNotifier.kt | 5 +- .../data/library/LibraryUpdateService.kt | 9 ++- .../ui/manga/MangaDetailsPresenter.kt | 29 ++++++-- .../util/manga/MangaShortcutManager.kt | 25 ++----- 5 files changed, 83 insertions(+), 56 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/image/coil/MangaFetcher.kt b/app/src/main/java/eu/kanade/tachiyomi/data/image/coil/MangaFetcher.kt index 76a606949f..964bc5757a 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/image/coil/MangaFetcher.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/image/coil/MangaFetcher.kt @@ -33,6 +33,8 @@ class MangaFetcher : Fetcher { companion object { const val realCover = "real_cover" + const val onlyCache = "only_cache" + const val onlyFetchRemotely = "only_fetch_remotely" } private val coverCache: CoverCache by injectLazy() @@ -58,24 +60,29 @@ class MangaFetcher : Fetcher { } private suspend fun httpLoader(manga: Manga, options: Options): FetchResult { - val customCoverFile = coverCache.getCustomCoverFile(manga) - if (customCoverFile.exists() && options.parameters.value(realCover) != true) { - return fileLoader(customCoverFile) + val onlyCache = options.parameters.value(onlyCache) == true + val shouldFetchRemotely = options.parameters.value(onlyFetchRemotely) == true && !onlyCache + if (!shouldFetchRemotely) { + val customCoverFile = coverCache.getCustomCoverFile(manga) + if (customCoverFile.exists() && options.parameters.value(realCover) != true) { + return fileLoader(customCoverFile) + } } val coverFile = coverCache.getCoverFile(manga) - if (coverFile.exists() && options.diskCachePolicy.readEnabled) { + if (!shouldFetchRemotely && coverFile.exists() && options.diskCachePolicy.readEnabled) { if (!manga.favorite) { coverFile.setLastModified(Date().time) } return fileLoader(coverFile) } - val (_, body) = awaitGetCall( + val (response, body) = awaitGetCall( manga, if (manga.favorite) { - !options.networkCachePolicy.readEnabled + onlyCache } else { false - } + }, + shouldFetchRemotely ) val tmpFile = File(coverFile.absolutePath + "_tmp") @@ -85,20 +92,46 @@ class MangaFetcher : Fetcher { } } - tmpFile.renameTo(coverFile) + if (response.isSuccessful || !coverFile.exists()) { + if (coverFile.exists()) { + coverFile.delete() + } + + tmpFile.renameTo(coverFile) + } if (manga.favorite) { coverCache.deleteCachedCovers() } return fileLoader(coverFile) } - private suspend fun awaitGetCall(manga: Manga, onlyCache: Boolean = false): Pair { - val call = getCall(manga, onlyCache) + val call = getCall(manga, onlyCache, forceNetwork) val response = call.await() return response to checkNotNull(response.body) { "Null response source" } } + private fun getCall(manga: Manga, onlyCache: Boolean, forceNetwork: Boolean): Call { + val source = sourceManager.get(manga.source) as? HttpSource + val client = source?.client ?: defaultClient + + val newClient = client.newBuilder().build() + + val request = Request.Builder().url(manga.thumbnail_url!!).also { + if (source != null) { + it.headers(source.headers) + } + if (forceNetwork) { + it.cacheControl(CacheControl.FORCE_NETWORK) + } else if (onlyCache) { + it.cacheControl(CacheControl.FORCE_CACHE) + } + }.build() + + return newClient.newCall(request) + } + /** * "text/plain" is often used as a default/fallback MIME type. * Attempt to guess a better MIME type from the file extension. @@ -139,24 +172,6 @@ class MangaFetcher : Fetcher { ) } - private fun getCall(manga: Manga, onlyCache: Boolean): Call { - val source = sourceManager.get(manga.source) as? HttpSource - val client = source?.client ?: defaultClient - - val newClient = client.newBuilder().build() - - val request = Request.Builder().url(manga.thumbnail_url!!).also { - if (source != null) { - it.headers(source.headers) - } - if (onlyCache) { - it.cacheControl(CacheControl.FORCE_CACHE) - } - }.build() - - return newClient.newCall(request) - } - private fun getResourceType(cover: String?): Type? { return when { cover.isNullOrEmpty() -> null diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateNotifier.kt b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateNotifier.kt index 579b92d1a7..3c2c132158 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateNotifier.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateNotifier.kt @@ -13,11 +13,13 @@ import androidx.core.content.ContextCompat import coil.Coil import coil.request.CachePolicy import coil.request.GetRequest +import coil.request.Parameters import coil.transform.CircleCropTransformation import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.database.models.Chapter import eu.kanade.tachiyomi.data.database.models.LibraryManga import eu.kanade.tachiyomi.data.database.models.Manga +import eu.kanade.tachiyomi.data.image.coil.MangaFetcher import eu.kanade.tachiyomi.data.notification.NotificationReceiver import eu.kanade.tachiyomi.data.notification.Notifications import eu.kanade.tachiyomi.data.preference.PreferencesHelper @@ -137,7 +139,8 @@ class LibraryUpdateNotifier(private val context: Context) { setSmallIcon(R.drawable.ic_tachi) try { val request = GetRequest.Builder(context).data(manga) - .networkCachePolicy(CachePolicy.DISABLED) + .parameters(Parameters.Builder().set(MangaFetcher.onlyCache, true).build()) + .networkCachePolicy(CachePolicy.READ_ONLY) .transformations(CircleCropTransformation()) .size(width = ICON_SIZE, height = ICON_SIZE).build() diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt index d7da69cf8e..54729c8a03 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt @@ -9,6 +9,7 @@ import android.os.PowerManager import coil.Coil import coil.request.CachePolicy import coil.request.LoadRequest +import coil.request.Parameters import eu.kanade.tachiyomi.data.cache.CoverCache import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.database.models.Category @@ -17,6 +18,7 @@ import eu.kanade.tachiyomi.data.database.models.LibraryManga import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.download.DownloadManager import eu.kanade.tachiyomi.data.download.DownloadService +import eu.kanade.tachiyomi.data.image.coil.MangaFetcher import eu.kanade.tachiyomi.data.library.LibraryUpdateRanker.rankingScheme import eu.kanade.tachiyomi.data.library.LibraryUpdateService.Companion.start import eu.kanade.tachiyomi.data.notification.Notifications @@ -475,7 +477,12 @@ class LibraryUpdateService( .memoryCachePolicy(CachePolicy.DISABLED).build() Coil.imageLoader(this@LibraryUpdateService).execute(request) } else { - coverCache.deleteFromCache(manga, false) + val request = + LoadRequest.Builder(this@LibraryUpdateService).data(manga) + .memoryCachePolicy(CachePolicy.DISABLED) + .parameters(Parameters.Builder().set(MangaFetcher.onlyFetchRemotely, true).build()) + .build() + Coil.imageLoader(this@LibraryUpdateService).execute(request) } db.insertManga(manga).executeAsBlocking() } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsPresenter.kt index 812ae65965..10d7562ab7 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsPresenter.kt @@ -4,6 +4,11 @@ import android.app.Application import android.graphics.Bitmap import android.net.Uri import android.os.Environment +import coil.Coil +import coil.request.CachePolicy +import coil.request.GetRequest +import coil.request.Parameters +import coil.request.SuccessResult import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.cache.CoverCache import eu.kanade.tachiyomi.data.database.DatabaseHelper @@ -15,6 +20,7 @@ import eu.kanade.tachiyomi.data.database.models.toMangaInfo import eu.kanade.tachiyomi.data.download.DownloadManager import eu.kanade.tachiyomi.data.download.model.Download import eu.kanade.tachiyomi.data.download.model.DownloadQueue +import eu.kanade.tachiyomi.data.image.coil.MangaFetcher import eu.kanade.tachiyomi.data.library.CustomMangaManager import eu.kanade.tachiyomi.data.library.LibraryServiceListener import eu.kanade.tachiyomi.data.library.LibraryUpdateService @@ -39,6 +45,7 @@ import eu.kanade.tachiyomi.util.storage.DiskUtil import eu.kanade.tachiyomi.util.system.ImageUtil import eu.kanade.tachiyomi.util.manga.MangaShortcutManager import eu.kanade.tachiyomi.util.system.executeOnIO +import eu.kanade.tachiyomi.util.system.launchIO import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job @@ -341,13 +348,25 @@ class MangaDetailsPresenter( if (thumbnailUrl != networkManga.thumbnail_url) { coverCache.deleteFromCache(thumbnailUrl) - } else { - coverCache.deleteFromCache(manga, false) - } - withContext(Dispatchers.Main) { - controller.setPaletteColor() } db.insertManga(manga).executeAsBlocking() + + launchIO { + val request = + GetRequest.Builder(preferences.context).data(manga) + .memoryCachePolicy(CachePolicy.DISABLED) + .parameters( + Parameters.Builder().set(MangaFetcher.onlyFetchRemotely, true) + .build() + ) + .build() + + if (Coil.imageLoader(preferences.context).execute(request) is SuccessResult) { + withContext(Dispatchers.Main) { + controller.setPaletteColor() + } + } + } } val finChapters = chapters.await() if (finChapters.isNotEmpty()) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/manga/MangaShortcutManager.kt b/app/src/main/java/eu/kanade/tachiyomi/util/manga/MangaShortcutManager.kt index 694a956363..f7cf8fab47 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/manga/MangaShortcutManager.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/manga/MangaShortcutManager.kt @@ -5,9 +5,10 @@ import android.content.Intent import android.content.pm.ShortcutInfo import android.content.pm.ShortcutManager import android.graphics.Bitmap -import android.graphics.BitmapFactory import android.graphics.drawable.BitmapDrawable import android.graphics.drawable.Icon +import coil.Coil +import coil.request.GetRequest import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.cache.CoverCache import eu.kanade.tachiyomi.data.database.DatabaseHelper @@ -26,7 +27,6 @@ import kotlinx.coroutines.GlobalScope import timber.log.Timber import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get -import java.util.Date import kotlin.math.min class MangaShortcutManager( @@ -71,25 +71,8 @@ class MangaShortcutManager( val shortcuts = recents.mapNotNull { item -> when (item) { is Manga -> { - val customCoverFile = coverCache.getCustomCoverFile(item) - val coverFile = if (customCoverFile.exists()) { - customCoverFile - } else { - val coverFile = coverCache.getCoverFile(item) - if (coverFile.exists()) { - if (!item.favorite) { - coverFile.setLastModified(Date().time) - } - coverFile - } else { - null - } - } - val bitmap = if (coverFile != null) { - BitmapFactory.decodeFile(coverFile.path) - } else { - null - } + val request = GetRequest.Builder(context).data(item).build() + val bitmap = (Coil.imageLoader(context).execute(request).drawable as? BitmapDrawable)?.bitmap ShortcutInfo.Builder( context,