-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Apply layout direction directly on views (#5368)
Apply layout direction directly on views This commit changes how RNN handles layout direction and adds support for LOCALE layout direction on Android. Until now RNN delegated handling layout direction to RN's I18nUtil. This usually is enough but under certain circumstances layout direction has to be applied directly on relevant views by RNN - usually when there are conflicts with another dependency which handles RTL.
- Loading branch information
Showing
16 changed files
with
236 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
lib/android/app/src/main/java/com/reactnativenavigation/parse/LayoutDirection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.reactnativenavigation.parse; | ||
|
||
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 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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...roid/app/src/main/java/com/reactnativenavigation/presentation/LayoutDirectionApplier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.reactnativenavigation.presentation; | ||
|
||
import com.facebook.react.ReactInstanceManager; | ||
import com.facebook.react.modules.i18nmanager.I18nUtil; | ||
import com.reactnativenavigation.parse.Options; | ||
import com.reactnativenavigation.viewcontrollers.ViewController; | ||
|
||
public class LayoutDirectionApplier { | ||
public void apply(ViewController root, Options options, ReactInstanceManager instanceManager) { | ||
if (options.layout.direction.hasValue() && instanceManager.getCurrentReactContext() != null) { | ||
root.getActivity().getWindow().getDecorView().setLayoutDirection(options.layout.direction.get()); | ||
I18nUtil.getInstance().allowRTL(instanceManager.getCurrentReactContext(), options.layout.direction.isRtl()); | ||
I18nUtil.getInstance().forceRTL(instanceManager.getCurrentReactContext(), options.layout.direction.isRtl()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.