Skip to content

Commit

Permalink
Cleanup Compose adapter interface
Browse files Browse the repository at this point in the history
  • Loading branch information
planarvoid committed Dec 10, 2024
1 parent 23b1ea1 commit 726dd24
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -63,7 +67,6 @@ class ComposePlaceholderManager(
IHtmlPreprocessor {
private val adapters = mutableMapOf<String, ComposePlaceholderAdapter>()
private val positionToIdMutex = Mutex()
private val positionToId = mutableSetOf<Placeholder>()
private val job = Job()
override val coroutineContext: CoroutineContext
get() = Dispatchers.Main + job
Expand Down Expand Up @@ -111,23 +114,14 @@ class ComposePlaceholderManager(
adapters[composeView.adapterKey]?.Placeholder(
composeView.uuid,
composeView.attrs,
composeView.width,
composeView.height,
)
}
}
}
}

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()
Expand Down Expand Up @@ -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)
}
}
}
Expand Down Expand Up @@ -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 ->
Expand All @@ -448,10 +437,6 @@ class ComposePlaceholderManager(
)
mutableState
}

positionToIdMutex.withLock {
positionToId.add(Placeholder(targetPosition, uuid))
}
}

private fun validateAttributes(attributes: AztecAttributes): Boolean {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -21,6 +19,15 @@ interface PlaceholderManager {
placeAtStart: Boolean
) -> Map<String, String>
)

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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 726dd24

Please sign in to comment.