Skip to content

fix: activity nullability #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.reactnativekeyboardcontroller

import android.util.Log
import androidx.appcompat.widget.FitWindowsLinearLayout
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsAnimationCompat
Expand All @@ -13,14 +14,22 @@ import com.facebook.react.views.view.ReactViewManager
import com.reactnativekeyboardcontroller.events.KeyboardTransitionEvent

class KeyboardControllerViewManager(reactContext: ReactApplicationContext) : ReactViewManager() {
private val TAG = KeyboardControllerViewManager::class.qualifiedName
private var mReactContext = reactContext
private var isStatusBarTranslucent = false

override fun getName() = "KeyboardControllerView"

override fun createViewInstance(reactContext: ThemedReactContext): ReactViewGroup {
val view = EdgeToEdgeReactViewGroup(reactContext)
val window = mReactContext.currentActivity!!.window
val activity = mReactContext.currentActivity

if (activity == null) {
Log.w(TAG, "Can not setup keyboard animation listener, since `currentActivity` is null")
return view
}

val window = activity.window
val decorView = window.decorView

ViewCompat.setOnApplyWindowInsetsListener(view) { v, insets ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.reactnativekeyboardcontroller
import android.animation.ArgbEvaluator
import android.animation.ValueAnimator
import android.os.Build
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
Expand All @@ -12,6 +13,7 @@ import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.UiThreadUtil

class StatusBarManagerCompatModule(private val mReactContext: ReactApplicationContext) : ReactContextBaseJavaModule(mReactContext) {
private val TAG = StatusBarManagerCompatModule::class.qualifiedName
private var controller: WindowInsetsControllerCompat? = null

override fun getName(): String = "StatusBarManagerCompat"
Expand All @@ -30,8 +32,14 @@ class StatusBarManagerCompatModule(private val mReactContext: ReactApplicationCo
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
@ReactMethod
private fun setColor(color: Int, animated: Boolean) {
val activity = mReactContext.currentActivity
if (activity == null) {
Log.w(TAG, "StatusBarManagerCompatModule: Ignored status bar change, current activity is null.")
return
}

UiThreadUtil.runOnUiThread {
val window = mReactContext.currentActivity!!.window
val window = activity.window

if (animated) {
val curColor: Int = window.statusBarColor
Expand Down Expand Up @@ -71,7 +79,13 @@ class StatusBarManagerCompatModule(private val mReactContext: ReactApplicationCo

private fun getController(): WindowInsetsControllerCompat? {
if (this.controller == null) {
val window = mReactContext.currentActivity!!.window
val activity = mReactContext.currentActivity
if (activity == null) {
Log.w(TAG, "StatusBarManagerCompatModule: can not get `WindowInsetsControllerCompat` because current activity is null.")
return this.controller
}

val window = activity.window

this.controller = WindowInsetsControllerCompat(window, window.decorView)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ import com.facebook.react.views.view.ReactViewGroup
import com.reactnativekeyboardcontroller.events.KeyboardTransitionEvent
import java.util.*

fun toDp(px: Float, context: Context): Int = (px / context.resources.displayMetrics.density).toInt()
fun toDp(px: Float, context: Context?): Int {
if (context == null) {
return 0
}

return (px / context.resources.displayMetrics.density).toInt()
}

/**
* A [WindowInsetsAnimationCompat.Callback] which will translate/move the given view during any
Expand Down Expand Up @@ -112,7 +118,7 @@ class TranslateDeferringInsetsAnimationCallback(
val navigationBar = insets?.getInsets(WindowInsetsCompat.Type.navigationBars())?.bottom ?: 0

// on hide it will be negative value, so we are using max function
return Math.max(toDp((keyboardHeight - navigationBar).toFloat(), context!!), 0)
return Math.max(toDp((keyboardHeight - navigationBar).toFloat(), context), 0)
}

override fun onProgress(
Expand All @@ -132,7 +138,7 @@ class TranslateDeferringInsetsAnimationCallback(
Insets.max(it, Insets.NONE)
}
val diffY = (diff.top - diff.bottom).toFloat()
val height = toDp(diffY, context!!)
val height = toDp(diffY, context)

var progress = 0.0
try {
Expand All @@ -143,7 +149,7 @@ class TranslateDeferringInsetsAnimationCallback(
Log.i(TAG, "DiffY: $diffY $height")

context
.getNativeModule(UIManagerModule::class.java)
?.getNativeModule(UIManagerModule::class.java)
?.eventDispatcher
?.dispatchEvent(KeyboardTransitionEvent(view.id, height, progress))

Expand Down