Skip to content

Commit 726dd24

Browse files
committed
Cleanup Compose adapter interface
1 parent 23b1ea1 commit 726dd24

File tree

4 files changed

+30
-69
lines changed

4 files changed

+30
-69
lines changed

media-placeholders/src/main/java/org/wordpress/aztec/placeholders/ComposePlaceholderAdapter.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import androidx.compose.runtime.Composable
44
import org.wordpress.aztec.AztecAttributes
55

66
interface ComposePlaceholderAdapter : PlaceholderManager.PlaceholderAdapter {
7+
/**
8+
* Use this method to draw the placeholder using Jetpack Compose.
9+
*/
710
@Composable
811
fun Placeholder(
912
placeholderUuid: String,
1013
attrs: AztecAttributes,
11-
width: Int,
12-
height: Int,
1314
) {}
1415
}

media-placeholders/src/main/java/org/wordpress/aztec/placeholders/ComposePlaceholderManager.kt

Lines changed: 13 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ import org.wordpress.aztec.AztecContentChangeWatcher
3232
import org.wordpress.aztec.AztecText
3333
import org.wordpress.aztec.Constants
3434
import org.wordpress.aztec.Html
35+
import org.wordpress.aztec.placeholders.PlaceholderManager.Companion.DEFAULT_HTML_TAG
36+
import org.wordpress.aztec.placeholders.PlaceholderManager.Companion.EDITOR_INNER_PADDING
37+
import org.wordpress.aztec.placeholders.PlaceholderManager.Companion.TYPE_ATTRIBUTE
38+
import org.wordpress.aztec.placeholders.PlaceholderManager.Companion.UUID_ATTRIBUTE
3539
import org.wordpress.aztec.plugins.html2visual.IHtmlPreprocessor
3640
import org.wordpress.aztec.plugins.html2visual.IHtmlTagHandler
3741
import org.wordpress.aztec.spans.AztecMediaClickableSpan
@@ -63,7 +67,6 @@ class ComposePlaceholderManager(
6367
IHtmlPreprocessor {
6468
private val adapters = mutableMapOf<String, ComposePlaceholderAdapter>()
6569
private val positionToIdMutex = Mutex()
66-
private val positionToId = mutableSetOf<Placeholder>()
6770
private val job = Job()
6871
override val coroutineContext: CoroutineContext
6972
get() = Dispatchers.Main + job
@@ -111,23 +114,14 @@ class ComposePlaceholderManager(
111114
adapters[composeView.adapterKey]?.Placeholder(
112115
composeView.uuid,
113116
composeView.attrs,
114-
composeView.width,
115-
composeView.height,
116117
)
117118
}
118119
}
119120
}
120121
}
121122

