Skip to content

Commit

Permalink
tmp status
Browse files Browse the repository at this point in the history
  • Loading branch information
swabbass committed Oct 14, 2021
1 parent ba9fe1d commit aff2b87
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 51 deletions.
6 changes: 3 additions & 3 deletions lib/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,13 @@ allprojects { p ->
}

dependencies {
implementation "androidx.core:core-ktx:1.3.2"
implementation "androidx.core:core-ktx:1.6.0"
implementation "org.jetbrains.kotlin:$kotlinStdlib:$kotlinVersion"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlinCoroutinesCore"
implementation "androidx.constraintlayout:constraintlayout:2.0.4"

implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.annotation:annotation:1.1.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'androidx.annotation:annotation:1.2.0'
implementation 'com.google.android.material:material:1.2.0-alpha03'

implementation 'com.github.wix-playground:ahbottomnavigation:3.3.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

import static com.reactnativenavigation.utils.UiUtils.pxToDp;

import android.app.Activity;

public class NavigationModule extends ReactContextBaseJavaModule {
private static final String NAME = "RNNBridgeModule";

Expand Down Expand Up @@ -88,10 +90,11 @@ public void getLaunchArgs(String commandId, Promise promise) {

private WritableMap createNavigationConstantsMap() {
ReactApplicationContext ctx = getReactApplicationContext();
final Activity currentActivity = ctx.getCurrentActivity();
WritableMap constants = Arguments.createMap();
constants.putString(Constants.BACK_BUTTON_JS_KEY, Constants.BACK_BUTTON_ID);
constants.putDouble(Constants.BOTTOM_TABS_HEIGHT_KEY, pxToDp(ctx, UiUtils.getBottomTabsHeight(ctx)));
constants.putDouble(Constants.STATUS_BAR_HEIGHT_KEY, pxToDp(ctx, StatusBarUtils.getStatusBarHeight(ctx)));
constants.putDouble(Constants.STATUS_BAR_HEIGHT_KEY, pxToDp(ctx, StatusBarUtils.getStatusBarHeight(currentActivity)));
constants.putDouble(Constants.TOP_BAR_HEIGHT_KEY, pxToDp(ctx, UiUtils.getTopBarHeight(ctx)));
return constants;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ModalFrameLayout(context: ReactContext) : FrameLayout(context) {
val translucent = context.currentActivity?.window?.let {
StatusBarUtils.isTranslucent(context.currentActivity?.window)
} ?: false
topMargin = if (translucent) 0 else StatusBarUtils.getStatusBarHeight(context)
topMargin = if (translucent) 0 else StatusBarUtils.getStatusBarHeight(context.currentActivity)
})
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.reactnativenavigation.react.modal

import android.app.Activity
import android.content.Context
import android.graphics.Point
import android.view.WindowManager
Expand Down Expand Up @@ -107,26 +108,26 @@ class ModalViewManager(val reactContext: ReactContext) : ViewGroupManager<ModalH
}
}

private fun getModalHostSize(context: Context): Point {
private fun getModalHostSize(activity: Activity): Point {
val MIN_POINT = Point()
val MAX_POINT = Point()
val SIZE_POINT = Point()
val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
val wm = activity.getSystemService(Context.WINDOW_SERVICE) as WindowManager
val display = Assertions.assertNotNull(wm).defaultDisplay
// getCurrentSizeRange will return the min and max width and height that the window can be
display.getCurrentSizeRange(MIN_POINT, MAX_POINT)
// getSize will return the dimensions of the screen in its current orientation
display.getSize(SIZE_POINT)
val attrs = intArrayOf(android.R.attr.windowFullscreen)
val theme = context.theme
val theme = activity.theme
val ta = theme.obtainStyledAttributes(attrs)
val windowFullscreen = ta.getBoolean(0, false)

// We need to add the status bar height to the height if we have a fullscreen window,
// because Display.getCurrentSizeRange doesn't include it.
var statusBarHeight = 0
if (windowFullscreen) {
statusBarHeight = StatusBarUtils.getStatusBarHeight(context)
statusBarHeight = StatusBarUtils.getStatusBarHeight(activity)
}
return if (SIZE_POINT.x < SIZE_POINT.y) {
// If we are vertical the width value comes from min width and height comes from max height
Expand All @@ -140,8 +141,10 @@ private fun getModalHostSize(context: Context): Point {
private class ModalHostShadowNode : LayoutShadowNode() {
override fun addChildAt(child: ReactShadowNodeImpl, i: Int) {
super.addChildAt(child, i)
val modalSize = getModalHostSize(themedContext)
child.setStyleWidth(modalSize.x.toFloat())
child.setStyleHeight(modalSize.y.toFloat())
themedContext?.currentActivity?.let {
val modalSize = getModalHostSize(it)
child.setStyleWidth(modalSize.x.toFloat())
child.setStyleHeight(modalSize.y.toFloat())
}
}
}
Original file line number Diff line number Diff line change
@@ -1,39 +1,107 @@
package com.reactnativenavigation.utils

import android.content.Context
import android.app.Activity
import android.graphics.Rect
import android.os.Build
import android.view.View
import android.view.Window
import android.view.WindowManager
import com.reactnativenavigation.utils.StatusBarUtils
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import kotlin.math.abs

object StatusBarUtils {
private const val STATUS_BAR_HEIGHT_M = 24
private const val STATUS_BAR_HEIGHT_L = 25
private var statusBarHeight = -1

@JvmStatic
fun getStatusBarHeight(activity: Activity?): Int {
val res= if (statusBarHeight > 0) {
statusBarHeight
} else {
statusBarHeight = activity?.let {
val rectangle = Rect()
val window: Window = activity.window
window.decorView.getWindowVisibleDisplayFrame(rectangle)
val statusBarHeight: Int = rectangle.top
val contentViewTop = window.findViewById<View>(Window.ID_ANDROID_CONTENT).top
abs(contentViewTop - statusBarHeight)
} ?: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) STATUS_BAR_HEIGHT_M else STATUS_BAR_HEIGHT_L
statusBarHeight
}
logd("StatusBarHeight $res","STATUSXXXX")
return res
}

@JvmStatic
fun saveStatusBarHeight(height: Int) {
statusBarHeight = height
}

fun getStatusBarHeight(context: Context): Int {
if (statusBarHeight > 0) {
return statusBarHeight
@JvmStatic
fun getStatusBarHeightDp(activity: Activity?): Int {
return UiUtils.pxToDp(activity, getStatusBarHeight(activity).toFloat())
.toInt()
}

@JvmStatic
fun isTranslucent(window: Window?): Boolean {
return window?.let {
val lp = window.attributes
return lp != null && lp.flags and WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS == WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
} ?: false
}

fun hideSystemUi(window: Window?, view: View) {
window?.let {
WindowCompat.setDecorFitsSystemWindows(window, false)
WindowInsetsControllerCompat(window, view).let { controller ->
controller.hide(WindowInsetsCompat.Type.systemBars())
controller.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
}
}
val resources = context.resources
val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
statusBarHeight = if (resourceId > 0) resources.getDimensionPixelSize(resourceId) else UiUtils.dpToPx(
context,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) STATUS_BAR_HEIGHT_M else STATUS_BAR_HEIGHT_L
)
return statusBarHeight
}

fun getStatusBarHeightDp(context: Context): Int {
return UiUtils.pxToDp(context, getStatusBarHeight(context).toFloat())
.toInt()
fun showSystemUi(window: Window?, view: View) {
window?.let {
WindowCompat.setDecorFitsSystemWindows(window, true)
WindowInsetsControllerCompat(window, view).show(WindowInsetsCompat.Type.navigationBars())
}
}

fun isTranslucent(window: Window): Boolean {
val lp = window.attributes
return lp != null && lp.flags and WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS == WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
fun hideNavigationBar(window: Window?, view: View) {
window?.let {
WindowCompat.setDecorFitsSystemWindows(window, false)
WindowInsetsControllerCompat(window, view).let { controller ->
controller.hide(WindowInsetsCompat.Type.navigationBars())
controller.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
}
}
}

fun showNavigationBar(window: Window?, view: View) {
window?.let {
WindowCompat.setDecorFitsSystemWindows(window, true)
WindowInsetsControllerCompat(window, view).show(WindowInsetsCompat.Type.navigationBars())
}
}

fun hideStatusBar(window: Window?, view: View) {
window?.let {
WindowCompat.setDecorFitsSystemWindows(window, true)
WindowInsetsControllerCompat(window, view).let { controller ->
controller.hide(WindowInsetsCompat.Type.statusBars())
controller.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
}
}
}

fun showStatusBar(window: Window?, view: View) {
window?.let {
WindowCompat.setDecorFitsSystemWindows(window, true)
WindowInsetsControllerCompat(window, view).show(WindowInsetsCompat.Type.statusBars())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void onViewDisappear() {
}

public void onViewBroughtToFront() {
presenter.onViewBroughtToFront(resolveCurrentOptions());
presenter.onViewBroughtToFront(this,resolveCurrentOptions());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ public void applyOptions(ViewController view, Options options) {
Options withDefaultOptions = options.copy().withDefaultOptions(defaultOptions);
applyOrientation(withDefaultOptions.layout.orientation);
applyViewOptions(view, withDefaultOptions);
applyStatusBarOptions(withDefaultOptions);
applyStatusBarOptions(view, withDefaultOptions);
applyNavigationBarOptions(withDefaultOptions.navigationBar);
}

public void onViewBroughtToFront(Options options) {
public void onViewBroughtToFront(ViewController<?> viewController, Options options) {
Options withDefaultOptions = options.copy().withDefaultOptions(defaultOptions);
applyStatusBarOptions(withDefaultOptions);
applyStatusBarOptions(viewController, withDefaultOptions);
}

private void applyOrientation(OrientationOptions options) {
Expand Down Expand Up @@ -83,12 +83,12 @@ private void applyBackgroundColor(ViewController view, Options options) {
}
}

private void applyStatusBarOptions(Options options) {
private void applyStatusBarOptions(ViewController viewController, Options options) {
StatusBarOptions statusBar = options.copy().withDefaultOptions(defaultOptions).statusBar;
setStatusBarBackgroundColor(statusBar);
setTextColorScheme(statusBar);
setTranslucent(statusBar);
setStatusBarVisible(statusBar.visible);
setStatusBarVisible(viewController, statusBar.visible);
}

private void setTranslucent(StatusBarOptions options) {
Expand All @@ -100,19 +100,17 @@ private void setTranslucent(StatusBarOptions options) {
}
}

private void setStatusBarVisible(Bool visible) {
View decorView = activity.getWindow().getDecorView();
int flags = decorView.getSystemUiVisibility();
private void setStatusBarVisible(ViewController viewController, Bool visible) {
final View view = viewController.view != null ? viewController.view : activity.getWindow().getDecorView();
if (visible.isFalse()) {
flags |= View.SYSTEM_UI_FLAG_FULLSCREEN;
StatusBarUtils.INSTANCE.hideStatusBar(activity.getWindow(), view);
} else {
flags &= ~View.SYSTEM_UI_FLAG_FULLSCREEN;
StatusBarUtils.INSTANCE.showStatusBar(activity.getWindow(), view);
}
decorView.setSystemUiVisibility(flags);
}

private void setStatusBarBackgroundColor(StatusBarOptions statusBar) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && statusBar.backgroundColor.canApplyValue()) {
if (statusBar.backgroundColor.canApplyValue()) {
activity.getWindow().setStatusBarColor(getStatusBarBackgroundColor(statusBar));
}
}
Expand All @@ -138,7 +136,7 @@ private void setTextColorScheme(StatusBarOptions statusBar) {
final View view = activity.getWindow().getDecorView();
//View.post is a Workaround, added to solve internal Samsung
//Android 9 issues. For more info see https://github.com/wix/react-native-navigation/pull/7231
view.post(()->{
view.post(() -> {
int flags = view.getSystemUiVisibility();
if (isDarkTextColorScheme(statusBar)) {
flags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
Expand All @@ -154,7 +152,7 @@ private void mergeStatusBarOptions(View view, StatusBarOptions statusBar) {
mergeStatusBarBackgroundColor(statusBar);
mergeTextColorScheme(statusBar);
mergeTranslucent(statusBar);
mergeStatusBarVisible(view, statusBar.visible, statusBar.drawBehind);
mergeStatusBarVisible(view, statusBar.visible);
}

private void mergeStatusBarBackgroundColor(StatusBarOptions statusBar) {
Expand All @@ -177,16 +175,13 @@ private void mergeTranslucent(StatusBarOptions options) {
}
}

private void mergeStatusBarVisible(View view, Bool visible, Bool drawBehind) {
private void mergeStatusBarVisible(View view, Bool visible) {
if (visible.hasValue()) {
int flags = view.getSystemUiVisibility();
if (visible.isTrue()) {
flags &= ~View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN & ~View.SYSTEM_UI_FLAG_FULLSCREEN;
StatusBarUtils.INSTANCE.showStatusBar(activity.getWindow(), view);
} else {
flags |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_FULLSCREEN;
StatusBarUtils.INSTANCE.hideStatusBar(activity.getWindow(), view);
}
if (flags != view.getSystemUiVisibility()) view.requestLayout();
view.setSystemUiVisibility(flags);
}
}

Expand Down
15 changes: 15 additions & 0 deletions playground/src/screens/StatusBarOptionsScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export default class StatusBarOptions extends React.Component<NavigationComponen
<Root componentId={this.props.componentId} style={style.root}>
<Button label="Full Screen Modal" onPress={this.fullScreenModal} />
<Button label="Push" onPress={this.push} />
<Button label="Hide Status Bar" onPress={this.hideStatusBar} />
<Button label="Show Status Bar" onPress={this.showStatusBar} />
<Button label="BottomTabs" onPress={this.bottomTabs} />
<Button label="Open Left" onPress={() => this.open('left')} />
<Button label="Open Right" onPress={() => this.open('right')} />
Expand All @@ -45,6 +47,19 @@ export default class StatusBarOptions extends React.Component<NavigationComponen
);
}

hideStatusBar = () =>
Navigation.mergeOptions(this.props.componentId, {
statusBar: {
visible: false,
},
});

showStatusBar = () =>
Navigation.mergeOptions(this.props.componentId, {
statusBar: {
visible: true,
},
});
fullScreenModal = () => Navigation.showModal(Screens.FullScreenModal);
push = () => Navigation.push(this, Screens.Pushed);
bottomTabs = () => Navigation.showModal(Screens.StatusBarBottomTabs);
Expand Down

0 comments on commit aff2b87

Please sign in to comment.