From 5722de2f03b7a9d19062538e5f6756ec3a414459 Mon Sep 17 00:00:00 2001 From: Ahmed El-Helw Date: Sat, 7 Dec 2024 23:39:24 +0400 Subject: [PATCH] Remove right to left hack on root layout Previously, in order to ensure the ayah toolbar was set properly, a hack was in place to always ensure that the root RelativeLayout is LTR. Removing this hack broke the ayah toolbar position. Adding it back, however, broke the proper directionality of the audio bar. This patch finally removes this hack and properly calculates the position in RTL. Consequently, it fixes the audio bar directionality. --- .../labs/androidquran/ui/PagerActivity.kt | 4 --- .../view/SlidingUpPanelLayout.java | 13 +++++----- .../main/res/layout/quran_page_activity.xml | 1 - .../quran/page/common/toolbar/AyahToolBar.kt | 26 ++++++++++++++++++- 4 files changed, 31 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/quran/labs/androidquran/ui/PagerActivity.kt b/app/src/main/java/com/quran/labs/androidquran/ui/PagerActivity.kt index d77de62cde..877e87b063 100644 --- a/app/src/main/java/com/quran/labs/androidquran/ui/PagerActivity.kt +++ b/app/src/main/java/com/quran/labs/androidquran/ui/PagerActivity.kt @@ -455,10 +455,6 @@ class PagerActivity : AppCompatActivity(), AudioBarListener, OnBookmarkTagsUpdat } val toolbar = findViewById(R.id.toolbar) - if (quranSettings.isArabicNames || QuranUtils.isRtl()) { - // remove when we remove LTR from quran_page_activity's root - ViewCompat.setLayoutDirection(toolbar, ViewCompat.LAYOUT_DIRECTION_RTL) - } setSupportActionBar(toolbar) supportActionBar?.setDisplayShowHomeEnabled(true) diff --git a/app/src/main/java/com/quran/labs/androidquran/view/SlidingUpPanelLayout.java b/app/src/main/java/com/quran/labs/androidquran/view/SlidingUpPanelLayout.java index 8498110ec3..89d4bccae2 100644 --- a/app/src/main/java/com/quran/labs/androidquran/view/SlidingUpPanelLayout.java +++ b/app/src/main/java/com/quran/labs/androidquran/view/SlidingUpPanelLayout.java @@ -43,10 +43,6 @@ import android.os.Build; import android.os.Parcel; import android.os.Parcelable; -import androidx.core.content.ContextCompat; -import androidx.core.view.MotionEventCompat; -import androidx.core.view.ViewCompat; -import androidx.customview.widget.ViewDragHelper; import android.util.AttributeSet; import android.view.Gravity; import android.view.MotionEvent; @@ -56,9 +52,12 @@ import android.view.ViewGroup; import android.view.accessibility.AccessibilityEvent; -import com.quran.labs.androidquran.R; +import androidx.core.content.ContextCompat; +import androidx.core.view.MotionEventCompat; +import androidx.core.view.ViewCompat; +import androidx.customview.widget.ViewDragHelper; -import timber.log.Timber; +import com.quran.labs.androidquran.R; public class SlidingUpPanelLayout extends ViewGroup { @@ -591,7 +590,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { final int childCount = getChildCount(); if (childCount > 2) { - Timber.e("onMeasure: More than two child views are not supported."); + // Timber.e("onMeasure: More than two child views are not supported."); } else if (getChildAt(1).getVisibility() == GONE) { panelHeight = 0; } diff --git a/app/src/main/res/layout/quran_page_activity.xml b/app/src/main/res/layout/quran_page_activity.xml index 9a5f3c6bc1..0607010200 100644 --- a/app/src/main/res/layout/quran_page_activity.xml +++ b/app/src/main/res/layout/quran_page_activity.xml @@ -5,7 +5,6 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:keepScreenOn="true" - android:layoutDirection="ltr" > Unit) = {} var isRecitationEnabled = false + var lastMeasuredWidth = 0 @Inject lateinit var ayahToolBarPresenter: AyahToolBarPresenter @@ -112,6 +113,15 @@ class AyahToolBar @JvmOverloads constructor( toolBarPip.layout(pipLeft, menuHeight - 1, pipLeft + pipWidth, menuHeight + pipHeight) menuLayout.layout(0, 0, menuWidth, menuHeight) } + + // handle first layout of toolbar + if (lastMeasuredWidth != totalWidth && lastMeasuredWidth == 0) { + // whenever we're RTL, we need to adjust the translationX + if (layoutDirection == LAYOUT_DIRECTION_RTL) { + translationX = translationX - totalWidth + } + lastMeasuredWidth = totalWidth + } } override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { @@ -213,7 +223,21 @@ class AyahToolBar @JvmOverloads constructor( pipOffset = internalPosition.pipOffset val x = internalPosition.x val y = internalPosition.y - setPosition(x, y) + + // hack to help fix RTL when measuredWidth is not yet set. if this is set, + // we adjust the translationX _after_ onLayout + lastMeasuredWidth = measuredWidth + val actualX = if (layoutDirection == LAYOUT_DIRECTION_RTL) { + // in RTL, x=0 is on the very right of the screen. translationX is still to the + // right, however (i.e. translationX of 100 is 100 off the screen to the right). + // consequently, we need to subtract the width of the view to get the actual x, + // which is some negative value between -measuredWidth and 0 to properly render + // when RTL. + x - measuredWidth + } else { + x + } + setPosition(actualX, y) if (needsLayout) { requestLayout() }