From 7bff243d1dbcebd7f3fed11788568aac432e22a8 Mon Sep 17 00:00:00 2001 From: Kirill Zyusko Date: Thu, 16 Nov 2023 17:57:52 +0100 Subject: [PATCH] fix: remove `isLaidOut` check from `copyBoundsInWindow` method (#275) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 📜 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)| |---------------------------------------------------|------------------------------------------------------------------| |Screenshot 2023-11-16 at 17 31 24|Screenshot 2023-11-16 at 17 52 55| 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 https://github.com/kirillzyusko/react-native-keyboard-controller/issues/274 https://github.com/kirillzyusko/react-native-keyboard-controller/issues/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 --- .../com/reactnativekeyboardcontroller/extensions/View.kt | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/android/src/main/java/com/reactnativekeyboardcontroller/extensions/View.kt b/android/src/main/java/com/reactnativekeyboardcontroller/extensions/View.kt index dd777cba6..7ebc1c567 100644 --- a/android/src/main/java/com/reactnativekeyboardcontroller/extensions/View.kt +++ b/android/src/main/java/com/reactnativekeyboardcontroller/extensions/View.kt @@ -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 @@ -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") } }