Skip to content

Commit

Permalink
Apply layoutDirection to buttons container
Browse files Browse the repository at this point in the history
For some reason layout direction isn't propagated to subviews correctly in the Wix app.
This commit is mostly a workaround, it sets the layout direction on the ViewGroup containing TopBar buttons
so that they appear in the right order in RTL.
  • Loading branch information
guyca committed Nov 14, 2019
1 parent 305caeb commit 14b5221
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import android.graphics.PorterDuffColorFilter;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import androidx.appcompat.widget.ActionMenuView;
import androidx.appcompat.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -24,7 +22,12 @@

import javax.annotation.Nullable;

import androidx.appcompat.widget.ActionMenuView;
import androidx.appcompat.widget.Toolbar;

import static com.reactnativenavigation.utils.ObjectUtils.perform;
import static com.reactnativenavigation.utils.UiUtils.runOnPreDrawOnce;
import static com.reactnativenavigation.utils.ViewUtils.findChildByClass;

@SuppressLint("ViewConstructor")
public class TitleBar extends Toolbar {
Expand Down Expand Up @@ -140,6 +143,12 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
}

@Override
public void setLayoutDirection(int layoutDirection) {
super.setLayoutDirection(layoutDirection);
perform(findChildByClass(this, ActionMenuView.class), buttonsContainer -> buttonsContainer.setLayoutDirection(layoutDirection));
}

@Nullable
public TextView findTitleTextView() {
List<TextView> children = ViewUtils.findChildrenByClass(this, TextView.class, textView -> textView.getText().equals(getTitle()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.reactnativenavigation.mocks.TitleBarReactViewCreatorMock;
import com.reactnativenavigation.mocks.TopBarBackgroundViewCreatorMock;
Expand All @@ -19,6 +21,8 @@
import com.reactnativenavigation.views.StackLayout;
import com.reactnativenavigation.views.topbar.TopBar;

import org.mockito.Mockito;

public class TestUtils {
public static StackControllerBuilder newStackController(Activity activity) {
TopBarController topBarController = new TopBarController() {
Expand All @@ -40,4 +44,13 @@ protected TopBar createTopBar(Context context, StackLayout stackLayout) {
public static void hideBackButton(ViewController viewController) {
viewController.options.topBar.buttons.back.visible = new Bool(false);
}

public static <T extends View> T spyOn(T child) {
ViewGroup parent = (ViewGroup) child.getParent();
int indexOf = parent.indexOfChild(child);
parent.removeView(child);
T spy = Mockito.spy(child);
parent.addView(spy, indexOf);
return spy;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.reactnativenavigation.viewcontrollers;

import android.app.Activity;
import androidx.appcompat.widget.ActionMenuView;
import android.view.View;
import android.widget.TextView;

import com.reactnativenavigation.BaseTest;
import com.reactnativenavigation.TestUtils;
import com.reactnativenavigation.parse.params.Button;
import com.reactnativenavigation.parse.params.Text;
import com.reactnativenavigation.react.Constants;
Expand All @@ -21,7 +21,11 @@
import java.util.Collections;
import java.util.List;

import androidx.appcompat.widget.ActionMenuView;

import static com.reactnativenavigation.utils.Assertions.assertNotNull;
import static com.reactnativenavigation.utils.TitleBarHelper.createButtonController;
import static com.reactnativenavigation.utils.ViewUtils.findChildByClass;
import static org.assertj.core.api.Java6Assertions.assertThat;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.eq;
Expand Down Expand Up @@ -140,6 +144,15 @@ public void clear() {
verify(uut).removeView(title);
}

@Test
public void setLayoutDirection_directionIsExplicitlyAppliedToButtonsContainer() {
ActionMenuView buttonsContainer = findChildByClass(uut, ActionMenuView.class);
assertNotNull(buttonsContainer);
ActionMenuView spy = TestUtils.spyOn(buttonsContainer);
uut.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
verify(spy).setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
}

private List<TitleBarButtonController> leftButton(Button leftButton) {
return Collections.singletonList(createButtonController(activity, uut, leftButton));
}
Expand Down

0 comments on commit 14b5221

Please sign in to comment.