Skip to content

Commit

Permalink
Fix text alignment and bullet point formatting in PoliciesFragment
Browse files Browse the repository at this point in the history
  • Loading branch information
TanishMoral11 committed Nov 12, 2024
1 parent f8143d2 commit fdb0929
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 143 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package org.oppia.android.app.policies

import android.graphics.Paint
import android.text.SpannableString
import android.text.Spanned
import android.text.TextPaint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand All @@ -17,7 +13,6 @@ import org.oppia.android.databinding.PoliciesFragmentBinding
import org.oppia.android.util.parser.html.HtmlParser
import org.oppia.android.util.parser.html.PolicyType
import javax.inject.Inject
import org.oppia.android.util.locale.LeftAlignedSymbolsSpan

/** The presenter for [PoliciesFragment]. */
@FragmentScope
Expand Down Expand Up @@ -50,7 +45,6 @@ class PoliciesFragmentPresenter @Inject constructor(
var policyDescription = ""
var policyWebLink = ""

// Get policy content based on the selected policy page
if (policyPage == PolicyPage.PRIVACY_POLICY) {
policyDescription = resourceHandler.getStringInLocale(R.string.privacy_policy_content)
policyWebLink = resourceHandler.getStringInLocale(R.string.privacy_policy_web_link)
Expand All @@ -59,49 +53,27 @@ class PoliciesFragmentPresenter @Inject constructor(
policyWebLink = resourceHandler.getStringInLocale(R.string.terms_of_service_web_link)
}

// Parse the policy description to handle HTML and links
val parsedHtmlDescription = htmlParserFactory.create(
binding.policyDescriptionTextView.textAlignment = View.TEXT_ALIGNMENT_TEXT_START
binding.policyDescriptionTextView.text = htmlParserFactory.create(
policyOppiaTagActionListener = this,
displayLocale = resourceHandler.getDisplayLocale()
displayLocale = resourceHandler.getDisplayLocale(),
supportLtr = true
).parseOppiaHtml(
policyDescription,
binding.policyDescriptionTextView,
supportsLinks = true,
supportsConceptCards = false
)

binding.policyDescriptionTextView.apply {
layoutDirection = View.LAYOUT_DIRECTION_LTR
textAlignment = View.TEXT_ALIGNMENT_TEXT_START
textDirection = View.TEXT_DIRECTION_LTR
setSingleLine(false)
setMaxLines(Int.MAX_VALUE)
}

val spannableString = SpannableString(parsedHtmlDescription)

parsedHtmlDescription.split("\n").forEachIndexed { lineIndex, line ->
val lineStart = parsedHtmlDescription.indexOf(line)
if (line.trimStart().startsWith("")) {
val bulletIndex = lineStart + line.indexOf("")
spannableString.setSpan(
LeftAlignedSymbolsSpan(),
bulletIndex,
bulletIndex + 1,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
}

binding.policyDescriptionTextView.text = spannableString

binding.policyWebLinkTextView.textAlignment = View.TEXT_ALIGNMENT_TEXT_START
binding.policyWebLinkTextView.text = htmlParserFactory.create(
gcsResourceName = "",
entityType = "",
entityId = "",
imageCenterAlign = false,
customOppiaTagActionListener = null,
resourceHandler.getDisplayLocale()
resourceHandler.getDisplayLocale(),
supportLtr = true
).parseOppiaHtml(
policyWebLink,
binding.policyWebLinkTextView,
Expand All @@ -118,4 +90,4 @@ class PoliciesFragmentPresenter @Inject constructor(
(activity as RouteToPoliciesListener).onRouteToPolicies(PolicyPage.TERMS_OF_SERVICE)
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ class HtmlParser private constructor(
private val cacheLatexRendering: Boolean,
customOppiaTagActionListener: CustomOppiaTagActionListener?,
policyOppiaTagActionListener: PolicyOppiaTagActionListener?,
displayLocale: OppiaLocale.DisplayLocale
displayLocale: OppiaLocale.DisplayLocale,
private val supportLtr: Boolean = false
) {
private val conceptCardTagHandler by lazy {
ConceptCardTagHandler(
Expand All @@ -55,11 +56,11 @@ class HtmlParser private constructor(
consoleLogger
)
}
private val bulletTagHandler by lazy { LiTagHandler(context, displayLocale) }
private val bulletTagHandler by lazy { LiTagHandler(context, displayLocale, supportLtr) }
private val imageTagHandler by lazy { ImageTagHandler(consoleLogger) }

private val isRtl by lazy {
displayLocale.getLayoutDirection() == ViewCompat.LAYOUT_DIRECTION_RTL
(displayLocale.getLayoutDirection() == ViewCompat.LAYOUT_DIRECTION_RTL) && !supportLtr
}

/**
Expand All @@ -78,6 +79,7 @@ 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
Expand Down Expand Up @@ -123,17 +125,11 @@ 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
Expand Down Expand Up @@ -228,43 +224,22 @@ class HtmlParser private constructor(
entityId: String,
imageCenterAlign: Boolean,
customOppiaTagActionListener: CustomOppiaTagActionListener? = null,
displayLocale: OppiaLocale.DisplayLocale
displayLocale: OppiaLocale.DisplayLocale,
supportLtr: Boolean = false
): HtmlParser {
return HtmlParser(
context = context,
urlImageParserFactory = urlImageParserFactory,
gcsResourceName = gcsResourceName,
entityType = entityType,
entityId = entityId,
imageCenterAlign = imageCenterAlign,
consoleLogger = consoleLogger,
cacheLatexRendering = enableCacheLatexRendering.value,
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,
context,
urlImageParserFactory,
gcsResourceName,
entityType,
entityId,
imageCenterAlign,
consoleLogger,
cacheLatexRendering = enableCacheLatexRendering.value,
customOppiaTagActionListener = null,
policyOppiaTagActionListener = null,
displayLocale = displayLocale
customOppiaTagActionListener,
null,
displayLocale,
supportLtr = supportLtr
)
}

Expand All @@ -276,7 +251,9 @@ class HtmlParser private constructor(
*/
fun create(
policyOppiaTagActionListener: PolicyOppiaTagActionListener? = null,
displayLocale: OppiaLocale.DisplayLocale
displayLocale: OppiaLocale.DisplayLocale,
supportLtr: Boolean = false

): HtmlParser {
return HtmlParser(
context = context,
Expand All @@ -289,8 +266,9 @@ class HtmlParser private constructor(
cacheLatexRendering = false,
customOppiaTagActionListener = null,
policyOppiaTagActionListener = policyOppiaTagActionListener,
displayLocale = displayLocale
displayLocale = displayLocale,
supportLtr = supportLtr
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ const val CUSTOM_LIST_OL_TAG = "oppia-ol"
*/
class LiTagHandler(
private val context: Context,
private val displayLocale: OppiaLocale.DisplayLocale
private val displayLocale: OppiaLocale.DisplayLocale,
private val supportLtr: Boolean = false
) : CustomHtmlContentHandler.CustomTagHandler {
private val pendingLists = Stack<ListTag<*, *>>()
private val latestPendingList: ListTag<*, *>?
Expand Down Expand Up @@ -52,7 +53,12 @@ class LiTagHandler(
// Actually place the spans only if the root tree has been finished (as the entirety of the
// tree is needed for analysis).
val closingList = pendingLists.pop().also { it.recordList() }
if (pendingLists.isEmpty()) closingList.finishListTree(output, context, displayLocale)
if (pendingLists.isEmpty()) closingList.finishListTree(
output,
context,
displayLocale,
supportLtr
)
}
CUSTOM_LIST_LI_TAG -> latestPendingList?.closeItem(output)
}
Expand Down Expand Up @@ -122,8 +128,13 @@ class LiTagHandler(
* Recursively replaces all marks for this root list (and all its children) with renderable
* spans in the provided [text].
*/
fun finishListTree(text: Editable, context: Context, displayLocale: OppiaLocale.DisplayLocale) =
finishListRecursively(parentSpan = null, text, context, displayLocale)
fun finishListTree(
text: Editable,
context: Context,
displayLocale: OppiaLocale.DisplayLocale,
supportLtr: Boolean
) =
finishListRecursively(parentSpan = null, text, context, displayLocale, supportLtr)

/**
* Returns a new mark of type [M] for this tag.
Expand All @@ -136,22 +147,23 @@ class LiTagHandler(
parentSpan: ListItemLeadingMarginSpan?,
text: Editable,
context: Context,
displayLocale: OppiaLocale.DisplayLocale
displayLocale: OppiaLocale.DisplayLocale,
supportLtr: Boolean = false
) {
val childrenToProcess = childrenLists.toMutableMap()
markRangesToReplace.forEach { (startMark, endMark) ->
val styledSpan = startMark.toSpan(
parentSpan, context, displayLocale, peerItemCount = markRangesToReplace.size
parentSpan, context, displayLocale, peerItemCount = markRangesToReplace.size, supportLtr
)
text.replaceMarksWithSpan(startMark, endMark, styledSpan)
childrenToProcess.remove(startMark)?.finishListRecursively(
parentSpan = styledSpan, text, context, displayLocale
parentSpan = styledSpan, text, context, displayLocale, supportLtr
)
}

// Process the remaining children that are not lists themselves.
childrenToProcess.values.forEach {
it.finishListRecursively(parentSpan = null, text, context, displayLocale)
it.finishListRecursively(parentSpan = null, text, context, displayLocale, supportLtr)
}
}

Expand Down Expand Up @@ -188,7 +200,8 @@ class LiTagHandler(
parentSpan: ListItemLeadingMarginSpan?,
context: Context,
displayLocale: OppiaLocale.DisplayLocale,
peerItemCount: Int
peerItemCount: Int,
supportLtr: Boolean
): S

/** Marks the opening tag location of a list item inside an <ul> element. */
Expand All @@ -202,8 +215,15 @@ class LiTagHandler(
parentSpan: ListItemLeadingMarginSpan?,
context: Context,
displayLocale: OppiaLocale.DisplayLocale,
peerItemCount: Int
) = ListItemLeadingMarginSpan.UlSpan(parentSpan, context, indentationLevel, displayLocale)
peerItemCount: Int,
supportLtr: Boolean
) = ListItemLeadingMarginSpan.UlSpan(
parentSpan,
context,
indentationLevel,
displayLocale,
supportLtr
)
}

/** Marks the opening tag location of a list item inside an <ol> element. */
Expand All @@ -215,14 +235,16 @@ class LiTagHandler(
parentSpan: ListItemLeadingMarginSpan?,
context: Context,
displayLocale: OppiaLocale.DisplayLocale,
peerItemCount: Int
peerItemCount: Int,
supportLtr: Boolean
): ListItemLeadingMarginSpan.OlSpan {
return ListItemLeadingMarginSpan.OlSpan(
parentSpan,
context,
numberedItemPrefix = "${displayLocale.toHumanReadableString(number)}.",
longestNumberedItemPrefix = "${displayLocale.toHumanReadableString(peerItemCount)}.",
displayLocale
displayLocale,
supportLtr
)
}
}
Expand All @@ -236,7 +258,8 @@ class LiTagHandler(
parentSpan: ListItemLeadingMarginSpan?,
context: Context,
displayLocale: OppiaLocale.DisplayLocale,
peerItemCount: Int
peerItemCount: Int,
supportLtr: Boolean
) = error("Ending marks cannot be converted to spans.")
}
}
Expand Down Expand Up @@ -291,4 +314,4 @@ class LiTagHandler(
private fun <T : Mark<*>> Spannable.addMark(mark: T) =
setSpan(mark, length, length, Spanned.SPAN_MARK_MARK)
}
}
}
Loading

0 comments on commit fdb0929

Please sign in to comment.