Add sync message handling and stop formatting behavior.

This commit is contained in:
Cody Henthorne
2023-03-21 11:43:43 -04:00
committed by Greyson Parrelli
parent cedf512726
commit ee48e6c347
4 changed files with 105 additions and 16 deletions

View File

@@ -298,6 +298,8 @@ public class ComposeText extends EmojiEditText {
addTextChangedListener(mentionValidatorWatcher);
if (FeatureFlags.textFormatting()) {
addTextChangedListener(new ComposeTextStyleWatcher());
setCustomSelectionActionModeCallback(new ActionMode.Callback() {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
@@ -350,7 +352,7 @@ public class ComposeText extends EmojiEditText {
}
if (style != null) {
replacement.setSpan(style, 0, charSequence.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
replacement.setSpan(style, 0, charSequence.length(), MessageStyler.SPAN_FLAGS);
}
clearComposingText();

View File

@@ -0,0 +1,80 @@
package org.thoughtcrime.securesms.components
import android.text.Annotation
import android.text.Editable
import android.text.Spannable
import android.text.Spanned
import android.text.TextUtils
import android.text.TextWatcher
import android.text.style.CharacterStyle
import org.signal.core.util.StringUtil
import org.thoughtcrime.securesms.conversation.MessageStyler
/**
* Formatting should only grow when appending until a white space character is entered/pasted.
*
* This watcher observes changes to the text and will shrink supported style ranges as necessary
* to provide the desired behavior.
*/
class ComposeTextStyleWatcher : TextWatcher {
private val markerAnnotation = Annotation("text-formatting", "marker")
private var textSnapshotPriorToChange: CharSequence? = null
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
if (s is Spannable) {
s.removeSpan(markerAnnotation)
}
textSnapshotPriorToChange = s.subSequence(start, start + count)
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
if (s is Spannable) {
s.removeSpan(markerAnnotation)
if (count > 0) {
s.setSpan(markerAnnotation, start, start + count, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}
}
}
override fun afterTextChanged(s: Editable) {
val editStart = s.getSpanStart(markerAnnotation)
val editEnd = s.getSpanEnd(markerAnnotation)
s.removeSpan(markerAnnotation)
if (editStart < 0 || editEnd < 0 || editStart >= editEnd || (editStart == 0 && editEnd == s.length)) {
return
}
val change = s.subSequence(editStart, editEnd)
if (change.isEmpty() || textSnapshotPriorToChange == null || (editEnd - editStart == 1 && !StringUtil.isVisuallyEmpty(change[0])) || TextUtils.equals(textSnapshotPriorToChange, change)) {
textSnapshotPriorToChange = null
return
}
textSnapshotPriorToChange = null
var newEnd = editStart
for (i in change.indices) {
if (StringUtil.isVisuallyEmpty(change[i])) {
newEnd = editStart + i
break
}
}
s.getSpans(editStart, editEnd, CharacterStyle::class.java)
.filter { MessageStyler.isSupportedCharacterStyle(it) }
.forEach { style ->
val styleStart = s.getSpanStart(style)
val styleEnd = s.getSpanEnd(style)
if (styleEnd == editEnd && styleStart < styleEnd) {
s.removeSpan(style)
s.setSpan(style, styleStart, newEnd, MessageStyler.SPAN_FLAGS)
} else {
s.removeSpan(style)
}
}
}
}