Allow sending empty commands

This commit is contained in:
Sylwester Zieliński
2022-05-10 14:00:59 +02:00
parent 136bdd1520
commit 6b17eef8e4
5 changed files with 48 additions and 75 deletions

View File

@@ -150,27 +150,25 @@ internal class UARTManager(
@SuppressLint("WrongConstant")
fun send(text: String) {
if (rxCharacteristic == null) return
if (!TextUtils.isEmpty(text)) {
scope.launchWithCatch {
val writeType = if (useLongWrite) {
BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT
} else {
BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE
}
val request: WriteRequest =
writeCharacteristic(rxCharacteristic, text.toByteArray(), writeType)
if (!useLongWrite) {
request.split()
}
request.suspend()
data.value = data.value.copy(
messages = data.value.messages + UARTRecord(
text,
UARTRecordType.INPUT
)
)
log(10, "\"$text\" sent")
scope.launchWithCatch {
val writeType = if (useLongWrite) {
BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT
} else {
BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE
}
val request: WriteRequest =
writeCharacteristic(rxCharacteristic, text.toByteArray(), writeType)
if (!useLongWrite) {
request.split()
}
request.suspend()
data.value = data.value.copy(
messages = data.value.messages + UARTRecord(
text,
UARTRecordType.INPUT
)
)
log(10, "\"$text\" sent")
}
}

View File

@@ -13,6 +13,7 @@ import no.nordicsemi.android.service.BleManagerResult
import no.nordicsemi.android.service.IdleResult
import no.nordicsemi.android.service.ServiceManager
import no.nordicsemi.android.uart.data.*
import no.nordicsemi.android.utils.EMPTY
import javax.inject.Inject
import javax.inject.Singleton
@@ -63,9 +64,8 @@ class UARTRepository @Inject internal constructor(
}
fun runMacro(macro: UARTMacro) {
macro.command?.parseWithNewLineChar(macro.newLineChar)?.let {
manager?.send(it)
}
val command = macro.command?.parseWithNewLineChar(macro.newLineChar)
manager?.send(command ?: String.EMPTY)
}
fun clearItems() {

View File

@@ -1,16 +1,12 @@
package no.nordicsemi.android.uart.view
import androidx.compose.foundation.layout.Column
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.res.stringResource
import no.nordicsemi.android.material.you.TextField
import no.nordicsemi.android.uart.R
import no.nordicsemi.android.utils.EMPTY
@@ -49,13 +45,16 @@ private fun NameInput(
isError: MutableState<Boolean>
) {
Column {
TextField(
text = name.value,
hint = stringResource(id = R.string.uart_configuration_hint)
) {
isError.value = false
name.value = it
}
OutlinedTextField(
value = name.value,
label = { Text(stringResource(id = R.string.uart_configuration_hint)) },
singleLine = true,
onValueChange = {
isError.value = false
name.value = it
}
)
val errorText = if (isError.value) {
stringResource(id = R.string.uart_name_empty)

View File

@@ -1,17 +1,12 @@
package no.nordicsemi.android.uart.view
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.*
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.GridItemSpan
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
@@ -27,7 +22,6 @@ import androidx.compose.ui.unit.dp
import no.nordicsemi.android.material.you.RadioButtonGroup
import no.nordicsemi.android.material.you.RadioButtonItem
import no.nordicsemi.android.material.you.RadioGroupViewEntity
import no.nordicsemi.android.material.you.TextField
import no.nordicsemi.android.uart.R
import no.nordicsemi.android.uart.data.MacroEol
import no.nordicsemi.android.uart.data.MacroIcon
@@ -40,7 +34,6 @@ private const val GRID_SIZE = 5
internal fun UARTAddMacroDialog(macro: UARTMacro?, onEvent: (UARTViewEvent) -> Unit) {
val newLineChar = rememberSaveable { mutableStateOf(macro?.newLineChar ?: MacroEol.LF) }
val command = rememberSaveable { mutableStateOf(macro?.command ?: String.EMPTY) }
val isError = rememberSaveable { mutableStateOf(false) }
val selectedIcon = rememberSaveable { mutableStateOf(macro?.icon ?: MacroIcon.values()[0]) }
AlertDialog(
@@ -52,11 +45,7 @@ internal fun UARTAddMacroDialog(macro: UARTMacro?, onEvent: (UARTViewEvent) -> U
},
confirmButton = {
TextButton(onClick = {
if (isCommandValid(command.value)) {
onEvent(OnCreateMacro(UARTMacro(selectedIcon.value, command.value, newLineChar.value)))
} else {
isError.value = true
}
onEvent(OnCreateMacro(UARTMacro(selectedIcon.value, command.value, newLineChar.value)))
}) {
Text(stringResource(id = R.string.uart_macro_dialog_confirm))
}
@@ -81,7 +70,7 @@ internal fun UARTAddMacroDialog(macro: UARTMacro?, onEvent: (UARTViewEvent) -> U
}
item(span = { GridItemSpan(GRID_SIZE) }) {
CommandInput(command, isError)
CommandInput(command)
}
items(20) { item ->
@@ -109,26 +98,17 @@ internal fun UARTAddMacroDialog(macro: UARTMacro?, onEvent: (UARTViewEvent) -> U
}
@Composable
private fun CommandInput(
command: MutableState<String>,
isError: MutableState<Boolean>
) {
private fun CommandInput(command: MutableState<String>) {
Column {
TextField(
text = command.value,
hint = stringResource(id = R.string.uart_macro_dialog_command)
) {
isError.value = false
command.value = it
}
if (isError.value) {
Text(
text = stringResource(id = R.string.uart_macro_error),
style = MaterialTheme.typography.labelMedium,
color = MaterialTheme.colorScheme.error
)
}
OutlinedTextField(
modifier = Modifier
.fillMaxWidth(),
value = command.value,
label = { Text(stringResource(id = R.string.uart_macro_dialog_command)) },
onValueChange = {
command.value = it
}
)
Spacer(modifier = Modifier.size(16.dp))
}
@@ -153,7 +133,3 @@ private fun NewLineCharSection(checkedItem: MacroEol, onItemClick: (MacroEol) ->
}
}
}
private fun isCommandValid(command: String): Boolean {
return command.isNotBlank()
}