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