react-native-keyboard-controller 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,6 @@
1
1
  package com.reactnativekeyboardcontroller
2
2
 
3
+ import android.util.Log
3
4
  import androidx.appcompat.widget.FitWindowsLinearLayout
4
5
  import androidx.core.view.ViewCompat
5
6
  import androidx.core.view.WindowInsetsAnimationCompat
@@ -13,6 +14,7 @@ import com.facebook.react.views.view.ReactViewManager
13
14
  import com.reactnativekeyboardcontroller.events.KeyboardTransitionEvent
14
15
 
15
16
  class KeyboardControllerViewManager(reactContext: ReactApplicationContext) : ReactViewManager() {
17
+ private val TAG = KeyboardControllerViewManager::class.qualifiedName
16
18
  private var mReactContext = reactContext
17
19
  private var isStatusBarTranslucent = false
18
20
 
@@ -20,7 +22,14 @@ class KeyboardControllerViewManager(reactContext: ReactApplicationContext) : Rea
20
22
 
21
23
  override fun createViewInstance(reactContext: ThemedReactContext): ReactViewGroup {
22
24
  val view = EdgeToEdgeReactViewGroup(reactContext)
23
- val window = mReactContext.currentActivity!!.window
25
+ val activity = mReactContext.currentActivity
26
+
27
+ if (activity == null) {
28
+ Log.w(TAG, "Can not setup keyboard animation listener, since `currentActivity` is null")
29
+ return view
30
+ }
31
+
32
+ val window = activity.window
24
33
  val decorView = window.decorView
25
34
 
26
35
  ViewCompat.setOnApplyWindowInsetsListener(view) { v, insets ->
@@ -3,6 +3,7 @@ package com.reactnativekeyboardcontroller
3
3
  import android.animation.ArgbEvaluator
4
4
  import android.animation.ValueAnimator
5
5
  import android.os.Build
6
+ import android.util.Log
6
7
  import androidx.annotation.RequiresApi
7
8
  import androidx.core.view.WindowInsetsCompat
8
9
  import androidx.core.view.WindowInsetsControllerCompat
@@ -12,6 +13,7 @@ import com.facebook.react.bridge.ReactMethod
12
13
  import com.facebook.react.bridge.UiThreadUtil
13
14
 
14
15
  class StatusBarManagerCompatModule(private val mReactContext: ReactApplicationContext) : ReactContextBaseJavaModule(mReactContext) {
16
+ private val TAG = StatusBarManagerCompatModule::class.qualifiedName
15
17
  private var controller: WindowInsetsControllerCompat? = null
16
18
 
17
19
  override fun getName(): String = "StatusBarManagerCompat"
@@ -30,8 +32,14 @@ class StatusBarManagerCompatModule(private val mReactContext: ReactApplicationCo
30
32
  @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
31
33
  @ReactMethod
32
34
  private fun setColor(color: Int, animated: Boolean) {
35
+ val activity = mReactContext.currentActivity
36
+ if (activity == null) {
37
+ Log.w(TAG, "StatusBarManagerCompatModule: Ignored status bar change, current activity is null.")
38
+ return
39
+ }
40
+
33
41
  UiThreadUtil.runOnUiThread {
34
- val window = mReactContext.currentActivity!!.window
42
+ val window = activity.window
35
43
 
36
44
  if (animated) {
37
45
  val curColor: Int = window.statusBarColor
@@ -71,7 +79,13 @@ class StatusBarManagerCompatModule(private val mReactContext: ReactApplicationCo
71
79
 
72
80
  private fun getController(): WindowInsetsControllerCompat? {
73
81
  if (this.controller == null) {
74
- val window = mReactContext.currentActivity!!.window
82
+ val activity = mReactContext.currentActivity
83
+ if (activity == null) {
84
+ Log.w(TAG, "StatusBarManagerCompatModule: can not get `WindowInsetsControllerCompat` because current activity is null.")
85
+ return this.controller
86
+ }
87
+
88
+ val window = activity.window
75
89
 
76
90
  this.controller = WindowInsetsControllerCompat(window, window.decorView)
77
91
  }
@@ -31,7 +31,13 @@ import com.facebook.react.views.view.ReactViewGroup
31
31
  import com.reactnativekeyboardcontroller.events.KeyboardTransitionEvent
32
32
  import java.util.*
33
33
 
34
- fun toDp(px: Float, context: Context): Int = (px / context.resources.displayMetrics.density).toInt()
34
+ fun toDp(px: Float, context: Context?): Int {
35
+ if (context == null) {
36
+ return 0
37
+ }
38
+
39
+ return (px / context.resources.displayMetrics.density).toInt()
40
+ }
35
41
 
36
42
  /**
37
43
  * A [WindowInsetsAnimationCompat.Callback] which will translate/move the given view during any
@@ -112,7 +118,7 @@ class TranslateDeferringInsetsAnimationCallback(
112
118
  val navigationBar = insets?.getInsets(WindowInsetsCompat.Type.navigationBars())?.bottom ?: 0
113
119
 
114
120
  // on hide it will be negative value, so we are using max function
115
- return Math.max(toDp((keyboardHeight - navigationBar).toFloat(), context!!), 0)
121
+ return Math.max(toDp((keyboardHeight - navigationBar).toFloat(), context), 0)
116
122
  }
117
123
 
118
124
  override fun onProgress(
@@ -132,7 +138,7 @@ class TranslateDeferringInsetsAnimationCallback(
132
138
  Insets.max(it, Insets.NONE)
133
139
  }
134
140
  val diffY = (diff.top - diff.bottom).toFloat()
135
- val height = toDp(diffY, context!!)
141
+ val height = toDp(diffY, context)
136
142
 
137
143
  var progress = 0.0
138
144
  try {
@@ -143,7 +149,7 @@ class TranslateDeferringInsetsAnimationCallback(
143
149
  Log.i(TAG, "DiffY: $diffY $height")
144
150
 
145
151
  context
146
- .getNativeModule(UIManagerModule::class.java)
152
+ ?.getNativeModule(UIManagerModule::class.java)
147
153
  ?.eventDispatcher
148
154
  ?.dispatchEvent(KeyboardTransitionEvent(view.id, height, progress))
149
155
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-keyboard-controller",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Keyboard manager which works in identical way on both iOS and Android",
5
5
  "main": "lib/commonjs/index",
6
6
  "module": "lib/module/index",