diff --git a/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt b/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt index a5d97bfec..cc4e30682 100644 --- a/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt +++ b/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt @@ -1501,22 +1501,38 @@ 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) @@ -1524,14 +1540,36 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown 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() { diff --git a/media-placeholders/src/main/java/org/wordpress/aztec/placeholders/PlaceholderManager.kt b/media-placeholders/src/main/java/org/wordpress/aztec/placeholders/PlaceholderManager.kt index c4898c100..ff95be985 100644 --- a/media-placeholders/src/main/java/org/wordpress/aztec/placeholders/PlaceholderManager.kt +++ b/media-placeholders/src/main/java/org/wordpress/aztec/placeholders/PlaceholderManager.kt @@ -20,6 +20,7 @@ import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock +import kotlinx.coroutines.withContext import org.wordpress.aztec.AztecAttributes import org.wordpress.aztec.AztecContentChangeWatcher import org.wordpress.aztec.AztecText @@ -519,7 +520,7 @@ class PlaceholderManager( } } - private suspend fun clearAllViews() { + private suspend fun clearAllViews() = withContext(Dispatchers.Main){ positionToIdMutex.withLock { for (placeholder in positionToId) { container.findViewWithTag(placeholder.uuid)?.let { @@ -664,7 +665,7 @@ class PlaceholderManager( } override fun beforeHtmlProcessed(source: String): String { - runBlocking { + launch { clearAllViews() } return source diff --git a/settings.gradle b/settings.gradle index ed67ef1fb..811816072 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,6 +1,6 @@ pluginManagement { gradle.ext.kotlinVersion = '1.6.10' - gradle.ext.agpVersion = '8.1.0' + gradle.ext.agpVersion = '8.1.2' gradle.ext.automatticPublishToS3Version = '0.8.0' plugins {