Skip to content

Commit 7662544

Browse files
authored
chore: cleanup android utils (#373)
# Summary Tiny refactor for android utils file structure
1 parent 7fc45e8 commit 7662544

3 files changed

Lines changed: 97 additions & 92 deletions

File tree

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.swmansion.enriched.utils
2+
3+
import android.text.Spannable
4+
import android.text.SpannableString
5+
import android.text.SpannableStringBuilder
6+
import com.swmansion.enriched.spans.interfaces.EnrichedBlockSpan
7+
import com.swmansion.enriched.spans.interfaces.EnrichedParagraphSpan
8+
9+
fun Spannable.getSafeSpanBoundaries(
10+
start: Int,
11+
end: Int,
12+
): Pair<Int, Int> {
13+
val safeStart = start.coerceAtMost(end).coerceAtLeast(0)
14+
val safeEnd = end.coerceAtLeast(start).coerceAtMost(this.length)
15+
16+
return Pair(safeStart, safeEnd)
17+
}
18+
19+
fun Spannable.getParagraphBounds(
20+
start: Int,
21+
end: Int,
22+
): Pair<Int, Int> {
23+
var startPosition = start.coerceAtLeast(0).coerceAtMost(this.length)
24+
var endPosition = end.coerceAtLeast(0).coerceAtMost(this.length)
25+
26+
// Find the start of the paragraph
27+
while (startPosition > 0 && this[startPosition - 1] != '\n') {
28+
startPosition--
29+
}
30+
31+
// Find the end of the paragraph
32+
while (endPosition < this.length && this[endPosition] != '\n') {
33+
endPosition++
34+
}
35+
36+
if (startPosition >= endPosition) {
37+
// If the start position is equal or greater than the end position, return the same position
38+
startPosition = endPosition
39+
}
40+
41+
return Pair(startPosition, endPosition)
42+
}
43+
44+
fun Spannable.getParagraphBounds(index: Int): Pair<Int, Int> = this.getParagraphBounds(index, index)
45+
46+
fun Spannable.mergeSpannables(
47+
start: Int,
48+
end: Int,
49+
string: String,
50+
): Spannable = this.mergeSpannables(start, end, SpannableString(string))
51+
52+
fun Spannable.mergeSpannables(
53+
start: Int,
54+
end: Int,
55+
spannable: Spannable,
56+
): Spannable {
57+
var finalStart = start
58+
var finalEnd = end
59+
60+
val builder = SpannableStringBuilder(this)
61+
val startBlockSpans = spannable.getSpans(0, 0, EnrichedBlockSpan::class.java)
62+
val startParagraphSpans = spannable.getSpans(0, 0, EnrichedParagraphSpan::class.java)
63+
val endBlockSpans = spannable.getSpans(this.length, this.length, EnrichedBlockSpan::class.java)
64+
val endParagraphSpans = spannable.getSpans(this.length, this.length, EnrichedParagraphSpan::class.java)
65+
val (paragraphStart, paragraphEnd) = this.getParagraphBounds(start, end)
66+
val isNewLineStart = startBlockSpans.isNotEmpty() || startParagraphSpans.isNotEmpty()
67+
val isNewLineEnd = endBlockSpans.isNotEmpty() || endParagraphSpans.isNotEmpty()
68+
69+
if (isNewLineStart && start != paragraphStart) {
70+
builder.insert(start, "\n")
71+
finalStart = start + 1
72+
finalEnd = end + 1
73+
}
74+
75+
if (isNewLineEnd && end != paragraphEnd) {
76+
builder.insert(finalEnd, "\n")
77+
}
78+
79+
builder.replace(finalStart, finalEnd, spannable)
80+
81+
return builder
82+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.swmansion.enriched.utils
2+
3+
import android.text.SpannableStringBuilder
4+
5+
// Removes zero-width spaces from the given range in the SpannableStringBuilder without affecting spans
6+
fun SpannableStringBuilder.removeZWS(
7+
start: Int,
8+
end: Int,
9+
) {
10+
for (i in (end - 1) downTo start) {
11+
if (this[i] == EnrichedConstants.ZWS) {
12+
delete(i, i + 1)
13+
}
14+
}
15+
}
Lines changed: 0 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
package com.swmansion.enriched.utils
22

3-
import android.text.Spannable
4-
import android.text.SpannableString
5-
import android.text.SpannableStringBuilder
63
import android.util.Log
7-
import com.swmansion.enriched.spans.interfaces.EnrichedBlockSpan
8-
import com.swmansion.enriched.spans.interfaces.EnrichedParagraphSpan
94
import org.json.JSONObject
105

116
fun jsonStringToStringMap(json: String): Map<String, String> {
@@ -24,90 +19,3 @@ fun jsonStringToStringMap(json: String): Map<String, String> {
2419

2520
return result
2621
}
27-
28-
fun Spannable.getSafeSpanBoundaries(
29-
start: Int,
30-
end: Int,
31-
): Pair<Int, Int> {
32-
val safeStart = start.coerceAtMost(end).coerceAtLeast(0)
33-
val safeEnd = end.coerceAtLeast(start).coerceAtMost(this.length)
34-
35-
return Pair(safeStart, safeEnd)
36-
}
37-
38-
fun Spannable.getParagraphBounds(
39-
start: Int,
40-
end: Int,
41-
): Pair<Int, Int> {
42-
var startPosition = start.coerceAtLeast(0).coerceAtMost(this.length)
43-
var endPosition = end.coerceAtLeast(0).coerceAtMost(this.length)
44-
45-
// Find the start of the paragraph
46-
while (startPosition > 0 && this[startPosition - 1] != '\n') {
47-
startPosition--
48-
}
49-
50-
// Find the end of the paragraph
51-
while (endPosition < this.length && this[endPosition] != '\n') {
52-
endPosition++
53-
}
54-
55-
if (startPosition >= endPosition) {
56-
// If the start position is equal or greater than the end position, return the same position
57-
startPosition = endPosition
58-
}
59-
60-
return Pair(startPosition, endPosition)
61-
}
62-
63-
fun Spannable.getParagraphBounds(index: Int): Pair<Int, Int> = this.getParagraphBounds(index, index)
64-
65-
fun Spannable.mergeSpannables(
66-
start: Int,
67-
end: Int,
68-
string: String,
69-
): Spannable = this.mergeSpannables(start, end, SpannableString(string))
70-
71-
fun Spannable.mergeSpannables(
72-
start: Int,
73-
end: Int,
74-
spannable: Spannable,
75-
): Spannable {
76-
var finalStart = start
77-
var finalEnd = end
78-
79-
val builder = SpannableStringBuilder(this)
80-
val startBlockSpans = spannable.getSpans(0, 0, EnrichedBlockSpan::class.java)
81-
val startParagraphSpans = spannable.getSpans(0, 0, EnrichedParagraphSpan::class.java)
82-
val endBlockSpans = spannable.getSpans(this.length, this.length, EnrichedBlockSpan::class.java)
83-
val endParagraphSpans = spannable.getSpans(this.length, this.length, EnrichedParagraphSpan::class.java)
84-
val (paragraphStart, paragraphEnd) = this.getParagraphBounds(start, end)
85-
val isNewLineStart = startBlockSpans.isNotEmpty() || startParagraphSpans.isNotEmpty()
86-
val isNewLineEnd = endBlockSpans.isNotEmpty() || endParagraphSpans.isNotEmpty()
87-
88-
if (isNewLineStart && start != paragraphStart) {
89-
builder.insert(start, "\n")
90-
finalStart = start + 1
91-
finalEnd = end + 1
92-
}
93-
94-
if (isNewLineEnd && end != paragraphEnd) {
95-
builder.insert(finalEnd, "\n")
96-
}
97-
98-
builder.replace(finalStart, finalEnd, spannable)
99-
100-
return builder
101-
}
102-
103-
// Removes zero-width spaces from the given range in the SpannableStringBuilder without affecting spans
104-
fun SpannableStringBuilder.removeZWS(
105-
start: Int,
106-
end: Int,
107-
) {
108-
for (i in (end - 1) downTo start) {
109-
if (this[i] == EnrichedConstants.ZWS) {
110-
delete(i, i + 1)
111-
}
112-
}
113-
}

0 commit comments

Comments
 (0)