-
Notifications
You must be signed in to change notification settings - Fork 544
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into feature/test-url-image-parser-loading
- Loading branch information
Showing
24 changed files
with
593 additions
and
183 deletions.
There are no files selected for viewing
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
33 changes: 15 additions & 18 deletions
33
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,38 +1,35 @@ | ||
package org.oppia.android.app.player.state.itemviewmodel | ||
|
||
import android.text.Spannable | ||
import android.text.SpannableStringBuilder | ||
import org.oppia.android.util.parser.html.CustomHtmlContentHandler | ||
|
||
/** [StateItemViewModel] for content-card state. */ | ||
class ContentViewModel( | ||
val htmlContent: CharSequence, | ||
val gcsEntityId: String, | ||
val hasConversationView: Boolean, | ||
val isSplitView: Boolean, | ||
val supportsConceptCards: Boolean | ||
val supportsConceptCards: Boolean, | ||
val customTagHandlers: Map<String, CustomHtmlContentHandler.CustomTagHandler> | ||
) : StateItemViewModel(ViewType.CONTENT) { | ||
|
||
private val underscoreRegex = Regex("(?<=\\s|[,.;?!])_{3,}(?=\\s|[,.;?!])") | ||
private val replacementText = "Blank" | ||
|
||
/** Returns content description by extracting text from [htmlContent]. */ | ||
fun getContentDescription(): String { | ||
val contentDescription = CustomHtmlContentHandler.getContentDescription( | ||
htmlContent.toString(), | ||
imageRetriever = null, | ||
customTagHandlers = customTagHandlers | ||
) | ||
return replaceRegexWithBlank(contentDescription) | ||
} | ||
|
||
/** | ||
* 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 | ||
} | ||
private fun replaceRegexWithBlank(inputText: CharSequence): String = | ||
underscoreRegex.replace(inputText, replacementText) | ||
} |
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
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.