fix: Try loading custom cover if non-custom cover is not found

Fixes GH-150
This commit is contained in:
Ahmad Ansori Palembani 2024-08-12 15:49:58 +07:00
parent 84a3f91b80
commit 65260e8bd7
Signed by: null2264
GPG key ID: BA64F8B60AF3EFB6
2 changed files with 18 additions and 12 deletions

View file

@ -40,6 +40,7 @@
- Fixed status bar stuck in dark mode when app is following system theme - Fixed status bar stuck in dark mode when app is following system theme
- Fixed splash screen state only getting updates if library is empty (Should slightly reduce splash screen duration) - Fixed splash screen state only getting updates if library is empty (Should slightly reduce splash screen duration)
- Fixed kitsu tracker issue due to domain change - Fixed kitsu tracker issue due to domain change
- Fixed entry custom cover won't load if entry doesn't have cover from source
## Translation ## Translation
- Update Japanese translation (akir45) - Update Japanese translation (akir45)

View file

@ -20,6 +20,9 @@ import eu.kanade.tachiyomi.network.await
import eu.kanade.tachiyomi.source.SourceManager import eu.kanade.tachiyomi.source.SourceManager
import eu.kanade.tachiyomi.source.online.HttpSource import eu.kanade.tachiyomi.source.online.HttpSource
import eu.kanade.tachiyomi.util.manga.MangaCoverMetadata import eu.kanade.tachiyomi.util.manga.MangaCoverMetadata
import java.io.File
import java.net.HttpURLConnection
import java.util.Date
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
@ -35,9 +38,6 @@ import okio.buffer
import okio.sink import okio.sink
import okio.source import okio.source
import uy.kohesive.injekt.injectLazy import uy.kohesive.injekt.injectLazy
import java.io.File
import java.net.HttpURLConnection
import java.util.*
class MangaCoverFetcher( class MangaCoverFetcher(
private val manga: Manga, private val manga: Manga,
@ -56,7 +56,7 @@ class MangaCoverFetcher(
override suspend fun fetch(): FetchResult { override suspend fun fetch(): FetchResult {
// diskCacheKey is thumbnail_url // diskCacheKey is thumbnail_url
url = manga.thumbnail_url ?: error("No cover specified") url = manga.thumbnail_url.orEmpty()
return when (getResourceType(url)) { return when (getResourceType(url)) {
Type.URL -> httpLoader() Type.URL -> httpLoader()
Type.File -> { Type.File -> {
@ -64,23 +64,28 @@ class MangaCoverFetcher(
fileLoader(File(url.substringAfter("file://"))) fileLoader(File(url.substringAfter("file://")))
} }
Type.URI -> fileUriLoader(url) Type.URI -> fileUriLoader(url)
null -> error("Invalid image") null -> tryCustomCover() ?: error("No cover specified")
} }
} }
@Suppress("RedundantSuspendModifier")
private suspend fun tryCustomCover(): FetchResult? {
val customCoverFile by lazy { coverCache.getCustomCoverFile(manga) }
if (options.extras.getOrDefault(USE_CUSTOM_COVER_KEY) && customCoverFile.exists()) {
setRatioAndColorsInScope(manga, customCoverFile)
return fileLoader(customCoverFile)
}
return null
}
private suspend fun httpLoader(): FetchResult { private suspend fun httpLoader(): FetchResult {
val diskRead = options.diskCachePolicy.readEnabled val diskRead = options.diskCachePolicy.readEnabled
val networkRead = options.networkCachePolicy.readEnabled val networkRead = options.networkCachePolicy.readEnabled
val onlyCache = !networkRead && diskRead val onlyCache = !networkRead && diskRead
val shouldFetchRemotely = networkRead && !diskRead && !onlyCache val shouldFetchRemotely = networkRead && !diskRead && !onlyCache
val useCustomCover = options.extras.getOrDefault(USE_CUSTOM_COVER_KEY)
// Use custom cover if exists
if (!shouldFetchRemotely) { if (!shouldFetchRemotely) {
val customCoverFile by lazy { coverCache.getCustomCoverFile(manga) } val customCoverLoader = tryCustomCover()
if (useCustomCover && customCoverFile.exists()) { if (customCoverLoader != null) return customCoverLoader
setRatioAndColorsInScope(manga, customCoverFile)
return fileLoader(customCoverFile)
}
} }
val coverFile = coverCache.getCoverFile(manga) val coverFile = coverCache.getCoverFile(manga)
if (!shouldFetchRemotely && coverFile.exists() && options.diskCachePolicy.readEnabled) { if (!shouldFetchRemotely && coverFile.exists() && options.diskCachePolicy.readEnabled) {