diff --git a/.eslintignore b/.eslintignore index a837da2..1660fb4 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,4 +1,5 @@ lib +build src/__mocks__ docs/build docs/.docusaurus diff --git a/android/src/main/java/com/lodev09/truesheet/core/Utils.kt b/android/src/main/java/com/lodev09/truesheet/core/Utils.kt index f8738d2..f5f5d71 100644 --- a/android/src/main/java/com/lodev09/truesheet/core/Utils.kt +++ b/android/src/main/java/com/lodev09/truesheet/core/Utils.kt @@ -1,32 +1,51 @@ package com.lodev09.truesheet.core import android.annotation.SuppressLint +import android.content.Context +import android.os.Build import android.util.DisplayMetrics +import android.view.WindowInsets +import android.view.WindowManager import com.facebook.react.bridge.Promise import com.facebook.react.bridge.ReactContext import com.facebook.react.uimanager.PixelUtil object Utils { - @SuppressLint("DiscouragedApi", "InternalInsetResource") - fun screenHeight(reactContext: ReactContext): Int { - val activity = reactContext.currentActivity ?: return 0 - - // Get the screen metrics + @SuppressLint("DiscouragedApi") + private fun getIdentifierHeight(context: ReactContext, name: String): Int = + context.resources.getDimensionPixelSize( + context.resources.getIdentifier(name, "dimen", "android") + ).takeIf { it > 0 } ?: 0 + + @SuppressLint("InternalInsetResource", "DiscouragedApi") + fun screenHeight(context: ReactContext): Int { + val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager val displayMetrics = DisplayMetrics() - activity.windowManager.defaultDisplay.getMetrics(displayMetrics) - val screenHeight = displayMetrics.heightPixels - val resources = activity.resources + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + context.display?.getRealMetrics(displayMetrics) + } else { + windowManager.defaultDisplay.getMetrics(displayMetrics) + } + + val screenHeight = displayMetrics.heightPixels + val statusBarHeight = getIdentifierHeight(context, "status_bar_height") + val hasNavigationBar = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + context.getSystemService(WindowManager::class.java) + ?.currentWindowMetrics + ?.windowInsets + ?.isVisible(WindowInsets.Type.navigationBars()) ?: false + } else { + context.resources.getIdentifier("navigation_bar_height", "dimen", "android") > 0 + } - // Calculate status bar height - var statusBarHeight = 0 - val resourceId: Int = resources.getIdentifier("status_bar_height", "dimen", "android") - if (resourceId > 0) { - statusBarHeight = resources.getDimensionPixelSize(resourceId) + val navigationBarHeight = if (hasNavigationBar) { + getIdentifierHeight(context, "navigation_bar_height") + } else { + 0 } - // Calculate max usable height - return screenHeight - statusBarHeight + return screenHeight - statusBarHeight - navigationBarHeight } fun toDIP(value: Int): Float = PixelUtil.toDIPFromPixel(value.toFloat()) diff --git a/tsconfig.json b/tsconfig.json index 4409437..789c729 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -27,5 +27,5 @@ "allowJs": false, "allowSyntheticDefaultImports": true }, - "exclude": ["lib", "src/__mocks__", "docs/.docusaurus"] + "exclude": ["lib", "build", "src/__mocks__", "docs/.docusaurus"] }