-
Notifications
You must be signed in to change notification settings - Fork 527
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b4ad7a3
commit 8d0328c
Showing
2 changed files
with
30 additions
and
1 deletion.
There are no files selected for viewing
30 changes: 29 additions & 1 deletion
30
app/src/main/java/org/oppia/android/app/player/state/itemviewmodel/ContentViewModel.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 |
---|---|---|
@@ -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 | ||
} | ||
} |
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