Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.swmansion.enriched

import android.os.Build
import android.view.textclassifier.TextClassification
import android.view.textclassifier.TextClassifier
import android.view.textclassifier.TextSelection
import androidx.annotation.RequiresApi
import com.swmansion.enriched.constants.Strings

@RequiresApi(Build.VERSION_CODES.O)
internal class EnrichedTextClassifier(
private val delegate: TextClassifier,
) : TextClassifier {
@RequiresApi(Build.VERSION_CODES.P)
override fun suggestSelection(request: TextSelection.Request): TextSelection =
if (touchesReplacementBlock(request.text, request.startIndex, request.endIndex)) {
TextSelection.Builder(request.startIndex, request.endIndex).build()
} else {
delegate.suggestSelection(request)
}

@RequiresApi(Build.VERSION_CODES.P)
override fun classifyText(request: TextClassification.Request): TextClassification =
if (touchesReplacementBlock(request.text, request.startIndex, request.endIndex)) {
TextClassification.Builder().build()
} else {
delegate.classifyText(request)
}
Comment on lines +10 to +28

private fun touchesReplacementBlock(
text: CharSequence,
start: Int,
end: Int,
): Boolean {
val from = (start - 1).coerceAtLeast(0)
val to = (end + 1).coerceAtMost(text.length)
return (from until to).any { text[it] == Strings.OBJECT_REPLACEMENT_CHAR }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class EnrichedTextInputView : AppCompatEditText {
private var typefaceDirty = false

private var inputMethodManager: InputMethodManager? = null
private var enrichedTextClassifier: EnrichedTextClassifier? = null
private var autoFocus = false
private var didAttachToWindow = false

Expand Down Expand Up @@ -210,6 +211,12 @@ class EnrichedTextInputView : AppCompatEditText {
transformationMethod = LineSeparatorTransformationMethod()
addTextChangedListener(EnrichedTextWatcher(this))
filters = arrayOf(NonEditableParagraphFilter(), ParagraphLimitFilter(this))
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
enrichedTextClassifier =
EnrichedTextClassifier(textClassifier).also {
setTextClassifier(it)
}
}
}
Comment on lines +214 to 220

override fun onLayout(
Expand Down