Properly handle attachment picker visibility

This commit is contained in:
Naveen 2022-11-08 02:13:24 +05:30
parent e87c8eeff7
commit b4c5648ddc
4 changed files with 40 additions and 15 deletions

View File

@ -34,8 +34,7 @@ import android.widget.RelativeLayout
import androidx.annotation.StringRes
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.res.ResourcesCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.updateLayoutParams
import androidx.core.view.*
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.simplemobiletools.commons.dialogs.ConfirmationDialog
@ -488,10 +487,11 @@ class ThreadActivity : SimpleActivity() {
thread_add_attachment.setOnClickListener {
if (attachment_picker_holder.isVisible()) {
isAttachmentPickerVisible = false
showKeyboard(thread_type_message)
WindowCompat.getInsetsController(window, thread_type_message).show(WindowInsetsCompat.Type.ime())
} else {
isAttachmentPickerVisible = true
hideKeyboard()
showOrHideAttachmentPicker()
WindowCompat.getInsetsController(window, thread_type_message).hide(WindowInsetsCompat.Type.ime())
}
window.decorView.requestApplyInsets()
}
@ -1445,19 +1445,40 @@ class ThreadActivity : SimpleActivity() {
}
private fun setupKeyboardListener() {
val imeTypeMask = WindowInsetsCompat.Type.ime()
val navigationBarMask = WindowInsetsCompat.Type.navigationBars()
window.decorView.setOnApplyWindowInsetsListener { _, insets ->
showOrHideAttachmentPicker()
insets
}
window.decorView.setOnApplyWindowInsetsListener { view, windowInsets ->
val insets = WindowInsetsCompat.toWindowInsetsCompat(windowInsets)
if (insets.isVisible(imeTypeMask)) {
config.keyboardHeight = insets.getInsets(imeTypeMask).bottom - insets.getInsets(navigationBarMask).bottom
val callback = object : WindowInsetsAnimationCompat.Callback(DISPATCH_MODE_CONTINUE_ON_SUBTREE) {
override fun onPrepare(animation: WindowInsetsAnimationCompat) {
super.onPrepare(animation)
showOrHideAttachmentPicker()
}
override fun onProgress(insets: WindowInsetsCompat, runningAnimations: MutableList<WindowInsetsAnimationCompat>) = insets
}
ViewCompat.setWindowInsetsAnimationCallback(window.decorView, callback)
}
private fun showOrHideAttachmentPicker() {
val type = WindowInsetsCompat.Type.ime()
val insets = ViewCompat.getRootWindowInsets(window.decorView) ?: return
val isKeyboardVisible = insets.isVisible(type)
if (isKeyboardVisible) {
val keyboardHeight = insets.getInsets(type).bottom
val bottomBarHeight = insets.getInsets(WindowInsetsCompat.Type.navigationBars()).bottom
// check keyboard height just to be sure, 150 seems like a good middle ground between ime and navigation bar
config.keyboardHeight = if (keyboardHeight > 150) {
keyboardHeight - bottomBarHeight
} else {
getDefaultKeyboardHeight()
}
hideAttachmentPicker()
} else if (isAttachmentPickerVisible) {
showAttachmentPicker()
}
view.onApplyWindowInsets(windowInsets)
}
}
}

View File

@ -1065,3 +1065,5 @@ fun Context.clearExpiredScheduledMessages(threadId: Long, messagesToDelete: List
return
}
}
fun Context.getDefaultKeyboardHeight() = resources.getDimensionPixelSize(R.dimen.default_keyboard_height)

View File

@ -2,6 +2,7 @@ package com.simplemobiletools.smsmessenger.helpers
import android.content.Context
import com.simplemobiletools.commons.helpers.BaseConfig
import com.simplemobiletools.smsmessenger.extensions.getDefaultKeyboardHeight
import com.simplemobiletools.smsmessenger.models.Conversation
class Config(context: Context) : BaseConfig(context) {
@ -88,6 +89,6 @@ class Config(context: Context) : BaseConfig(context) {
set(wasDbCleared) = prefs.edit().putBoolean(WAS_DB_CLEARED, wasDbCleared).apply()
var keyboardHeight: Int
get() = prefs.getInt(SOFT_KEYBOARD_HEIGHT, 600)
get() = prefs.getInt(SOFT_KEYBOARD_HEIGHT, context.getDefaultKeyboardHeight())
set(keyboardHeight) = prefs.edit().putInt(SOFT_KEYBOARD_HEIGHT, keyboardHeight).apply()
}

View File

@ -13,4 +13,5 @@
<dimen name="medium_icon_size">36dp</dimen>
<dimen name="attachment_button_height">96dp</dimen>
<dimen name="attachment_button_width">90dp</dimen>
<dimen name="default_keyboard_height">250dp</dimen>
</resources>