From b4c5648ddc4727f3a9d2863090d78cfaf7950446 Mon Sep 17 00:00:00 2001 From: Naveen Date: Tue, 8 Nov 2022 02:13:24 +0530 Subject: [PATCH] Properly handle attachment picker visibility --- .../smsmessenger/activities/ThreadActivity.kt | 49 +++++++++++++------ .../smsmessenger/extensions/Context.kt | 2 + .../smsmessenger/helpers/Config.kt | 3 +- app/src/main/res/values/dimens.xml | 1 + 4 files changed, 40 insertions(+), 15 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt index c536b538..12adb04a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt @@ -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 - hideAttachmentPicker() - } else if (isAttachmentPickerVisible) { - showAttachmentPicker() + val callback = object : WindowInsetsAnimationCompat.Callback(DISPATCH_MODE_CONTINUE_ON_SUBTREE) { + override fun onPrepare(animation: WindowInsetsAnimationCompat) { + super.onPrepare(animation) + showOrHideAttachmentPicker() } - view.onApplyWindowInsets(windowInsets) + override fun onProgress(insets: WindowInsetsCompat, runningAnimations: MutableList) = 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() } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt index 77b47faf..6b97672f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt @@ -1065,3 +1065,5 @@ fun Context.clearExpiredScheduledMessages(threadId: Long, messagesToDelete: List return } } + +fun Context.getDefaultKeyboardHeight() = resources.getDimensionPixelSize(R.dimen.default_keyboard_height) diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/Config.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/Config.kt index fa076222..c1fa3d74 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/Config.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/Config.kt @@ -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() } diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml index d8f37a56..324712d5 100644 --- a/app/src/main/res/values/dimens.xml +++ b/app/src/main/res/values/dimens.xml @@ -13,4 +13,5 @@ 36dp 96dp 90dp + 250dp