Skip to content

Commit 7cf14c6

Browse files
committed
fix: allow same word with different shortcuts
1 parent 9b7cb2e commit 7cf14c6

1 file changed

Lines changed: 20 additions & 8 deletions

File tree

app/src/main/java/helium314/keyboard/settings/screens/PersonalDictionaryScreen.kt

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,10 @@ private fun EditWordDialog(word: Word, locale: Locale?, onDismissRequest: () ->
134134
val focusRequester = remember { FocusRequester() }
135135
var newWord by remember { mutableStateOf(word) }
136136
var newLocale by remember { mutableStateOf(locale) }
137-
val wordValid = (newWord.word == word.word && locale == newLocale) || !doesWordExist(newWord.word, newLocale, ctx)
137+
val identityUnchanged = newWord.word == word.word
138+
&& (newWord.shortcut.isNullOrEmpty() && word.shortcut.isNullOrEmpty() || newWord.shortcut == word.shortcut)
139+
&& locale == newLocale
140+
val wordValid = identityUnchanged || !doesWordExist(newWord.word, newWord.shortcut, newLocale, ctx)
138141
fun save() {
139142
if (newWord != word || locale != newLocale) {
140143
deleteWord(word, locale, ctx.contentResolver)
@@ -257,18 +260,27 @@ private fun deleteWord(wordDetails: Word, locale: Locale?, resolver: ContentReso
257260
}
258261
}
259262

260-
private fun doesWordExist(word: String, locale: Locale?, context: Context): Boolean {
263+
private fun doesWordExist(word: String, shortcut: String?, locale: Locale?, context: Context): Boolean {
261264
val hasWordProjection = arrayOf(UserDictionary.Words.WORD, UserDictionary.Words.LOCALE)
262265

263266
val select: String
264-
val selectArgs: Array<String>?
267+
val selectArgs: Array<String>
265268
if (locale == null) {
266-
select = "${UserDictionary.Words.WORD}=? AND ${UserDictionary.Words.LOCALE} is null"
267-
selectArgs = arrayOf(word)
269+
if (shortcut.isNullOrEmpty()) {
270+
select = "${UserDictionary.Words.WORD}=? AND ${UserDictionary.Words.LOCALE} is null AND (${UserDictionary.Words.SHORTCUT} is null OR ${UserDictionary.Words.SHORTCUT}='')"
271+
selectArgs = arrayOf(word)
272+
} else {
273+
select = "${UserDictionary.Words.WORD}=? AND ${UserDictionary.Words.LOCALE} is null AND ${UserDictionary.Words.SHORTCUT}=?"
274+
selectArgs = arrayOf(word, shortcut)
275+
}
268276
} else {
269-
select = "${UserDictionary.Words.WORD}=? AND ${UserDictionary.Words.LOCALE}=?"
270-
// requires use of locale string (as opposed to more useful language tag) for interaction with Android system
271-
selectArgs = arrayOf(word, locale.toString())
277+
if (shortcut.isNullOrEmpty()) {
278+
select = "${UserDictionary.Words.WORD}=? AND ${UserDictionary.Words.LOCALE}=? AND (${UserDictionary.Words.SHORTCUT} is null OR ${UserDictionary.Words.SHORTCUT}='')"
279+
selectArgs = arrayOf(word, locale.toString())
280+
} else {
281+
select = "${UserDictionary.Words.WORD}=? AND ${UserDictionary.Words.LOCALE}=? AND ${UserDictionary.Words.SHORTCUT}=?"
282+
selectArgs = arrayOf(word, locale.toString(), shortcut)
283+
}
272284
}
273285
val cursor = context.contentResolver.query(UserDictionary.Words.CONTENT_URI, hasWordProjection, select, selectArgs, null)
274286
cursor.use {

0 commit comments

Comments
 (0)