From e68f461288a950fa0801a968b16991da6f1a38a9 Mon Sep 17 00:00:00 2001 From: Yogev Ben David Date: Sun, 14 Mar 2021 13:26:55 +0200 Subject: [PATCH] Remove layout.direction --- .../options/LayoutDirection.java | 57 ------------------- .../options/LayoutOptions.java | 4 -- .../bottomtabs/BottomTabsPresenter.kt | 2 - .../viewcontrollers/stack/StackPresenter.java | 2 - .../viewcontroller/LayoutDirectionApplier.kt | 11 ++-- .../views/bottomtabs/BottomTabs.java | 6 -- .../views/stack/topbar/TopBar.java | 5 -- .../stack/StackPresenterTest.kt | 14 ----- lib/ios/RNNCommandsHandler.m | 26 ++++----- lib/ios/RNNLayoutOptions.h | 1 - lib/ios/RNNLayoutOptions.m | 3 - lib/src/interfaces/Options.ts | 7 --- playground/src/app.ts | 2 + playground/src/commons/options/Options.ts | 1 - website/docs/api/options-layout.mdx | 8 --- 15 files changed, 19 insertions(+), 130 deletions(-) delete mode 100644 lib/android/app/src/main/java/com/reactnativenavigation/options/LayoutDirection.java diff --git a/lib/android/app/src/main/java/com/reactnativenavigation/options/LayoutDirection.java b/lib/android/app/src/main/java/com/reactnativenavigation/options/LayoutDirection.java deleted file mode 100644 index 2c0206a86f9..00000000000 --- a/lib/android/app/src/main/java/com/reactnativenavigation/options/LayoutDirection.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.reactnativenavigation.options; - -import android.text.TextUtils; -import android.view.View; - -import java.util.Locale; - -public enum LayoutDirection { - RTL(View.LAYOUT_DIRECTION_RTL), - LTR(View.LAYOUT_DIRECTION_LTR), - LOCALE(View.LAYOUT_DIRECTION_LOCALE), - DEFAULT(View.LAYOUT_DIRECTION_LTR); - - private final int direction; - - LayoutDirection(int direction) { - this.direction = direction; - } - - public static LayoutDirection fromString(String direction) { - switch (direction) { - case "rtl": - return RTL; - case "ltr": - return LTR; - case "locale": - return LOCALE; - default: - return DEFAULT; - } - } - - public boolean hasValue() { - return this != DEFAULT; - } - - public int get() { - return direction; - } - - public int inverse() { - return (isRtl() ? LTR : RTL).direction; - } - - public boolean isRtl() { - switch (direction) { - case View.LAYOUT_DIRECTION_LTR: - return false; - case View.LAYOUT_DIRECTION_RTL: - return true; - case View.LAYOUT_DIRECTION_LOCALE: - return TextUtils.getLayoutDirectionFromLocale(Locale.getDefault()) == View.LAYOUT_DIRECTION_RTL; - default: - return false; - } - } -} diff --git a/lib/android/app/src/main/java/com/reactnativenavigation/options/LayoutOptions.java b/lib/android/app/src/main/java/com/reactnativenavigation/options/LayoutOptions.java index 5c1833b562e..5b119106b75 100644 --- a/lib/android/app/src/main/java/com/reactnativenavigation/options/LayoutOptions.java +++ b/lib/android/app/src/main/java/com/reactnativenavigation/options/LayoutOptions.java @@ -20,7 +20,6 @@ public static LayoutOptions parse(Context context, JSONObject json) { result.componentBackgroundColor = ColorParser.parse(context, json, "componentBackgroundColor"); result.topMargin = NumberParser.parse(json, "topMargin"); result.orientation = OrientationOptions.parse(json); - result.direction = LayoutDirection.fromString(json.optString("direction", "")); return result; } @@ -29,14 +28,12 @@ public static LayoutOptions parse(Context context, JSONObject json) { public Colour componentBackgroundColor = new NullColor(); public Number topMargin = new NullNumber(); public OrientationOptions orientation = new OrientationOptions(); - public LayoutDirection direction = LayoutDirection.DEFAULT; public void mergeWith(LayoutOptions other) { if (other.backgroundColor.hasValue()) backgroundColor = other.backgroundColor; if (other.componentBackgroundColor.hasValue()) componentBackgroundColor = other.componentBackgroundColor; if (other.topMargin.hasValue()) topMargin = other.topMargin; if (other.orientation.hasValue()) orientation = other.orientation; - if (other.direction.hasValue()) direction = other.direction; } public void mergeWithDefault(LayoutOptions defaultOptions) { @@ -44,6 +41,5 @@ public void mergeWithDefault(LayoutOptions defaultOptions) { if (!componentBackgroundColor.hasValue()) componentBackgroundColor = defaultOptions.componentBackgroundColor; if (!topMargin.hasValue()) topMargin = defaultOptions.topMargin; if (!orientation.hasValue()) orientation = defaultOptions.orientation; - if (!direction.hasValue()) direction = defaultOptions.direction; } } diff --git a/lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/bottomtabs/BottomTabsPresenter.kt b/lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/bottomtabs/BottomTabsPresenter.kt index 21c4e602949..cb52494703b 100644 --- a/lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/bottomtabs/BottomTabsPresenter.kt +++ b/lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/bottomtabs/BottomTabsPresenter.kt @@ -65,7 +65,6 @@ class BottomTabsPresenter( private fun mergeBottomTabsOptions(options: Options, view: ViewController<*>) { val bottomTabsOptions = options.bottomTabsOptions - if (options.layout.direction.hasValue()) bottomTabs.setLayoutDirection(options.layout.direction) if (bottomTabsOptions.preferLargeIcons.hasValue()) bottomTabs.setPreferLargeIcons(bottomTabsOptions.preferLargeIcons.get()) if (bottomTabsOptions.titleDisplayMode.hasValue()) { bottomTabs.titleState = bottomTabsOptions.titleDisplayMode.toState() @@ -140,7 +139,6 @@ class BottomTabsPresenter( private fun applyBottomTabsOptions(options: Options) { val bottomTabsOptions = options.bottomTabsOptions - bottomTabs.setLayoutDirection(options.layout.direction) bottomTabs.setPreferLargeIcons(options.bottomTabsOptions.preferLargeIcons[false]) bottomTabs.titleState = bottomTabsOptions.titleDisplayMode[defaultTitleState] bottomTabs.setBackgroundColor(bottomTabsOptions.backgroundColor[Color.WHITE]) diff --git a/lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/stack/StackPresenter.java b/lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/stack/StackPresenter.java index 8bd26ccf989..271d74e3333 100644 --- a/lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/stack/StackPresenter.java +++ b/lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/stack/StackPresenter.java @@ -177,7 +177,6 @@ private void applyTopBarOptions(Options options, StackController stack, ViewCont TopBarOptions topBarOptions = options.topBar; topBar.setTestId(topBarOptions.testId.get("")); - topBar.setLayoutDirection(options.layout.direction); topBar.setHeight(topBarOptions.height.get(UiUtils.getTopBarHeightDp(activity))); topBar.setElevation(topBarOptions.elevation.get(DEFAULT_ELEVATION)); if (topBarOptions.topMargin.hasValue() && topBar.getLayoutParams() instanceof MarginLayoutParams) { @@ -447,7 +446,6 @@ private List mergeButtonsWithColor(@NonNull List b private void mergeTopBarOptions(TopBarOptions resolveOptions, Options options, StackController stack, ViewController child) { TopBarOptions topBarOptions = options.topBar; final View component = child.getView(); - if (options.layout.direction.hasValue()) topBar.setLayoutDirection(options.layout.direction); if (topBarOptions.height.hasValue()) topBar.setHeight(topBarOptions.height.get()); if (topBarOptions.elevation.hasValue()) topBar.setElevation(topBarOptions.elevation.get()); if (topBarOptions.topMargin.hasValue() && topBar.getLayoutParams() instanceof MarginLayoutParams) { diff --git a/lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/viewcontroller/LayoutDirectionApplier.kt b/lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/viewcontroller/LayoutDirectionApplier.kt index 89216ae0c6b..b9fa4976703 100644 --- a/lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/viewcontroller/LayoutDirectionApplier.kt +++ b/lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/viewcontroller/LayoutDirectionApplier.kt @@ -1,15 +1,18 @@ package com.reactnativenavigation.viewcontrollers.viewcontroller +import android.view.View import com.facebook.react.ReactInstanceManager import com.facebook.react.modules.i18nmanager.I18nUtil import com.reactnativenavigation.options.Options class LayoutDirectionApplier { fun apply(root: ViewController<*>, options: Options, instanceManager: ReactInstanceManager) { - if (options.layout.direction.hasValue() && instanceManager.currentReactContext != null) { - root.activity.window.decorView.layoutDirection = options.layout.direction.get() - I18nUtil.getInstance().allowRTL(instanceManager.currentReactContext, options.layout.direction.isRtl) - I18nUtil.getInstance().forceRTL(instanceManager.currentReactContext, options.layout.direction.isRtl) + if (instanceManager.currentReactContext != null) { + if (I18nUtil.getInstance().isRTL(instanceManager.currentReactContext)) { + root.activity.window.decorView.layoutDirection = View.LAYOUT_DIRECTION_RTL + } else { + root.activity.window.decorView.layoutDirection = View.LAYOUT_DIRECTION_LTR + } } } } \ No newline at end of file diff --git a/lib/android/app/src/main/java/com/reactnativenavigation/views/bottomtabs/BottomTabs.java b/lib/android/app/src/main/java/com/reactnativenavigation/views/bottomtabs/BottomTabs.java index 569ebf4d1c1..45a897fed44 100644 --- a/lib/android/app/src/main/java/com/reactnativenavigation/views/bottomtabs/BottomTabs.java +++ b/lib/android/app/src/main/java/com/reactnativenavigation/views/bottomtabs/BottomTabs.java @@ -9,7 +9,6 @@ import com.aurelhubert.ahbottomnavigation.AHBottomNavigation; import com.aurelhubert.ahbottomnavigation.AHBottomNavigationItem; import com.reactnativenavigation.R; -import com.reactnativenavigation.options.LayoutDirection; import java.util.ArrayList; import java.util.List; @@ -118,11 +117,6 @@ public void setSelectedIcon(int index, Drawable icon) { } } - public void setLayoutDirection(LayoutDirection direction) { - LinearLayout tabsContainer = findChildByClass(this, LinearLayout.class); - if (tabsContainer != null) tabsContainer.setLayoutDirection(direction.get()); - } - private boolean hasItemsAndIsMeasured(int w, int h, int oldw, int oldh) { return w != 0 && h != 0 && (w != oldw || h != oldh) && getItemsCount() > 0; } diff --git a/lib/android/app/src/main/java/com/reactnativenavigation/views/stack/topbar/TopBar.java b/lib/android/app/src/main/java/com/reactnativenavigation/views/stack/topbar/TopBar.java index 29292d3439a..90d5cb45348 100644 --- a/lib/android/app/src/main/java/com/reactnativenavigation/views/stack/topbar/TopBar.java +++ b/lib/android/app/src/main/java/com/reactnativenavigation/views/stack/topbar/TopBar.java @@ -24,7 +24,6 @@ import com.reactnativenavigation.R; import com.reactnativenavigation.options.Alignment; import com.reactnativenavigation.options.FontOptions; -import com.reactnativenavigation.options.LayoutDirection; import com.reactnativenavigation.options.SubtitleOptions; import com.reactnativenavigation.options.TitleOptions; import com.reactnativenavigation.options.params.Colour; @@ -301,10 +300,6 @@ public void setOverflowButtonColor(int color) { mainToolBar.getLeftButtonsBar().setOverflowButtonColor(color); } - public void setLayoutDirection(LayoutDirection direction) { - mainToolBar.setLayoutDirection(direction.get()); - } - public void removeRightButton(ButtonController button) { removeRightButton(button.getButtonIntId()); } diff --git a/lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/stack/StackPresenterTest.kt b/lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/stack/StackPresenterTest.kt index b4b9513e004..84e8a2f4375 100644 --- a/lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/stack/StackPresenterTest.kt +++ b/lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/stack/StackPresenterTest.kt @@ -162,20 +162,6 @@ class StackPresenterTest : BaseTest() { Assertions.assertThat(lp2.marginStart).isEqualTo(UiUtils.dpToPx(activity, DEFAULT_LEFT_MARGIN)) } - @Test - fun applyChildOptions_setTitleComponentAlignmentRTL() { - val options = Options() - options.layout.direction = LayoutDirection.RTL - options.topBar.title.component = component(Alignment.Fill) - uut.applyChildOptions(options, parent, child) - val lp2 = topBar.mainToolBar.getTitleComponent().layoutParams as RelativeLayout.LayoutParams - Assertions.assertThat(lp2.rules[RelativeLayout.CENTER_IN_PARENT]).isNotEqualTo(RelativeLayout.TRUE) - Assertions.assertThat(lp2.rules[RelativeLayout.CENTER_VERTICAL]).isEqualTo(RelativeLayout.TRUE) - Assertions.assertThat(lp2.rules[RelativeLayout.LEFT_OF]).isEqualTo(topBar.mainToolBar.rightButtonsBar.id) - Assertions.assertThat(lp2.rules[RelativeLayout.RIGHT_OF]).isEqualTo(topBar.mainToolBar.leftButtonsBar.id) - Assertions.assertThat(lp2.marginStart).isEqualTo(UiUtils.dpToPx(activity, DEFAULT_LEFT_MARGIN)) - } - @Test fun onChildDestroyed_destroyTitleComponent() { val options = Options() diff --git a/lib/ios/RNNCommandsHandler.m b/lib/ios/RNNCommandsHandler.m index f83afa24768..127fb8c0461 100644 --- a/lib/ios/RNNCommandsHandler.m +++ b/lib/ios/RNNCommandsHandler.m @@ -63,22 +63,16 @@ - (void)setRoot:(NSDictionary *)layout [self assertReady]; RNNAssertMainQueue(); - if (_controllerFactory.defaultOptions.layout.direction.hasValue) { - if ([_controllerFactory.defaultOptions.layout.direction.get isEqualToString:@"rtl"]) { - [[RCTI18nUtil sharedInstance] allowRTL:YES]; - [[RCTI18nUtil sharedInstance] forceRTL:YES]; - [[UIView appearance] - setSemanticContentAttribute:UISemanticContentAttributeForceRightToLeft]; - [[UINavigationBar appearance] - setSemanticContentAttribute:UISemanticContentAttributeForceRightToLeft]; - } else { - [[RCTI18nUtil sharedInstance] allowRTL:NO]; - [[RCTI18nUtil sharedInstance] forceRTL:NO]; - [[UIView appearance] - setSemanticContentAttribute:UISemanticContentAttributeForceLeftToRight]; - [[UINavigationBar appearance] - setSemanticContentAttribute:UISemanticContentAttributeForceLeftToRight]; - } + if ([RCTI18nUtil sharedInstance].isRTL) { + [[UIView appearance] + setSemanticContentAttribute:UISemanticContentAttributeForceRightToLeft]; + [[UINavigationBar appearance] + setSemanticContentAttribute:UISemanticContentAttributeForceRightToLeft]; + } else { + [[UIView appearance] + setSemanticContentAttribute:UISemanticContentAttributeForceLeftToRight]; + [[UINavigationBar appearance] + setSemanticContentAttribute:UISemanticContentAttributeForceLeftToRight]; } [_modalManager dismissAllModalsAnimated:NO diff --git a/lib/ios/RNNLayoutOptions.h b/lib/ios/RNNLayoutOptions.h index e421fe2d064..e4e278e7ec8 100644 --- a/lib/ios/RNNLayoutOptions.h +++ b/lib/ios/RNNLayoutOptions.h @@ -4,7 +4,6 @@ @property(nonatomic, strong) Color *backgroundColor; @property(nonatomic, strong) Color *componentBackgroundColor; -@property(nonatomic, strong) Text *direction; @property(nonatomic, strong) id orientation; @property(nonatomic, strong) Bool *autoHideHomeIndicator; diff --git a/lib/ios/RNNLayoutOptions.m b/lib/ios/RNNLayoutOptions.m index 3e09f88eeb9..6c73b64a814 100644 --- a/lib/ios/RNNLayoutOptions.m +++ b/lib/ios/RNNLayoutOptions.m @@ -8,7 +8,6 @@ - (instancetype)initWithDict:(NSDictionary *)dict { self = [super initWithDict:dict]; self.backgroundColor = [ColorParser parse:dict key:@"backgroundColor"]; self.componentBackgroundColor = [ColorParser parse:dict key:@"componentBackgroundColor"]; - self.direction = [TextParser parse:dict key:@"direction"]; self.orientation = dict[@"orientation"]; self.autoHideHomeIndicator = [BoolParser parse:dict key:@"autoHideHomeIndicator"]; return self; @@ -19,8 +18,6 @@ - (void)mergeOptions:(RNNLayoutOptions *)options { self.backgroundColor = options.backgroundColor; if (options.componentBackgroundColor.hasValue) self.componentBackgroundColor = options.componentBackgroundColor; - if (options.direction.hasValue) - self.direction = options.direction; if (options.orientation) self.orientation = options.orientation; if (options.autoHideHomeIndicator) diff --git a/lib/src/interfaces/Options.ts b/lib/src/interfaces/Options.ts index 944c0d1db10..8beeb49d967 100644 --- a/lib/src/interfaces/Options.ts +++ b/lib/src/interfaces/Options.ts @@ -165,13 +165,6 @@ export interface OptionsLayout { * #### (Android specific) */ topMargin?: number; - - /** - * Set language direction. - * only works with DefaultOptions - */ - direction?: 'rtl' | 'ltr' | 'locale'; - /** * Controls the application's preferred home indicator auto-hiding. * #### (iOS specific) diff --git a/playground/src/app.ts b/playground/src/app.ts index 5946338d36e..e5740b24124 100644 --- a/playground/src/app.ts +++ b/playground/src/app.ts @@ -4,6 +4,7 @@ import addProcessors from './commons/Processors'; import { setDefaultOptions } from './commons/options/Options'; import testIDs from './testIDs'; import Screens from './screens/Screens'; +import { I18nManager } from 'react-native'; // @ts-ignore alert = (title, message) => @@ -18,6 +19,7 @@ alert = (title, message) => }); function start() { + I18nManager.allowRTL(true); registerScreens(); addProcessors(); setDefaultOptions(); diff --git a/playground/src/commons/options/Options.ts b/playground/src/commons/options/Options.ts index ab87fbac907..34afa13dfb2 100644 --- a/playground/src/commons/options/Options.ts +++ b/playground/src/commons/options/Options.ts @@ -11,7 +11,6 @@ const setDefaultOptions = () => layout: { componentBackgroundColor: Colors.background, orientation: ['portrait'], - direction: 'locale', }, bottomTabs: { titleDisplayMode: 'alwaysShow', diff --git a/website/docs/api/options-layout.mdx b/website/docs/api/options-layout.mdx index 0beaaf4ee8f..a30a5f07afd 100644 --- a/website/docs/api/options-layout.mdx +++ b/website/docs/api/options-layout.mdx @@ -48,14 +48,6 @@ Set the layout top margin. | ------ | -------- | -------- | | number | No | Android | -### `direction` - -Set language direction, only works with DefaultOptions. `locale` is an Android specific option which sets the direction according to the device locale. - -| Type | Required | Platform | -| ---------------------------- | -------- | -------- | -| enum('rtl', 'ltr', 'locale') | No | Both | - ### `autoHideHomeIndicator` Controls the application's preferred home indicator auto-hiding.