From eddbce892669af93086141e26d9442740546e04c Mon Sep 17 00:00:00 2001 From: vojtasmrcek Date: Mon, 23 Oct 2023 16:33:34 +0200 Subject: [PATCH 1/3] Move loading from HTML to a background thread --- .../kotlin/org/wordpress/aztec/AztecText.kt | 71 ++++++++++++++----- 1 file changed, 55 insertions(+), 16 deletions(-) diff --git a/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt b/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt index ebc396d4f..29406f0d8 100644 --- a/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt +++ b/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt @@ -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 @@ -1497,22 +1498,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) @@ -1520,14 +1537,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() { From cabe5ba78b4d09699c126063d82e3ba782d8e4e7 Mon Sep 17 00:00:00 2001 From: vojtasmrcek Date: Mon, 23 Oct 2023 16:42:51 +0200 Subject: [PATCH 2/3] Remove unused import --- aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt b/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt index 29406f0d8..d45b4197f 100644 --- a/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt +++ b/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt @@ -44,7 +44,6 @@ 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 From 5a3fe765da10cd18cb4fde1a0af027f26713d385 Mon Sep 17 00:00:00 2001 From: vojtasmrcek Date: Thu, 2 Nov 2023 10:39:56 +0100 Subject: [PATCH 3/3] Clear views with delay to fix a crash --- .../org/wordpress/aztec/placeholders/PlaceholderManager.kt | 5 +++-- settings.gradle | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) 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 {