Skip to content

Commit dc964e4

Browse files
committed
Use concurrent linked queue when deleting media
1 parent 72a1a57 commit dc964e4

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

aztec/src/main/kotlin/org/wordpress/aztec/spans/AztecMediaSpan.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,6 @@ abstract class AztecMediaSpan(context: Context, drawable: Drawable?, override va
100100
onMediaDeletedListener?.onMediaDeleted(attributes)
101101
}
102102
fun beforeMediaDeleted() {
103-
onMediaDeletedListener?.onMediaDeleted(attributes)
103+
onMediaDeletedListener?.beforeMediaDeleted(attributes)
104104
}
105105
}

aztec/src/main/kotlin/org/wordpress/aztec/watchers/DeleteMediaElementWatcherAPI25AndHigher.kt

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import android.text.TextWatcher
55
import org.wordpress.aztec.AztecText
66
import org.wordpress.aztec.spans.AztecMediaSpan
77
import java.lang.ref.WeakReference
8+
import java.util.concurrent.ConcurrentLinkedQueue
89

910
class DeleteMediaElementWatcherAPI25AndHigher(aztecText: AztecText) : TextWatcher {
1011
private val aztecTextRef: WeakReference<AztecText?> = WeakReference(aztecText)
1112
private var deleted = false
1213
private var queueHasBeenPopulatedInThisTimeframe = false
13-
private var deletedSpans = ArrayList<AztecMediaSpan>()
14+
private val deletedSpans = ConcurrentLinkedQueue<AztecMediaSpan>()
1415

1516
override fun beforeTextChanged(text: CharSequence, start: Int, count: Int, after: Int) {
1617
if (aztecTextRef.get()?.isTextChangedListenerDisabled() != false) {
@@ -29,23 +30,18 @@ class DeleteMediaElementWatcherAPI25AndHigher(aztecText: AztecText) : TextWatche
2930
aztecTextRef.get()?.text?.getSpans(start, start + count, AztecMediaSpan::class.java)
3031
?.forEach {
3132
deletedSpans.add(it)
33+
if (!queueHasBeenPopulatedInThisTimeframe) {
34+
it.beforeMediaDeleted()
35+
}
3236
}
33-
34-
if (!queueHasBeenPopulatedInThisTimeframe) {
35-
deletedSpans.forEach { it.beforeMediaDeleted() }
36-
}
3737
// only call the onMediaDeleted callback if we are sure the ObservationQueue has not been filled with
3838
// platform-only events in a short time. These platform-originated events shall not be confused with
3939
// real user deletions.
40-
aztecTextRef.get()?.postDelayed( object : Runnable {
41-
override fun run() {
42-
if (!queueHasBeenPopulatedInThisTimeframe) {
43-
deletedSpans.forEach { it.onMediaDeleted() }
44-
}
45-
// reset flag
46-
deletedSpans.clear()
47-
queueHasBeenPopulatedInThisTimeframe = false
40+
aztecTextRef.get()?.postDelayed({
41+
while (!queueHasBeenPopulatedInThisTimeframe && deletedSpans.isNotEmpty()) {
42+
deletedSpans.poll().onMediaDeleted()
4843
}
44+
queueHasBeenPopulatedInThisTimeframe = false
4945
}, 500)
5046
}
5147
}

0 commit comments

Comments
 (0)