Fix bookmarks images loading

master
Koitharu 10 months ago
parent 18466a2c1a
commit 17656233ef
Signed by: Koitharu
GPG Key ID: 676DEE768C17A9D7

@ -1,7 +1,8 @@
package org.koitharu.kotatsu.bookmarks.domain package org.koitharu.kotatsu.bookmarks.domain
import org.koitharu.kotatsu.core.util.MimeTypes
import org.koitharu.kotatsu.core.util.ext.isImage
import org.koitharu.kotatsu.list.ui.model.ListModel import org.koitharu.kotatsu.list.ui.model.ListModel
import org.koitharu.kotatsu.local.data.hasImageExtension
import org.koitharu.kotatsu.parsers.model.Manga import org.koitharu.kotatsu.parsers.model.Manga
import org.koitharu.kotatsu.parsers.model.MangaPage import org.koitharu.kotatsu.parsers.model.MangaPage
import java.time.Instant import java.time.Instant
@ -17,9 +18,6 @@ data class Bookmark(
val percent: Float, val percent: Float,
) : ListModel { ) : ListModel {
val imageLoadData: Any
get() = if (isImageUrlDirect()) imageUrl else toMangaPage()
override fun areItemsTheSame(other: ListModel): Boolean { override fun areItemsTheSame(other: ListModel): Boolean {
return other is Bookmark && return other is Bookmark &&
manga.id == other.manga.id && manga.id == other.manga.id &&
@ -30,11 +28,9 @@ data class Bookmark(
fun toMangaPage() = MangaPage( fun toMangaPage() = MangaPage(
id = pageId, id = pageId,
url = imageUrl, url = imageUrl,
preview = null, preview = imageUrl.takeIf {
MimeTypes.getMimeTypeFromUrl(it)?.isImage == true
},
source = manga.source, source = manga.source,
) )
private fun isImageUrlDirect(): Boolean {
return hasImageExtension(imageUrl)
}
} }

@ -28,6 +28,7 @@ import org.koitharu.kotatsu.local.data.PagesCache
import org.koitharu.kotatsu.parsers.model.MangaPage import org.koitharu.kotatsu.parsers.model.MangaPage
import org.koitharu.kotatsu.parsers.util.mimeType import org.koitharu.kotatsu.parsers.util.mimeType
import org.koitharu.kotatsu.parsers.util.requireBody import org.koitharu.kotatsu.parsers.util.requireBody
import org.koitharu.kotatsu.parsers.util.runCatchingCancellable
import org.koitharu.kotatsu.reader.domain.PageLoader import org.koitharu.kotatsu.reader.domain.PageLoader
import javax.inject.Inject import javax.inject.Inject
@ -42,6 +43,13 @@ class MangaPageFetcher(
) : Fetcher { ) : Fetcher {
override suspend fun fetch(): FetchResult? { override suspend fun fetch(): FetchResult? {
if (!page.preview.isNullOrEmpty()) {
runCatchingCancellable {
imageLoader.fetch(checkNotNull(page.preview), options)
}.onSuccess {
return it
}
}
val repo = mangaRepositoryFactory.create(page.source) val repo = mangaRepositoryFactory.create(page.source)
val pageUrl = repo.getPageUrl(page) val pageUrl = repo.getPageUrl(page)
if (options.diskCachePolicy.readEnabled) { if (options.diskCachePolicy.readEnabled) {

@ -34,10 +34,10 @@ import org.koitharu.kotatsu.core.util.ext.getThemeColor
import org.koitharu.kotatsu.core.util.ext.mangaExtra import org.koitharu.kotatsu.core.util.ext.mangaExtra
import org.koitharu.kotatsu.core.util.ext.mangaSourceExtra import org.koitharu.kotatsu.core.util.ext.mangaSourceExtra
import org.koitharu.kotatsu.favourites.domain.model.Cover import org.koitharu.kotatsu.favourites.domain.model.Cover
import org.koitharu.kotatsu.parsers.exception.ParseException
import org.koitharu.kotatsu.parsers.model.Manga import org.koitharu.kotatsu.parsers.model.Manga
import org.koitharu.kotatsu.parsers.model.MangaPage import org.koitharu.kotatsu.parsers.model.MangaPage
import org.koitharu.kotatsu.parsers.model.MangaSource import org.koitharu.kotatsu.parsers.model.MangaSource
import org.koitharu.kotatsu.parsers.util.nullIfEmpty
import org.koitharu.kotatsu.reader.ui.pager.ReaderPage import org.koitharu.kotatsu.reader.ui.pager.ReaderPage
import kotlin.coroutines.resume import kotlin.coroutines.resume
import androidx.appcompat.R as appcompatR import androidx.appcompat.R as appcompatR
@ -103,14 +103,14 @@ class CoverImageView @JvmOverloads constructor(
fun setImageAsync(page: ReaderPage) = enqueueRequest( fun setImageAsync(page: ReaderPage) = enqueueRequest(
newRequestBuilder() newRequestBuilder()
.data(page.preview?.nullIfEmpty() ?: page.toMangaPage()) .data(page.toMangaPage())
.mangaSourceExtra(page.source) .mangaSourceExtra(page.source)
.build(), .build(),
) )
fun setImageAsync(page: MangaPage) = enqueueRequest( fun setImageAsync(page: MangaPage) = enqueueRequest(
newRequestBuilder() newRequestBuilder()
.data(page.preview?.nullIfEmpty() ?: page) .data(page)
.mangaSourceExtra(page.source) .mangaSourceExtra(page.source)
.build(), .build(),
) )
@ -146,7 +146,7 @@ class CoverImageView @JvmOverloads constructor(
bookmark: Bookmark bookmark: Bookmark
) = enqueueRequest( ) = enqueueRequest(
newRequestBuilder() newRequestBuilder()
.data(bookmark.imageLoadData) .data(bookmark.toMangaPage())
.decodeRegion(bookmark.scroll) .decodeRegion(bookmark.scroll)
.bookmarkExtra(bookmark) .bookmarkExtra(bookmark)
.build(), .build(),
@ -190,6 +190,7 @@ class CoverImageView @JvmOverloads constructor(
is HttpException -> response.code.toString() is HttpException -> response.code.toString()
is HttpStatusException -> statusCode.toString() is HttpStatusException -> statusCode.toString()
is FileNotFoundException -> "404" is FileNotFoundException -> "404"
is ParseException -> "</>"
else -> cause?.getShortMessage() else -> cause?.getShortMessage()
} }
} }

@ -1,11 +0,0 @@
package org.koitharu.kotatsu.local.data
import java.io.File
fun hasImageExtension(string: String): Boolean {
val ext = string.substringAfterLast('.', "")
return ext.equals("png", ignoreCase = true) || ext.equals("jpg", ignoreCase = true)
|| ext.equals("jpeg", ignoreCase = true) || ext.equals("webp", ignoreCase = true)
}
fun hasImageExtension(file: File) = hasImageExtension(file.name)

@ -31,15 +31,15 @@ class CoverRestoreInterceptor @Inject constructor(
val request = chain.request val request = chain.request
val result = chain.proceed() val result = chain.proceed()
if (result is ErrorResult && result.throwable.shouldRestore()) { if (result is ErrorResult && result.throwable.shouldRestore()) {
request.extras[mangaKey]?.let { request.extras[bookmarkKey]?.let {
return if (restoreManga(it)) { return if (restoreBookmark(it)) {
chain.withRequest(request.newBuilder().build()).proceed() chain.withRequest(request.newBuilder().build()).proceed()
} else { } else {
result result
} }
} }
request.extras[bookmarkKey]?.let { request.extras[mangaKey]?.let {
return if (restoreBookmark(it)) { return if (restoreManga(it)) {
chain.withRequest(request.newBuilder().build()).proceed() chain.withRequest(request.newBuilder().build()).proceed()
} else { } else {
result result

Loading…
Cancel
Save