@@ -5,12 +5,13 @@ import android.text.TextWatcher
55import org.wordpress.aztec.AztecText
66import org.wordpress.aztec.spans.AztecMediaSpan
77import java.lang.ref.WeakReference
8+ import java.util.concurrent.ConcurrentLinkedQueue
89
910class 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