Skip to content

Commit

Permalink
Enhance text highlighting for notes #73
Browse files Browse the repository at this point in the history
  • Loading branch information
alialbaali committed Jun 20, 2023
1 parent b6b2f6f commit cabf3e2
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions app/src/main/java/com/noto/app/util/ViewUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -491,15 +491,14 @@ fun BottomAppBar.isHiddenAsFlow() = callbackFlow {
fun TextView.setHighlightedText(text: String, term: String, color: NotoColor, matchIndices: IntRange? = null) {
val indices = text.indicesOf(term, ignoreCase = true).filter { it.first < it.last }
val colorResource = context?.colorResource(color.toResource()) ?: return
val lightColorResource = colorResource.withDefaultAlpha(DisabledAlpha)
val lightColorResource = context?.colorAttributeResource(R.attr.notoSecondaryColor)?.withDefaultAlpha(DisabledAlpha / 2) ?: return
val onColorResource = context?.colorAttributeResource(R.attr.notoBackgroundColor) ?: return
val isDarkColor = color == NotoColor.Black || color == NotoColor.Gray
val onLightColorResource = context?.colorAttributeResource(R.attr.notoPrimaryColor) ?: return

val highlightedText = text.toSpannable().apply {
indices.forEach { range ->
val foregroundColorSpan = ForegroundColorSpan(colorResource)
val backgroundColorSpan = CustomBackgroundColorSpan(context, colorResource, onColorResource, textSize)
val lightBackgroundColorSpan = CustomBackgroundColorSpan(context, lightColorResource, onColorResource, textSize)
val lightBackgroundColorSpan = CustomBackgroundColorSpan(context, lightColorResource, onLightColorResource, textSize)
val boldFontSpan = context.tryLoadingFontResource(R.font.nunito_black)?.style?.let(::StyleSpan)
val boldSpan = StyleSpan(Typeface.BOLD)
val startIndex = range.first.coerceIn(0, this.length)
Expand All @@ -509,11 +508,7 @@ fun TextView.setHighlightedText(text: String, term: String, color: NotoColor, ma
setSpan(boldSpan, startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
if (boldFontSpan != null) setSpan(boldFontSpan, startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
} else {
if (isDarkColor) {
setSpan(lightBackgroundColorSpan, startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
} else {
setSpan(foregroundColorSpan, startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
}
setSpan(lightBackgroundColorSpan, startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
}
}
}
Expand Down

0 comments on commit cabf3e2

Please sign in to comment.