Skip to content

Commit

Permalink
fixed reader for external epubs (images + spine)
Browse files Browse the repository at this point in the history
  • Loading branch information
LagradOst committed Jun 15, 2024
1 parent 2e2eaec commit 6c5bd3f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.lagradost.quicknovel

import android.content.Context
import android.content.Intent
import android.content.res.Resources
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Rect
Expand All @@ -12,6 +13,8 @@ import android.text.Spanned
import android.widget.Toast
import androidx.annotation.WorkerThread
import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.toDrawable
import androidx.core.text.getSpans
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
Expand Down Expand Up @@ -55,6 +58,7 @@ import io.noties.markwon.MarkwonConfiguration
import io.noties.markwon.SoftBreakAddsNewLinePlugin
import io.noties.markwon.html.HtmlPlugin
import io.noties.markwon.image.AsyncDrawable
import io.noties.markwon.image.AsyncDrawableSpan
import io.noties.markwon.image.ImageSizeResolver
import io.noties.markwon.image.glide.GlideImagesPlugin
import kotlinx.coroutines.delay
Expand Down Expand Up @@ -141,6 +145,21 @@ abstract class AbstractBook {
abstract fun getChapterTitle(index: Int): UiText
abstract fun getLoadingStatus(index: Int): String?

@Throws
open fun loadImage(image: String): ByteArray? {
return null
}

fun loadImageBitmap(image: String): Bitmap? {
try {
val data = this.loadImage(image) ?: return null
return BitmapFactory.decodeByteArray(data, 0, data.size)
} catch (t: Throwable) {
logError(t)
return null
}
}

@WorkerThread
@Throws
abstract suspend fun getChapterData(index: Int, reload: Boolean): String
Expand Down Expand Up @@ -239,6 +258,7 @@ class QuickBook(val data: QuickStreamData) : AbstractBook() {
}

class RegularBook(val data: Book) : AbstractBook() {

init {
var refs = mutableListOf<TOCReference>()
data.tableOfContents.tocReferences.forEach { ref ->
Expand All @@ -252,6 +272,10 @@ class RegularBook(val data: Book) : AbstractBook() {

override val canReload = false

override fun loadImage(image: String): ByteArray? {
return data.resources.resourceMap.values.find { x -> x.mediaType.name.contains("image") && image.endsWith(x.href) }?.data
}

override fun size(): Int {
return data.tableOfContents.tocReferences.size
}
Expand All @@ -270,7 +294,25 @@ class RegularBook(val data: Book) : AbstractBook() {
}

override suspend fun getChapterData(index: Int, reload: Boolean): String {
return data.tableOfContents.tocReferences[index].resource.reader.readText()
val start = data.tableOfContents.tocReferences[index].resource
val startIdx = data.spine.getResourceIndex(start)

val end = data.tableOfContents.tocReferences.getOrNull(index + 1)?.resource
var endIdx = data.spine.getResourceIndex(end)
if (endIdx == -1) {
endIdx = data.spine.size()
}
val builder = StringBuilder()
for (i in startIdx until endIdx) {
val ref = data.spine.spineReferences[i]
// I have no idea, but nonlinear = stop?
if (!ref.isLinear && i != startIdx) {
break
}
builder.append(ref.resource.reader.readText())
}

return builder.toString()
}

override fun expand(last: String): Boolean {
Expand Down Expand Up @@ -639,7 +681,13 @@ class ReadActivityViewModel : ViewModel() {
markwonMutex.withLock {
parsed = markwon.parse(rawText)
rendered = markwon.render(parsed)

val asyncDrawables = rendered.getSpans<AsyncDrawableSpan>()
for (async in asyncDrawables) {
async.drawable.result =
book.loadImageBitmap(async.drawable.destination)?.toDrawable(
Resources.getSystem()
)
}
//render(rawText, markwon)
// this was removed because the reducer did not work
/*val split = reducer.reduce(parsed)
Expand Down Expand Up @@ -734,7 +782,7 @@ class ReadActivityViewModel : ViewModel() {
if (loadedChapter >= book.size()) {
currentIndex = book.size() - 1
updateIndexAsync(currentIndex, notify = false)
showToast("Resize $loadedChapter -> $currentIndex", Toast.LENGTH_LONG)
showToast("Resize $loadedChapter -> $currentIndex", Toast.LENGTH_LONG)
}

val char = getKey(
Expand Down Expand Up @@ -782,6 +830,7 @@ class ReadActivityViewModel : ViewModel() {
.usePlugin(GlideImagesPlugin.create(object : GlideImagesPlugin.GlideStore {
override fun load(drawable: AsyncDrawable): RequestBuilder<Drawable> {
return try {

val newUrl = drawable.destination.substringAfter("&url=")
val url =
book.resolveUrl(
Expand All @@ -794,6 +843,7 @@ class ReadActivityViewModel : ViewModel() {

Glide.with(context)
.load(GlideUrl(url) { mapOf("user-agent" to USER_AGENT) })

} catch (e: Exception) {
logError(e)
Glide.with(context)
Expand Down
18 changes: 11 additions & 7 deletions app/src/main/java/com/lagradost/quicknovel/ui/TextAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ data class TextConfig(
val textSize: Int,
val textFont: String,
val defaultFont: Typeface,
val backgroundColor : Int,
val bionicReading : Boolean
val backgroundColor: Int,
val bionicReading: Boolean
) {
private val fontFile: File? by lazy {
if (textFont == "") null else systemFonts.firstOrNull { it.name == textFont }
Expand Down Expand Up @@ -207,9 +207,11 @@ data class TextConfig(
private fun setTextColor(textView: TextView) {
textView.setTextColor(textColor)
}

private fun setBgTextColor(textView: TextView) {
textView.setTextColor(backgroundColor)
}

fun setArgs(progressBar: ProgressBar) {
progressBar.progressTintList = ColorStateList.valueOf(textColor)
progressBar.indeterminateTintList = ColorStateList.valueOf(textColor)
Expand Down Expand Up @@ -252,7 +254,7 @@ class TextAdapter(private val viewModel: ReadActivityViewModel, var config: Text
return true
}

fun changeBionicReading(to : Boolean) : Boolean {
fun changeBionicReading(to: Boolean): Boolean {
if (config.bionicReading == to) return false
config = config.copy(bionicReading = to)
return true
Expand Down Expand Up @@ -291,7 +293,7 @@ class TextAdapter(private val viewModel: ReadActivityViewModel, var config: Text
DRAW_FAILED -> SingleFailedBinding.inflate(inflater, parent, false)
DRAW_CHAPTER -> SingleFinishedChapterBinding.inflate(inflater, parent, false)
DRAW_LOAD -> SingleLoadBinding.inflate(inflater, parent, false)
DRAW_OVERSCROLL -> SingleOverscrollChapterBinding.inflate(inflater,parent,false)
DRAW_OVERSCROLL -> SingleOverscrollChapterBinding.inflate(inflater, parent, false)
else -> throw NotImplementedError()
}

Expand Down Expand Up @@ -467,9 +469,11 @@ class TextAdapter(private val viewModel: ReadActivityViewModel, var config: Text
is ChapterLoadSpanned -> {
DRAW_LOAD
}

is ChapterOverscrollSpanned -> {
DRAW_OVERSCROLL
}

else -> throw NotImplementedError()
}
}
Expand Down Expand Up @@ -547,7 +551,7 @@ class TextAdapter(private val viewModel: ReadActivityViewModel, var config: Text
}


private fun bindImage(binding : SingleImageBinding, img : AsyncDrawable) {
private fun bindImage(binding: SingleImageBinding, img: AsyncDrawable) {
val url = img.destination
if (binding.root.url == url) return
binding.root.url = url // don't reload if already set
Expand All @@ -558,13 +562,13 @@ class TextAdapter(private val viewModel: ReadActivityViewModel, var config: Text
when (binding) {
is SingleImageBinding -> {
val img = obj.text.getSpans<AsyncDrawableSpan>(0, obj.text.length)[0]
bindImage(binding,img.drawable)
bindImage(binding, img.drawable)

binding.root.setOnClickListener { root ->
if (root !is TextImageView) {
return@setOnClickListener
}
showImage(root.context,img.drawable)
showImage(root.context, img.drawable)
}

/*val size = 300.toPx
Expand Down

0 comments on commit 6c5bd3f

Please sign in to comment.