From b173a2a52e954cd540eb7b474267b43da796a75f Mon Sep 17 00:00:00 2001 From: Spencer <74568012+XichengSpencer@users.noreply.github.com> Date: Mon, 22 Jan 2024 17:06:01 -0500 Subject: [PATCH] Fix #4206 Create New HtmlParser Factory Method (#5277) ## Explanation Fix #4206 Create New HtmlParser Factory Method to only take customOppiaTagActionListener and displayLocale as parameter and replace factory method usage in MathExpressionParserViewModel with this new implementation ## Essential Checklist - [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)). ## For UI-specific PRs only - Add a screenshot demonstrating that you ran affected Espresso tests locally & that they're passing ![Screenshot 2024-01-07 142738](https://github.com/oppia/oppia-android/assets/74568012/70cb0448-2ed5-458f-bbba-cf8877e11ef1) ![Screenshot 2024-01-07 145604](https://github.com/oppia/oppia-android/assets/74568012/e465d9de-8153-456b-a1db-28428cc04ad8) --------- Co-authored-by: Adhiambo Peres <59600948+adhiamboperes@users.noreply.github.com> --- .../MathExpressionParserViewModel.kt | 5 -- .../android/util/parser/html/HtmlParser.kt | 54 ++++++++++++++----- 2 files changed, 41 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/org/oppia/android/app/devoptions/mathexpressionparser/MathExpressionParserViewModel.kt b/app/src/main/java/org/oppia/android/app/devoptions/mathexpressionparser/MathExpressionParserViewModel.kt index e808c6fdb68..95fb16f7a05 100644 --- a/app/src/main/java/org/oppia/android/app/devoptions/mathexpressionparser/MathExpressionParserViewModel.kt +++ b/app/src/main/java/org/oppia/android/app/devoptions/mathexpressionparser/MathExpressionParserViewModel.kt @@ -31,12 +31,7 @@ class MathExpressionParserViewModel @Inject constructor( private val htmlParserFactory: HtmlParser.Factory ) : ObservableViewModel() { private val htmlParser by lazy { - // TODO(#4206): Replace this with the variant that doesn't require GCS properties. htmlParserFactory.create( - gcsResourceName = "", - entityType = "", - entityId = "", - imageCenterAlign = false, displayLocale = appLanguageResourceHandler.getDisplayLocale() ) } diff --git a/utility/src/main/java/org/oppia/android/util/parser/html/HtmlParser.kt b/utility/src/main/java/org/oppia/android/util/parser/html/HtmlParser.kt index 97db2dce9db..f08fcfe807a 100755 --- a/utility/src/main/java/org/oppia/android/util/parser/html/HtmlParser.kt +++ b/utility/src/main/java/org/oppia/android/util/parser/html/HtmlParser.kt @@ -78,7 +78,6 @@ class HtmlParser private constructor( supportsLinks: Boolean = false, supportsConceptCards: Boolean = false ): Spannable { - var htmlContent = rawString // Canvas does not support RTL, it always starts from left to right in RTL due to which compound drawables are @@ -124,11 +123,17 @@ class HtmlParser private constructor( } val imageGetter = urlImageParserFactory?.create( - htmlContentTextView, gcsResourceName, entityType, entityId, imageCenterAlign + htmlContentTextView, + gcsResourceName, + entityType, + entityId, + imageCenterAlign ) val htmlSpannable = CustomHtmlContentHandler.fromHtml( - htmlContent, imageGetter, computeCustomTagHandlers(supportsConceptCards, htmlContentTextView) + htmlContent, + imageGetter, + computeCustomTagHandlers(supportsConceptCards, htmlContentTextView) ) val urlPattern = Patterns.WEB_URL @@ -226,17 +231,40 @@ class HtmlParser private constructor( displayLocale: OppiaLocale.DisplayLocale ): HtmlParser { return HtmlParser( - context, - urlImageParserFactory, - gcsResourceName, - entityType, - entityId, - imageCenterAlign, - consoleLogger, + context = context, + urlImageParserFactory = urlImageParserFactory, + gcsResourceName = gcsResourceName, + entityType = entityType, + entityId = entityId, + imageCenterAlign = imageCenterAlign, + consoleLogger = consoleLogger, cacheLatexRendering = enableCacheLatexRendering.value, - customOppiaTagActionListener, - null, - displayLocale + customOppiaTagActionListener = customOppiaTagActionListener, + policyOppiaTagActionListener = null, + displayLocale = displayLocale + ) + } + + /** + * Returns a new [HtmlParser] with the empty entity type and ID for loading images, + * doesn't require GCS properties and imageCenterAlign set to false + * optionally specified [CustomOppiaTagActionListener] for handling custom Oppia tag events. + */ + fun create( + displayLocale: OppiaLocale.DisplayLocale + ): HtmlParser { + return HtmlParser( + context = context, + urlImageParserFactory = urlImageParserFactory, + gcsResourceName = "", + entityType = "", + entityId = "", + imageCenterAlign = false, + consoleLogger = consoleLogger, + cacheLatexRendering = enableCacheLatexRendering.value, + customOppiaTagActionListener = null, + policyOppiaTagActionListener = null, + displayLocale = displayLocale ) }