<commit_before>package org.wordpress.aztec.watchers

import android.text.Editable
import android.text.TextWatcher
import org.wordpress.aztec.AztecText
import org.wordpress.aztec.Constants
import org.wordpress.aztec.spans.AztecMediaSpan
import java.lang.ref.WeakReference

class DeleteMediaElementWatcher(aztecText: AztecText) : TextWatcher {

    private val aztecTextRef: WeakReference<AztecText?> = WeakReference(aztecText)

    private var deletedMedia: Boolean = false
    private var mediaSpan: AztecMediaSpan? = null

    override fun beforeTextChanged(text: CharSequence, start: Int, count: Int, after: Int) {
        deletedMedia = count > 0 && text[start + count - 1] == Constants.IMG_CHAR

        if (deletedMedia) {
            val aztecText = aztecTextRef.get()
            mediaSpan = aztecText?.text?.getSpans(start, start + count, AztecMediaSpan::class.java)?.firstOrNull()
        }
    }

    override fun onTextChanged(text: CharSequence, start: Int, before: Int, count: Int) {
        // no op
    }

    override fun afterTextChanged(text: Editable) {
        if (aztecTextRef.get()?.isTextChangedListenerDisabled() ?: true) {
            return
        }

        if (deletedMedia) {
            deletedMedia = false
            mediaSpan?.onMediaDeleted()
        }
    }

    companion object {
        fun install(text: AztecText) {
            text.addTextChangedListener(DeleteMediaElementWatcher(text))
        }
    }
}<commit_msg>fixed deletion handler by only considering events when they're enabled<commit_after>package org.wordpress.aztec.watchers

import android.text.Editable
import android.text.TextWatcher
import org.wordpress.aztec.AztecText
import org.wordpress.aztec.Constants
import org.wordpress.aztec.spans.AztecMediaSpan
import java.lang.ref.WeakReference

class DeleteMediaElementWatcher(aztecText: AztecText) : TextWatcher {

    private val aztecTextRef: WeakReference<AztecText?> = WeakReference(aztecText)

    override fun beforeTextChanged(text: CharSequence, start: Int, count: Int, after: Int) {
        if (aztecTextRef.get()?.isTextChangedListenerDisabled() ?: true) {
            return
        }
        var deletedMedia = count > 0 && text[start + count - 1] == Constants.IMG_CHAR

        if (deletedMedia) {
            var mediaSpan = aztecTextRef.get()?.text?.getSpans(start, start + count, AztecMediaSpan::class.java)?.firstOrNull()
            mediaSpan?.onMediaDeleted()
        }
    }

    override fun onTextChanged(text: CharSequence, start: Int, before: Int, count: Int) {
        // no op
    }

    override fun afterTextChanged(text: Editable) {
        // no op
    }

    companion object {
        fun install(text: AztecText) {
            text.addTextChangedListener(DeleteMediaElementWatcher(text))
        }
    }
}
{'fraction_non_alphanumeric': 0.09731663685152057, 'fraction_numerical': 0.0014311270125223613, 'mean_word_length': 3.1238938053097347, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7252881', 'n_tokens_mistral': 866, 'n_tokens_neox': 806, 'n_words': 178}