Skip to content

Commit

Permalink
fix: fix android footer position bug (#25)
Browse files Browse the repository at this point in the history
* fix: fix android footer position bug

* refactor: optimized method

* refactor: optimize 2
  • Loading branch information
lodev09 authored May 22, 2024
1 parent 97b89b0 commit a9607b4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
lib
build
src/__mocks__
docs/build
docs/.docusaurus
49 changes: 34 additions & 15 deletions android/src/main/java/com/lodev09/truesheet/core/Utils.kt
Original file line number Diff line number Diff line change
@@ -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())
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@
"allowJs": false,
"allowSyntheticDefaultImports": true
},
"exclude": ["lib", "src/__mocks__", "docs/.docusaurus"]
"exclude": ["lib", "build", "src/__mocks__", "docs/.docusaurus"]
}

0 comments on commit a9607b4

Please sign in to comment.