Skip to content

Commit b4c5648

Browse files
committed
Properly handle attachment picker visibility
1 parent e87c8ee commit b4c5648

File tree

4 files changed

+41
-16
lines changed

4 files changed

+41
-16
lines changed

app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ import android.widget.RelativeLayout
3434
import androidx.annotation.StringRes
3535
import androidx.constraintlayout.widget.ConstraintLayout
3636
import androidx.core.content.res.ResourcesCompat
37-
import androidx.core.view.WindowInsetsCompat
38-
import androidx.core.view.updateLayoutParams
37+
import androidx.core.view.*
3938
import com.google.gson.Gson
4039
import com.google.gson.reflect.TypeToken
4140
import com.simplemobiletools.commons.dialogs.ConfirmationDialog
@@ -488,10 +487,11 @@ class ThreadActivity : SimpleActivity() {
488487
thread_add_attachment.setOnClickListener {
489488
if (attachment_picker_holder.isVisible()) {
490489
isAttachmentPickerVisible = false
491-
showKeyboard(thread_type_message)
490+
WindowCompat.getInsetsController(window, thread_type_message).show(WindowInsetsCompat.Type.ime())
492491
} else {
493492
isAttachmentPickerVisible = true
494-
hideKeyboard()
493+
showOrHideAttachmentPicker()
494+
WindowCompat.getInsetsController(window, thread_type_message).hide(WindowInsetsCompat.Type.ime())
495495
}
496496
window.decorView.requestApplyInsets()
497497
}
@@ -1445,19 +1445,40 @@ class ThreadActivity : SimpleActivity() {
14451445
}
14461446

14471447
private fun setupKeyboardListener() {
1448-
val imeTypeMask = WindowInsetsCompat.Type.ime()
1449-
val navigationBarMask = WindowInsetsCompat.Type.navigationBars()
1450-
1451-
window.decorView.setOnApplyWindowInsetsListener { view, windowInsets ->
1452-
val insets = WindowInsetsCompat.toWindowInsetsCompat(windowInsets)
1453-
if (insets.isVisible(imeTypeMask)) {
1454-
config.keyboardHeight = insets.getInsets(imeTypeMask).bottom - insets.getInsets(navigationBarMask).bottom
1455-
hideAttachmentPicker()
1456-
} else if (isAttachmentPickerVisible) {
1457-
showAttachmentPicker()
1448+
window.decorView.setOnApplyWindowInsetsListener { _, insets ->
1449+
showOrHideAttachmentPicker()
1450+
insets
1451+
}
1452+
1453+
val callback = object : WindowInsetsAnimationCompat.Callback(DISPATCH_MODE_CONTINUE_ON_SUBTREE) {
1454+
override fun onPrepare(animation: WindowInsetsAnimationCompat) {
1455+
super.onPrepare(animation)
1456+
showOrHideAttachmentPicker()
14581457
}
14591458

1460-
view.onApplyWindowInsets(windowInsets)
1459+
override fun onProgress(insets: WindowInsetsCompat, runningAnimations: MutableList<WindowInsetsAnimationCompat>) = insets
1460+
}
1461+
ViewCompat.setWindowInsetsAnimationCallback(window.decorView, callback)
1462+
}
1463+
1464+
private fun showOrHideAttachmentPicker() {
1465+
val type = WindowInsetsCompat.Type.ime()
1466+
val insets = ViewCompat.getRootWindowInsets(window.decorView) ?: return
1467+
val isKeyboardVisible = insets.isVisible(type)
1468+
1469+
if (isKeyboardVisible) {
1470+
val keyboardHeight = insets.getInsets(type).bottom
1471+
val bottomBarHeight = insets.getInsets(WindowInsetsCompat.Type.navigationBars()).bottom
1472+
1473+
// check keyboard height just to be sure, 150 seems like a good middle ground between ime and navigation bar
1474+
config.keyboardHeight = if (keyboardHeight > 150) {
1475+
keyboardHeight - bottomBarHeight
1476+
} else {
1477+
getDefaultKeyboardHeight()
1478+
}
1479+
hideAttachmentPicker()
1480+
} else if (isAttachmentPickerVisible) {
1481+
showAttachmentPicker()
14611482
}
14621483
}
14631484
}

app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,3 +1065,5 @@ fun Context.clearExpiredScheduledMessages(threadId: Long, messagesToDelete: List
10651065
return
10661066
}
10671067
}
1068+
1069+
fun Context.getDefaultKeyboardHeight() = resources.getDimensionPixelSize(R.dimen.default_keyboard_height)

app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/Config.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.simplemobiletools.smsmessenger.helpers
22

33
import android.content.Context
44
import com.simplemobiletools.commons.helpers.BaseConfig
5+
import com.simplemobiletools.smsmessenger.extensions.getDefaultKeyboardHeight
56
import com.simplemobiletools.smsmessenger.models.Conversation
67

78
class Config(context: Context) : BaseConfig(context) {
@@ -88,6 +89,6 @@ class Config(context: Context) : BaseConfig(context) {
8889
set(wasDbCleared) = prefs.edit().putBoolean(WAS_DB_CLEARED, wasDbCleared).apply()
8990

9091
var keyboardHeight: Int
91-
get() = prefs.getInt(SOFT_KEYBOARD_HEIGHT, 600)
92+
get() = prefs.getInt(SOFT_KEYBOARD_HEIGHT, context.getDefaultKeyboardHeight())
9293
set(keyboardHeight) = prefs.edit().putInt(SOFT_KEYBOARD_HEIGHT, keyboardHeight).apply()
9394
}

app/src/main/res/values/dimens.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@
1313
<dimen name="medium_icon_size">36dp</dimen>
1414
<dimen name="attachment_button_height">96dp</dimen>
1515
<dimen name="attachment_button_width">90dp</dimen>
16+
<dimen name="default_keyboard_height">250dp</dimen>
1617
</resources>

0 commit comments

Comments
 (0)