From 5931b4a595d5c28810e7ac01f97ff9583cc46138 Mon Sep 17 00:00:00 2001 From: Jose Flores Date: Thu, 9 Dec 2021 20:31:16 -0600 Subject: [PATCH] Fixes index out of bounds crash when attempting to add characters or moving the cursor to the ends of the string being edited. --- .../sample/hellotodo/EditTextWorkflow.kt | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/samples/hello-terminal/todo-terminal-app/src/main/java/com/squareup/sample/hellotodo/EditTextWorkflow.kt b/samples/hello-terminal/todo-terminal-app/src/main/java/com/squareup/sample/hellotodo/EditTextWorkflow.kt index 89597c9e9b..c1c1d320f7 100644 --- a/samples/hello-terminal/todo-terminal-app/src/main/java/com/squareup/sample/hellotodo/EditTextWorkflow.kt +++ b/samples/hello-terminal/todo-terminal-app/src/main/java/com/squareup/sample/hellotodo/EditTextWorkflow.kt @@ -31,7 +31,7 @@ class EditTextWorkflow : StatefulWorkflow { - state = moveCursor(props, state, 1) - props.text.insertCharAt(state.cursorPosition, key.character!!) + val newText = props.text.insertCharAt(state.cursorPosition, key.character!!) + setOutput(newText) + state = moveCursor(newText, state, 1) } - Backspace -> { - if (props.text.isNotEmpty()) { - state = moveCursor(props, state, -1) - props.text.removeRange(state.cursorPosition - 1, state.cursorPosition) + if (props.text.isNotEmpty() && state.cursorPosition > 0) { + val newText = props.text.removeRange(state.cursorPosition - 1, state.cursorPosition) + setOutput(newText) + state = moveCursor(newText, state, -1) } } - ArrowLeft -> state = moveCursor(props, state, -1) - ArrowRight -> state = moveCursor(props, state, 1) + ArrowLeft -> state = moveCursor(props.text, state, -1) + ArrowRight -> state = moveCursor(props.text, state, 1) else -> { // Nothing to do. } @@ -88,13 +89,15 @@ class EditTextWorkflow : StatefulWorkflow