Skip to content

Commit d6837e8

Browse files
committed
Fix issues with placeholders and blocked thread
1 parent 9cf35b9 commit d6837e8

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2161,15 +2161,18 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
21612161
lineBlockFormatter.insertVideo(shouldAddMediaInline, drawable, attributes, onVideoTappedListener, onMediaDeletedListener)
21622162
}
21632163

2164-
fun removeMedia(predicate: (Attributes) -> Boolean) {
2164+
fun removeMedia(notifyContentChange: Boolean = true, predicate: (Attributes) -> Boolean) {
21652165
removeMedia(object : AttributePredicate {
21662166
override fun matches(attrs: Attributes): Boolean {
21672167
return predicate(attrs)
21682168
}
2169-
})
2169+
}, notifyContentChange)
21702170
}
21712171

2172-
fun removeMedia(attributePredicate: AttributePredicate) {
2172+
fun removeMedia(attributePredicate: AttributePredicate, notifyContentChange: Boolean = true) {
2173+
if (!notifyContentChange) {
2174+
disableTextChangedListener()
2175+
}
21732176
text.getSpans(0, text.length, AztecMediaSpan::class.java)
21742177
.filter {
21752178
attributePredicate.matches(it.attributes)
@@ -2217,6 +2220,9 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
22172220
}
22182221
mediaSpan.onMediaDeleted()
22192222
}
2223+
if (!notifyContentChange) {
2224+
enableTextChangedListener()
2225+
}
22202226
}
22212227

22222228
interface AttributePredicate {

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

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import android.graphics.drawable.Drawable
77
import android.text.Editable
88
import android.text.Layout
99
import android.text.Spanned
10+
import android.util.Log
1011
import android.view.MotionEvent
1112
import android.view.View
1213
import android.view.ViewTreeObserver
@@ -110,7 +111,15 @@ class PlaceholderManager(
110111
* @param updateItem function to update current parameters with new params
111112
*/
112113
suspend fun insertOrUpdateItem(type: String, shouldMergeItem: (currentItemType: String) -> Boolean = { true }, updateItem: (currentAttributes: Map<String, String>?, currentType: String?) -> Map<String, String>) {
113-
val from = (aztecText.selectionStart - 1).coerceAtLeast(0)
114+
val previousIndex = (aztecText.selectionStart - 1).coerceAtLeast(0)
115+
val indexBeforePrevious = (aztecText.selectionStart - 2).coerceAtLeast(0)
116+
val from = if (aztecText.editableText[previousIndex] == Constants.IMG_CHAR) {
117+
previousIndex
118+
} else if (aztecText.editableText[previousIndex] == '\n') {
119+
indexBeforePrevious
120+
} else {
121+
aztecText.selectionStart
122+
}
114123
val editableText = aztecText.editableText
115124
val currentItem = editableText.getSpans(
116125
from,
@@ -129,7 +138,7 @@ class PlaceholderManager(
129138
currentAttributes[name] = value
130139
}
131140
val updatedAttributes = updateItem(currentAttributes, currentType)
132-
removeItem { aztecAttributes ->
141+
removeItem(false) { aztecAttributes ->
133142
aztecAttributes.getValue(UUID_ATTRIBUTE) == uuid
134143
}
135144
val attrs = AztecAttributes().apply {
@@ -152,8 +161,8 @@ class PlaceholderManager(
152161
* Call this method to remove a placeholder from both the AztecText and the overlaying layer programatically.
153162
* @param predicate determines whether a span should be removed
154163
*/
155-
fun removeItem(predicate: (Attributes) -> Boolean) {
156-
aztecText.removeMedia { predicate(it) }
164+
fun removeItem(notifyContentChange: Boolean = true, predicate: (Attributes) -> Boolean) {
165+
aztecText.removeMedia(notifyContentChange) { predicate(it) }
157166
}
158167

159168
private suspend fun buildPlaceholderDrawable(adapter: PlaceholderAdapter, attrs: AztecAttributes): Drawable {
@@ -166,9 +175,13 @@ class PlaceholderManager(
166175
* Call this method to reload all the placeholders
167176
*/
168177
suspend fun reloadAllPlaceholders() {
169-
positionToIdMutex.withLock {
170-
positionToId.forEach {
171-
insertContentOverSpanWithId(it.uuid)
178+
val tempPositionToId = positionToId.toList()
179+
tempPositionToId.forEach { placeholder ->
180+
val isValid = positionToIdMutex.withLock {
181+
positionToId.contains(placeholder)
182+
}
183+
if (isValid) {
184+
insertContentOverSpanWithId(placeholder.uuid)
172185
}
173186
}
174187
}
@@ -401,14 +414,17 @@ class PlaceholderManager(
401414
}
402415

403416
private suspend fun clearAllViews() {
417+
Log.d("vojta", "Before clearing all with lock")
404418
positionToIdMutex.withLock {
419+
Log.d("vojta", "Clearing all with lock")
405420
for (placeholder in positionToId) {
406421
container.findViewWithTag<View>(placeholder.uuid)?.let {
407422
it.visibility = View.GONE
408423
container.removeView(it)
409424
}
410425
}
411426
positionToId.clear()
427+
Log.d("vojta", "Cleared all with lock")
412428
}
413429
}
414430

0 commit comments

Comments
 (0)