Commit 7db7a3ae authored by Leonardo Aramaki's avatar Leonardo Aramaki

Fix closing keyboard behavior and rename few methods

parent b6b8ada6
......@@ -4,7 +4,6 @@ import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.app.AppCompatActivity
import chat.rocket.android.R
import chat.rocket.android.helper.KeyboardHelper
import chat.rocket.android.util.extensions.addFragment
......@@ -68,13 +67,13 @@ class ChatRoomActivity : SwipeBackActivity(), HasSupportFragmentInjector {
}
override fun onBackPressed() {
super.onBackPressed()
val frag = supportFragmentManager.findFragmentByTag(EmojiFragment.TAG) as EmojiFragment?
if (frag != null && frag.isShown()) {
frag.hide()
if (frag != null && frag.isExpanded()) {
frag.collapse()
} else {
KeyboardHelper.hideSoftKeyboard(this)
finishActivity()
super.onBackPressed()
}
}
......
......@@ -261,7 +261,7 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiFragment.EmojiKeyboardLi
}
}
override fun onKeyPressed(keyCode: Int) {
override fun onNonEmojiKeyPressed(keyCode: Int) {
when (keyCode) {
KeyEvent.KEYCODE_BACK -> with(text_message) {
if (selectionStart > 0) text.delete(selectionStart - 1, selectionStart)
......@@ -270,6 +270,14 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiFragment.EmojiKeyboardLi
}
}
override fun onEmojiPanelCollapsed() {
setReactionButtonIcon(R.drawable.ic_reaction_24dp)
}
override fun onEmojiPanelExpanded() {
}
private fun setReactionButtonIcon(@DrawableRes drawableId: Int) {
button_add_reaction.setImageResource(drawableId)
button_add_reaction.setTag(drawableId)
......@@ -278,7 +286,7 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiFragment.EmojiKeyboardLi
private fun hideAllKeyboards() {
activity?.let {
KeyboardHelper.hideSoftKeyboard(it)
attachOrGetEmojiFragment()?.hide()
attachOrGetEmojiFragment()?.collapse()
setReactionButtonIcon(R.drawable.ic_reaction_24dp)
}
}
......@@ -328,7 +336,7 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiFragment.EmojiKeyboardLi
activity?.let {
val fragment = EmojiFragment.getOrAttach(it, R.id.emoji_fragment_placeholder, composer)
if (fragment.isCollapsed()) {
fragment.openHidden()
fragment.expandHidden()
}
setReactionButtonIcon(R.drawable.ic_reaction_24dp)
}
......@@ -353,7 +361,7 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiFragment.EmojiKeyboardLi
sendMessage(textMessage)
attachOrGetEmojiFragment()?.let {
if (it.softKeyboardVisible) {
it.hide()
it.collapse()
}
}
clearMessageComposition()
......@@ -390,7 +398,7 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiFragment.EmojiKeyboardLi
when (tag) {
R.drawable.ic_reaction_24dp -> {
KeyboardHelper.hideSoftKeyboard(it)
if (!emojiFragment.isShown()) {
if (!emojiFragment.isExpanded()) {
emojiFragment.show()
}
setReactionButtonIcon(R.drawable.ic_keyboard_black_24dp)
......@@ -402,6 +410,8 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiFragment.EmojiKeyboardLi
}
}
}
text_message.requestFocus()
}
}
......
......@@ -13,7 +13,6 @@ import android.view.*
import android.widget.EditText
import android.widget.ImageView
import chat.rocket.android.R
import chat.rocket.android.util.extensions.setVisible
class EmojiFragment : Fragment() {
......@@ -98,6 +97,9 @@ class EmojiFragment : Fragment() {
EmojiRepository.saveKeyboardHeight(currentKeyboardHeight)
setKeyboardHeight(currentKeyboardHeight)
softKeyboardVisible = true
parentContainer.postDelayed({
expandHidden()
}, 100)
} else if (lastVisibleDecorViewHeight + MIN_KEYBOARD_HEIGHT_PX < visibleDecorViewHeight) {
// Notify listener about keyboard being hidden.
softKeyboardVisible = false
......@@ -122,7 +124,7 @@ class EmojiFragment : Fragment() {
}
backspaceView.setOnClickListener {
listener?.onKeyPressed(KeyEvent.KEYCODE_BACK)
listener?.onNonEmojiKeyPressed(KeyEvent.KEYCODE_BACK)
}
}
......@@ -139,7 +141,15 @@ class EmojiFragment : Fragment() {
}
}
viewPager.adapter = CategoryPagerAdapter(object : EmojiKeyboardListener {
override fun onKeyPressed(keyCode: Int) {
override fun onEmojiPanelExpanded() {
// do nothing
}
override fun onEmojiPanelCollapsed() {
// do nothing
}
override fun onNonEmojiKeyPressed(keyCode: Int) {
// do nothing
}
......@@ -215,25 +225,35 @@ class EmojiFragment : Fragment() {
}
}
private fun setKeyboardVisibility(visibility: Int) {
if (visibility != parentContainer.visibility) {
parentContainer.visibility = visibility
}
}
/**
* Show the emoji keyboard.
*/
fun show() {
parentContainer.setVisible(true)
setKeyboardVisibility(View.VISIBLE)
}
fun openHidden() {
parentContainer.visibility = View.INVISIBLE
/**
* Expand the emoji keyboard with invisible contents.
*/
fun expandHidden() {
setKeyboardVisibility(View.INVISIBLE)
}
/**
* Hide the emoji keyboard.
*/
fun hide() {
fun collapse() {
// Since the emoji keyboard is always behind the soft keyboard assume it's also dismissed
// when the emoji one is about to get close. Hence we should invoke our listener to update
// the UI as if the soft keyboard is hidden.
parentContainer.setVisible(false)
listener?.onEmojiPanelCollapsed()
setKeyboardVisibility(View.GONE)
}
/**
......@@ -241,7 +261,7 @@ class EmojiFragment : Fragment() {
*
* @return <code>true</code> if opened.
*/
fun isShown() = parentContainer.visibility == View.VISIBLE
fun isExpanded() = parentContainer.visibility == View.VISIBLE
/**
* Whether the emoji keyboard is collapsed.
......@@ -265,6 +285,10 @@ class EmojiFragment : Fragment() {
*
* @see android.view.KeyEvent
*/
fun onKeyPressed(keyCode: Int)
fun onNonEmojiKeyPressed(keyCode: Int)
fun onEmojiPanelCollapsed()
fun onEmojiPanelExpanded()
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment