From 726dd2474ca7a841641d4b979ec8408e416f5356 Mon Sep 17 00:00:00 2001 From: vojtasmrcek Date: Tue, 10 Dec 2024 09:41:23 +0100 Subject: [PATCH] Cleanup Compose adapter interface --- .../placeholders/ComposePlaceholderAdapter.kt | 5 +- .../placeholders/ComposePlaceholderManager.kt | 68 ++++--------------- .../aztec/placeholders/PlaceholderManager.kt | 11 ++- .../placeholders/ViewPlaceholderManager.kt | 15 ++-- 4 files changed, 30 insertions(+), 69 deletions(-) diff --git a/media-placeholders/src/main/java/org/wordpress/aztec/placeholders/ComposePlaceholderAdapter.kt b/media-placeholders/src/main/java/org/wordpress/aztec/placeholders/ComposePlaceholderAdapter.kt index cef9ee788..b3097786a 100644 --- a/media-placeholders/src/main/java/org/wordpress/aztec/placeholders/ComposePlaceholderAdapter.kt +++ b/media-placeholders/src/main/java/org/wordpress/aztec/placeholders/ComposePlaceholderAdapter.kt @@ -4,11 +4,12 @@ import androidx.compose.runtime.Composable import org.wordpress.aztec.AztecAttributes interface ComposePlaceholderAdapter : PlaceholderManager.PlaceholderAdapter { + /** + * Use this method to draw the placeholder using Jetpack Compose. + */ @Composable fun Placeholder( placeholderUuid: String, attrs: AztecAttributes, - width: Int, - height: Int, ) {} } diff --git a/media-placeholders/src/main/java/org/wordpress/aztec/placeholders/ComposePlaceholderManager.kt b/media-placeholders/src/main/java/org/wordpress/aztec/placeholders/ComposePlaceholderManager.kt index c3b13e071..3bd505f06 100644 --- a/media-placeholders/src/main/java/org/wordpress/aztec/placeholders/ComposePlaceholderManager.kt +++ b/media-placeholders/src/main/java/org/wordpress/aztec/placeholders/ComposePlaceholderManager.kt @@ -32,6 +32,10 @@ import org.wordpress.aztec.AztecContentChangeWatcher import org.wordpress.aztec.AztecText import org.wordpress.aztec.Constants import org.wordpress.aztec.Html +import org.wordpress.aztec.placeholders.PlaceholderManager.Companion.DEFAULT_HTML_TAG +import org.wordpress.aztec.placeholders.PlaceholderManager.Companion.EDITOR_INNER_PADDING +import org.wordpress.aztec.placeholders.PlaceholderManager.Companion.TYPE_ATTRIBUTE +import org.wordpress.aztec.placeholders.PlaceholderManager.Companion.UUID_ATTRIBUTE import org.wordpress.aztec.plugins.html2visual.IHtmlPreprocessor import org.wordpress.aztec.plugins.html2visual.IHtmlTagHandler import org.wordpress.aztec.spans.AztecMediaClickableSpan @@ -63,7 +67,6 @@ class ComposePlaceholderManager( IHtmlPreprocessor { private val adapters = mutableMapOf() private val positionToIdMutex = Mutex() - private val positionToId = mutableSetOf() private val job = Job() override val coroutineContext: CoroutineContext get() = Dispatchers.Main + job @@ -111,8 +114,6 @@ class ComposePlaceholderManager( adapters[composeView.adapterKey]?.Placeholder( composeView.uuid, composeView.attrs, - composeView.width, - composeView.height, ) } } @@ -120,14 +121,7 @@ class ComposePlaceholderManager( } fun onDestroy() { - positionToId.forEach { - composeViewState.value = composeViewState.value.let { state -> - val mutableState = state.toMutableMap() - mutableState.remove(it.uuid) - mutableState - } - } - positionToId.clear() + composeViewState.value = emptyMap() aztecText.contentChangeWatcher.unregisterObserver(this) adapters.values.forEach { it.onDestroy() } adapters.clear() @@ -352,13 +346,13 @@ class ComposePlaceholderManager( * Call this method to reload all the placeholders */ suspend fun reloadAllPlaceholders() { - val tempPositionToId = positionToId.toList() + val tempPositionToId = composeViewState.value tempPositionToId.forEach { placeholder -> val isValid = positionToIdMutex.withLock { - positionToId.contains(placeholder) + composeViewState.value.containsKey(placeholder.key) } if (isValid) { - insertContentOverSpanWithId(placeholder.uuid) + insertContentOverSpanWithId(placeholder.value.uuid) } } } @@ -427,11 +421,6 @@ class ComposePlaceholderManager( if (widthSame && heightSame && topMarginSame && leftMarginSame) { return } - positionToIdMutex.withLock { - positionToId.removeAll { - it.uuid == uuid - } - } } composeViewState.value = composeViewState.value.let { state -> @@ -448,10 +437,6 @@ class ComposePlaceholderManager( ) mutableState } - - positionToIdMutex.withLock { - positionToId.add(Placeholder(targetPosition, uuid)) - } } private fun validateAttributes(attributes: AztecAttributes): Boolean { @@ -490,11 +475,6 @@ class ComposePlaceholderManager( val uuid = attrs.getValue(UUID_ATTRIBUTE) val adapter = adapters[attrs.getValue(TYPE_ATTRIBUTE)] adapter?.onPlaceholderDeleted(uuid) - launch { - positionToIdMutex.withLock { - positionToId.removeAll { it.uuid == uuid } - } - } composeViewState.value = composeViewState.value.let { state -> val mutableState = state.toMutableMap() mutableState.remove(uuid) @@ -614,44 +594,22 @@ class ComposePlaceholderManager( private suspend fun clearAllViews() { positionToIdMutex.withLock { - for (placeholder in positionToId) { - composeViewState.value = composeViewState.value.let { state -> - val mutableState = state.toMutableMap() - mutableState.remove(placeholder.uuid) - mutableState - } - } - positionToId.clear() + composeViewState.value = emptyMap() } } override fun onVisibility(visibility: Int) { launch { positionToIdMutex.withLock { - for (placeholder in positionToId) { - composeViewState.value = composeViewState.value.let { state -> - val mutableState = state.toMutableMap() - mutableState[placeholder.uuid]?.copy( - visible = View.VISIBLE == visibility - )?.let { - mutableState[placeholder.uuid] = it - } - mutableState - } + composeViewState.value = composeViewState.value.let { state -> + state.mapValues { (_, value) -> value.copy( + visible = View.VISIBLE == visibility + ) } } } } } - data class Placeholder(val elementPosition: Int, val uuid: String) - - companion object { - private const val DEFAULT_HTML_TAG = "placeholder" - private const val UUID_ATTRIBUTE = "uuid" - private const val TYPE_ATTRIBUTE = "type" - private const val EDITOR_INNER_PADDING = 20 - } - override fun beforeHtmlProcessed(source: String): String { runBlocking { clearAllViews() 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 e9e512dbb..63fb68651 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 @@ -1,9 +1,7 @@ package org.wordpress.aztec.placeholders -import android.content.Context import android.view.MotionEvent import android.view.View -import androidx.compose.runtime.Composable import org.wordpress.aztec.AztecAttributes import org.xml.sax.Attributes import kotlin.math.min @@ -21,6 +19,15 @@ interface PlaceholderManager { placeAtStart: Boolean ) -> Map ) + + data class Placeholder(val elementPosition: Int, val uuid: String) + + companion object { + internal const val DEFAULT_HTML_TAG = "placeholder" + internal const val UUID_ATTRIBUTE = "uuid" + internal const val TYPE_ATTRIBUTE = "type" + internal const val EDITOR_INNER_PADDING = 20 + } /** * A adapter for a custom view drawn over the placeholder in the Aztec text. */ diff --git a/media-placeholders/src/main/java/org/wordpress/aztec/placeholders/ViewPlaceholderManager.kt b/media-placeholders/src/main/java/org/wordpress/aztec/placeholders/ViewPlaceholderManager.kt index 21d2a64b6..b7fe41f5a 100644 --- a/media-placeholders/src/main/java/org/wordpress/aztec/placeholders/ViewPlaceholderManager.kt +++ b/media-placeholders/src/main/java/org/wordpress/aztec/placeholders/ViewPlaceholderManager.kt @@ -23,6 +23,11 @@ import org.wordpress.aztec.AztecContentChangeWatcher import org.wordpress.aztec.AztecText import org.wordpress.aztec.Constants import org.wordpress.aztec.Html +import org.wordpress.aztec.placeholders.PlaceholderManager.* +import org.wordpress.aztec.placeholders.PlaceholderManager.Companion.DEFAULT_HTML_TAG +import org.wordpress.aztec.placeholders.PlaceholderManager.Companion.EDITOR_INNER_PADDING +import org.wordpress.aztec.placeholders.PlaceholderManager.Companion.TYPE_ATTRIBUTE +import org.wordpress.aztec.placeholders.PlaceholderManager.Companion.UUID_ATTRIBUTE import org.wordpress.aztec.plugins.html2visual.IHtmlPreprocessor import org.wordpress.aztec.plugins.html2visual.IHtmlTagHandler import org.wordpress.aztec.spans.AztecMediaClickableSpan @@ -542,16 +547,6 @@ class ViewPlaceholderManager( } } - data class Placeholder(val elementPosition: Int, val uuid: String) - - companion object { - private const val TAG = "PlaceholderManager" - private const val DEFAULT_HTML_TAG = "placeholder" - private const val UUID_ATTRIBUTE = "uuid" - private const val TYPE_ATTRIBUTE = "type" - private const val EDITOR_INNER_PADDING = 20 - } - override fun beforeHtmlProcessed(source: String): String { runBlocking { clearAllViews()