Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/src/main/java/be/scri/helpers/DatabaseHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class DatabaseHelper(
getRequiredData(language),
)

fun getEmojiMaxKeywordLength(): Int = dbManagers.emojiManager.maxKeywordLength

fun findCaseAnnnotationForPreposition(language: String): HashMap<String, MutableList<String>> =
if (language != "DE" && language != "RU") {
hashMapOf()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,27 @@ import android.database.sqlite.SQLiteDatabase
class EmojiDataManager(
private val context: Context,
) {
// Track max keyword length.
var maxKeywordLength = 0

fun getEmojiKeywords(language: String): HashMap<String, MutableList<String>> {
val dbFile = context.getDatabasePath("${language}LanguageData.sqlite")
return processEmojiKeywords(dbFile.path)
}

private fun processEmojiKeywords(dbPath: String): HashMap<String, MutableList<String>> {
val hashMap = HashMap<String, MutableList<String>>()
val db = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READONLY)

db.use { database ->
database.rawQuery("SELECT * FROM emoji_keywords", null).use { cursor ->
SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READONLY).use { db ->
// Get max keyword length.
db.rawQuery("SELECT MAX(LENGTH(word)) FROM emoji_keywords", null).use { cursor ->
if (cursor.moveToFirst()) {
maxKeywordLength = cursor.getInt(0)
}
}

// Keyword processing.
db.rawQuery("SELECT * FROM emoji_keywords", null).use { cursor ->
processEmojiCursor(cursor, hashMap)
}
}
Expand Down
45 changes: 44 additions & 1 deletion app/src/main/java/be/scri/services/GeneralKeyboardIME.kt
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ abstract class GeneralKeyboardIME(

private lateinit var dbHelper: DatabaseHelper
lateinit var emojiKeywords: HashMap<String, MutableList<String>>
var emojiMaxKeywordLength: Int = 0
lateinit var nounKeywords: HashMap<String, List<String>>
lateinit var pluralWords: List<String>
lateinit var caseAnnotation: HashMap<String, MutableList<String>>
Expand Down Expand Up @@ -295,6 +296,7 @@ abstract class GeneralKeyboardIME(
dbHelper = DatabaseHelper(this)
dbHelper.loadDatabase(languageAlias)
emojiKeywords = dbHelper.getEmojiKeywords(languageAlias)
emojiMaxKeywordLength = dbHelper.getEmojiMaxKeywordLength()
pluralWords = dbHelper.checkIfWordIsPlural(languageAlias)!!
nounKeywords = dbHelper.findGenderOfWord(languageAlias)

Expand Down Expand Up @@ -1229,12 +1231,52 @@ abstract class GeneralKeyboardIME(

/**
* Inserts the specified emoji into the current input field.
* Replaces the last word if there's no trailing space.
*
* @param emoji The emoji character to be inserted.
*/
private fun insertEmoji(emoji: String) {
val inputConnection = currentInputConnection ?: return
inputConnection.commitText(emoji, 1)
val maxLookBack = emojiMaxKeywordLength.coerceAtLeast(1)

inputConnection.beginBatchEdit()
try {
val previousText = inputConnection.getTextBeforeCursor(maxLookBack, 0)?.toString() ?: ""

// Find last word boundary efficiently
val lastSpaceIndex = previousText.lastIndexOf(' ')
val hasSpace = lastSpaceIndex != -1

when {
// Case 1: Ends with space or empty
previousText.isEmpty() || hasSpace && lastSpaceIndex == previousText.length - 1 -> {
inputConnection.commitText(emoji, 1)
}

// Case 2: Has previous word
hasSpace -> {
val lastWord = previousText.substring(lastSpaceIndex + 1)
if (emojiKeywords.containsKey(lastWord.lowercase())) {
inputConnection.deleteSurroundingText(lastWord.length, 0)
inputConnection.commitText(emoji, 1)
} else {
inputConnection.commitText(emoji, 1)
}
}

// Case 3: Entire text is the word
else -> {
if (emojiKeywords.containsKey(previousText.lowercase())) {
inputConnection.deleteSurroundingText(previousText.length, 0)
inputConnection.commitText(emoji, 1)
} else {
inputConnection.commitText(emoji, 1)
}
}
}
} finally {
inputConnection.endBatchEdit()
}
}

/**
Expand Down Expand Up @@ -1577,6 +1619,7 @@ abstract class GeneralKeyboardIME(
internal companion object {
const val DEFAULT_SHIFT_PERM_TOGGLE_SPEED = 500
const val TEXT_LENGTH = 20
const val MAX_EMOJI_KEYWORD_LENGTH = 20
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing here @bhanu-dev82 is that this might not hold always. Maybe we can load this in from the DB for the emojis table? Let me know what you think here :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes i will implement it and commit

const val NOUN_TYPE_SIZE = 22f
const val SUGGESTION_SIZE = 15f
const val DARK_THEME = "#aeb3be"
Expand Down
Loading