Skip to content

Commit

Permalink
compare with prev text value to avoid multiple applyMarkdownFormattin…
Browse files Browse the repository at this point in the history
…g calls
  • Loading branch information
wildan-m committed Sep 11, 2024
1 parent 0dfd7b9 commit c176a5e
Showing 1 changed file with 9 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

public class MarkdownTextWatcher implements TextWatcher {
private final MarkdownUtils mMarkdownUtils;

private boolean mShouldSkip = false;
private Editable mPreviousEditable;

public MarkdownTextWatcher(@NonNull MarkdownUtils markdownUtils) {
mMarkdownUtils = markdownUtils;
Expand All @@ -22,23 +22,17 @@ public void beforeTextChanged(CharSequence s, int start, int count, int after) {

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (mShouldSkip) {
return;
}
// Set the flag to indicate text is being changed
mShouldSkip = true;

}

@Override
public void afterTextChanged(Editable editable) {
if (!mShouldSkip) {
return;
}

if (editable instanceof SpannableStringBuilder) {
if (mPreviousEditable != null && mPreviousEditable.toString().equals(editable.toString())) {
return;
}
mMarkdownUtils.applyMarkdownFormatting((SpannableStringBuilder) editable);
}

// Reset the flag after formatting is applied
mShouldSkip = false;
}}
mPreviousEditable = new SpannableStringBuilder(editable);
}
}
}

0 comments on commit c176a5e

Please sign in to comment.