Skip to content

Commit 8e5bd89

Browse files
committed
refactor(android): look up inline spans by type and reuse applyStyleOnRange
1 parent 06c8864 commit 8e5bd89

2 files changed

Lines changed: 4 additions & 28 deletions

File tree

android/src/main/java/com/swmansion/enriched/textinput/EnrichedTextInputConnectionWrapper.kt

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import android.view.inputmethod.InputConnection
77
import android.view.inputmethod.InputConnectionWrapper
88
import com.facebook.react.bridge.ReactContext
99
import com.facebook.react.uimanager.UIManagerHelper
10-
import com.swmansion.enriched.common.spans.interfaces.EnrichedInlineSpan
1110
import com.swmansion.enriched.textinput.events.OnInputKeyPressEvent
1211
import com.swmansion.enriched.textinput.spans.EnrichedSpans
1312

@@ -109,14 +108,8 @@ class EnrichedTextInputConnectionWrapper(
109108
composingStart: Int,
110109
composingEnd: Int,
111110
): List<InlineSpanSnapshot> =
112-
editable
113-
.getSpans(composingStart, composingEnd, EnrichedInlineSpan::class.java)
114-
.mapNotNull { span ->
115-
val style =
116-
EnrichedSpans.inlineSpans.entries
117-
.firstOrNull { (_, config) -> config.clazz.isInstance(span) }
118-
?.key
119-
?: return@mapNotNull null
111+
EnrichedSpans.inlineSpans.flatMap { (style, config) ->
112+
editable.getSpans(composingStart, composingEnd, config.clazz).mapNotNull { span ->
120113
val start = editable.getSpanStart(span).coerceAtLeast(composingStart)
121114
val end = editable.getSpanEnd(span).coerceAtMost(composingEnd)
122115

@@ -126,6 +119,7 @@ class EnrichedTextInputConnectionWrapper(
126119
null
127120
}
128121
}
122+
}
129123

130124
private fun restoreInlineSpans(snapshot: ComposingTextSnapshot) {
131125
val editable = editText.text ?: return
@@ -182,7 +176,7 @@ class EnrichedTextInputConnectionWrapper(
182176

183177
val mappedStart = newRangeStart + intersectionStart - oldRangeStart
184178
val mappedEnd = newRangeStart + intersectionEnd - oldRangeStart
185-
editText.inlineStyles?.restoreStyleOnRange(
179+
editText.inlineStyles?.applyStyleOnRange(
186180
snapshot.style,
187181
composingStart + mappedStart,
188182
composingStart + mappedEnd,

android/src/main/java/com/swmansion/enriched/textinput/styles/InlineStyles.kt

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -161,24 +161,6 @@ class InlineStyles(
161161
setAndMergeSpans(spannable, type, start, end)
162162
}
163163

164-
fun restoreStyleOnRange(
165-
name: String,
166-
start: Int,
167-
end: Int,
168-
) {
169-
if (start >= end) return
170-
171-
val config = EnrichedSpans.inlineSpans[name] ?: return
172-
val spannable = view.text as? Spannable ?: return
173-
val spans = spannable.getSpans(start, end, config.clazz)
174-
175-
if (spans.any { spannable.getSpanStart(it) <= start && spannable.getSpanEnd(it) >= end }) {
176-
return
177-
}
178-
179-
setSpan(spannable, config.clazz, start, end)
180-
}
181-
182164
fun toggleStyle(name: String) {
183165
if (view.selection == null) return
184166
val (start, end) = view.selection.getInlineSelection()

0 commit comments

Comments
 (0)