Skip to content

Commit

Permalink
Remove right to left hack on root layout
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ahmedre committed Dec 7, 2024
1 parent d04d415 commit 5722de2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -455,10 +455,6 @@ class PagerActivity : AppCompatActivity(), AudioBarListener, OnBookmarkTagsUpdat
}

val toolbar = findViewById<Toolbar>(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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/layout/quran_page_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"
android:layoutDirection="ltr"
>

<androidx.viewpager.widget.NonRestoringViewPager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class AyahToolBar @JvmOverloads constructor(
var flavor: String = ""
var longPressLambda: ((CharSequence) -> Unit) = {}
var isRecitationEnabled = false
var lastMeasuredWidth = 0

@Inject
lateinit var ayahToolBarPresenter: AyahToolBarPresenter
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
}
Expand Down

0 comments on commit 5722de2

Please sign in to comment.