fix: don't auto-correct commands

Prevent /fc from turning into /FC.
This commit is contained in:
Anna 2020-10-25 18:10:06 -04:00
parent 8f25847e91
commit a3a00a4acf
1 changed files with 32 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package io.annaclemens.xivchat.ui.messages
import android.os.Bundle
import android.text.InputType
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuInflater
@ -11,6 +12,7 @@ import android.view.inputmethod.EditorInfo
import android.widget.SearchView
import androidx.activity.viewModels
import androidx.core.view.iterator
import androidx.core.widget.addTextChangedListener
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
@ -114,6 +116,36 @@ class MessagesTabsFragment : Fragment() {
}
root.messageInput.apply {
// don't auto-correct for command names
this.editText?.addTextChangedListener { text ->
if (text == null) {
return@addTextChangedListener
}
val editText = this.editText ?: return@addTextChangedListener
val startsWithSlash = text.startsWith('/')
val start = editText.selectionStart
val end = editText.selectionEnd
val firstSpace = text.indexOf(' ').let { if (it == -1) text.length else it }
val inFirstWord = start <= firstSpace || end <= firstSpace
val new = if (startsWithSlash && inFirstWord) {
// disable autocorrect if in the first word and it starts with a slash
editText.inputType and InputType.TYPE_TEXT_FLAG_AUTO_CORRECT.inv()
} else {
// enable autcorrect otherwise
editText.inputType or InputType.TYPE_TEXT_FLAG_AUTO_CORRECT
}
// only change the input type if necessary (holding down backspace stops when input type changes)
if (editText.inputType != new) {
editText.inputType = new
}
}
fun sendMessage() {
val view = this.editText ?: return
val messageText = view.text.toString()