-
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.
Fix #4848 Content Description Generation for Content and SelectionInt…
…eractionContent ViewModels (#5704) <!-- READ ME FIRST: Please fill in the explanation section below and check off every point from the Essential Checklist! --> ## Explanation <!-- - Explain what your PR does. If this PR fixes an existing bug, please include - "Fixes #bugnum:" in the explanation so that GitHub can auto-close the issue - when this PR is merged. --> Fix #4848 This PR addresses post-PR review issues from #5614 by refining content description generation. The key updates include: - The `getContentDescription` function is now utilized in `ContentViewModel` and `SelectionInteractionContentViewModel`, ensuring accurate content descriptions with the necessary `customTagHandlers` 1. `ContentViewModel`: Handles `CUSTOM_LIST_LI_TAG`, `CUSTOM_LIST_OL_TAG`, `CUSTOM_LIST_UL_TAG`, `CUSTOM_IMG_TAG`, `CUSTOM_CONCEPT_CARD_TAG`, and `CUSTOM_MATH_TAG` 2. `SelectionInteractionContentViewModel`: Handles `CUSTOM_IMG_TAG` - Improved handling of tags where content resides in attributes, such as anchor tags: `<a href="https://example.com">Click here</a>`, ensuring proper extraction of meaningful descriptions - Handled edge cases in `CustomHtmlContentHandler` to improve `getContentDescription` logic, addressing inconsistencies and included comments for better understanding. ## Essential Checklist <!-- Please tick the relevant boxes by putting an "x" in them. --> - [x] The PR title and explanation each start with "Fix #bugnum: " (If this PR fixes part of an issue, prefix the title with "Fix part of #bugnum: ...".) - [x] Any changes to [scripts/assets](https://github.com/oppia/oppia-android/tree/develop/scripts/assets) files have their rationale included in the PR explanation. - [x] The PR follows the [style guide](https://github.com/oppia/oppia-android/wiki/Coding-style-guide). - [x] The PR does not contain any unnecessary code changes from Android Studio ([reference](https://github.com/oppia/oppia-android/wiki/Guidance-on-submitting-a-PR#undo-unnecessary-changes)). - [x] The PR is made from a branch that's **not** called "develop" and is up-to-date with "develop". - [x] The PR is **assigned** to the appropriate reviewers ([reference](https://github.com/oppia/oppia-android/wiki/Guidance-on-submitting-a-PR#clarification-regarding-assignees-and-reviewers-section)).
- Loading branch information
Showing
12 changed files
with
238 additions
and
101 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
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
Oops, something went wrong.