Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move loading from HTML to a background thread #1064

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 54 additions & 16 deletions aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt
Original file line number Diff line number Diff line change
@@ -1501,37 +1501,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() {
Original file line number Diff line number Diff line change
@@ -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<View>(placeholder.uuid)?.let {
@@ -664,7 +665,7 @@ class PlaceholderManager(
}

override fun beforeHtmlProcessed(source: String): String {
runBlocking {
launch {
clearAllViews()
}
return source
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -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 {