122123
fun onDestroy() {
123-
positionToId.forEach {
124-
composeViewState.value = composeViewState.value.let { state ->
125-
val mutableState = state.toMutableMap()
126-
mutableState.remove(it.uuid)
127-
mutableState
128-
}
129-
}
130-
positionToId.clear()
124+
composeViewState.value = emptyMap()
131125
aztecText.contentChangeWatcher.unregisterObserver(this)
132126
adapters.values.forEach { it.onDestroy() }
133127
adapters.clear()
@@ -352,13 +346,13 @@ class ComposePlaceholderManager(
352346
* Call this method to reload all the placeholders
353347
*/
354348
suspend fun reloadAllPlaceholders() {
355-
val tempPositionToId = positionToId.toList()
349+
val tempPositionToId = composeViewState.value
356350
tempPositionToId.forEach { placeholder ->
357351
val isValid = positionToIdMutex.withLock {
358-
positionToId.contains(placeholder)
352+
composeViewState.value.containsKey(placeholder.key)
359353
}
360354
if (isValid) {
361-
insertContentOverSpanWithId(placeholder.uuid)
355+
insertContentOverSpanWithId(placeholder.value.uuid)
362356
}
363357
}
364358
}
@@ -427,11 +421,6 @@ class ComposePlaceholderManager(
427421
if (widthSame && heightSame && topMarginSame && leftMarginSame) {
428422
return
429423
}
430-
positionToIdMutex.withLock {
431-
positionToId.removeAll {
432-
it.uuid == uuid
433-
}
434-
}
435424
}
436425

437426
composeViewState.value = composeViewState.value.let { state ->
@@ -448,10 +437,6 @@ class ComposePlaceholderManager(
448437
)
449438
mutableState
450439
}
451-
452-
positionToIdMutex.withLock {
453-
positionToId.add(Placeholder(targetPosition, uuid))
454-
}
455440
}
456441

457442
private fun validateAttributes(attributes: AztecAttributes): Boolean {
@@ -490,11 +475,6 @@ class ComposePlaceholderManager(
490475
val uuid = attrs.getValue(UUID_ATTRIBUTE)
491476
val adapter = adapters[attrs.getValue(TYPE_ATTRIBUTE)]
492477
adapter?.onPlaceholderDeleted(uuid)
493-
launch {
494-
positionToIdMutex.withLock {
495-
positionToId.removeAll { it.uuid == uuid }
496-
}
497-
}
498478
composeViewState.value = composeViewState.value.let { state ->
499479
val mutableState = state.toMutableMap()
500480
mutableState.remove(uuid)
@@ -614,44 +594,22 @@ class ComposePlaceholderManager(
614594

615595
private suspend fun clearAllViews() {
616596
positionToIdMutex.withLock {
617-
for (placeholder in positionToId) {
618-
composeViewState.value = composeViewState.value.let { state ->
619-
val mutableState = state.toMutableMap()
620-
mutableState.remove(placeholder.uuid)
621-
mutableState
622-
}
623-
}
624-
positionToId.clear()
597+
composeViewState.value = emptyMap()
625598
}
626599
}
627600

628601
override fun onVisibility(visibility: Int) {
629602
launch {
630603
positionToIdMutex.withLock {
631-
for (placeholder in positionToId) {
632-
composeViewState.value = composeViewState.value.let { state ->
633-
val mutableState = state.toMutableMap()
634-
mutableState[placeholder.uuid]?.copy(
635-
visible = View.VISIBLE == visibility
636-
)?.let {
637-
mutableState[placeholder.uuid] = it
638-
}
639-
mutableState
640-
}
604+
composeViewState.value = composeViewState.value.let { state ->
605+
state.mapValues { (_, value) -> value.copy(
606+
visible = View.VISIBLE == visibility
607+
) }
641608
}
642609
}
643610
}
644611
}
645612

646-
data class Placeholder(val elementPosition: Int, val uuid: String)
647-
648-
companion object {
649-
private const val DEFAULT_HTML_TAG = "placeholder"
650-
private const val UUID_ATTRIBUTE = "uuid"
651-
private const val TYPE_ATTRIBUTE = "type"
652-
private const val EDITOR_INNER_PADDING = 20
653-
}
654-
655613
override fun beforeHtmlProcessed(source: String): String {
656614
runBlocking {
657615
clearAllViews()

media-placeholders/src/main/java/org/wordpress/aztec/placeholders/PlaceholderManager.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package org.wordpress.aztec.placeholders
22

3-
import android.content.Context
43
import android.view.MotionEvent
54
import android.view.View
6-
import androidx.compose.runtime.Composable
75
import org.wordpress.aztec.AztecAttributes
86
import org.xml.sax.Attributes
97
import kotlin.math.min
@@ -21,6 +19,15 @@ interface PlaceholderManager {
2119
placeAtStart: Boolean
2220
) -> Map<String, String>
2321
)
22+
23+
data class Placeholder(val elementPosition: Int, val uuid: String)
24+
25+
companion object {
26+
internal const val DEFAULT_HTML_TAG = "placeholder"
27+
internal const val UUID_ATTRIBUTE = "uuid"
28+
internal const val TYPE_ATTRIBUTE = "type"
29+
internal const val EDITOR_INNER_PADDING = 20
30+
}
2431
/**
2532
* A adapter for a custom view drawn over the placeholder in the Aztec text.
2633
*/

media-placeholders/src/main/java/org/wordpress/aztec/placeholders/ViewPlaceholderManager.kt

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ import org.wordpress.aztec.AztecContentChangeWatcher
2323
import org.wordpress.aztec.AztecText
2424
import org.wordpress.aztec.Constants
2525
import org.wordpress.aztec.Html
26+
import org.wordpress.aztec.placeholders.PlaceholderManager.*
27+
import org.wordpress.aztec.placeholders.PlaceholderManager.Companion.DEFAULT_HTML_TAG
28+
import org.wordpress.aztec.placeholders.PlaceholderManager.Companion.EDITOR_INNER_PADDING
29+
import org.wordpress.aztec.placeholders.PlaceholderManager.Companion.TYPE_ATTRIBUTE
30+
import org.wordpress.aztec.placeholders.PlaceholderManager.Companion.UUID_ATTRIBUTE
2631
import org.wordpress.aztec.plugins.html2visual.IHtmlPreprocessor
2732
import org.wordpress.aztec.plugins.html2visual.IHtmlTagHandler
2833
import org.wordpress.aztec.spans.AztecMediaClickableSpan
@@ -542,16 +547,6 @@ class ViewPlaceholderManager(
542547
}
543548
}
544549

545-
data class Placeholder(val elementPosition: Int, val uuid: String)
546-
547-
companion object {
548-
private const val TAG = "PlaceholderManager"
549-
private const val DEFAULT_HTML_TAG = "placeholder"
550-
private const val UUID_ATTRIBUTE = "uuid"
551-
private const val TYPE_ATTRIBUTE = "type"
552-
private const val EDITOR_INNER_PADDING = 20
553-
}
554-
555550
override fun beforeHtmlProcessed(source: String): String {
556551
runBlocking {
557552
clearAllViews()

0 commit comments

Comments
 (0)