Skip to content

Commit

Permalink
fix: remove isLaidOut check from copyBoundsInWindow method (#275)
Browse files Browse the repository at this point in the history
## 馃摐 Description

Removed `isLaidOut` check from `copyBoundsInWindow` method.

## 馃挕 Motivation and Context

When we are using `native-stack` then on initial mount the view property
`isLaidOut` will be `true`. When we open a new screen and return back to
the previous `isLaidOut` will be `false`.

I've tried to call `invalidate`/`requestLayout`/`forceLayout` methods in
`onAttachedToWindow` method, but it doesn't seem to have any effect at
all.

The I decided to compare how `copyBoundsInWindow` works if `isLaidOut`
check is removed. And turned out there is no difference between two
executions:

|Screen opened first time (isLaidOut check present)|Returned to the
screen (isLaidOut is missing to go to if-statement)|

|---------------------------------------------------|------------------------------------------------------------------|
|<img width="709" alt="Screenshot 2023-11-16 at 17 31 24"
src="https://github.com/kirillzyusko/react-native-keyboard-controller/assets/22820318/8f79bfae-3ed8-4e2c-abb6-a8d6a07eab28">|<img
width="696" alt="Screenshot 2023-11-16 at 17 52 55"
src="https://github.com/kirillzyusko/react-native-keyboard-controller/assets/22820318/4523be4d-36e7-4733-a71f-de7ff0dc5bd1">|

So taking this information into consideration I've decided to remove
that check.

Also for the sake of safety and avoidance of unexpected crashes I
decided to remove `throw Exception` construction and replace it with a
simple logger (since I'm not handling exceptions in my code it's better
to log it).

Closes
#274
#203

## 馃摙 Changelog

### Android
- removed `isLaidOut` check from `copyBoundsInWindow` method;
- replaced throwing error to logger.

## 馃 How Has This Been Tested?

Tested on Pixel 7 Pro (android 14).

## 馃摳 Screenshots (if appropriate):


https://github.com/kirillzyusko/react-native-keyboard-controller/assets/22820318/ef8661da-8a78-4d4a-9362-668365736fed

## 馃摑 Checklist

- [x] CI successfully passed
  • Loading branch information
kirillzyusko committed Nov 16, 2023
1 parent 7f36574 commit 7bff243
Showing 1 changed file with 3 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.reactnativekeyboardcontroller.extensions

import android.graphics.Rect
import android.os.Build
import android.util.Log
import android.view.View
import androidx.annotation.RequiresApi

Expand Down Expand Up @@ -37,15 +38,12 @@ private val tmpIntArr = IntArray(2)
*/
@RequiresApi(Build.VERSION_CODES.KITKAT)
fun View.copyBoundsInWindow(rect: Rect) {
if (isLaidOut && isAttachedToWindow) {
if (isAttachedToWindow) {
rect.set(0, 0, width, height)
getLocationInWindow(tmpIntArr)
rect.offset(tmpIntArr[0], tmpIntArr[1])
} else {
throw IllegalArgumentException(
"Can not copy bounds as view is not laid out" +
" or attached to window",
)
Log.w("View.copyBoundsInWindow", "Can not copy bounds as view is not attached to window")
}
}

Expand Down

0 comments on commit 7bff243

Please sign in to comment.