Twidere-App-Android-Twitter.../twidere/src/main/kotlin/org/mariotaku/twidere/view/ComposeEditText.kt

96 lines
3.1 KiB
Kotlin
Raw Normal View History

2016-08-28 06:24:48 +02:00
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2015 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.twidere.view
import android.content.Context
import android.text.InputType
import android.text.Selection
import android.text.method.ArrowKeyMovementMethod
import android.text.method.MovementMethod
import android.util.AttributeSet
import android.widget.AdapterView
import org.mariotaku.chameleon.view.ChameleonMultiAutoCompleteTextView
2016-08-28 06:24:48 +02:00
import org.mariotaku.twidere.adapter.ComposeAutoCompleteAdapter
import org.mariotaku.twidere.model.UserKey
import org.mariotaku.twidere.util.EmojiSupportUtils
import org.mariotaku.twidere.util.widget.StatusTextTokenizer
class ComposeEditText(
2016-12-18 06:21:24 +01:00
context: Context,
attrs: AttributeSet? = null
) : ChameleonMultiAutoCompleteTextView(context, attrs) {
2016-08-28 06:24:48 +02:00
private var adapter: ComposeAutoCompleteAdapter? = null
var accountKey: UserKey? = null
set(value) {
field = value
updateAccountKey()
}
init {
EmojiSupportUtils.initForTextView(this)
setTokenizer(StatusTextTokenizer())
onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, id ->
removeIMESuggestions()
}
// HACK: remove AUTO_COMPLETE flag to force IME show auto completion
setRawInputType(inputType and InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE.inv())
}
override fun getDefaultMovementMethod(): MovementMethod {
return ArrowKeyMovementMethod.getInstance()
}
override fun onAttachedToWindow() {
super.onAttachedToWindow()
if (!isInEditMode && adapter == null) {
adapter = ComposeAutoCompleteAdapter(context)
}
setAdapter(adapter)
2016-08-28 06:24:48 +02:00
updateAccountKey()
}
override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
adapter?.closeCursor()
adapter = null
}
override fun onTextContextMenuItem(id: Int): Boolean {
try {
return super.onTextContextMenuItem(id)
} catch (e: AbstractMethodError) {
// http://crashes.to/s/69acd0ea0de
return true
}
}
2016-08-28 06:24:48 +02:00
private fun updateAccountKey() {
adapter?.accountKey = accountKey
}
private fun removeIMESuggestions() {
val selectionEnd = selectionEnd
val selectionStart = selectionStart
Selection.removeSelection(text)
setSelection(selectionStart, selectionEnd)
}
}