Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #5039 : Align policy text and symbols to the left, partial fix for list items #5573

Open
wants to merge 19 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import org.oppia.android.app.model.PoliciesFragmentArguments
import org.oppia.android.app.model.PolicyPage
import org.oppia.android.app.translation.AppLanguageResourceHandler
import org.oppia.android.databinding.PoliciesFragmentBinding
import org.oppia.android.util.locale.LeftAlignedSymbolsSpan
import org.oppia.android.util.parser.html.HtmlParser
import org.oppia.android.util.parser.html.LeftAlignedSymbolsSpan
import org.oppia.android.util.parser.html.PolicyType
import javax.inject.Inject

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ kt_android_library(
name = "list_item_leading_margin_span",
srcs = [
"ListItemLeadingMarginSpan.kt",
"LeftAlignedSymbolsSpan.kt",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TanishMoral11, to answer your question regarding adding this class to the build file:

  1. Sometimes a kt lib can have multiple source files like you have added here, and for tag_handlers, but it depends on usecase. E.g., if you want to use LeftAlignedSymbolsSpan in a different part of the codepase, you will need to add a dependency on the lib, list_item_leading_margin_span, which will provide both ListItemLeadingMarginSpan and LeftAlignedSymbolsSpan. On the other hand, each class can have it's own lib if they are only used in a standalone way, like most of the other utility classes in this package and their corresponding libs in this BUILD file.

],
visibility = [
"//app:__subpackages__",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.oppia.android.util.locale
package org.oppia.android.util.parser.html

import android.graphics.Canvas
import android.graphics.Paint
import android.text.style.ReplacementSpan

// Custom span to force LTR alignment for symbols as well
// Custom span to force LTR alignment for all text including symbols
/**
* Custom span to force LTR (left-to-right) alignment for all text,
* including symbols, regardless of the system's text direction.
*/
class LeftAlignedSymbolsSpan : ReplacementSpan() {
override fun getSize(
paint: Paint,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package org.oppia.android.util.parser.html
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.graphics.RectF
import android.text.Layout
import android.text.Spanned
import android.text.style.LeadingMarginSpan
import androidx.core.view.ViewCompat
import org.oppia.android.util.R
import org.oppia.android.util.R.dimen.spacing_before_bullet
import org.oppia.android.util.locale.OppiaLocale

// TODO(#562): Add screenshot tests to check whether the drawing logic works correctly on all devices.
Expand Down Expand Up @@ -40,7 +40,7 @@ sealed class ListItemLeadingMarginSpan : LeadingMarginSpan {
private val bulletRadius = resources.getDimensionPixelSize(R.dimen.bullet_radius)

private val bulletDiameter by lazy { bulletRadius * 2 }
private val baseMargin = (16f * context.resources.displayMetrics.density).toInt()
private val baseMargin = context.resources.getDimensionPixelSize((spacing_before_bullet))

private val isRtl by lazy {
displayLocale.getLayoutDirection() == ViewCompat.LAYOUT_DIRECTION_RTL
Expand Down Expand Up @@ -119,7 +119,8 @@ sealed class ListItemLeadingMarginSpan : LeadingMarginSpan {
private val displayLocale: OppiaLocale.DisplayLocale
) : ListItemLeadingMarginSpan() {
private val resources = context.resources
private val baseMargin = (16f * context.resources.displayMetrics.density).toInt()
private val baseMargin =
context.resources.getDimensionPixelSize((R.dimen.spacing_before_number_prefix))

// Try to use a computed margin, but otherwise guess if there's no guaranteed spacing.
private var computedLeadingMargin =
Expand All @@ -143,15 +144,6 @@ sealed class ListItemLeadingMarginSpan : LeadingMarginSpan {
val isFirstCharacter = startCharOfSpan == start

if (isFirstCharacter) {
// Force left alignment
paint.textAlign = Paint.Align.LEFT

val textWidth = Rect().also {
paint.getTextBounds(
numberedItemPrefix, /* start= */ 0, /* end= */ numberedItemPrefix.length, it
)
}.width()

// Positioning calculation
val prefixStartX = x.toFloat() + baseMargin

Expand Down
Loading