Skip to content

Commit

Permalink
Add replaceRegexWithBlank function
Browse files Browse the repository at this point in the history
  • Loading branch information
subhajitxyz committed Oct 12, 2024
1 parent b4ad7a3 commit 8d0328c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,10 +1,38 @@
package org.oppia.android.app.player.state.itemviewmodel

import android.text.Spannable
import android.text.SpannableStringBuilder

/** [StateItemViewModel] for content-card state. */
class ContentViewModel(
val htmlContent: CharSequence,
val gcsEntityId: String,
val hasConversationView: Boolean,
val isSplitView: Boolean,
val supportsConceptCards: Boolean
) : StateItemViewModel(ViewType.CONTENT)
) : StateItemViewModel(ViewType.CONTENT) {

private val underscoreRegex = Regex("(?<=\\s|[,.;?!])_{3,}(?=\\s|[,.;?!])")
private val replacementText = "Blank"

/**
* Replaces "2+ underscores, with space/punctuation on both sides" in the input text with a
* replacement string "blank", returning a Spannable.
* Adjusts offsets to handle text length changes during replacements.
*/
fun replaceRegexWithBlank(inputText: CharSequence): Spannable {
val spannableStringBuilder = SpannableStringBuilder(inputText)
val matches = underscoreRegex.findAll(inputText)
var lengthOffset = 0

for (match in matches) {
val matchStart = match.range.first + lengthOffset
val matchEnd = match.range.last + 1 + lengthOffset
spannableStringBuilder.replace(matchStart, matchEnd, replacementText)

// Adjust offset due to change in length (difference between old and new text length)
lengthOffset += replacementText.length - (matchEnd - matchStart)
}
return spannableStringBuilder
}
}
1 change: 1 addition & 0 deletions app/src/main/res/layout/content_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
android:minWidth="48dp"
android:minHeight="48dp"
android:text="@{htmlContent}"
android:contentDescription="@{viewModel.replaceRegexWithBlank(htmlContent)}"
android:textColor="@color/component_color_shared_primary_text_color"
android:textColorLink="@color/component_color_shared_link_text_color"
android:textSize="16sp"
Expand Down

0 comments on commit 8d0328c

Please sign in to comment.