feature(language): add vi string + config Vn

Signed-off-by: Hung <>
This commit is contained in:
Henry 2023-06-26 16:25:45 +07:00 committed by Hung
parent b57a0461cf
commit 01aeabdf9a
5 changed files with 72 additions and 3 deletions

View File

@ -14,6 +14,7 @@ import com.simplemobiletools.keyboard.extensions.getKeyboardLanguages
import com.simplemobiletools.keyboard.helpers.KEYBOARD_HEIGHT_MULTIPLIER_LARGE
import com.simplemobiletools.keyboard.helpers.KEYBOARD_HEIGHT_MULTIPLIER_MEDIUM
import com.simplemobiletools.keyboard.helpers.KEYBOARD_HEIGHT_MULTIPLIER_SMALL
import com.simplemobiletools.keyboard.helpers.LANGUAGE_VIETNAMESE_TELEX
import kotlinx.android.synthetic.main.activity_settings.*
import java.util.*
import kotlin.system.exitProcess

View File

@ -51,3 +51,4 @@ const val KEYBOARD_HEIGHT_MULTIPLIER_MEDIUM = 2
const val KEYBOARD_HEIGHT_MULTIPLIER_LARGE = 3
const val EMOJI_SPEC_FILE_PATH = "media/emoji_spec.txt"
const val LANGUAGE_VN_TELEX = "language/extension.json"

View File

@ -1,8 +1,11 @@
package com.simplemobiletools.keyboard.helpers
import android.content.Context
import org.json.JSONObject
import java.io.InputStream
private var cachedEmojiData: MutableList<String>? = null
val cachedVNTelexData: HashMap<String,String> = HashMap()
/**
* Reads the emoji list at the given [path] and returns an parsed [MutableList]. If the
@ -59,3 +62,27 @@ fun parseRawEmojiSpecsFile(context: Context, path: String): MutableList<String>
cachedEmojiData = emojis
return emojis
}
fun parseRawJsonSpecsFile(context: Context, path: String): HashMap<String,String> {
if (cachedVNTelexData.isNotEmpty()) {
return cachedVNTelexData
}
try {
val inputStream: InputStream = context.assets.open(path)
val jsonString = inputStream.bufferedReader().use{it.readText()}
val jsonData = JSONObject(jsonString)
val rulesObj = jsonData.getJSONObject("rules")
val ruleKeys = rulesObj.keys()
while (ruleKeys.hasNext()){
val key = ruleKeys.next()
val value = rulesObj.getString(key)
cachedVNTelexData[key] = value
}
} catch (ex: Exception) {
ex.printStackTrace()
return HashMap()
}
return cachedVNTelexData
}

View File

@ -178,10 +178,38 @@ class SimpleKeyboardIME : InputMethodService(), OnKeyboardActionListener, Shared
switchToLetters = true
}
} else {
inputConnection.commitText(codeChar.toString(), 1)
if (originalText == null) {
updateShiftKeyState()
when {
originalText != null && originalText.isNotEmpty() && cachedVNTelexData.isNotEmpty() -> {
val fullText = originalText.toString() + codeChar.toString()
val lastIndexEmpty = if (fullText.contains(" ")) {
fullText.lastIndexOf(" ")
} else 0
if (lastIndexEmpty >= 0) {
val word = fullText.subSequence(lastIndexEmpty, fullText.length).trim().toString()
val wordChars = word.toCharArray()
val predictWord = StringBuilder()
for (char in wordChars.size -1 downTo 0) {
predictWord.append(wordChars[char])
val shouldChangeText = predictWord.reverse().toString()
if (cachedVNTelexData.containsKey(shouldChangeText)) {
inputConnection.setComposingRegion(fullText.length - shouldChangeText.length, fullText.length)
inputConnection.setComposingText(cachedVNTelexData[shouldChangeText], fullText.length)
inputConnection.setComposingRegion(fullText.length, fullText.length)
return
}
}
inputConnection.commitText(codeChar.toString(), 1)
}
}
else -> {
inputConnection.commitText(codeChar.toString(), 1)
if (originalText == null) {
updateShiftKeyState()
}
}
}
}
}
}

View File

@ -418,6 +418,11 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
}
setupEmojiPalette(toolbarColor = toolbarColor, backgroundColor = mBackgroundColor, textColor = mTextColor)
if(context.config.keyboardLanguage == LANGUAGE_VIETNAMESE_TELEX){
setupLanguageTelex()
}else{
cachedVNTelexData.clear()
}
setupStoredClips()
}
@ -1506,6 +1511,13 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
}
}
// For Vietnamese - Telex
private fun setupLanguageTelex() {
ensureBackgroundThread {
parseRawJsonSpecsFile(context, LANGUAGE_VN_TELEX)
}
}
private fun setupEmojiAdapter(emojis: List<String>) {
mEmojiPaletteHolder?.emojis_list?.apply {
val emojiItemWidth = context.resources.getDimensionPixelSize(R.dimen.emoji_item_size)