Skip to content

Commit 7a1a607

Browse files
committed
fix(android): height calculation
1 parent b540fa1 commit 7a1a607

File tree

4 files changed

+40
-13
lines changed

4 files changed

+40
-13
lines changed

android/src/main/java/com/lodev09/truesheet/TrueSheetBehavior.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class TrueSheetBehavior(private val reactContext: ReactContext) : BottomSheetBeh
135135
*/
136136
fun configure() {
137137
// Update the usable sheet height
138-
maxScreenHeight = Utils.activityView(reactContext)?.height ?: 0
138+
maxScreenHeight = Utils.screenHeight(reactContext)
139139

140140
var contentHeight = 0
141141

android/src/main/java/com/lodev09/truesheet/TrueSheetDialog.kt

+6-2
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,12 @@ class TrueSheetDialog(
6262
*/
6363
fun registerKeyboardManager() {
6464
keyboardManager.registerKeyboardListener(object : KeyboardManager.OnKeyboardListener {
65-
override fun onKeyboardStateChange(isVisible: Boolean) {
66-
behavior.maxScreenHeight = Utils.activityView(reactContext)?.height ?: 0
65+
override fun onKeyboardStateChange(isVisible: Boolean, visibleHeight: Int?) {
66+
when (isVisible) {
67+
true -> behavior.maxScreenHeight = visibleHeight ?: 0
68+
else -> behavior.maxScreenHeight = Utils.screenHeight(reactContext)
69+
}
70+
6771
behavior.footerView?.apply {
6872
y = (behavior.maxScreenHeight - (sheetView.top ?: 0) - height).toFloat()
6973
}

android/src/main/java/com/lodev09/truesheet/core/KeyboardManager.kt

+11-6
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,20 @@ import com.facebook.react.bridge.ReactContext
88

99
class KeyboardManager(reactContext: ReactContext) {
1010
interface OnKeyboardListener {
11-
fun onKeyboardStateChange(isVisible: Boolean)
11+
fun onKeyboardStateChange(isVisible: Boolean, visibleHeight: Int?)
1212
}
1313

14-
private var screenView: View? = Utils.activityView(reactContext)
14+
private var contentView: View? = null
1515
private var onGlobalLayoutListener: OnGlobalLayoutListener? = null
1616
private var isKeyboardVisible = false
1717

18+
init {
19+
val activity = reactContext.currentActivity
20+
contentView = activity?.findViewById(android.R.id.content)
21+
}
22+
1823
fun registerKeyboardListener(listener: OnKeyboardListener?) {
19-
screenView?.apply {
24+
contentView?.apply {
2025
unregisterKeyboardListener()
2126

2227
onGlobalLayoutListener = object : OnGlobalLayoutListener {
@@ -28,13 +33,13 @@ class KeyboardManager(reactContext: ReactContext) {
2833
// Will ask InputMethodManager.isAcceptingText() to detect if keyboard appeared or not.
2934
val inputManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
3035
if (height != previousHeight && inputManager.isAcceptingText()) {
31-
listener?.onKeyboardStateChange(true)
36+
listener?.onKeyboardStateChange(true, height)
3237

3338
previousHeight = height
3439
isKeyboardVisible = true
3540
}
3641
} else if (isKeyboardVisible) {
37-
listener?.onKeyboardStateChange(false)
42+
listener?.onKeyboardStateChange(false, null)
3843
previousHeight = 0
3944
isKeyboardVisible = false
4045
}
@@ -47,7 +52,7 @@ class KeyboardManager(reactContext: ReactContext) {
4752

4853
fun unregisterKeyboardListener() {
4954
onGlobalLayoutListener?.let {
50-
screenView?.getViewTreeObserver()?.removeOnGlobalLayoutListener(onGlobalLayoutListener)
55+
contentView?.getViewTreeObserver()?.removeOnGlobalLayoutListener(onGlobalLayoutListener)
5156
}
5257
}
5358
}

android/src/main/java/com/lodev09/truesheet/core/Utils.kt

+22-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,32 @@
11
package com.lodev09.truesheet.core
22

3-
import android.view.View
3+
import android.annotation.SuppressLint
4+
import android.util.DisplayMetrics
45
import com.facebook.react.bridge.Promise
56
import com.facebook.react.bridge.ReactContext
67
import com.facebook.react.uimanager.PixelUtil
78

89
object Utils {
9-
fun activityView(reactContext: ReactContext): View? {
10-
val activity = reactContext.currentActivity ?: return null
11-
return activity.findViewById(android.R.id.content)
10+
@SuppressLint("DiscouragedApi", "InternalInsetResource")
11+
fun screenHeight(reactContext: ReactContext): Int {
12+
val activity = reactContext.currentActivity ?: return 0
13+
14+
// Get the screen metrics
15+
val displayMetrics = DisplayMetrics()
16+
activity.windowManager.defaultDisplay.getMetrics(displayMetrics)
17+
val screenHeight = displayMetrics.heightPixels
18+
19+
val resources = activity.resources
20+
21+
// Calculate status bar height
22+
var statusBarHeight = 0
23+
val resourceId: Int = resources.getIdentifier("status_bar_height", "dimen", "android")
24+
if (resourceId > 0) {
25+
statusBarHeight = resources.getDimensionPixelSize(resourceId)
26+
}
27+
28+
// Calculate max usable height
29+
return screenHeight - statusBarHeight
1230
}
1331

1432
fun toDIP(value: Int): Float = PixelUtil.toDIPFromPixel(value.toFloat())

0 commit comments

Comments
 (0)