From de9592764a079fcdeecb5a13a675b56c5d546630 Mon Sep 17 00:00:00 2001 From: polwex Date: Sun, 24 Nov 2024 15:23:46 +0700 Subject: [PATCH] fixed backspace issue --- .../com/sortug/thaiinput/MyKeyboardService.kt | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/sortug/thaiinput/MyKeyboardService.kt b/app/src/main/java/com/sortug/thaiinput/MyKeyboardService.kt index bf6222e..fff60df 100644 --- a/app/src/main/java/com/sortug/thaiinput/MyKeyboardService.kt +++ b/app/src/main/java/com/sortug/thaiinput/MyKeyboardService.kt @@ -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