Minor code changes

This commit is contained in:
topjohnwu 2020-08-14 03:17:10 -07:00
parent 79aa261ca2
commit 3f053b8547
3 changed files with 17 additions and 31 deletions

View File

@ -11,11 +11,8 @@ import android.graphics.drawable.ShapeDrawable
import android.net.Uri import android.net.Uri
import android.text.Spanned import android.text.Spanned
import android.text.style.DynamicDrawableSpan import android.text.style.DynamicDrawableSpan
import android.view.View
import android.widget.TextView import android.widget.TextView
import androidx.annotation.WorkerThread import androidx.annotation.WorkerThread
import androidx.core.view.marginLeft
import androidx.core.view.marginRight
import com.caverock.androidsvg.SVG import com.caverock.androidsvg.SVG
import com.caverock.androidsvg.SVGParseException import com.caverock.androidsvg.SVGParseException
import com.topjohnwu.magisk.core.ResMgr import com.topjohnwu.magisk.core.ResMgr
@ -25,8 +22,7 @@ import io.noties.markwon.image.*
import io.noties.markwon.image.data.DataUriSchemeHandler import io.noties.markwon.image.data.DataUriSchemeHandler
import io.noties.markwon.image.network.OkHttpNetworkSchemeHandler import io.noties.markwon.image.network.OkHttpNetworkSchemeHandler
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async import kotlinx.coroutines.launch
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import org.commonmark.node.Image import org.commonmark.node.Image
@ -59,24 +55,17 @@ class MarkwonImagePlugin(okHttp: OkHttpClient) : AbstractMarkwonPlugin() {
if (spans == null || spans.isEmpty()) if (spans == null || spans.isEmpty())
return return
// Download and create all drawables in parallel // Get TextView sizes before setText() to resize all images
val wr = WaitRunnable {
val width = tv.width - tv.paddingLeft - tv.paddingRight
spans.forEach { it.canvasWidth = width }
}
tv.post(wr)
runBlocking(Dispatchers.IO) { runBlocking(Dispatchers.IO) {
// Download and decode all drawables in parallel // Download and decode all drawables in parallel
val defers = spans.map { async { it.load() } } spans.forEach { launch { it.load() } }
wr.waitUntilDone()
// Get TextView sizes beforehand to resize all images
// We get its parent here as the TextView itself might be hidden
val parent = tv.parent as View
parent.post(WaitRunnable{
// Make sure it is rendered
val width = parent.width -
tv.paddingLeft - tv.paddingRight -
tv.marginLeft - tv.marginRight
spans.forEach { it.canvasWidth = width }
})
// Make sure all is done before returning
defers.awaitAll()
} }
} }
@ -157,8 +146,8 @@ class MarkwonImagePlugin(okHttp: OkHttpClient) : AbstractMarkwonPlugin() {
) : DynamicDrawableSpan(ALIGN_BOTTOM) { ) : DynamicDrawableSpan(ALIGN_BOTTOM) {
var canvasWidth = 0 var canvasWidth = 0
var measured = false private var measured = false
lateinit var draw: Drawable private lateinit var draw: Drawable
fun load() { fun load() {
draw = loadDrawable(dest) ?: ShapeDrawable() draw = loadDrawable(dest) ?: ShapeDrawable()
@ -179,9 +168,7 @@ class MarkwonImagePlugin(okHttp: OkHttpClient) : AbstractMarkwonPlugin() {
} }
private fun measure(paint: Paint) { private fun measure(paint: Paint) {
if (measured) if (measured || canvasWidth == 0)
return
if (canvasWidth == 0)
return return
measured = true measured = true
val bound = SizeResolver.resolveImageSize(size, defaultBounds(), canvasWidth, paint.textSize) val bound = SizeResolver.resolveImageSize(size, defaultBounds(), canvasWidth, paint.textSize)
@ -212,8 +199,7 @@ class MarkwonImagePlugin(okHttp: OkHttpClient) : AbstractMarkwonPlugin() {
override fun supportedTypes() = listOf("image/svg+xml") override fun supportedTypes() = listOf("image/svg+xml")
override fun decode(contentType: String?, inputStream: InputStream): Drawable { override fun decode(contentType: String?, inputStream: InputStream): Drawable {
val svg: SVG val svg = try {
svg = try {
SVG.getFromInputStream(inputStream) SVG.getFromInputStream(inputStream)
} catch (e: SVGParseException) { } catch (e: SVGParseException) {
throw IllegalStateException("Exception decoding SVG", e) throw IllegalStateException("Exception decoding SVG", e)
@ -222,7 +208,7 @@ class MarkwonImagePlugin(okHttp: OkHttpClient) : AbstractMarkwonPlugin() {
val w = svg.documentWidth val w = svg.documentWidth
val h = svg.documentHeight val h = svg.documentHeight
if (w == 0f || h == 0f) { if (w <= 0 || h <= 0) {
val picture = svg.renderToPicture() val picture = svg.renderToPicture()
return PictureDrawable(picture) return PictureDrawable(picture)
} }

View File

@ -388,7 +388,7 @@ var TextView.precomputedText: CharSequence
coroutineScope.launch(Dispatchers.IO) { coroutineScope.launch(Dispatchers.IO) {
if (SDK_INT >= 29) { if (SDK_INT >= 29) {
// Internally PrecomputedTextCompat will platform API on API 29+ // Internally PrecomputedTextCompat will use platform API on API 29+
// Due to some stupid crap OEM (Samsung) implementation, this can actually // Due to some stupid crap OEM (Samsung) implementation, this can actually
// crash our app. Directly use platform APIs with some workarounds // crash our app. Directly use platform APIs with some workarounds
val pre = PrecomputedText.create(value, textMetricsParams) val pre = PrecomputedText.create(value, textMetricsParams)

View File

@ -47,7 +47,7 @@ object MarkDownWindow : KoinComponent {
if (e is CancellationException) if (e is CancellationException)
throw e throw e
Timber.e(e) Timber.e(e)
tv.setText(R.string.download_file_error) tv.post { tv.setText(R.string.download_file_error) }
} }
} }
} }