Skip to content

Commit

Permalink
Move loading from HTML to a background thread
Browse files Browse the repository at this point in the history
  • Loading branch information
planarvoid committed Oct 23, 2023
1 parent caf8f43 commit eddbce8
Showing 1 changed file with 55 additions and 16 deletions.
71 changes: 55 additions & 16 deletions aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import android.text.TextWatcher
import android.text.style.SuggestionSpan
import android.util.AttributeSet
import android.util.DisplayMetrics
import android.util.Log
import android.util.TypedValue
import android.view.KeyEvent
import android.view.LayoutInflater
Expand Down Expand Up @@ -1497,37 +1498,75 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
}

open fun fromHtml(source: String, isInit: Boolean = true) {
val builder = SpannableStringBuilder()
val parser = AztecParser(alignmentRendering, plugins)
val builder = getBuilderFromHtml(source)
setupCursorPosition(builder)
calculateSha(isInit)
loadMedia()
}

var cleanSource = CleaningUtils.cleanNestedBoldTags(source)
cleanSource = Format.removeSourceEditorFormatting(cleanSource, isInCalypsoMode, isInGutenbergMode)
builder.append(parser.fromHtml(cleanSource, context, shouldSkipTidying(), shouldIgnoreWhitespace()))
open suspend fun fromHtmlAsync(source: String, isInit: Boolean = true) = withContext(Dispatchers.Default) {
val builder = getBuilderFromHtml(source)
withContext(Dispatchers.Main) {
setupCursorPosition(builder)
}

Format.preProcessSpannedText(builder, isInCalypsoMode)
calculateSha(isInit)
loadMedia()
}

switchToAztecStyle(builder, 0, builder.length)
disableTextChangedListener()
private fun loadMedia() {
loadImages()
loadVideos()
mediaCallback?.mediaLoadingStarted()
}

builder.getSpans(0, builder.length, AztecDynamicImageSpan::class.java).forEach {
it.textView = WeakReference(this)
private fun calculateSha(isInit: Boolean) {
if (isInit) {
initialEditorContentParsedSHA256 = calculateInitialHTMLSHA(
toPlainHtml(false),
initialEditorContentParsedSHA256
)
}
}

private fun setupCursorPosition(builder: SpannableStringBuilder) {
val cursorPosition = consumeCursorPosition(builder)
setSelection(0)

setTextKeepState(builder)
enableTextChangedListener()

setSelection(cursorPosition)
}

if (isInit) {
initialEditorContentParsedSHA256 = calculateInitialHTMLSHA(toPlainHtml(false), initialEditorContentParsedSHA256)
}
private fun getBuilderFromHtml(source: String): SpannableStringBuilder {
val builder = SpannableStringBuilder()
val parser = AztecParser(alignmentRendering, plugins)

loadImages()
loadVideos()
mediaCallback?.mediaLoadingStarted()
var cleanSource = CleaningUtils.cleanNestedBoldTags(source)
cleanSource = Format.removeSourceEditorFormatting(
cleanSource,
isInCalypsoMode,
isInGutenbergMode
)
builder.append(
parser.fromHtml(
cleanSource,
context,
shouldSkipTidying(),
shouldIgnoreWhitespace()
)
)

Format.preProcessSpannedText(builder, isInCalypsoMode)

switchToAztecStyle(builder, 0, builder.length)
disableTextChangedListener()

builder.getSpans(0, builder.length, AztecDynamicImageSpan::class.java).forEach {
it.textView = WeakReference(this)
}
return builder
}

private fun loadImages() {
Expand Down

0 comments on commit eddbce8

Please sign in to comment.