-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathSuggestionHandler.kt
More file actions
199 lines (171 loc) · 7.35 KB
/
Copy pathSuggestionHandler.kt
File metadata and controls
199 lines (171 loc) · 7.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// SPDX-License-Identifier: GPL-3.0-or-later
package be.scri.helpers
import android.os.Handler
import android.os.Looper
import be.scri.services.GeneralKeyboardIME
import be.scri.services.GeneralKeyboardIME.ScribeState
/**
* Handles auto-suggestions such as noun gender, plurality, case, and emojis.
*
* @property ime The [GeneralKeyboardIME] instance this handler is associated with.
*/
class SuggestionHandler(
private val ime: GeneralKeyboardIME,
) {
private val handler = Handler(Looper.getMainLooper())
private var emojiSuggestionRunnable: Runnable? = null
private var linguisticSuggestionRunnable: Runnable? = null
private var wordSuggestionRunnable: Runnable? = null
/**
* Companion object for holding constants related to suggestion handling.
*/
companion object {
private const val SUGGESTION_DELAY_MS = 50L
}
/**
* Processes the given word to find and display relevant LINGUISTIC suggestions.
* This includes noun gender, plurality, and case annotations.
* This is intended to be called AFTER a word is completed (e.g., after space).
*
* @param completedWord The word that was just completed.
*/
fun processLinguisticSuggestions(completedWord: String?) {
linguisticSuggestionRunnable?.let { handler.removeCallbacks(it) }
linguisticSuggestionRunnable =
Runnable {
if (ime.currentState != ScribeState.IDLE) {
clearAllSuggestionsAndHideButtonUI()
return@Runnable
}
if (completedWord.isNullOrEmpty()) {
clearLinguisticSuggestions()
return@Runnable
}
val genderSuggestion = ime.findGenderForLastWord(ime.nounKeywords, completedWord)
val isPluralByDirectCheck = ime.findWhetherWordIsPlural(ime.pluralWords, completedWord)
val caseSuggestion = ime.getCaseAnnotationForPreposition(ime.caseAnnotation, completedWord)
val hasLinguisticSuggestion =
genderSuggestion != null ||
isPluralByDirectCheck ||
caseSuggestion != null ||
ime.isSingularAndPlural
if (hasLinguisticSuggestion) {
ime.nounTypeSuggestion = genderSuggestion
ime.checkIfPluralWord = isPluralByDirectCheck
ime.caseAnnotationSuggestion = caseSuggestion
ime.updateAutoSuggestText(
genderSuggestion,
isPluralByDirectCheck || ime.isSingularAndPlural,
caseSuggestion,
)
} else {
ime.disableAutoSuggest()
}
}
handler.postDelayed(linguisticSuggestionRunnable!!, SUGGESTION_DELAY_MS)
}
fun processWordSuggestions(completedWord: String?) {
wordSuggestionRunnable?.let { handler.removeCallbacks(it) }
wordSuggestionRunnable =
Runnable {
if (ime.currentState != ScribeState.IDLE) {
clearAllSuggestionsAndHideButtonUI()
return@Runnable
}
if (completedWord.isNullOrEmpty()) {
clearLinguisticSuggestions()
return@Runnable
}
val nextWordSuggestion = ime.getNextWordSuggestions(ime.suggestionWords, completedWord)
if (nextWordSuggestion != null) {
ime.wordSuggestions = nextWordSuggestion
ime.updateAutoSuggestText(
ime.nounTypeSuggestion,
ime.checkIfPluralWord || ime.isSingularAndPlural,
ime.caseAnnotationSuggestion,
nextWordSuggestion,
)
} else {
ime.disableAutoSuggest()
}
}
handler.postDelayed(wordSuggestionRunnable!!, SUGGESTION_DELAY_MS)
}
/**
* Processes the given word to find and display relevant EMOJI suggestions.
* This is intended to be called AS the user types.
*
* @param currentWord The word currently being typed.
*/
fun processEmojiSuggestions(currentWord: String?) {
emojiSuggestionRunnable?.let { handler.removeCallbacks(it) }
emojiSuggestionRunnable =
Runnable {
if (ime.currentState != ScribeState.IDLE) {
clearAllSuggestionsAndHideButtonUI()
return@Runnable
}
ime.lastWord = currentWord
if (currentWord.isNullOrEmpty()) {
ime.updateButtonVisibility(false)
return@Runnable
}
var emojis: MutableList<String>?
if (ime.emojiColonModeOn) {
val currentWordCleaned = currentWord.removePrefix(":") // Drop colon that triggered emojiColonMode
emojis =
if (
currentWordCleaned.isEmpty()
) { // For no word typed yet, show common emojis
EmojiUtils.COMMON_EMOJIS.toMutableList()
} else {
ime.findEmojisForPrefix(ime.emojiKeywords, currentWordCleaned)
}
} else {
emojis = null
}
val hasEmojiSuggestion = !emojis.isNullOrEmpty()
if (hasEmojiSuggestion) {
ime.autoSuggestEmojis = emojis
ime.updateButtonVisibility(true)
ime.updateEmojiSuggestion(true, emojis)
} else if (ime.emojiColonModeOn) { // Show blank buttons when there are no matches
ime.autoSuggestEmojis = mutableListOf()
ime.updateEmojiSuggestion(true, mutableListOf())
} else {
ime.updateButtonVisibility(false)
}
}
handler.postDelayed(emojiSuggestionRunnable!!, SUGGESTION_DELAY_MS)
}
/**
* Clears only the linguistic suggestions (gender, case, plural) from the UI.
* Leaves emoji suggestions untouched.
*/
fun clearLinguisticSuggestions() {
linguisticSuggestionRunnable?.let { handler.removeCallbacks(it) }
ime.disableAutoSuggest()
ime.nounTypeSuggestion = null
ime.checkIfPluralWord = false
ime.caseAnnotationSuggestion = null
ime.isSingularAndPlural = false
}
/**
* Clears all suggestion states (noun type, plurality, case, emojis) from the IME
* and hides suggestion-related UI elements.
*/
fun clearAllSuggestionsAndHideButtonUI() {
emojiSuggestionRunnable?.let { handler.removeCallbacks(it) }
linguisticSuggestionRunnable?.let { handler.removeCallbacks(it) }
wordSuggestionRunnable?.let { handler.removeCallbacks(it) }
ime.disableAutoSuggest()
if (ime.currentState != ScribeState.SELECT_COMMAND) {
ime.updateButtonVisibility(false)
}
ime.nounTypeSuggestion = null
ime.checkIfPluralWord = false
ime.caseAnnotationSuggestion = null
ime.autoSuggestEmojis = null
ime.isSingularAndPlural = false
}
}