Fix Markdown rendering

Close #3074
This commit is contained in:
topjohnwu
2020-08-14 02:00:06 -07:00
parent d8b1d79879
commit ac2a9da4c4
9 changed files with 371 additions and 84 deletions
@@ -1,34 +1,21 @@
package com.topjohnwu.magisk.view
import android.content.Context
import android.text.Spanned
import android.view.LayoutInflater
import android.widget.TextView
import androidx.core.text.PrecomputedTextCompat
import androidx.core.widget.TextViewCompat
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.data.repository.StringRepository
import com.topjohnwu.magisk.ktx.coroutineScope
import io.noties.markwon.Markwon
import kotlinx.coroutines.*
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.koin.core.KoinComponent
import org.koin.core.inject
import timber.log.Timber
import kotlin.coroutines.coroutineContext
class PrecomputedTextSetter : Markwon.TextSetter {
override fun setText(tv: TextView, text: Spanned, bufferType: TextView.BufferType, onComplete: Runnable) {
val scope = tv.tag as? CoroutineScope ?: GlobalScope
scope.launch(Dispatchers.Default) {
val pre = PrecomputedTextCompat.create(text, TextViewCompat.getTextMetricsParams(tv))
tv.post {
TextViewCompat.setPrecomputedText(tv, pre)
onComplete.run()
}
}
}
}
object MarkDownWindow : KoinComponent {
private val repo: StringRepository by inject()
@@ -41,26 +28,27 @@ object MarkDownWindow : KoinComponent {
}
suspend fun show(activity: Context, title: String?, input: suspend () -> String) {
val mdRes = R.layout.markdown_window_md2
val mv = LayoutInflater.from(activity).inflate(mdRes, null)
val tv = mv.findViewById<TextView>(R.id.md_txt)
tv.tag = CoroutineScope(coroutineContext)
try {
markwon.setMarkdown(tv, input())
} catch (e: Exception) {
if (e is CancellationException)
throw e
Timber.e(e)
tv.setText(R.string.download_file_error)
}
val view = LayoutInflater.from(activity).inflate(R.layout.markdown_window_md2, null)
MagiskDialog(activity)
.applyTitle(title ?: "")
.applyView(mv)
.applyView(view)
.applyButton(MagiskDialog.ButtonType.NEGATIVE) {
titleRes = android.R.string.cancel
}
.reveal()
val tv = view.findViewById<TextView>(R.id.md_txt)
tv.coroutineScope = CoroutineScope(coroutineContext)
withContext(Dispatchers.IO) {
try {
markwon.setMarkdown(tv, input())
} catch (e: Exception) {
if (e is CancellationException)
throw e
Timber.e(e)
tv.setText(R.string.download_file_error)
}
}
}
}