-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Highlights the beginning of each word * May help some people read faster|understand more * Thickness option to change highlighting strength Resolves: #109
- Loading branch information
Showing
18 changed files
with
373 additions
and
57 deletions.
There are no files selected for viewing
11 changes: 9 additions & 2 deletions
11
app/src/main/java/ua/acclorite/book_story/domain/reader/ReaderTextAlignment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
.../main/java/ua/acclorite/book_story/presentation/core/components/common/HighlightedText.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package ua.acclorite.book_story.presentation.core.components.common | ||
|
||
import androidx.compose.foundation.text.BasicText | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.text.AnnotatedString | ||
import androidx.compose.ui.text.SpanStyle | ||
import androidx.compose.ui.text.TextStyle | ||
import androidx.compose.ui.text.buildAnnotatedString | ||
import androidx.compose.ui.text.font.FontWeight | ||
import androidx.compose.ui.text.style.TextOverflow | ||
import androidx.compose.ui.text.withStyle | ||
import kotlin.math.roundToInt | ||
|
||
@Composable | ||
fun HighlightedText( | ||
modifier: Modifier = Modifier, | ||
text: AnnotatedString, | ||
highlightThickness: FontWeight, | ||
style: TextStyle | ||
) { | ||
val highlightedText = remember(text, highlightThickness) { | ||
buildAnnotatedString { | ||
text.splitAnnotatedString().forEach { word -> | ||
if (word.text.none { it.isLetter() }) { | ||
append(word) | ||
append(" ") | ||
return@forEach | ||
} | ||
|
||
val textWord = word.text.filterDigitsAtStartAndEnd() | ||
val digitsAtStart = word.text.digitsAtStart() | ||
val focusArea = when (textWord.length) { | ||
in 1..3 -> 1 | ||
4 -> 2 | ||
else -> (textWord.length * 0.5f).roundToInt() | ||
} + digitsAtStart | ||
|
||
if (digitsAtStart > 0) { | ||
append(word.subSequence(0, digitsAtStart)) | ||
} | ||
|
||
withStyle(style = SpanStyle(fontWeight = highlightThickness)) { | ||
append(word.subSequence(digitsAtStart, focusArea)) | ||
} | ||
|
||
append(word.subSequence(focusArea, word.text.length)) | ||
append(" ") | ||
} | ||
} | ||
} | ||
|
||
BasicText( | ||
text = highlightedText, | ||
modifier = modifier, | ||
style = style, | ||
overflow = TextOverflow.Ellipsis | ||
) | ||
} | ||
|
||
private fun String.digitsAtStart(): Int { | ||
var count = 0 | ||
for (char in this) { | ||
if (!char.isLetter()) count++ | ||
else break | ||
} | ||
return count | ||
} | ||
|
||
private fun String.filterDigitsAtStartAndEnd(): String { | ||
return dropWhile { !it.isLetter() }.dropLastWhile { !it.isLetter() } | ||
} | ||
|
||
private fun AnnotatedString.splitAnnotatedString(): List<AnnotatedString> { | ||
val result = mutableListOf<AnnotatedString>() | ||
|
||
val wordRegex = Regex("\\S+") | ||
wordRegex.findAll(this).forEach { matchResult -> | ||
val wordStart = matchResult.range.first | ||
val wordEnd = matchResult.range.last + 1 | ||
|
||
val word = subSequence(wordStart, wordEnd) | ||
result.add(word) | ||
} | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.