Commit 1ee16334 authored by Leonardo Aramaki's avatar Leonardo Aramaki

Implement loading the emoji keyboard from the message composer

parent be00b615
......@@ -18,8 +18,12 @@ import chat.rocket.android.chatroom.presentation.ChatRoomPresenter
import chat.rocket.android.chatroom.presentation.ChatRoomView
import chat.rocket.android.chatroom.viewmodel.MessageViewModel
import chat.rocket.android.helper.EndlessRecyclerViewScrollListener
import chat.rocket.android.helper.KeyboardHelper
import chat.rocket.android.helper.MessageParser
import chat.rocket.android.util.extensions.*
import chat.rocket.android.widget.emoji.Emoji
import chat.rocket.android.widget.emoji.EmojiBottomPicker
import chat.rocket.android.widget.emoji.EmojiParser
import dagger.android.support.AndroidSupportInjection
import kotlinx.android.synthetic.main.fragment_chat_room.*
import kotlinx.android.synthetic.main.message_attachment_options.*
......@@ -43,7 +47,7 @@ private const val BUNDLE_CHAT_ROOM_TYPE = "chat_room_type"
private const val BUNDLE_IS_CHAT_ROOM_READ_ONLY = "is_chat_room_read_only"
private const val REQUEST_CODE_FOR_PERFORM_SAF = 42
class ChatRoomFragment : Fragment(), ChatRoomView {
class ChatRoomFragment : Fragment(), ChatRoomView, EmojiBottomPicker.OnEmojiClickCallback {
@Inject lateinit var presenter: ChatRoomPresenter
@Inject lateinit var parser: MessageParser
private lateinit var adapter: ChatRoomAdapter
......@@ -217,6 +221,14 @@ class ChatRoomFragment : Fragment(), ChatRoomView {
}
override fun onEmojiAdded(emoji: Emoji) {
val cursorPosition = text_message.selectionStart
val text = text_message.text.insert(cursorPosition, emoji.shortname).toString()
text_message.content = EmojiParser.parse(text)
text_message.setSelection(cursorPosition + emoji.unicode.length)
KeyboardHelper.showSoftKeyboard(text_message)
}
private fun setupComposer() {
if (isChatRoomReadOnly) {
text_room_is_read_only.setVisible(true)
......@@ -265,6 +277,13 @@ class ChatRoomFragment : Fragment(), ChatRoomView {
hideAttachmentOptions()
}, 600)
}
button_add_reaction.setOnClickListener {
activity?.let {
KeyboardHelper.hideSoftKeyboard(it)
EmojiBottomPicker().show(it.supportFragmentManager, "EmojiBottomPicker")
}
}
}
}
......
......@@ -2,6 +2,11 @@ package chat.rocket.android.helper
import android.graphics.Rect
import android.view.View
import android.content.Context.INPUT_METHOD_SERVICE
import android.app.Activity
import android.content.Context
import android.view.inputmethod.InputMethodManager
object KeyboardHelper {
......@@ -21,4 +26,29 @@ object KeyboardHelper {
val heightDiff = rootView.bottom - rect.bottom
return heightDiff > softKeyboardHeight * dm.density
}
/**
* Hide the soft keyboard.
*
* @param activity The current focused activity.
*/
fun hideSoftKeyboard(activity: Activity) {
val currentFocus = activity.currentFocus
if (currentFocus != null) {
val inputMethodManager = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(currentFocus.windowToken, 0)
}
}
/**
* Show the soft keyboard for the given view.
*
* @param view View to receive input focus.
*/
fun showSoftKeyboard(view: View) {
if (view.requestFocus()) {
val inputMethodManager = view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
}
}
}
\ No newline at end of file
......@@ -7,6 +7,10 @@ import android.widget.TextView
import android.provider.OpenableColumns
import android.webkit.MimeTypeMap
import android.provider.MediaStore
import android.text.Spannable
import android.text.Spanned
import android.text.TextUtils
import chat.rocket.android.widget.emoji.EmojiParser
import ru.noties.markwon.Markwon
fun String.ifEmpty(value: String): String {
......@@ -40,7 +44,13 @@ var TextView.content: CharSequence
set(value) {
Markwon.unscheduleDrawables(this)
Markwon.unscheduleTableRows(this)
if (value is Spanned) {
val result = EmojiParser.parse(value.toString()) as Spannable
TextUtils.copySpansFrom(value, 0, value.length, Any::class.java, result, 0)
text = result
} else {
text = value
}
Markwon.scheduleDrawables(this)
Markwon.scheduleTableRows(this)
}
......
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