fixed backspace issue

This commit is contained in:
polwex 2024-11-24 15:23:46 +07:00
parent 8120191874
commit de9592764a

View File

@ -154,12 +154,32 @@ class MyKeyboardService : InputMethodService() {
private fun onBackspace() {
Log.d("MyKeyboardService", "onBackspace called")
val inputConnection = currentInputConnection ?: return
inputConnection.deleteSurroundingText(1, 0)
// Only make API request if we still have text
if (currentInput.isNotEmpty()) {
currentInput = currentInput.substring(0, currentInput.length - 1)
coroutineScope.launch {
try {
val response = withContext(Dispatchers.IO) {
RetrofitClient.googleInputToolsApiService.getInputSuggestions(currentInput)
}
val parsedResponse = parseResponse(response)
val options = parsedResponse.results.flatMap { it.options }
Log.d("MyKeyboardService", "Parsed options after backspace: $options")
displaySuggestions(options)
} catch (e: Exception) {
Log.e("MyKeyboardService", "Error in API call after backspace", e)
displaySuggestions(listOf("Error: ${e.message}"))
}
}
} else {
inputConnection.deleteSurroundingText(1, 0)
// Clear suggestions if we've deleted all text
suggestionsContainer.removeAllViews()
}
Log.d("MyKeyboardService", "Current input after backspace: $currentInput")
Log.d("MyKeyboardService", "Current input after backspace: $currentInput")
}
private fun onSpace(){
val inputConnection = currentInputConnection ?: return