Skip to content

Instantly share code, notes, and snippets.

@xxnjdlys
Created April 11, 2019 13:37
Show Gist options
  • Save xxnjdlys/e299c3f9be22d9b65c1ebd2f946589bf to your computer and use it in GitHub Desktop.
Save xxnjdlys/e299c3f9be22d9b65c1ebd2f946589bf to your computer and use it in GitHub Desktop.
package language.chat.meet.talk.widget.messages
import android.content.Context
import android.support.text.emoji.EmojiCompat
import android.support.text.emoji.FontRequestEmojiCompatConfig
import android.support.text.emoji.bundled.BundledEmojiCompatConfig
import android.support.v7.widget.GridLayoutManager
import android.text.Editable
import android.text.TextWatcher
import android.util.AttributeSet
import android.view.View
import android.widget.*
import kotlinx.android.synthetic.main.view_message_input.view.*
import language.chat.meet.talk.R
import com.speaky.common.base.BaseRecyclerViewAdapter
import com.speaky.common.utils.Lg
import com.speaky.common.im.model.Emoji
import language.chat.meet.talk.ui.chat.adapter.EmojiListAdapter
import android.support.v4.provider.FontRequest
/**
* Created by zhangyang on 2017/11/24.
*/
class MessageInput:LinearLayout, View.OnClickListener,TextWatcher{
private var mMessageInput: EditText? = null
private var mBtnSend : ImageView? = null
private var mBtnTranlate : ImageView? = null
private var inputListener: InputListener? = null
private var emojiList:ArrayList<Emoji>? = null
private var input: CharSequence = ""
constructor(context: Context) : this(context, null)
constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
init(context,attrs)
}
private fun init(context: Context,attrs: AttributeSet?){
val fontRequest = FontRequest(
"com.google.android.gms.fonts",
"com.google.android.gms",
"Noto Color Emoji Compat",
language.chat.meet.talk.R.array.com_google_android_gms_fonts_certs)
EmojiCompat.init(FontRequestEmojiCompatConfig(context.applicationContext,fontRequest))
// EmojiCompat.init(FontRequestEmojiCompatConfig(context.applicationContext,fontRequest))
createEmojoList()
initView(context)
}
private fun initView(context: Context){
View.inflate(context, R.layout.view_message_input,this)
mMessageInput = findViewById(R.id.messageInput)
mBtnSend = findViewById(R.id.messageSendButton)
mBtnTranlate = findViewById(R.id.messageTransButton)
mMessageInput!!.addTextChangedListener(this)
mBtnSend!!.setOnClickListener(this)
mBtnTranlate!!.setOnClickListener(this)
val emojiAdapter = EmojiListAdapter(context, emojiList!!)
rvEmoji.adapter = emojiAdapter
rvEmoji.layoutManager = GridLayoutManager(context,9)
emojiAdapter.setItemClickListener(object: BaseRecyclerViewAdapter.OnItemClickListener{
override fun onItemClick(view: View?, position: Int) {
Lg.d("Emoji Adapter onItemClick: $position" )
val emoji = emojiList?.get(position)
appendEmoji(emoji!!.unicode)
}
})
}
private fun createEmojoList(){
emojiList = ArrayList()
emojiList!!.add(Emoji("大拇哥","\uD83D\uDC4D"))
emojiList!!.add(Emoji("色","\uD83D\uDE0D"))
emojiList!!.add(Emoji("大哭","\uD83D\uDE2D"))
emojiList!!.add(Emoji("红心","\u2764\ufe0f"))
emojiList!!.add(Emoji("亲亲红心","\uD83D\uDE18"))
emojiList!!.add(Emoji("玫瑰","\uD83C\uDF39"))
emojiList!!.add(Emoji("鼓掌","\uD83D\uDC4F"))
emojiList!!.add(Emoji("合掌感恩","\uD83D\uDE4F"))
emojiList!!.add(Emoji("露齿笑","\uD83D\uDE01"))
emojiList!!.add(Emoji("尴尬流汗","\uD83D\uDE05"))
emojiList!!.add(Emoji("俏皮","\uD83D\uDE09"))
emojiList!!.add(Emoji("戴墨镜酷酷的","\uD83D\uDE0E"))
emojiList!!.add(Emoji("可爱小手","\uD83E\uDD17"))
emojiList!!.add(Emoji("思考","\uD83E\uDD14"))
// emojiList!!.add(Emoji("竖眉毛","\uD83E\uDD28"))
emojiList!!.add(Emoji("Sleep","\uD83D\uDE34"))
emojiList!!.add(Emoji("白眼","\uD83D\uDE44"))
emojiList!!.add(Emoji("拉链嘴","\uD83E\uDD10"))
emojiList!!.add(Emoji("吃惊","\uD83D\uDE2F"))
emojiList!!.add(Emoji("睡觉,鼻涕泡","\uD83D\uDE2A"))
emojiList!!.add(Emoji("睡觉,打呼噜","\uD83D\uDE34"))
emojiList!!.add(Emoji("吐舌头","\uD83D\uDE1B"))
emojiList!!.add(Emoji("伤心,半圆嘴","\uD83D\uDE26"))
emojiList!!.add(Emoji("吃惊,吓死了","\uD83D\uDE31"))
emojiList!!.add(Emoji("么么哒","\uD83D\uDE17"))
emojiList!!.add(Emoji("晕了","\uD83D\uDE35"))
emojiList!!.add(Emoji("骂人","\uD83E\uDD2C"))
emojiList!!.add(Emoji("吐舌头","\uD83D\uDE1B"))
emojiList!!.add(Emoji("头戴光环","\uD83D\uDE07"))
emojiList!!.add(Emoji("OK","\uD83D\uDC4C"))
emojiList!!.add(Emoji("红唇","\uD83D\uDC8B"))
emojiList!!.add(Emoji("钱眼儿","\uD83E\uDD11"))
emojiList!!.add(Emoji("气死老娘了","\uD83D\uDE24"))
emojiList!!.add(Emoji("爆炸","\uD83D\uDCA5"))
emojiList!!.add(Emoji("爱的手语","\uD83E\uDD1F"))
emojiList!!.add(Emoji("yeah","\u270c\ufe0f"))
emojiList!!.add(Emoji("恶魔","\uD83D\uDE08"))
}
fun setOnFocusListener(listener: OnFocusChangeListener?): MessageInput{
mMessageInput!!.onFocusChangeListener = listener
return this
}
fun appendEmoji(unicode: String){
mMessageInput?.let {
it.append(unicode)
}
}
/**
* Sets callback for 'submit' button.
*
* @param inputListener input callback
*/
fun setInputListener(inputListener: InputListener) {
this.inputListener = inputListener
}
override fun afterTextChanged(s: Editable?) {
Lg.d("xxnjdlys","afterTextChanged")
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
Lg.d("xxnjdlys","beforeTextChanged")
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
input = s?:""
mBtnSend!!.setEnabled(input!!.length > 0)
}
fun clearText(){
mMessageInput!!.setText("")
}
override fun onClick(v: View?) {
val id = v!!.id
if (id == R.id.messageSendButton) {
val isSubmitted = onSubmit()
if (isSubmitted) {
mMessageInput!!.setText("")
}
}
if (id == R.id.messageTransButton) {
onTranslate()
}
}
private fun onSubmit(): Boolean {
return inputListener != null && inputListener!!.onSubmit(input)
}
private fun onTranslate() {
inputListener?.onTranslate(input)
}
/**
* Interface definition for a callback to be invoked when user pressed 'submit' button
*/
interface InputListener {
/**
* Fires when user presses 'send' button.
*
* @param input input entered by user
* @return if input text is valid, you must return `true` and input will be cleared, otherwise return false.
*/
fun onSubmit(input: CharSequence): Boolean
fun onTranslate(input: CharSequence)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment