1
1
package com.zoontek.rnedgetoedge
2
2
3
- import android.app.Activity
4
3
import android.content.res.Configuration
5
4
import android.graphics.Color
6
5
import android.os.Build
7
- import android.util.TypedValue
6
+ import android.os.Handler
7
+ import android.os.Looper
8
8
import android.view.WindowManager
9
9
10
10
import androidx.core.content.ContextCompat
@@ -13,75 +13,60 @@ import androidx.core.view.WindowInsetsCompat
13
13
import androidx.core.view.WindowInsetsControllerCompat
14
14
15
15
import com.facebook.common.logging.FLog
16
+ import com.facebook.react.bridge.ReactApplicationContext
16
17
import com.facebook.react.bridge.ReadableMap
17
18
import com.facebook.react.common.ReactConstants
18
19
19
20
object RNEdgeToEdgeModuleImpl {
20
21
const val NAME = " RNEdgeToEdge"
21
- private var isInitialHostResume = true
22
22
23
- fun onHostResume (activity : Activity ? ) {
24
- if (activity == null ) {
25
- return FLog .w(ReactConstants .TAG , " $NAME : Ignored, current activity is null." )
26
- }
23
+ private fun applyEdgeToEdge (reactContext : ReactApplicationContext ) {
24
+ val activity = reactContext.currentActivity
25
+ ? : return FLog .w(ReactConstants .TAG , " $NAME : Ignored, current activity is null." )
27
26
28
27
activity.runOnUiThread {
29
28
val window = activity.window
30
29
val view = window.decorView
31
30
val context = view.context
32
31
33
- val isDarkMode =
34
- view.resources.configuration.uiMode and Configuration .UI_MODE_NIGHT_MASK ==
35
- Configuration .UI_MODE_NIGHT_YES
36
-
37
32
WindowCompat .setDecorFitsSystemWindows(window, false )
38
33
39
34
window.statusBarColor = Color .TRANSPARENT
35
+ window.navigationBarColor = ContextCompat .getColor(context, R .color.navigationBarColor)
40
36
41
- window.navigationBarColor = when {
42
- Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q -> Color .TRANSPARENT
43
- Build .VERSION .SDK_INT >= Build .VERSION_CODES .O && ! isDarkMode ->
44
- ContextCompat .getColor(context, R .color.systemBarLightScrim)
45
- else -> ContextCompat .getColor(context, R .color.systemBarDarkScrim)
37
+ val isDarkMode =
38
+ view.resources.configuration.uiMode and Configuration .UI_MODE_NIGHT_MASK ==
39
+ Configuration .UI_MODE_NIGHT_YES
40
+
41
+ WindowInsetsControllerCompat (window, view).run {
42
+ isAppearanceLightNavigationBars = ! isDarkMode
46
43
}
47
44
48
45
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q ) {
49
46
window.isStatusBarContrastEnforced = false
50
47
window.isNavigationBarContrastEnforced = true
51
48
}
52
49
53
- WindowInsetsControllerCompat (window, view).run {
54
- if (isInitialHostResume) {
55
- val typedValue = TypedValue ()
56
-
57
- isAppearanceLightStatusBars = activity
58
- .theme
59
- .resolveAttribute(android.R .attr.windowLightStatusBar, typedValue, true ) &&
60
- typedValue.data != 0
61
- }
62
-
63
- isInitialHostResume = false
64
- isAppearanceLightNavigationBars = ! isDarkMode
65
- }
66
-
67
50
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .P ) {
68
51
window.attributes.layoutInDisplayCutoutMode = when {
69
- Build .VERSION .SDK_INT >= Build .VERSION_CODES .R ->
70
- WindowManager .LayoutParams .LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS
52
+ Build .VERSION .SDK_INT >= Build .VERSION_CODES .R -> WindowManager .LayoutParams .LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS
71
53
else -> WindowManager .LayoutParams .LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
72
54
}
73
55
}
74
56
}
75
57
}
76
58
77
- fun onHostDestroy ( ) {
78
- isInitialHostResume = true
59
+ fun onHostResume ( reactContext : ReactApplicationContext ) {
60
+ applyEdgeToEdge(reactContext)
79
61
}
80
62
81
- fun setSystemBarsConfig (activity : Activity ? , config : ReadableMap ) {
82
- if (activity == null ) {
83
- return FLog .w(ReactConstants .TAG , " $NAME : Ignored, current activity is null." )
84
- }
63
+ fun onConfigChange (reactContext : ReactApplicationContext ) {
64
+ Handler (Looper .getMainLooper()).postDelayed({ applyEdgeToEdge(reactContext) }, 100 )
65
+ }
66
+
67
+ fun setSystemBarsConfig (reactContext : ReactApplicationContext , config : ReadableMap ) {
68
+ val activity = reactContext.currentActivity
69
+ ? : return FLog .w(ReactConstants .TAG , " $NAME : Ignored, current activity is null." )
85
70
86
71
val statusBarHidden =
87
72
config.takeIf { it.hasKey(" statusBarHidden" ) }?.getBoolean(" statusBarHidden" )
0 commit comments